#include <bits/stdc++.h>
using namespace std;
vector<int> primes;
using ll = long long int;
using dl = double;
ll aarray[200000 + 10];
ll ar[200000 + 10];
int main() {
  ios_base::sync_with_stdio(false);
  string ptr, str, uni;
  ll b, u, l, B, n, a = -1, s, d, r, i, j, m, k, c, t, g, sum, flag, x, y, ans;
  vector<ll> vp;
  set<ll> sc;
  char ch;
  cin >> t;
  while (t--) {
    char magic[1001][1001];
    cin >> n >> m >> x >> y;
    for (i = 1; i <= n + 1; i++) {
      for (j = 1; j <= m + 1; j++) {
        magic[i][j] = 'a';
      }
    }
    ans = 0;
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= m; j++) {
        char ch;
        cin >> ch;
        magic[i][j] = ch;
      }
    }
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= m;) {
        if (magic[i][j] == '.' && magic[i][j + 1] == '.') {
          ans += min((2 * x), y);
          j += 2;
        } else if (magic[i][j] == '.' && magic[i][j + 1] != '.') {
          ans = ans + x;
          j++;
        } else
          j++;
      }
    }
    cout << ans << endl;
  }
}
