#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n, m, x, y;
    cin >> n >> m >> x >> y;
    y = min(2 * x, y);
    string s[n];
    for (long long i = 0; i < n; i++) {
      cin >> s[i];
    }
    long long sin = 0;
    long long dou = 0;
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < m; j++) {
        if (s[i][j] == '.') {
          if (s[i][j + 1] == '.') {
            dou++;
            j++;
          } else {
            sin++;
          }
        }
      }
    }
    cout << (sin * x) + (dou * y) << endl;
  }
  return 0;
}
