#include "bits/stdc++.h"
// #ifndef ONLINE_JUDGE
// #include "debug.h"
// #else
// #define dbg(x...)
// #endif
using namespace std;
using ll = long long;
#define int ll
#define endl '\n'
const int mod = 1000000007;
// clang-format off
template<typename typC,typename typD> istream &operator>>(istream &cin,pair<typC,typD> &a) { return cin>>a.first>>a.second; }
template<typename typC> istream &operator>>(istream &cin,vector<typC> &a) { for (auto &x:a) cin>>x; return cin; }
template<typename typC,typename typD> ostream &operator<<(ostream &cout,const pair<typC,typD> &a) { return cout<<a.first<<' '<<a.second; }
template<typename typC,typename typD> ostream &operator<<(ostream &cout,const vector<pair<typC,typD>> &a) { for (auto &x:a) cout<<x<<'\n'; return cout; }
template<typename typC> ostream &operator<<(ostream &cout,const vector<typC> &a) { int n=a.size(); if (!n) return cout; cout<<a[0]; for (int i=1; i<n; i++) cout<<' '<<a[i]; return cout; }
// #define _GLIBCXX_DEBUG
// clang-format on
int dp[2001][2001][2];
int f(int i, int j, int prev, vector<vector<char>> &v, int x, int y)
{
int n = v.size();
int m = v[0].size();
if ((i == n - 1 && j == m - 2 && prev == 0) || (i == n - 2 && j == m - 1 && prev == 1))
return 0;
if (i >= n || j >= m)
return 1e9;
if (v[i][j] == '#')
return 1e9;
if (dp[i][j][prev] != -1)
return dp[i][j][prev];
int p1 = 1e9, p2 = 1e9, p3 = 1e9;
if (prev == 0)
{
if (j < m - 1)
p1 = x + f(i, j + 1, 0, v, x, y);
if (j < m - 1)
p2 = y + f(i, j + 1, 1, v, x, y);
}
if (prev == 1)
{
if (i < n - 1)
p1 = x + f(i + 1, j, 1, v, x, y);
if (i < n - 1)
p2 = y + f(i + 1, j, 0, v, x, y);
}
return dp[i][j][prev] = min(p1, p2);
}
void solve()
{
ll t, m, n, a, b;
string h;
cin >> n >> m >> a >> b;
vector<vector<char>> v(n, vector<char>(m));
cin >> v;
for (int i = 0; i <= n + 1; i++)
{
for (int j = 0; j <= m + 1; j++)
{
for (int k = 0; k <= 1; k++)
dp[i][j][k] = -1;
}
}
int ans = f(0, 1, 0, v, a, b) + a;
ans = min(ans, f(1, 0, 1, v, a, b) + a);
ans = min(ans, f(0, 1, 1, v, a, b) + b);
ans = min(ans, f(1, 0, 0, v, a, b) + b);
cout << ans << endl;
}
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(0);
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}