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