#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) {
    long long n, m, x, y;
    cin >> n >> m >> x >> y;
    char a[n + 1][m + 1];
    long long cnt = 0;
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= m; j++) {
        cin >> a[i][j];
        if (a[i][j] == '.') cnt++;
      }
    }
    long long bg = 2 * x;
    if (bg <= y) {
      long long ans = cnt * x;
      cout << ans << "\n";
    } else {
      long long ans = 0;
      for (long long i = 1; i <= n; i++) {
        for (long long j = 1; j <= m - 1; j++) {
          if (a[i][j] == '.' && a[i][j + 1] == '.') {
            ans += y;
            a[i][j] = '*';
            a[i][j + 1] = '*';
            j++;
          } else if (a[i][j] == '.') {
            ans += x;
            a[i][j] = '*';
          }
        }
      }
      for (long long i = 1; i <= n; i++) {
        for (long long j = 1; j <= m; j++) {
          if (a[i][j] == '.') {
            ans += x;
          }
        }
      }
      cout << ans << "\n";
    }
  }
}
