#include <bits/stdc++.h>
using namespace std;
using namespace std;
bool pr(int n) {
  for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) return false;
  }
  return true;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m, x, y;
    int c1 = 0, c2 = 0;
    cin >> n >> m >> x >> y;
    vector<string> s(n);
    for (int i = 0; i < n; i++) {
      cin >> s[i];
      for (int j = 0; j < m; j++) {
        if (j + 1 != m && s[i][j] == '.' && s[i][j + 1] == '.') {
          c1++;
          j++;
          c2++;
        }
        if (s[i][j] == '.') c2++;
      }
    }
    if (x * 2 <= y)
      cout << x * c2 << endl;
    else
      cout << (c2 - c1 * 2) * x + c1 * y << endl;
  }
}
