#include <bits/stdc++.h>
#define int long long
void solve() {
int m, n;
std::cin >> m >> n;
int x, y;
std::cin >> x >> y;
std::vector<std::string> g(m);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
char c;
std::cin >> c;
g[i] += c;
}
}
std::vector<int> dp(n, LLONG_MAX);
dp[0] = 0;
for (int i = 1; i < n; i++) {
if (g[0][i] != '#') {
dp[i] = x * (i - 1LL) + y;
} else {
break;
}
}
for (int i = 1; i < m - 1; i++) {
std::vector<int> ndp(n, LLONG_MAX);
std::vector<int> suf(n, LLONG_MAX), pre(n, LLONG_MAX);
if (dp[n - 1] != LLONG_MAX && g[i][n - 1] != '#') {
suf[n - 1] = dp[n - 1] + x * (n - 2LL);
}
for (int j = n - 2; j > 0; j--) {
if (g[i][j] == '#' || dp[j] == LLONG_MAX) {
suf[j] = suf[j + 1];
continue;
}
suf[j] = std::min(suf[j + 1], dp[j] + x * (j - 1LL));
}
if (dp[0] != LLONG_MAX && g[i][0] != '#') {
pre[0] = dp[0] + x * (n - 2LL);
}
for (int j = 1; j < n - 1; j++) {
if (g[i][j] == '#' || dp[j] == LLONG_MAX) {
pre[j] = pre[j - 1];
continue;
}
pre[j] = std::min(pre[j - 1], dp[j] + x * (n - j - 2LL));
}
for (int j = 0; j < n; j++) {
if (g[i][j] == '#') {
continue;
}
if (dp[j] != LLONG_MAX) {
ndp[j] = dp[j] + x;
}
if (j > 0 && pre[j - 1] != LLONG_MAX) {
ndp[j] = std::min(ndp[j], pre[j - 1] - x * (n - j - 1LL) + 2LL * y);
}
if (j < n - 1 && suf[j + 1] != LLONG_MAX) {
ndp[j] = std::min(ndp[j], suf[j + 1] - x * j + 2LL * y);
}
}
std::swap(dp, ndp);
}
int ans = dp[n - 1];
for (int j = n - 2; j >= 0; j--) {
if (g[m - 1][j] == '#') {
break;
}
if (dp[j] == LLONG_MAX) {
continue;
}
ans = std::min(ans, dp[j] + y + x * (n - j - 2LL));
}
if (ans == LLONG_MAX) {
std::cout << -1 << "\n";
} else {
std::cout << ans << "\n";
}
}
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
}