#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  long long int T;
  cin >> T;
  while (T--) {
    long long int n, m, x, y;
    cin >> n >> m >> x >> y;
    char a[n + 70][m + 70];
    memset(a, 0, sizeof(a));
    for (long long int i = 1; i <= n; ++i) {
      a[i][m + 1] = '$';
      a[i][0] = '$';
      for (long long int j = 1; j <= m; ++j) {
        cin >> a[i][j];
      }
    }
    long long int cnt1 = 0, cnt2 = 0;
    for (long long int i = 1; i <= n; ++i) {
      for (long long int j = 1; j <= m; ++j) {
        if (a[i][j] == '.' && a[i][j + 1] == '.') {
          cnt2++;
          j++;
          continue;
        }
        if (a[i][j] == '.') {
          cnt1++;
        }
      }
    }
    if (2 * x - y <= 0) {
      cout << (2 * cnt2 + cnt1) * x << "\n";
    } else {
      cout << x * (cnt1) + y * (cnt2) << "\n";
    }
  }
  return 0;
}
