#include <bits/stdc++.h>
using namespace std;
const int INF = 20000000;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m, x, y;
    cin >> n >> m >> x >> y;
    long long cost = 0;
    for (int i = 0; i < n; i++) {
      char z[m];
      for (int j = 0; j < m; j++) cin >> z[j];
      if (2 * x <= y) {
        for (int j = 0; j < m; j++)
          if (z[j] == '.') cost += x;
      } else {
        int cnt = 0;
        for (int j = 0; j < m; j++) {
          if (z[j] == '.') cnt++;
          if (z[j] == '*' || j == m - 1) {
            cost += y * (cnt / 2) + (cnt % 2) * x;
            cnt = 0;
          }
        }
      }
    }
    cout << cost << "\n";
  }
  return 0;
}
