#include <bits/stdc++.h>
int main() {
  long long int t, n, m, x, y, i, j, cost, count_sdot, count_ddot;
  scanf("%lld", &t);
  while (t--) {
    scanf("%lld %lld %lld %lld", &n, &m, &x, &y);
    char a[m];
    cost = 0;
    count_sdot = 0;
    count_ddot = 0;
    if (2 * x <= y) {
      for (i = 0; i < n; i++) {
        scanf("%s", a);
        for (j = 0; j < m; j++) {
          if (a[j] == '.') {
            count_sdot++;
          }
        }
      }
      cost = count_sdot * x;
    } else {
      for (i = 0; i < n; i++) {
        scanf("%s", a);
        for (j = 0; j < m; j++) {
          if (a[j] == '.' && a[j + 1] == '.') {
            count_ddot++;
            j++;
          } else if (a[j] == '.' && a[j + 1] != '.') {
            count_sdot++;
          }
        }
      }
      cost = count_ddot * y + count_sdot * x;
    }
    printf("%lld\n", cost);
  }
  return 0;
}
