#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
char arr[1010][1010];
bool visited[1010][1010];
long long int n, m, x, y, K, N, cnt;
bool is_valid(long long int i, long long int j) {
  return i >= 0 && i < n && j >= 0 && j < m;
}
void dfs(long long int i, long long int j) {
  visited[i][j] = true;
  cnt++;
  if (is_valid(i, j + 1) && arr[i][j + 1] == '.' && !visited[i][j + 1])
    dfs(i, j + 1);
  if (is_valid(i, j - 1) && arr[i][j - 1] == '.' && !visited[i][j - 1])
    dfs(i, j - 1);
}
void solve() {
  cin >> n >> m >> x >> y;
  K = 0, N = 0;
  cnt = 0;
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < m; j++) {
      cin >> arr[i][j];
      visited[i][j] = false;
      if (arr[i][j] == '.') N++;
    }
  }
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < m; j++) {
      if (!visited[i][j] && arr[i][j] == '.') {
        dfs(i, j);
        K = K + (cnt) / 2;
        cnt = 0;
      }
    }
  }
  long long int rem = N - (K * 2);
  cout << min(N * x, K * y + rem * x) << endl;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  long long int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
