#include <bits/stdc++.h>
using namespace std;
const int maxN = 1005;
char c[maxN][maxN];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    int x, y;
    cin >> x >> y;
    y = min(y, 2 * x);
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        cin >> c[i][j];
        if (c[i][j] == '.') {
          if (j != 1 && c[i][j - 1] == '.' && c[i][j] == '.') {
            c[i][j] = '*';
            c[i][j - 1] = '*';
            ans += y;
          }
        }
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (c[i][j] == '.') ans += x;
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
