#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n, m, x, y;
    cin >> n >> m >> x >> y;
    char grid[n][m];
    char ch;
    int ct = 0;
    if ((2 * x) <= (y)) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          cin >> ch;
          grid[i][j] = ch;
          if (ch == '.') {
            ct++;
          }
        }
      }
      cout << (ct * x) << "\n";
      continue;
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> ch;
        grid[i][j] = ch;
      }
    }
    int cst = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m;) {
        if ((j + 1) < m && grid[i][j] == '.' && grid[i][j + 1] == '.') {
          cst += y;
          j++;
          j++;
        } else if (grid[i][j] == '*') {
          j++;
          continue;
        } else if (grid[i][j] == '.') {
          cst += x;
          j++;
        }
      }
    }
    cout << cst << "\n";
  }
  return 0;
}
