#include <bits/stdc++.h>
using namespace std;
const int mnx = 3e5 + 9;
const int mod = 1e9 + 7;
const long long inf = 1e15 + 4;
long long n, m, x, y;
char ch[1111][1111];
long long pr[1111][1111];
void solve() {
  cin >> n >> m >> x >> y;
  long long cnt = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> ch[i][j];
      if (ch[i][j] == '.') cnt++;
      pr[i][j] = 0;
    }
  }
  if (x * 2 <= y) {
    cout << cnt * x << '\n';
    return;
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (j < m && pr[i][j] == 0 && pr[i][j + 1] == 0) {
        if (ch[i][j] == '.' && ch[i][j + 1] == '.') {
          ans += y;
          pr[i][j] = pr[i][j + 1] = 1;
          continue;
        }
      }
      if (ch[i][j] == '.' && pr[i][j] == 0) {
        ans += x;
      }
    }
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int tt = 1;
  cin >> tt;
  while (tt--) {
    solve();
  }
  return 0;
}
