#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  char a[101][1001];
  int t;
  cin >> t;
  while (t--) {
    int n, m, x, y;
    cin >> n >> m >> x >> y;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) cin >> a[i][j];
    }
    if (y > 2 * x) {
      int count = 0;
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++)
          if (a[i][j] == '.') count++;
      }
      cout << count * x;
    } else {
      int countx = 0, county = 0;
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          if (j < m - 1 and a[i][j] == '.' and a[i][j + 1] == '.') {
            county++;
            j++;
          } else if (a[i][j] == '.')
            countx++;
        }
      }
      cout << countx * x + county * y;
    }
    cout << "\n";
  }
  return 0;
}
