#ifndef LOCAL
#include <bits/stdc++.h>
#define debug(...)
#endif
using namespace std;
#define int long long
#define cinv(v) for (auto &it:v) cin>>it;
#define coutv(v) for (auto &it:v) cout<< it<<' '; cout<<'\n';
vector<vector<pair<pair<int,int>,int> > > dir{
{{{1, 0}, 0}, {{0, -1}, 1}, {{0, 1}, 2}},
{{{1, 0}, 0}, {{0, -1}, 1}},
{{{1, 0}, 0}, {{0, 1}, 2}},
};
const int INF = 1e15;
void shelby() {
int n, m;
cin >> n >> m;
array<int, 2> cost;
vector<vector<char> > mat(n, vector<char>(m));
cin >> cost[0] >> cost[1];
for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) cin >> mat[i][j];
vector memo(n, vector(m, vector<int>(3, -1)));
auto dp = [&](auto &&self, int x, int y, int k, int d = 0)-> int {
debug(x, y, k, d);
if (x >= n || y >= m || x < 0 || y < 0 || mat[x][y] == '#') return INF;
if (x == n - 1 && y == m - 1) return 0;
int &ret = memo[x][y][k];
if (~ret) return ret;
ret = INF;
for (auto &it: dir[k]) {
int x1 = x + it.first.first, y1 = y + it.first.second;
ret = min(ret, cost[k != it.second] + self(self, x1, y1, it.second, d + 1));
}
return ret;
};
int res = min(dp(dp, 0, 1, 2), dp(dp, 1, 0, 0));
if (res == INF) res = -1;
cout << res << '\n';
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
cin >> t;
for (int _ = 1; _ <= t; ++_) {
// cout << "Case " << _ << ": ";
shelby();
}
}