#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m, x, y;
    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];
      }
    }
    int county = 0, count = 0;
    if (2 * x > y) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          if (a[i][j] == '.') count++;
          if (j != m - 1) {
            if (a[i][j] == a[i][j + 1] && a[i][j] == '.') {
              county++;
              j++;
            }
          }
        }
      }
      cout << (county * y) + (count - county) * x;
    } else {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          if (a[i][j] == '.') count++;
        }
      }
      cout << count * x;
    }
    cout << "\n";
  }
  return 0;
}
