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