#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  for (int k = 0; k < t; k++) {
    int n, m, x, y;
    cin >> n >> m >> x >> y;
    char arr[n][m];
    int counter = 0;
    int counter1 = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> arr[i][j];
        if (arr[i][j] == '.') {
          counter++;
        }
        if (j >= 1 && arr[i][j] == arr[i][j - 1] && arr[i][j] == '.') {
          counter1++;
          arr[i][j] = '?';
        }
      }
    }
    if (x * 2 <= y) {
      cout << counter * x << endl;
    } else {
      cout << counter1 * y + (counter - 2 * counter1) * x << endl;
    }
  }
}
