#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m, x, y;
  cin >> n >> m >> x >> y;
  int i, j;
  char c, last;
  int sol = 0;
  for (i = 0; i < n; i++) {
    last = '*';
    for (j = 0; j < m; j++) {
      cin >> c;
      if (c == '.')
        if (last == '.' && y < 2 * x) {
          sol -= x;
          sol += y;
          last = '*';
          continue;
        } else {
          sol += x;
        }
      last = c;
    }
  }
  cout << sol << '\n';
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
}
