#include <bits/stdc++.h>
using namespace std;
const int Maxn = 1010;
char str[Maxn][Maxn];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, m, x, y;
    scanf("%d%d%d%d", &n, &m, &x, &y);
    if (x * 2 < y) y = x * 2;
    for (int i = 0; i < n; i++) {
      scanf("%s", str[i]);
    }
    int sum = 0, ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (str[i][j] == '.') {
          if (str[i][j + 1] != '.') {
            sum++;
          } else {
            ans++;
            j++;
          }
        }
      }
    }
    printf("%d\n", sum * x + ans * y);
  }
  return 0;
}
