#include <bits/stdc++.h>
using namespace std;
char arr[1010][1010];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m, x, y;
    cin >> n >> m >> x >> y;
    for (int i = 0; i <= n; i++) {
      arr[i][0] = '*';
      arr[i][m + 1] = '*';
    }
    for (int i = 0; i <= m; i++) {
      arr[0][i] = '*';
      arr[n + 1][i] = '*';
    }
    int count = 0;
    for (int i = 1; i <= n; i++) {
      string s;
      cin >> s;
      for (int j = 1; j <= m; j++) {
        arr[i][j] = s[j - 1];
        if (arr[i][j] == '.') {
          count++;
        }
      }
    }
    if (2 * x <= y) {
      cout << x * count << endl;
      continue;
    }
    int tot = 0;
    for (int i = 1; i <= n; i++) {
      int cou = 0;
      char curr = arr[i][0];
      for (int j = 0; j <= m + 1; j++) {
        if (arr[i][j] == '.') {
          curr = '.';
          cou++;
        }
        if (arr[i][j] == '*' && curr == '.') {
          curr = '*';
          if (cou % 2 == 0) {
            tot += y * (cou / 2);
          } else {
            tot += y * (cou / 2) + x;
          }
          cou = 0;
        }
      }
    }
    cout << tot << endl;
  }
}
