#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.14159265358979323846;
void solve() {
  int n, m, x, y, w = 0, b = 0;
  cin >> n >> m >> x >> y;
  char a[n][m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
      if (a[i][j] == '.') w++;
    }
  }
  long long ans = 0;
  if (x <= y / 2) {
    ans = x * w;
    cout << ans << endl;
  } else {
    long long w2 = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m - 1;) {
        if (a[i][j] == '.' && a[i][j + 1] == '.') {
          w2++;
          j += 2;
        } else {
          j++;
        }
      }
    }
    ans = (w - 2 * w2) * x + (w2)*y;
    cout << ans << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  cin >> t;
  while (t--) solve();
  return 0;
}
