#include <bits/stdc++.h>
const double II = acos(-1);
const double PP = (II * 1.0) / (180.00);
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1000 + 10;
char s[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);
    for (int i = 1; i <= n; ++i) scanf("%s", s[i] + 1);
    int sum_white = 0, sum = 0;
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= m; j++) {
        if (s[i][j] == '*') continue;
        if (s[i][j] == '.' && s[i][j + 1] == '.') {
          sum_white += 2;
          sum += y;
          j++;
          continue;
        } else {
          sum_white++;
          sum += x;
          j++;
        }
      }
    }
    if (2 * x <= y)
      printf("%d\n", sum_white * x);
    else {
      printf("%d\n", sum);
    }
  }
  return 0;
}
