#include <bits/stdc++.h>using namespace std;
const int dx[] = {0, -1, 0, 1}, dy[] = {-1, 0, 1, 0};
char mp[32][32];
int dis[32][32][4];
bool check(const int &x, const int &y, const int &rt)
{
if (mp[x][y] == '*') return false;
for (int i = 0; i < 4; ++i)
{
if (i == rt) continue;
if (mp[x + dx[i]][y + dy[i]] == '*') return false;
}
return true;
}
int getdown(const int &x, const int &y, const int &rt) {for (int i = 1; ; ++i) if (!check(x + i, y, rt)) return i - 1;}
struct node
{
int x, y, rt, d;
node (const int &_x, const int &_y, const int &_rt, const int &_d) : x(_x), y(_y), rt(_rt), d(_d) {}
bool operator < (const node &b) const {return this->d > b.d;}
};
void solve()
{
memset(dis, 0x3f, sizeof(dis));
int n, m;
cin >> n >> m;
int l, r, d, s, c;
cin >> l >> r >> d >> s >> c;
int x, y, rt;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
cin >> mp[i][j];
if (mp[i][j] == 'C') x = i, y = j;
}
}
for (int i = 1; i <= n; ++i) mp[i][0] = mp[i][m + 1] = '*';
for (int j = 1; j <= m; ++j) mp[0][j] = mp[n + 1][j] = '*';
for (rt = 0; mp[x + dx[rt]][y + dy[rt]] == '#'; ++rt);
dis[x][y][rt] = 0;
priority_queue < node > p;
p.emplace(x, y, rt, 0);
while (!p.empty())
{
auto [x, y, rt, w] = p.top(); p.pop();
if (w > dis[x][y][rt]) continue;
if (check(x, y - 1, rt) && w + l < dis[x][y - 1][rt]) {dis[x][y - 1][rt] = w + l; p.emplace(x, y - 1, rt, w + l);}
if (check(x, y + 1, rt) && w + r < dis[x][y + 1][rt]) {dis[x][y + 1][rt] = w + r; p.emplace(x, y + 1, rt, w + r);}
if (check(x + 1, y, rt) && w + d < dis[x + 1][y][rt]) {dis[x + 1][y][rt] = w + d; p.emplace(x + 1, y, rt, w + d);}
if (check(x, y, (rt + 1) % 4) && w + c < dis[x][y][(rt + 1) % 4]) {dis[x][y][(rt + 1) % 4] = w + c; p.emplace(x, y, (rt + 1) % 4, w + c);}
int sk = getdown(x, y, rt);
if (w + s < dis[x + sk][y][rt]) {dis[x + sk][y][rt] = w + s; p.emplace(x + sk, y, rt, w + s);}
}
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
int minans = 0x3f3f3f3f;
for (int k = 0; k < 4; ++k) minans = min(minans, dis[i][j][k]);
cout << (minans == 0x3f3f3f3f ? -1 : minans) << " ";
}
cout << endl;
}
}
int main()
{
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int T;
cin >> T;
while (T--) solve();
return 0;
}