#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m, x, y, ans = 0, d;
    cin >> n >> m >> x >> y;
    char a[n][m];
    int cntd = 0, cnt2d = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> a[i][j];
        if (a[i][j] == '.') cntd++;
      }
    }
    if (2 * x <= y)
      cout << cntd * x << '\n';
    else {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          if (a[i][j] == '.' && a[i][j + 1] == '.' && j + 1 < m) {
            ans += y;
            j++;
          } else if (a[i][j] == '.') {
            ans += x;
          }
        }
      }
      cout << ans << '\n';
    }
  }
}
