#include <bits/stdc++.h>
using namespace std;

int main() {
    int t;
    cin>>t;
    while(t--) {
        long long n, m, x, y;
        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];
        }
        long long count_dots = 0;
        long long cons_count = 0;
        long long cost = 0;
        for(int i=0; i<n; i++) {
            for(int j=0; j<m; j++) {
                if(arr[i][j] == '.')
                    count_dots++;
            }
        }
        for(int i=0; i<n; i++) {
            if(m==1)
                cost = count_dots*x;
            else {
                for(int j=0; j<m-1; j++) {
                    if(arr[i][j] == '.' && arr[i][j+1] == '.') {
                        cost += y;
                        cons_count++;
                        j++;
                    }
                    else if(arr[i][j]=='.')
                        cost += x;
                    j++;
                }
                // if(arr[i][m-1]=='.' && arr[i][m-2]!='.') {
                //     cost+=x;
                // }
            }
        }
        cost = min((count_dots-2*cons_count)*x + cons_count*y, count_dots*x);
        cout<<cost<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    int t;
    cin>>t;
    while(t--) {
        long long n, m, x, y;
        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];
        }
        long long count_dots = 0;
        long long cons_count = 0;
        long long cost = 0;
        for(int i=0; i<n; i++) {
            for(int j=0; j<m; j++) {
                if(arr[i][j] == '.')
                    count_dots++;
            }
        }
        for(int i=0; i<n; i++) {
            if(m==1)
                cost = count_dots*x;
            else {
                for(int j=0; j<m-1; j++) {
                    if(arr[i][j] == '.' && arr[i][j+1] == '.') {
                        cost += y;
                        cons_count++;
                        j++;
                    }
                    else if(arr[i][j]=='.')
                        cost += x;
                    j++;
                }
                if(arr[i][m-1]=='.' && arr[i][m-2]!='.') {
                    cost+=x;
                }
            }
        }
        cost = min((cons_count-count_dots*2)*y + count_dots*x, count_dots*x);
        cout<<cost<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    int t;
    cin>>t;
    while(t--) {
        long long n, m, x, y;
        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];
        }
        long long count_dots = 0;
        long long cost = 0;
        for(int i=0; i<n; i++) {
            for(int j=0; j<m; j++) {
                if(arr[i][j] == '.')
                    count_dots++;
            }
        }
        int val=0;
        if(m==1)
                cost = count_dots*x;
        else{
            int i=0, j=0;
            for( ; i<n; i++) {
                for( ; j<m-1; j++) {
                    if(arr[i][j] == '.' && arr[i][j+1] == '.') {
                        //cost += y;
                        val ++;
                        j++;
                    }
                }
            }
        }
        cost = val*y + (count_dots-2*val)*x;
        cost = min(cost, count_dots*x);
        cout<<cost<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    int t;
    cin>>t;
    while(t--) {
        long long n, m, x, y;
        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];
        }
        long long count_dots = 0;
        long long cost = 0;
        for(int i=0; i<n; i++) {
            for(int j=0; j<m; j++) {
                if(arr[i][j] == '.') {
                    if(j+1<m && 2*x>y) {
                        if(arr[i][j+1]=='.') {
                            cost+=y;
                            j++;
                        }
                        else cost+=x;
                    }
                    else cost += x;
                }
            }
        }
        cout<<cost<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    int t;
    cin>>t;
    while(t--) {
        long long n, m, x, y;
        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];
        }
        long long count_dots = 0;
        long long cons_count = 0;
        long long cost = 0;
        for(int i=0; i<n; i++) {
            for(int j=0; j<m; j++) {
                if(arr[i][j] == '.')
                    count_dots++;
            }
        }
        for(int i=0; i<n; i++) {
            if(m==1)
                cost = count_dots*x;
            else {
                for(int j=0; j<m-1; j++) {
                    if(arr[i][j] == '.' && arr[i][j+1] == '.') {
                        cost += y;
                        cons_count++;
                        j++;
                    }
                    else if(arr[i][j]=='.')
                        cost += x;
                    j++;
                }
                // if(arr[i][m-1]=='.' && arr[i][m-2]!='.') {
                //     cost+=x;
                // }
            }
        }
        cost = min((count_dots*2-cons_count)*y + count_dots*x, count_dots*x);
        cout<<cost<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    int t;
    cin>>t;
    while(t--) {
        long long n, m, x, y;
        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];
        }
        long long count_dots = 0;
        long long cost = 0;
        for(int i=0; i<n; i++) {
            for(int j=0; j<m; j++) {
                if(arr[i][j] == '.')
                    count_dots++;
            }
        }
        for(int i=0; i<n; i++) {
            if(m==1)
                cost = count_dots*x;
            else {
                for(int j=0; j<m-1; ) {
                    if(arr[i][j] == '.' && arr[i][j+1] == '.') {
                        cost += y;
                        j = j+2;
                    }
                    else if(arr[i][j]=='.')
                        cost += x;
                    j++;
                }
                if(arr[i][m-1]=='.' && arr[i][m-2]!='.') {
                    cost+=x;
                }
            }
        }
        cost = min(cost, count_dots*x);
        cout<<cost<<endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    int t;
    cin>>t;
    while(t--) {
        long long n, m, x, y;
        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];
        }
        long long count_dots = 0;
        long long cost = 0;
        for(int i=0; i<n; i++) {
            for(int j=0; j<m; j++) {
                if(arr[i][j] == '.') {
                    if(j+1<m && 2*x>y) {
                        if(arr[i][j+1]=='.') {
                            cost+=y;
                            j++;
                        }
                        else cost+=x;
                    }
                    else cost += x;
                    j++;
                }
            }
        }
        cout<<cost<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    int t;
    cin>>t;
    while(t--) {
        long long n, m, x, y;
        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];
        }
        long long count_dots = 0;
        long long cost = 0;
        for(int i=0; i<n; i++) {
            for(int j=0; j<m; j++) {
                if(arr[i][j] == '.')
                    count_dots++;
            }
        }
        if(m==1)
                cost = count_dots*x;
        else{
            for(int i=0; i<n; i++) {
                for(int j=0; j<m; j++) {
                    if(arr[i][j] == '.' && arr[i][j+1] == '.') {
                        cost += y;
                        j++;
                    }
                    else if(arr[i][j+1]=='.' && arr[i][j]!='.' ||
                        arr[i][j]=='.' && arr[i][j+1]!='.')
                        cost += x;
                }
            }
        }
        cost = min(cost, count_dots*x);
        cout<<cost<<endl;
    }
    return 0;
}


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


#include<iostream>
using namespace std;
int main()
{
    int tc;
    cin>>tc;
    while(tc--){
        int n,m,x,y,white=0,sum=0,k=0;
        cin>>n>>m>>x>>y;
        char ch[n+1][m+1];
        for(int i=0;i<=n;i++){
            for(int j=0;j<=m;j++){
                ch[i][j]='*';
            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cin>>ch[i][j];
            }
        }
        if(2*x<=y){
            for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                    if(ch[i][j]=='.') white++;
                }
            }
            cout<<white*x<<endl;
        }
//        cout<<white<<endl;
        else{
            for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                        if(ch[i][j]=='.'){
                            if((j+1)<m&&ch[i][j+1]=='.'){
                                sum+=y;
                                j++;
                            }
                            else {
                                sum+=x;
                            }
                        }
                }
            }
            cout<<sum<<endl;
        }
    }
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int tc;
    cin>>tc;
    while(tc--){
        int n,m,x,y,white=0,sum=0,k=0;
        cin>>n>>m>>x>>y;
        char ch[n+1][m+1];
        for(int i=0;i<=n;i++){
            for(int j=0;j<=m;j++){
                ch[i][j]='*';
            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cin>>ch[i][j];
            }
        }
        if(2*x<=y){
            for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                    if(ch[i][j]=='.') white++;
                }
            }
            cout<<white*x<<endl;
        }
//        cout<<white<<endl;
        else{
            for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                        if(ch[i][j]=='.'){
                            if((j+1)<m&&ch[i][j+1]=='.'){
                                sum+=y;
                                j++;
                            }
                            else {
                                sum+=x;
                            }
                        }
                }
            }
            cout<<sum<<endl;
        }
    }
    return 0;


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


#include<iostream>
#include<cmath>
using namespace std;
char ch[101][101];
int  main()
{
    int t;
    cin>>t;
    while(t--){
        int n,m,x,y,white=0,block=0;
        cin>>n>>m>>x>>y;
        for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            cin>>ch[i][j];
        for(int i=0;i<n;i++)
        for(int j=0;j<m;j++){
            if(ch[i][j]=='.') white++;
            if(ch[i][j]=='.'&&ch[i][j+1]=='.'&&m>1) block++;
        }
        //cout<<white<<endl;
        if(2*x<=y) cout<<x*white<<endl;
        else{
                if(block!=0)
            cout<<block*y+x*(white-block*2)<<endl;
                else
                    cout<<x*white<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
char ch[101][101];
int  main()
{
    int t;
    cin>>t;
    while(t--){
        int n,m,x,y,white=0,block=0,sum=0;
        cin>>n>>m>>x>>y;
        for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            cin>>ch[i][j];
        for(int i=0;i<n;i++)
        for(int j=0;j<m;j++){
            if(ch[i][j]=='.') white++;
        }
        //cout<<white<<endl;
        if(2*x<y) cout<<x*white<<endl;
        else if(m==1&&white>0) cout<<x*white<<endl;
        else{
            for(int i=0;i<n;i++){
                for(int j=0;j<m;){
                    if(ch[i][j]=='.'&&ch[i][j+1]=='*'&&m>1){
                        sum+=x;
                        j+=2;
                    }
                    else if(ch[i][j]=='.'&&ch[i][j+1]=='.'&&m>1){
                        sum+=y;
                        j+=2;
                    }
                }
            }
            cout<<sum<<endl;
        }
    }
    return 0;
}


#define ll long long
#define MOD 1000000007
#define f(i, a, b) for(int i=a; i<b; i++)
#include<bits/stdc++.h>
using namespace std;

int main()
{
 int t;
 cin>>t;
 while(t--){
    int n,m,x,y,adj=0,no_of=0; ll sum=0;
    cin>>n>>m>>x>>y;
    vector<vector<char> >v(n,vector<char>(m));
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin>>v[i][j];
            if(v[i][j]=='.')no_of++;
        }
    }
    for(int i=0;i<n;i++)
        for(int j=0;j<m-1;j++){
              if(v[i][j]==v[i][j+1]){
                adj++;
                j++;
              }
 }
 if(adj!=0){
   if(adj*y<adj*2*x){
    sum+=adj*y;
    no_of-=adj*2;
   }
   else if(adj*y>=adj*2*x){
    sum+=adj*2*x;
    no_of-=adj*2;
   }
      sum+=no_of*x;
      cout<<sum<<endl;
 }else{
     sum=no_of*x;
       cout<<sum<<endl;
 }

  }
}


#define ll long long
#define MOD 1000000007
#define f(i, a, b) for(int i=a; i<b; i++)
#include<bits/stdc++.h>
using namespace std;

int main()
{
 int t;
 cin>>t;
 while(t--){
    int n,m,x,y,sum=0,adj=0,no_of=0;
    cin>>n>>m>>x>>y;
    vector<vector<char> >v(n,vector<char>(m));
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin>>v[i][j];
            if(v[i][j]=='.')no_of++;
        }
    }
    for(int i=0;i<n;i++)
        for(int j=0;j<m-1;j++){
              if(v[i][j]==v[i][j+1]){
                adj++;
                j++;
              }
 }
 if(adj!=0){
   if(adj*y<adj*2*x){
    sum+=adj*y;
    no_of-=adj*2;
   }
   else if(adj*y>adj*2*x){
    sum+=adj*2*x;
    no_of-=adj*2;
   }
      sum+=no_of*x;
      cout<<sum<<endl;
 }else{
     sum=no_of*x;
       cout<<sum<<endl;
 }

  }
}


#define ll long long
#define MOD 1000000007
#define f(i, a, b) for(int i=a; i<b; i++)
#include<bits/stdc++.h>
using namespace std;

int main()
{
 int t;
 cin>>t;
 while(t--){
    int n,m,x,y,sum=0,adj=0,no_of=0;
    cin>>n>>m>>x>>y;
    vector<vector<char> >v(n,vector<char>(m));
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin>>v[i][j];
            if(v[i][j]=='.')no_of++;
        }
    }
    for(int i=0;i<n;i++)
        for(int j=0;j<m-1;j++){
              if(v[i][j]==v[i][j+1])adj++;
 }
 if(adj!=0){
   if(adj*y<adj*2*x){
    sum+=adj*y;
    no_of-=adj*2;
   }
   else if(adj*y>adj*2*x){
    sum+=adj*2*x;
    no_of-=adj*2;
   }
      sum+=no_of*x;
      cout<<sum<<endl;
 }else{
     sum=no_of*x;
       cout<<sum<<endl;
 }

  }
}


#define ll long long
#define MOD 1000000007
#define f(i, a, b) for(int i=a; i<b; i++)
#include<bits/stdc++.h>
using namespace std;

int main()
{
 int t;
 cin>>t;
 while(t--){
    ll n,m,x,y,adj=0,no_of=0; ll sum=0;
    cin>>n>>m>>x>>y;
    vector<vector<char> >v(n,vector<char>(m));
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin>>v[i][j];
            if(v[i][j]=='.')no_of++;
        }
    }
    for(int i=0;i<n;i++)
        for(int j=0;j<m-1;j++){
              if(v[i][j]==v[i][j+1]){
                adj++;
              }
 }
 if(adj!=0){
   if(adj*y<adj*2*x){
    sum+=adj*y;
    no_of-=adj*2;
   }
   else if(adj*y>=adj*2*x){
    sum+=adj*2*x;
    no_of-=adj*2;
   }
      sum+=no_of*x;
      cout<<sum<<endl;
 }else{
     sum=no_of*x;
       cout<<sum<<endl;
 }

  }
}


#define ll long long
#define MOD 1000000007
#define f(i, a, b) for(int i=a; i<b; i++)
#include<bits/stdc++.h>
using namespace std;

int main()
{
 int t;
 cin>>t;
 while(t--){
    ll n,m,x,y,adj=0,no_of=0; ll sum=0;
    cin>>n>>m>>x>>y;
    vector<vector<char> >v(n,vector<char>(m));
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin>>v[i][j];
            if(v[i][j]=='.')no_of++;
        }
    }
    for(int i=0;i<n;i++)
        for(int j=0;j<m-1;j++){
              if(v[i][j]==v[i][j+1]){
                adj++;
                j++;
              }
 }
 if(adj!=0){
   if(adj*y<adj*2*x){
    sum+=adj*y;
    no_of-=adj*2;
   }
      sum+=no_of*x;
      cout<<sum<<endl;
 }else{
     sum=no_of*x;
       cout<<sum<<endl;
 }

  }
}


#define ll long long
#define MOD 1000000007
#define f(i, a, b) for(int i=a; i<b; i++)
#include<bits/stdc++.h>
using namespace std;

int main()
{
 int t;
 cin>>t;
 while(t--){
    ll n,m,x,y,adj=0,no_of=0; ll sum=0;
    cin>>n>>m>>x>>y;
    vector<vector<char> >v(n,vector<char>(m));
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin>>v[i][j];
            if(v[i][j]=='.')no_of++;
        }
    }
    for(int i=0;i<n;i++)
        for(int j=0;j<m-1;j++){
              if(v[i][j]==v[i][j+1] && v[i][j]!='*'){
                adj++;
                j++;
              }
 }
 if(adj!=0){
   if(adj*y<adj*2*x){
    sum+=adj*y;
    no_of-=adj*2;
   }
      sum+=no_of*x;
      cout<<sum<<endl;
 }else{
     sum=no_of*x;
       cout<<sum<<endl;
 }

  }
}


#define ll long long
#define MOD 1000000007
#define f(i, a, b) for(int i=a; i<b; i++)
#include<bits/stdc++.h>
using namespace std;

int main()
{
 int t;
 cin>>t;
 while(t--){
    int n,m,x,y,adj=0,no_of=0; ll sum=0;
    cin>>n>>m>>x>>y;
    vector<vector<char> >v(n,vector<char>(m));
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin>>v[i][j];
            if(v[i][j]=='.')no_of++;
        }
    }
    for(int i=0;i<n;i++)
        for(int j=0;j<m-1;j++){
              if(v[i][j]==v[i][j+1]){
                adj++;
                j++;
              }
 }
 if(adj!=0){
   if(2*x>y){
    sum+=adj*y;
    no_of-=adj*2;
   }
      sum+=no_of*x;
      cout<<sum<<endl;
 }else{
     sum=no_of*x;
       cout<<sum<<endl;
 }

  }
}


#define ll long long
#define MOD 1000000007
#define f(i, a, b) for(int i=a; i<b; i++)
#include<bits/stdc++.h>
using namespace std;

int main()
{
 int t;
 cin>>t;
 while(t--){
    int n,m,x,y,adj=0,no_of=0; ll sum=0;
    cin>>n>>m>>x>>y;
    vector<vector<char> >v(n,vector<char>(m));
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin>>v[i][j];
            if(v[i][j]=='.')no_of++;
        }
    }
    for(int i=0;i<n;i++)
        for(int j=0;j<m-1;j++){
              if(v[i][j]==v[i][j+1]){
                adj++;
                j++;
              }
 }
 if(adj!=0){
   if(adj*y<adj*2*x){
    sum+=adj*y;
    no_of-=adj*2;
   }
      sum+=no_of*x;
      cout<<sum<<endl;
 }else{
     sum=no_of*x;
       cout<<sum<<endl;
 }

  }
}


#include <bits/stdc++.h>
using namespace std;

int main() {

  int t;
  cin>>t;
  while(t--){
     int n,m,x,y;
     cin>>n>>m>>x>>y;

     string s[n+1];

     for (int i = 1; i <=n; i++)
     {
          cin>>s[i];

     }
     int ans=0;

     if(x<=y){

     for(int i=1;i<=n;i++)
      {for(int j=0;j<m;j++)
      {
        if(s[i][j]=='.')
        {
          ans+=x;

        }

      }
      }
      cout<<ans<<endl;

     }
     else{
       int cnt=0;
       for(int i=1;i<=n;i++)
          { cnt=0;
            for(int j=0;j<m;j++)
            {if(s[i][j]=='.')
              cnt++;}

             if(cnt%2==0)
              ans+=(cnt/2)*y;

            else
              ans+=(cnt/2)*y+x;
            }
        cout<<ans<<endl;
     }

  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{

  int t;
  cin >> t;
  while (t--)
  {
    int n, m, x, y;
    cin >> n >> m >> x >> y;

    string s[n + 1];

    for (int i = 1; i <= n; i++)
    {
      cin >> s[i];
    }
    int ans = 0;

    if (2*x <= y)
    {

      for (int i = 1; i <= n; i++)
      {
        for (int j = 0; j < m; j++)
        {
          if (s[i][j] == '.')
          {
            ans += x;
          }
        }
      }
      cout << ans << endl;
    }
    else
    {

      int u = 1;
      int cnt = 0;
      int gg = 0;
      for (int i = 1; i <= n; i++)
      {
        cnt = 0;
        gg = 0;
        for (int j = 0; j < m; j += u)
        {
          if (j < m - 1)
          {

            if (s[i][j] == '.' && s[i][j + 1] == '.')
            {
              cnt++;
              u = 2;
            }
          }
          else if (s[i][j] == '.')
          {
            gg++;
            u = 1;
          }
        }

        ans += cnt * y + gg * x;
      }

      cout << ans << endl;
    }
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{

  int t;
  cin >> t;
  while (t--)
  {
    int n, m, x, y;
    cin >> n >> m >> x >> y;

    string s[n + 1];

    for (int i = 1; i <= n; i++)
    {
      cin >> s[i];
    }
    int ans = 0;

    if (2*x <= y)
    {

      for (int i = 1; i <= n; i++)
      {
        for (int j = 0; j < m; j++)
        {
          if (s[i][j] == '.')
          {
            ans += x;
          }
        }
      }
      cout << ans << endl;
    }
    else
    {

      int cnt = 0;
      int gg = 0;

      for (int i = 1; i <= n; i++)
      {
        cnt = 0;
        gg = 0;
        int j=0;
        while(j<=m-1)
        {
          if (j < m - 1)
          {

            if (s[i][j] == '.' && s[i][j + 1] == '.')
            {
              cnt++;
              j+= 2;
            }
            else if(s[i][j]=='.')
                {gg++;j++;}

          }
           if (s[i][j] == '.')
          {
            gg++;
            j+= 1;
          }
          else {
            j++;
          }
        }
        ans += cnt * y + gg * x;
      }

      cout << ans << endl;
    }
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{

  int t;
  cin >> t;
  while (t--)
  {
    long int n, m, x, y;
    cin >> n >> m >> x >> y;

    string s[n + 1];

    for (int i = 1; i <= n; i++)
    {
      cin >> s[i];
    }
    long int ans = 0;

    if (2*x <= y)
    {

      for (int i = 1; i <= n; i++)
      {
        for (int j = 0; j < m; j++)
        {
          if (s[i][j] == '.')
          {
            ans += x;
          }
        }
      }
      cout << ans << endl;
    }
    else
    {

      int cnt = 0;
      int gg = 0;

      for (int i = 1; i <= n; i++)
      {
        cnt = 0;
        gg = 0;
        int j=0;
        while(j<=m-1)
        {
          if (j < m - 1)
          {

            if (s[i][j] == '.' && s[i][j + 1] == '.')
            {
              cnt++;
              j+= 2;
            }
            else if(s[i][j]=='.')
                {gg++;j++;}

          }
           if (s[i][j] == '.')
          {
            gg++;
            j+= 1;
          }
          else {
            j++;
          }
        }
        ans += cnt * y + gg * x;
      }

      cout << ans << endl;
    }
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

int solve(int n,int x,int y)
{
    return min((n/2)*y + (n%2)*x , x*n);
}

int main()
{

  int t;
  cin >> t;
  while (t--)
  {
    long int n, m, x, y;
    cin >> n >> m >> x >> y;

    string s[n + 1];

    for (int i = 1; i <= n; i++)
    {
      cin >> s[i];
    }
    long int ans = 0;
    for (int i = 1; i <= n; i++)
    {
      long int cnt = 0;

      for (int j = 0; j < m; j++)
      {
        if (s[i][j] == '.')
          cnt++;

        else
        {
          ans+=solve(cnt,x,y);
                    cnt = 0;
      }
      if (cnt > 0)
      {

        ans+=solve(cnt,x,y);
                    cnt = 0;
      }
    }

    cout << ans << endl;
  }
  }
return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {

  int t;
  cin>>t;
  while(t--){
     int n,m,x,y;
     cin>>n>>m>>x>>y;

     string s[n];

     for (int i = 1; i <=n; i++)
     {
          cin>>s[i];

     }
     int ans=0;

     if(x<=y){

     for(int i=1;i<=n;i++)
      {for(int j=0;j<m;j++)
      {
        if(s[i][j]=='.')
        {
          ans+=x;

        }

      }
      }
      cout<<ans<<endl;

     }
     else{
       int cnt=0;
       for(int i=1;i<=n;i++)
          { cnt=0;
            for(int j=0;j<m;j++)
            {if(s[i][j]=='.')
              cnt++;}

             if(cnt%2==0)
              ans+=(cnt/2)*y;

            else
              ans+=(cnt/2)*y+x;
            }
        cout<<ans<<endl;
     }

  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{

  int t;
  cin >> t;
  while (t--)
  {
    int n, m, x, y;
    cin >> n >> m >> x >> y;

    string s[n + 1];

    for (int i = 1; i <= n; i++)
    {
      cin >> s[i];
    }
    int ans = 0;

    if (x <= y)
    {

      for (int i = 1; i <= n; i++)
      {
        for (int j = 0; j < m; j++)
        {
          if (s[i][j] == '.')
          {
            ans += x;
          }
        }
      }
      cout << ans << endl;
    }
    else
    {

      int u = 1;
      int cnt = 0;
      int gg = 0;
      for (int i = 1; i <= n; i++)
      {
        cnt = 0;
        gg = 0;
        for (int j = 0; j < m; j += u)
        {
          if (j < m - 1)
          {

            if (s[i][j] == '.' && s[i][j + 1] == '.')
            {
              cnt++;
              u = 2;
            }
          }
          else if (s[i][j] == '.')
          {
            gg++;
            u = 1;
          }
        }

        ans += cnt * y + gg * x;
      }

      cout << ans << endl;
    }
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

int solve(int n,int x,int y)
{
    return min((n/2)*y + (n%2)*x , x*n);
}

int main()
{

  int t;
  cin >> t;
  while (t--)
  {
    long int n, m, x, y;
    cin >> n >> m >> x >> y;

    string s[n + 1];

    for (int i = 1; i <= n; i++)
    {
      cin >> s[i];
    }
    long int ans = 0;
    for (int i = 1; i <= n; i++)
    {
      long int cnt = 0;

      for (int j = 0; j < m; j++)
      {
        if (s[i][j] == '.')
          cnt++;

        else
        {
          ans+=solve(cnt,x,y);
                    cnt = 0;
      }

    }
    if (cnt > 0)
      {

        ans+=solve(cnt,x,y);
                    cnt = 0;
      }

  }
  cout<<ans<<endl;
  }
return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define code ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define int long long
#define endl "\n"
#define pb push_back
#define rep(i,j,k) for(int i=j;i<k;i+=1)
int a[1000005];
int32_t main(){
    code;
    int t;
    cin>>t;
    //t=1;
    while(t--){
             int n,m,one,two;
             cin>>n>>m>>one>>two;
             char c[n][m];
             int w=0;
             if(2*one<=two){
                    for(int i=0;i<n;i++){
                 for(int j=0;j<m;j++){
                     if(c[i][j]=='.'){
                         w++;
                     }
                 }
             }
                 cout<<one*w<<endl;
             }
             else {
                 int ans1=0;
                 int ans2=0;
                 for(int i=0;i<n;i++){
                     for(int j=0;j<m;){
                          if(c[i][j]=='.' && c[i][j+1]=='.' &&j+1<m){
                              ans2++;
                              j+=2;
                          }
                         else if(c[i][j]=='.'){
                             ans1++;
                             j+=1;
                         }
                         else j++;

                     }
                 }
                 cout<<one*ans1+two*ans2<<endl;
             }
         }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define code ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define int long long
#define endl "\n"
#define pb push_back
#define rep(i,j,k) for(int i=j;i<k;i+=1)
int a[1000005];
int32_t main(){
    code;
    int t;
    cin>>t;
    //t=1;
    while(t--){
             int n,m,one,two;
             cin>>n>>m>>one>>two;
             char c[n][m];
             int w=0;
             for(int i=0;i<n;i++){
                 for(int j=0;j<m;j++){
                     cin>>c[i][j];
                     if(c[i][j]=='.'){
                         w++;
                     }
                 }
             }
             if(2*one<=two){
                 cout<<one*w<<endl;
             }
             else {
                 int ans1=0;
                 int ans2=0;
                 for(int i=0;i<n;i++){
                     for(int j=0;j<m;j++){
                         if(j==m-1){
                             if(c[i][j]=='.'){
                                 ans1++;
                             }
                         }
                         else {
                             if(c[i][j]=='.'){
                                 if(c[i][j+1]=='.'){
                                     ans2++;
                                     j+=2;
                                 }
                                 else{
                                     ans1++;
                                     j++;
                                 }
                             }
                         }
                     }

                 }
                 cout<<one*ans1+two*ans2<<endl;
             }
         }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define code ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define int long long
#define endl "\n"
#define pb push_back
#define rep(i,j,k) for(int i=j;i<k;i+=1)
int a[1000005];
int32_t main(){
    code;
    int t;
    cin>>t;
    //t=1;
    while(t--){
             int n,m,one,two;
             cin>>n>>m>>one>>two;
             string s[n+1];
             rep(i,0,n){
                 cin>>s[i];
                 s[i]+="*";
             }
             int w=0;
             for(int i=0;i<n;i++){
                 //cout<<s[i]<<endl;
                 for(int j=0;j<s[i].length();j++){
                     if(s[i][j]=='.') w++;
                 }

             }
             if(2*one<=two){
                 cout<<one*w<<endl;
             }
             else {
                 int ans1=0;
                 int ans2=0;
                 for(int i=0;i<n;i++){
                 for(int j=0;j<s[i].length()-1;){
                     if(s[i][j]=='.'){
                         if(s[i][j+1]=='.'){
                             ans2++;
                             j+=2;
                         }
                         else {
                             ans1++;
                             j++;
                         }
                     }
                 }
             }
             cout<<ans1*one+ans2*two<<endl;

             }
         }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define code ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define int long long
#define endl "\n"
#define pb push_back
#define rep(i,j,k) for(int i=j;i<k;i+=1)
int32_t main(){
    code;
    int t;
    cin>>t;
    //t=1;
    while(t--){
             int n,m,one,two;
             cin>>n>>m>>one>>two;
            vector<string >c
             int w=0;
                  for(int i=0;i<n;i++){
                      string s;
                      cin>>s;
                      c.pb(s);
                 }
             if(2*one<=two){
                for(int i=0;i<n;i++){
                     for(int j=0;j<m;j++){
                     cin>>c[i][j];
                     if(c[i][j]=='.'){
                         w++;
                     }
                 }
                  }
                 cout<<one*w<<endl;
             }

             else {
                 int ans1=0;
                 int ans2=0;
                 for(int i=0;i<n;i++){
                     for(int j=0;j<m;){
                          if(c[i][j]=='.' && c[i][j+1]=='.' &&j+1<m){
                              ans2++;
                              j+=2;
                          }
                         else if(c[i][j]=='.'){
                             ans1++;
                             j+=1;
                         }

                     }
                 }
                 cout<<one*ans1+two*ans2<<endl;
             }
         }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define code ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
//#define int long long
#define endl "\n"
#define pb push_back
#define rep(i,j,k) for(int i=j;i<k;i+=1)
int32_t main(){
    code;
    int t;
    cin>>t;
    //t=1;
    while(t--){
             int n,m,one,two;
             cin>>n>>m>>one>>two;
            vector<string >c;
                  for(int i=0;i<n;i++){
                      string s;
                      cin>>s;
                      c.pb(s);
                 }
             if(2*one<=two){
                   int w=0;
                for(int i=0;i<n;i++){
                     for(int j=0;j<m;j++){
                     if(c[i][j]=='.'){
                         w++;
                     }
                 }
                  }
                 cout<<one*w<<endl;
             }

             else {
                 int ans1=0;
                 int ans2=0;
                 for(int i=0;i<n;i++){
                     for(int j=0;j<m;j++){
                          if(c[i][j]=='.' && c[i][j+1]=='.' &&j+1<m){
                              ans2++;
                              j++;
                          }
                         else if(c[i][j]=='.'){
                             ans1++;
                         }

                     }
                 }
                 cout<<one*ans1+two*ans2<<endl;
             }
         }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define code ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define int long long
#define endl "\n"
#define pb push_back
#define rep(i,j,k) for(int i=j;i<k;i+=1)
int32_t main(){
    code;
    int t;
    cin>>t;
    //t=1;
    while(t--){
             int n,m,one,two;
             cin>>n>>m>>one>>two;
             char c[n][m];
             int w=0;
                  for(int i=0;i<n;i++){
                 for(int j=0;j<m;j++){
                     cin>>c[i][j];
                     if(c[i][j]=='.'){
                         w++;
                     }
                 }
                  }
             if(2*one<=two){

                 cout<<one*w<<endl;
             }

             else {
                 int ans1=0;
                 int ans2=0;
                 for(int i=0;i<n;i++){
                     for(int j=0;j<m;){
                          if(c[i][j]=='.' && c[i][j+1]=='.' &&j+1<m){
                              ans2++;
                              j+=2;
                          }
                         else if(c[i][j]=='.'){
                             ans1++;
                             j+=1;
                         }
                         else j++;

                     }
                 }
                 cout<<one*ans1+two*ans2<<endl;
             }
         }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define code ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define int long long
#define endl "\n"
#define pb push_back
#define rep(i,j,k) for(int i=j;i<k;i+=1)
int a[1000005];
int32_t main(){
    code;
    int t;
    cin>>t;
    //t=1;
    while(t--){
             int n,m,one,two;
             cin>>n>>m>>one>>two;
             char c[n][m];
             int w=0;
             for(int i=0;i<n;i++){
                 for(int j=0;j<m;j++){
                     cin>>c[i][j];
                     if(c[i][j]=='.'){
                         w++;
                     }
                 }
             }
             if(2*one<=two){
                 cout<<one*w<<endl;
             }
             else {
                 int ans1=0;
                 int ans2=0;
                 for(int i=0;i<n;i++){
                     for(int j=0;j<m;){
                          if(c[i][j]=='.' && c[i][j+1]=='.' &&j+1<m){
                              ans2++;
                              j+=2;
                          }
                         else if(c[i][j]=='.'){
                             ans1++;
                             j+=1;
                         }

                     }
                 }
                 cout<<one*ans1+two*ans2<<endl;
             }
         }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define code ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define int long long
#define endl "\n"
#define pb push_back
#define rep(i,j,k) for(int i=j;i<k;i+=1)
int32_t main(){
    code;
    int t;
    cin>>t;
    //t=1;
    while(t--){
             int n,m,one,two;
             cin>>n>>m>>one>>two;
             char c[n][m];
             int w=0;
                  for(int i=0;i<n;i++){
                 for(int j=0;j<m;j++){
                     cin>>c[i][j];
                     if(c[i][j]=='.'){
                         w++;
                     }
                 }
                  }
             if(2*one<=two){

                 cout<<one*w<<endl;
             }

             else {
                 int ans1=0;
                 int ans2=0;
                 for(int i=0;i<n;i++){
                     for(int j=0;j<m;){
                          if(c[i][j]=='.' && c[i][j+1]=='.' &&j+1<m){
                              ans2++;
                              j+=2;
                          }
                         else if(c[i][j]=='.'){
                             ans1++;
                             j+=1;
                         }

                     }
                 }
                 cout<<one*ans1+two*ans2<<endl;
             }
         }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define code ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
//#define int long long
#define endl "\n"
#define pb push_back
#define rep(i,j,k) for(int i=j;i<k;i+=1)
int32_t main(){
    code;
    int t;
    cin>>t;
    //t=1;
    while(t--){
             int n,m,one,two;
             cin>>n>>m>>one>>two;
             char c[n][m];
             int w=0;
             for(int i=0;i<n;i++){
                 for(int j=0;j<m;j++){
                     cin>>c[i][j];
                     if(c[i][j]=='.'){
                         w++;
                     }
                 }
             }
             if(2*one<=two){
                 cout<<one*w<<endl;
             }
             else {
                 int ans1=0;
                 int ans2=0;
                 for(int i=0;i<n;i++){
                     for(int j=0;j<m;){
                         if(j==m-1){
                             if(c[i][j]=='.'){
                                 ans1++;
                                 j++;
                             }
                         }
                         else {
                             if(c[i][j]=='.'){
                                 if(c[i][j+1]=='.'){
                                     ans2++;
                                     j+=2;
                                 }
                                 else{
                                     ans1++;
                                     j++;
                                 }
                             }
                         }
                     }

                 }
                 cout<<one*ans1+two*ans2<<endl;
             }
         }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define code ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define int long long
#define endl "\n"
#define pb push_back
#define rep(i,j,k) for(int i=j;i<k;i+=1)
int a[1000005];
int32_t main(){
    code;
    int t;
    cin>>t;
    //t=1;
    while(t--){
             int n,m,one,two;
             cin>>n>>m>>one>>two;
             char c[n][m];
             int w=0;
             for(int i=0;i<n;i++){
                 for(int j=0;j<m;j++){
                     cin>>c[i][j];
                     if(c[i][j]=='.'){
                         w++;
                     }
                 }
             }
             if(2*one<=two){
                 cout<<one*w<<endl;
             }
             else {
                 int ans1=0;
                 int ans2=0;
                 for(int i=0;i<n;i++){
                     for(int j=0;j<m;){
                         if(j==m-1){
                             if(c[i][j]=='.'){
                                 ans1++;
                                 j++;
                             }
                         }
                         else {
                             if(c[i][j]=='.'){
                                 if(c[i][j+1]=='.'){
                                     ans2++;
                                     j+=2;
                                 }
                                 else{
                                     ans1++;
                                     j++;
                                 }
                             }
                         }
                     }

                 }
                 cout<<one*ans1+two*ans2<<endl;
             }
         }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define code ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define int long long
#define endl "\n"
#define pb push_back
#define rep(i,j,k) for(int i=j;i<k;i+=1)
int a[1000005];
int32_t main(){
    code;
    int t;
    cin>>t;
    //t=1;
    while(t--){
             int n,m,one,two;
             cin>>n>>m>>one>>two;
             char c[n][m];
             int w=0;
             if(2*one<=two){
                    for(int i=0;i<n;i++){
                 for(int j=0;j<m;j++){
                     cin>>c[i][j];
                     if(c[i][j]=='.'){
                         w++;
                     }
                 }
             }
                 cout<<one*w<<endl;
             }
             else {
                 int ans1=0;
                 int ans2=0;
                 for(int i=0;i<n;i++){
                     for(int j=0;j<m;){
                          if(c[i][j]=='.' && c[i][j+1]=='.' &&j+1<m){
                              ans2++;
                              j+=2;
                          }
                         else if(c[i][j]=='.'){
                             ans1++;
                             j+=1;
                         }

                     }
                 }
                 cout<<one*ans1+two*ans2<<endl;
             }
         }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define code ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
//#define int long long
#define endl "\n"
#define pb push_back
#define rep(i,j,k) for(int i=j;i<k;i+=1)
int32_t main(){
    code;
    int t;
    cin>>t;
    //t=1;
    while(t--){
             int n,m,one,two;
             cin>>n>>m>>one>>two;
            vector<string >c;
                  for(int i=0;i<n;i++){
                      string s;
                      cin>>s;
                      c.pb(s);
                 }
             if(2*one<=two){
                   int w=0;
                for(int i=0;i<n;i++){
                     for(int j=0;j<m;j++){
                     cin>>c[i][j];
                     if(c[i][j]=='.'){
                         w++;
                     }
                 }
                  }
                 cout<<one*w<<endl;
             }

             else {
                 int ans1=0;
                 int ans2=0;
                 for(int i=0;i<n;i++){
                     for(int j=0;j<m;){
                          if(c[i][j]=='.' && c[i][j+1]=='.' &&j+1<m){
                              ans2++;
                              j+=2;
                          }
                         else if(c[i][j]=='.'){
                             ans1++;
                             j+=1;
                         }

                     }
                 }
                 cout<<one*ans1+two*ans2<<endl;
             }
         }

    return 0;
}


#include <bits/stdc++.h>
#include <iomanip>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <numeric>
#include <map>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long int h,c,t,ans=0,temp=0,i=0,curr;
        cin>>h>>c>>t;
        if(floor(abs(t-h))==0)
            ans=1;
        else
        {
            long long int a,b;
            a=h-t;
            b=t-c;
            if(a==b)
                ans=2;
            else if(a==0)
                ans=1;
            else if(a>b)
            {
                long long int r=a/b;
                ans=2*r;
            }
            else
            {
                ans=a/abs(b-a);
                if(a%abs(b-a)!=0)
                    ans++;
                ans=2*ans+1;
            }
        }
        cout<<ans<<endl;
    }
}


#include <bits/stdc++.h>
#include <iomanip>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <numeric>
#include <map>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,m,x,y;
        cin>>n>>m>>x>>y;
        char a[n][m],i,j;
        long long int sum=0,count=0,count1=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                cin>>a[i][j];
                if(a[i][j]=='.')
                    count++;
            }
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(a[i][j]=='.' && a[i][j+1]=='.' && j+1<m)
                {
                    count1++;
                    j++;
                }
            }
        }
        long long int p=x*count;
        long long int q=x*(count-2*count1)+y*count1;
        sum=min(p,q);
        cout<<sum<<endl;

    }

}


#include <bits/stdc++.h>
#include <iomanip>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <numeric>
#include <map>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long int n,m,x,y;
        cin>>n>>m>>x>>y;
        char a[n][m],i,j;
        long long int sum=0,count=0,count1=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
                cin>>a[i][j];
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(a[i][j]=='.' && j+1<m)
                {
                    if(a[i][j+1]=='.')
                    {
                        count1++;
                        j++;
                    }
                }
                if(a[i][j]=='.')
                    count++;
            }
        }
        count=count+count1;
        long long int p=x*count;
        long long int q=x*(count-2*count1)+y*count1;
        sum=min(p,q);
        cout<<sum<<endl;

    }

}


#include <bits/stdc++.h>
#include <iomanip>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <numeric>
#include <map>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long int n,m,x,y,i,j;
        cin>>n>>m>>x>>y;
        char a[n][m];
        long long int sum=0,count=0,count1=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
                cin>>a[i][j];
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(a[i][j]=='.' && j+1<m)
                {
                    if(a[i][j+1]=='.')
                    {
                        count1++;
                        j++;
                    }
                }
                if(a[i][j]=='.')
                    count++;
            }
        }
        count=count+count1;
        long long int p=x*count;
        long long int q=x*(count-2*count1)+y*count1;
        sum=min(p,q);
        cout<<sum<<endl;

    }

}


#include <bits/stdc++.h>
#include <iomanip>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <numeric>
#include <map>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,m,x,y;
        cin>>n>>m>>x>>y;
        char a[n][m],i,j;
        long long int sum=0,count=0,count1=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
                cin>>a[i][j];
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(a[i][j]=='.' && a[i][j+1]=='.' && j+1<m)
                {
                    count1++;
                    count=count+2;
                    j++;
                }
                else if(a[i][j]=='.')
                    count++;
            }
        }
        long long int p=x*count;
        long long int q=x*(count-2*count1)+y*count1;
        sum=min(p,q);
        cout<<sum<<endl;

    }

}


#include <bits/stdc++.h>
#include <iomanip>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <numeric>
#include <map>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long int n,m,x,y;
        cin>>n>>m>>x>>y;
        char a[n][m],i,j;
        long long int sum=0,count=0,count1=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
                cin>>a[i][j];
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(a[i][j]=='.' && a[i][j+1]=='.' && j+1<m)
                {
                    count1++;
                    count=count+2;
                    j++;
                }
                else if(a[i][j]=='.')
                    count++;
            }
        }
        long long int p=x*count;
        long long int q=x*(count-2*count1)+y*count1;
        sum=min(p,q);
        cout<<sum<<endl;

    }

}


#include <bits/stdc++.h>
#include <iomanip>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <numeric>
#include <map>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,m,x,y;
        cin>>n>>m>>x>>y;
        char a[n][m],i,j;
        long long int sum=0,count=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                cin>>a[i][j];
                if(a[i][j]=='.')
                    count++;
            }
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
             {
                 if(a[i][j]=='.')
                 {
                    if(a[i][j+1]=='.' && j+1<m)
                    {
                        sum=sum+y;
                        j++;
                    }
                    else
                        sum=sum+x;
                    //cout<<sum<<endl;
                 }
             }
        }
        //cout<<count<<endl;
        cout<<min(x*count,sum)<<endl;

    }

}


#include<bits/stdc++.h>
#define ll long long int
#define PI 3.14159265358979323846
#define ed "\n"
#define sp " "
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,m,x,y;
        cin>>n>>m>>x>>y;
        ll xpoint=0;
        ll ypoint=0;
        ll flag=0;
        for(ll i=0;i<n;i++)
        {
            char ch[m];
            for(ll j=0;j<m;j++)
            {
                cin>>ch[j];
                if(ch[j]=='.')
                {
                    xpoint++;
                    if(j>0&&ch[j-1]=='.'){
                        ypoint++;
                        flag++;
                        if(flag>1&&flag%2==1) ypoint--;

                }
                }
                else
                {
                    ypoint=0;
                    flag=0;
                }
            }
            flag=0;
            ypoint=0;
        }
        if(x<=y/2||ypoint==0)
            cout<<xpoint*x<<ed;
        else
            cout<<ypoint*y+(xpoint-2*ypoint)*x<<ed;
    }
return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define PI 3.14159265358979323846
#define ed "\n"
#define sp " "
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,m,x,y;
        cin>>n>>m>>x>>y;
        ll xpoint=0;
        ll ypoint=0;
        ll flag=0;
        for(ll i=0;i<n;i++)
        {
            char ch[m];
            for(ll j=0;j<m;j++)
            {
                cin>>ch[j];
                if(ch[j]=='.')
                {
                    xpoint++;
                    if(j>0&&ch[j-1]=='.'){
                        ypoint++;
                        flag++;
                        if(flag>1&&flag%2==1) ypoint--;

                }
                }
                else
                {
                    flag=0;
                }
            }
            flag=0;
        }
        if(x<=y/2||ypoint==0)
            cout<<xpoint*x<<ed;
        else
            cout<<ypoint*y+(xpoint-2*ypoint)*x<<ed;
    }
return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define PI 3.14159265358979323846
#define ed "\n"
#define sp " "
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,m,x,y;
        cin>>n>>m>>x>>y;
        ll xpoint=0;
        ll ypoint=0;
        ll flag=0;
        for(ll i=0;i<n;i++)
        {
            char ch[m];
            for(ll j=0;j<m;j++)
            {
                cin>>ch[j];
                if(ch[j]=='.')
                {
                    xpoint++;
                    if(j>0&&ch[j-1]=='.'){
                        ypoint++;
                        flag++;
                        if(flag%2==0) ypoint--;

                }
                }
                else
                {
                    flag=0;
                }
            }
            flag=0;
        }
        if(x<=y/2)
            cout<<xpoint*x<<ed;
        else
            cout<<ypoint*y+(xpoint-2*ypoint)*x<<ed;
    }
return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define PI 3.14159265358979323846
#define ed "\n"
#define sp " "
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,m,x,y;
        cin>>n>>m>>x>>y;
        ll xpoint=0;
        ll ypoint=0;
        for(ll i=0;i<n;i++)
        {
            char ch[m];
            for(ll j=0;j<m;j++)
            {
                cin>>ch[j];
                if(ch[j]=='.')
                {
                    xpoint++;
                    if(j>0&&ch[j-1]=='.')
                        ypoint++;
                }
            }
        }
        if(x<=y/2||ypoint==0)
            cout<<xpoint*x<<ed;
        else
            cout<<ypoint*y+(xpoint-2*ypoint)*x<<ed;
    }
return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define PI 3.14159265358979323846
#define ed "\n"
#define sp " "
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,m,x,y;
        cin>>n>>m>>x>>y;
        ll xpoint=0;
        ll ypoint=0;
        ll flag=0;
        for(ll i=0;i<n;i++)
        {
            char ch[m];
            for(ll j=0;j<m;j++)
            {
                cin>>ch[j];
                if(ch[j]=='.')
                {
                    xpoint++;
                    if(j>0&&ch[j-1]=='.'){
                        ypoint++;
                        flag++;

                        if(flag>1&&flag%2==1) ypoint--;

                }
                }
            }
            flag=0;
        }
        if(x<=y/2||ypoint==0)
            cout<<xpoint*x<<ed;
        else
            cout<<ypoint*y+(xpoint-2*ypoint)*x<<ed;
    }
return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define PI 3.14159265358979323846
#define ed "\n"
#define sp " "
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,m,x,y;
        cin>>n>>m>>x>>y;
        ll xpoint=0;
        ll ypoint=0;
        ll flag=0;
        for(ll i=0;i<n;i++)
        {
            char ch[m];
            for(ll j=0;j<m;j++)
            {
                cin>>ch[j];
                if(ch[j]=='.')
                {
                    xpoint++;
                    if(j>0&&ch[j-1]=='.'){
                        ypoint++;
                        flag++;
                        if(flag>1&&flag%2==1) ypoint--;

                }
                }
                else
                {
                    ypoint=0;
                    flag=0;
                }
            }
            flag=0;
        }
        if(x<=y/2||ypoint==0)
            cout<<xpoint*x<<ed;
        else
            cout<<ypoint*y+(xpoint-2*ypoint)*x<<ed;
    }
return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define PI 3.14159265358979323846
#define ed "\n"
#define sp " "
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,m,x,y;
        cin>>n>>m>>x>>y;
        ll xpoint=0;
        ll ypoint=0;
        for(ll i=0;i<n;i++)
        {
            char ch[m];
            for(ll j=0;j<m;j++)
            {
                cin>>ch[j];
                if(ch[j]=='.')
                {
                    xpoint++;
                    if(j>0&&ch[j-1]=='.')
                        ypoint++;
                }
            }
        }
        if(x<=y/2||ypoint==0)
            cout<<xpoint*x<<ed;
        else
            cout<<xpoint-ypoint<<ed;
    }
return 0;
}


#include <iostream>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, m, x, y;
        cin >> n >> m >> x >> y;
        int two = 0, one = 0;
        char a[n][m];
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                cin >> a[i][j];
            }
        }
        if (m != 1)
        {
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m - 1; j++)
                {
                    if (a[i][j] == '.' && a[i][j + 1] == '.' && j + 1 < m)
                    {
                        two++;
                    }
                    else if (a[i][j] == '.')
                    {
                        one++;
                    }
                }
            }
            if (2 * x >= y)
            {
                cout << ((two * 1LL * y) + (one * 1LL * x)) << endl;
            }
            else
            {
                cout << ((two * 1LL * 2) + one) * 1LL * x << endl;
            }
        }
        else
        {
            for (int i = 0; i < n; i++)
            {
                if (a[i][0] == '.')
                {
                    one++;
                }
            }
            cout << one * 1LL * x << endl;
        }
    }
}


#include <iostream>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, m, x, y;
        cin >> n >> m >> x >> y;
        int two = 0, one = 0;
        char a[n][m];
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                cin >> a[i][j];
            }
        }
        if (m != 1)
        {
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if ((a[i][j] == '.' && a[i][j + 1] == '.'))
                    {
                        two++;
                        j++;
                    }
                    else if (a[i][j] == '.')
                    {
                        one++;
                    }
                }
            }
            if ((2 * x) > y)
            {
                cout << ((two * 1LL * y) + (one * 1LL * x)) << endl;
            }
            else
            {
                cout << ((two * 1LL * 2) + one) * 1LL * x << endl;
            }
        }
        else
        {
            for (int i = 0; i < n; i++)
            {
                if (a[i][0] == '.')
                {
                    one++;
                }
            }
            cout << one * 1LL * x << endl;
        }
    }
}


#include <iostream>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, m, x, y;
        cin >> n >> m >> x >> y;
        int two = 0, one = 0;
        char a[n][m];
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                cin >> a[i][j];
            }
        }
        if (m != 1)
        {
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if ((a[i][j] == '.' && a[i][j + 1] == '.'))
                    {
                        two++;
                        j++;
                    }
                    else if (a[i][j] == '.')
                    {
                        one++;
                    }
                }
            }
            if ((2 * x) > y)
            {
                cout << ((two * 1LL * y) + (one * 1LL * x)) << endl;
            }
            else
            {
                cout << ((two * 2) + one) * 1LL * x << endl;
            }
        }
        else
        {
            for (int i = 0; i < n; i++)
            {
                if (a[i][0] == '.')
                {
                    one++;
                }
            }
            cout << one * 1LL * x << endl;
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
#define int long long int

int32_t main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, m, x, y;
        cin >> n >> m >> x >> y;
        char a[n][m];
        int one = 0, two = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                cin >> a[i][j];
            }
        }
        for (int i = 0; i < n; i++)
        {
            int temp = 0;
            for (int j = 0; j < m; j++)
            {
                if (a[i][j] == '*')
                {
                    two += temp / 2;
                    one += temp % 2;
                    temp = 0;
                }
                else
                {
                    temp++;
                }
            }
            two += temp / 2;
            one += temp % 2;
        }
        cout << min((x * one + y * two), x * (one + 2 * two)) << endl;
    }
    return 0;
}


#include <iostream>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, m, x, y;
        cin >> n >> m >> x >> y;
        int two = 0, one = 0;
        char a[n][m];
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                cin >> a[i][j];
            }
        }
        if (m != 1)
        {
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m - 1; j++)
                {
                    if (a[i][j] == '.' && a[i][j + 1] == '.' && j + 1 < m)
                    {
                        two++;
                    }
                    else if (a[i][j] == '.')
                    {
                        one++;
                    }
                }
            }
            if (2 * x >= y)
            {
                cout << ((two * y) + (one * x)) << endl;
            }
            else
            {
                cout << ((two * 2) + one) * x << endl;
            }
        }
        else
        {
            for (int i = 0; i < n; i++)
            {
                if (a[i][0] == '.')
                {
                    one++;
                }
            }
            cout << one * x << endl;
        }
    }
}


#include <iostream>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, m, x, y;
        cin >> n >> m >> x >> y;
        int two = 0, one = 0;
        char a[n][m];
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                cin >> a[i][j];
            }
        }
        if (m != 1)
        {
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m - 1; j++)
                {
                    if ((a[i][j] == '.' && a[i][j + 1] == '.'))
                    {
                        two++;
                    }
                    else if (a[i][j] == '.')
                    {
                        one++;
                    }
                }
            }
            if ((2 * x) > y)
            {
                cout << ((two * 1LL * y) + (one * 1LL * x)) << endl;
            }
            else
            {
                cout << ((two * 1LL * 2) + one) * 1LL * x << endl;
            }
        }
        else
        {
            for (int i = 0; i < n; i++)
            {
                if (a[i][0] == '.')
                {
                    one++;
                }
            }
            cout << one * 1LL * x << endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t,n,m,x,y;
    cin>>t;
    while(t--)
    {
        cin>>n>>m>>x>>y;
        char ar[n+1][m+1];
        int ans[n+1][m+1];
        memset(ans,0,sizeof(ans));
        int cnt=0;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                cin>>ar[i][j];
                if(ar[i][j]=='.')
                    cnt++;

            }
        }
        for(int i=0; i<n; i++)
        {
            int dot=0;
            for(int j=0; j<m; j++)
            {
                if(ar[i][j]=='.' && ar[i][j+1]=='.')
                {
                    ans[i][j]=0;
                    ans[i][j+1]=2;
                    j+=2;
                }
                else if(ar[i][j]=='.')
                {
                    ans[i][j]=1;
                }

            }
        }
        int fin=0;
        for(int i=0; i<n; i++)
        {

            for(int j=0; j<m; j++)
            {
                if(ans[i][j]==1)
                {
                    fin+=x;
                }
                else if(ans[i][j]==2)
                {
                    fin+=(y);
                }
            }
        }
        cout<<min(fin,cnt*x)<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t,n,m,x,y;
    cin>>t;
    while(t--)
    {
        cin>>n>>m>>x>>y;
        char ar[n+1][m+1];
        int ans[n+1][m+1];
        memset(ans,0,sizeof(ans));
        int cnt=0;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                cin>>ar[i][j];
                if(ar[i][j]=='.')
                    cnt++;

            }
        }
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(ar[i][j]=='.')
                {
                    if(ar[i][j+1]=='.' && j+1<m)
                    {
                        ans[i][j+1]=2;
                        j+=2;
                    }
                    else
                    {
                        ans[i][j]=1;
                    }
                }

            }
        }
//        for(int i=0; i<n; i++)
//        {
//
//            for(int j=0; j<m; j++)
//            {
//                cout<<ans[i][j]<<" ";
//            }
//            cout<<endl;
//        }
        int fin=0;
        for(int i=0; i<n; i++)
        {

            for(int j=0; j<m; j++)
            {
                if(ans[i][j]==1)
                {
                    fin+=x;
                }
                else if(ans[i][j]==2)
                {
                    fin+=(y);
                }
            }
        }
        if(fin==0)
            fin=INT_MAX;

        cout<<min(fin,cnt*x)<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t,n,m,x,y;
    cin>>t;
    while(t--)
    {
        cin>>n>>m>>x>>y;
        char ar[n+1][m+1];
        int ans[n+1][m+1];
        memset(ans,0,sizeof(ans));
        int cnt=0;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                cin>>ar[i][j];
                if(ar[i][j]=='.')
                    cnt++;

            }
        }
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(ar[i][j]=='.')
                {
                    ans[i][j]=1;
                }

            }
        }

        int cnt1=0;
        for(int i=0; i<n; i++)
        {
            cnt1=0;
            for(int j=0; j<m; j++)
            {
                if(ans[i][j]==1)
                {
                    cnt1++;
                }
                if(cnt1==2)
                {
                    ans[i][j]=2;
                    ans[i][j-1]=0;
                    cnt1=0;
                }
            }
        }
        int fin=0;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(ans[i][j]==2)
                {
                    fin+=y;
                }
                else if(ans[i][j]==1)
                {
                    fin+=x;
                }
            }
        }
        for(int i=0; i<n; i++)
        {

            for(int j=0; j<m; j++)
            {
                cout<<ans[i][j]<<" ";
            }
            cout<<endl;
        }
        if(fin==0)
            fin=INT_MAX;

        cout<<min(fin,cnt*x)<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t,n,m,x,y;
    cin>>t;
    while(t--)
    {
        cin>>n>>m>>x>>y;
        char ar[n+1][m+1];
        int ans[n+1][m+1];
        memset(ans,0,sizeof(ans));
        int cnt=0;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                cin>>ar[i][j];
                if(ar[i][j]=='.')
                    cnt++;

            }
        }
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(ar[i][j]=='.')
                {
                    ans[i][j]=1;
                }

            }
        }

        int cnt1=0;
        for(int i=0; i<n; i++)
        {
            cnt1=0;
            for(int j=0; j<m; j++)
            {
                if(ans[i][j]==1)
                {
                    cnt1++;
                }
                if(cnt1==2)
                {
                    ans[i][j]=2;
                    ans[i][j-1]=0;
                    cnt1=0;
                }
            }
        }
        int fin=0;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(ans[i][j]==2)
                {
                    fin+=y;
                }
                else if(ans[i][j]==1)
                {
                    fin+=x;
                }
            }
        }
//        for(int i=0; i<n; i++)
//        {
//
//            for(int j=0; j<m; j++)
//            {
//                cout<<ans[i][j]<<" ";
//            }
//            cout<<endl;
//        }
        if(fin==0)
            fin=INT_MAX;

        cout<<min(fin,cnt*x)<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t,n,m,x,y;
    cin>>t;
    while(t--)
    {
        cin>>n>>m>>x>>y;
        vector<string>v;///for using the 2d string...
        string s;
        int cnt=0;
        for(int i=0; i<n; i++)
        {
            cin>>s;
            v.push_back(s);
        }
        if(2*x<=y) ///if the value of covering single white box is less than the (value of double box/2)
        {         /// then filling single box is efficient...

            y = 2*x;
        }
        int fin=0;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(v[i][j]=='.' && v[i][j+1]=='.')
                {
                    fin+=y;
                    j++;
                }
                else if(v[i][j]=='.')
                {
                    fin+=x;
                }

            }
        }
        cout<<fin<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t,n,m,x,y;
    cin>>t;
    while(t--)
    {
        cin>>n>>m>>x>>y;
        char ar[n+1][m+1];
        int ans[n+1][m+1];
        memset(ans,0,sizeof(ans));
        int cnt=0;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                cin>>ar[i][j];
                if(ar[i][j]=='.')
                    cnt++;

            }
        }
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(ar[i][j]=='.')
                {
                    if(ar[i][j+1]=='.' && j+1<m)
                    {
                        ans[i][j+1]=2;
                        j+=2;
                    }
                    else
                    {
                        ans[i][j]=1;
                    }
                }

            }
        }
        for(int i=0; i<n; i++)
        {

            for(int j=0; j<m; j++)
            {
                cout<<ans[i][j]<<" ";
            }
            cout<<endl;
        }
        int fin=0;
        for(int i=0; i<n; i++)
        {

            for(int j=0; j<m; j++)
            {
                if(ans[i][j]==1)
                {
                    fin+=x;
                }
                else if(ans[i][j]==2)
                {
                    fin+=(y);
                }
            }
        }
        if(fin==0)
            fin=INT_MAX;

        cout<<min(fin,cnt*x)<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
const int N=3000;
char s[N][N];
int row[N][N];
int t,n,m;
typedef long long ll;
ll x,y;
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>m>>x>>y;
		memset(row,0,sizeof row);
		for(int i=0;i<n;i++) cin>>s[i];
		int w=0,s1=0;
		for(int i=0;i<n;i++)
		{
			w=0;
			for(int j=0;j<m;j++)
			{
				if(s[i][j]=='.') w++,s1++;

			}

		}

		ll mx1=s1*x;
		ll mx2=0;
		int j,k,cnt=0;
		w=0;
		for(int i=0;i<n;i++)
		{
			w=0;
			for(int j=0;j<m;j++)
			{
				if(s[i][j]=='.') w++;
				else
				{
					if(w&1) mx2+=(w/2*y)+x;
					else mx2+=(w/2*y);
				}
				if(j==m-1)
				{
					if(w&1) mx2+=(w/2*y)+x;
					else mx2+=(w/2*y);
				}
			}
		}
		cout<<min(mx1,mx2)<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
const int N=3000;
char s[N][N];
int row[N][N];
int t,n,m;
typedef long long ll;
ll x,y;
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>m>>x>>y;
		memset(row,0,sizeof row);
		for(int i=0;i<n;i++) cin>>s[i];
		ll w=0,s1=0;
		for(int i=0;i<n;i++)
		{
			w=0;
			for(int j=0;j<m;j++)
			{
				if(s[i][j]=='.') w++,s1++;

			}

		}

		ll mx1=s1*x;
		ll mx2=0;
		int j,k,cnt=0;
		w=0;
		for(int i=0;i<n;i++)
		{
			w=0;
			for(int j=0;j<m;j++)
			{
				if(s[i][j]=='.') w++;
				else
				{
					if(w&1) mx2+=(w/2*y)+x;
					else mx2+=(w/2*y);
					w=0;
				}
				if(j==m-1)
				{
					if(w&1) mx2+=(w/2*y)+x;
					else mx2+=(w/2*y);
					w=0;
				}
			}
		}
		cout<<min(mx1,mx2)<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
const int N=3000;
char s[N][N];
int row[N];
int t,n,m;
typedef long long ll;
ll x,y;
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>m>>x>>y;
		memset(row,0,sizeof row);
		for(int i=0;i<n;i++) cin>>s[i];
		int w=0;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				if(s[i][j]=='.') w++;
				row[i]++;
			}
		}
		ll mx1=w*x;
		ll mx2=0;
		for(int i=0;i<n;i++)
		{
			if(row[i]%2==0) mx2+=(row[i]/2*y);
			else mx2+=(row[i]/2*y)+x;
		}
		cout<<min(mx1,mx2)<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
const int N=3000;
char s[N][N];
int row[N][N];
int t,n,m;
typedef long long ll;
ll x,y;
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		cin>>n>>m>>x>>y;
		memset(row,0,sizeof row);
		for(int i=0;i<n;i++) cin>>s[i];
		int w=0,s1=0;
		for(int i=0;i<n;i++)
		{
			w=0;
			for(int j=0;j<m;j++)
			{
				if(s[i][j]=='.') w++,s1++;

			}

		}

		ll mx1=s1*x;
		ll mx2=0;
		for(int i=0;i<n;i++)
		{
			for(int j=1;j<m;j++)
			{
				if(s[i][j-1]=='.'&&s[i][j]=='.')
				{
					mx2+=y;s[i][j-1]=s[i][j]='*';
				}

			}
			for(int j=0;j<m;j++) if(s[i][j]=='.') mx2+=x;
		}
		cout<<min(mx2,mx1)<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;

typedef vector<int>  vi;
typedef set<char> setchar;
typedef set<int> sett;
typedef deque <int> dq;
typedef long long ll;
#define vec(a)  a.begin(),a.end()
#define pb     push_back
#define pf     push_front
#define F first
#define S second
#define mem(a,b) memset(a,b,sizeof(a));
#define inI(a)  scanf("%d",&a);
#define inT(a,b)  scanf("%d %d",&a,&b);
#define inL(a)  scanf("%lld",&a);
#define SWAP(a,b) a= a^b, b=a^b, a=a^b

//int fx[]={+0,-1,-1,-1,+0,+1,+1,+1};
//int fy[]={-1,-1,+0,+1,+1,+1,+0,-1};
int fx[]= {0,-1,0,+1};
int fy[]= {-1,0,+1,0};

int main()
{

   // freopen("input.txt","r",stdin);
    int t;
    cin>>t;
    while(t--)
    {
        int n,m,x,y;
        cin>>n>>m>>x>>y;
        string s;
        int ans =0;
        for(int i=0; i<n; i++)
        {
            cin>>s;
            int cnt=0;
            //cout<<s<<"   = string \n";
            if(s.size()==1 && s==".")
            {
                ans+=x;
                //cout<<i<<" dhukchy  ..........2 \n";
            }
            else
            {
               // cout<<i<<" dhukchy \n";
                for(int j=0; j<s.size(); j++)
                {
                    if(s[j]=='*')
                    {
                        if(cnt==1)
                        {
                            ans+=x;
                            cnt=0;
                        }
                        else if(cnt>0)
                        {
                            if(cnt%2==0)
                            {
                                if(x*2< y)
                                {
                                    ans+=(x*2);
                                }
                                else
                                    ans+=y;
                                cnt=0;
                            }
                            else if(cnt%2==1)
                            {
                                int p = (cnt-1)*x;
                                int q = (cnt-1)/2;
                                if(p+x < (q*y)+x)
                                {
                                    ans+=(p+x);
                                    cnt=0;
                                }
                                else
                                {
                                    ans+=(q*y)+x;
                                    cnt=0;
                                }
                            }
                        }
                    }
                    else
                        cnt++;

                    if(s[s.size()-1]=='.' ||  (j == s.size()-1 && cnt))
                    {
                        if(cnt==1)
                        {
                            ans+=x;
                            cnt=0;
                        }
                        else if(cnt>0)
                        {
                            if(cnt%2==0)
                            {
                                if(x*2< y)
                                {
                                    ans+=(x*2);
                                }
                                else
                                    ans+=y;
                                cnt=0;
                            }
                            else if(cnt%2==1)
                            {
                                int p = (cnt-1)*x;
                                int q = (cnt-1)/2;
                                if(p+x < (q*y)+x)
                                {
                                    ans+=(p+x);
                                    cnt=0;
                                }
                                else
                                {
                                    ans+=(q*y)+x;
                                    cnt=0;
                                }
                            }
                        }

                    }

                }

            }

        }
        cout<<ans<<"\n";
    }

    return 0;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll in()
{
    ll re=0,f=1;char x=getchar();
    while(x<'0'||x>'9'){if(x=='-')f=-1;x=getchar();}
    while(x>='0'&&x<='9')re=re*10+x-48,x=getchar();
    return re*f;
}
int n,m;
vector<int>e[100010];
struct data{
    int u,v;
    void scan()
	{
		u=in();v=in();
		e[u].push_back(v);
	}
}d[100010];
int dgr[100010];
queue<int>q;
int query(int u,int v)
{
	printf("? %d %d\n",u,v);
	fflush(stdout);
	return in();
}
void del(int c)
{
	for(int i=0;i<e[c].size();++i)
	{
		int to=e[c][i];
		if(to)
		{
			--dgr[to];
			if(!dgr[to])q.push(to);
		}
	}
}
int vis[100010],ins[100010];
void dfs(int c)
{
	vis[c]=ins[c]=1;
	for(int i=0;i<e[c].size();++i)
	{
		int to=e[c][i];
		if(ins[to])e[c][i]=0;
		else ++dgr[to];
		if(!vis[to])dfs(to);
	}
	ins[c]=0;
}
int main()
{
    int n=in(),m=in();
    for(int i=1;i<=m;++i)d[i].scan();
    for(int i=1;i<=n;++i)if(!vis[i])dfs(i);
	for(int i=1;i<=n;++i)if(!dgr[i])q.push(i);
	int h=q.front();q.pop();
	while(!q.empty())
	{
		int t=q.front();q.pop();
		if(!query(h,t))swap(h,t);
		del(t);
	}
	printf("! %d\n",h);
	fflush(stdout);
}


#include "bits/stdc++.h"
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

using ll = long long;

vector<vector<int>> all;

// SCC: https://www.geeksforgeeks.org/strongly-connected-components/
class Graph
{
    int V;    // No. of vertices
    list<int> *adj;    // An array of adjacency lists

    // Fills Stack with vertices (in increasing order of finishing
    // times). The top element of stack has the maximum finishing
    // time
    void fillOrder(int v, bool visited[], stack<int> &Stack);

    // A recursive function to print DFS starting from v
    void DFSUtil(int v, bool visited[]);
public:
    Graph(int V);
    void addEdge(int v, int w);

    // The main function that finds and prints strongly connected
    // components
    void printSCCs();

    // Function that returns reverse (or transpose) of this graph
    Graph getTranspose();
};

Graph::Graph(int _V)
{
    this->V = _V;
    adj = new list<int>[_V];
}

// A recursive function to print DFS starting from v
vector<int> last;
void Graph::DFSUtil(int v, bool visited[])
{
    // Mark the current node as visited and print it
    visited[v] = true;
    last.push_back(v);
    //~ cout << v << " ";

    // Recur for all the vertices adjacent to this vertex
    list<int>::iterator i;
    for (i = adj[v].begin(); i != adj[v].end(); ++i)
        if (!visited[*i])
            DFSUtil(*i, visited);
}

Graph Graph::getTranspose()
{
    Graph g(V);
    for (int v = 0; v < V; v++)
    {
        // Recur for all the vertices adjacent to this vertex
        list<int>::iterator i;
        for(i = adj[v].begin(); i != adj[v].end(); ++i)
        {
            g.adj[*i].push_back(v);
        }
    }
    return g;
}

void Graph::addEdge(int v, int w)
{
    adj[v].push_back(w); // Add w to v’s list.
}

void Graph::fillOrder(int v, bool visited[], stack<int> &Stack)
{
    // Mark the current node as visited and print it
    visited[v] = true;

    // Recur for all the vertices adjacent to this vertex
    list<int>::iterator i;
    for(i = adj[v].begin(); i != adj[v].end(); ++i)
        if(!visited[*i])
            fillOrder(*i, visited, Stack);

    // All vertices reachable from v are processed by now, push v
    Stack.push(v);
}

// The main function that finds and prints all strongly connected
// components
void Graph::printSCCs()
{
    stack<int> Stack;

    // Mark all the vertices as not visited (For first DFS)
    bool *visited = new bool[V];
    for(int i = 0; i < V; i++)
        visited[i] = false;

    // Fill vertices in stack according to their finishing times
    for(int i = 0; i < V; i++)
        if(visited[i] == false)
            fillOrder(i, visited, Stack);

    // Create a reversed graph
    Graph gr = getTranspose();

    // Mark all the vertices as not visited (For second DFS)
    for(int i = 0; i < V; i++)
        visited[i] = false;

    // Now process all vertices in order defined by Stack
    while (Stack.empty() == false)
    {
        // Pop a vertex from stack
        int v = Stack.top();
        Stack.pop();

        // Print Strongly connected component of the popped vertex
        if (visited[v] == false)
        {
			last.clear();
            gr.DFSUtil(v, visited);
            all.push_back(last);
            //~ cout << endl;
        }
    }
}

const int nax = 2e5 + 5;
int gg[nax];
vector<int> inv[nax];
int in[nax];
vector<int> memo[nax];

int main() {
	int n, m;
	scanf("%d%d", &n, &m);
	Graph gr(n);
	vector<pair<int,int>> edges;
	for(int i = 0; i < m; ++i) {
		int a, b;
		scanf("%d%d", &a, &b);
		--a;
		--b;
		gr.addEdge(a, b);
		edges.emplace_back(a, b);
		memo[a].push_back(b);
	}
	gr.printSCCs();
	debug() << imie(all);
	for(int i = 0; i < (int) all.size(); ++i) {
		inv[i] = all[i];
		for(int x : all[i]) {
			gg[x] = i;
		}
	}
	for(pair<int,int> p : edges) {
		if(gg[p.first] != gg[p.second]) {
			++in[gg[p.second]];
		}
	}
	int a = -1;
	for(int i = 0; i < n; ++i) {
		if(!in[gg[i]]) {
			a = i;
			break;
		}
	}
	assert(a != -1);
	set<int> ggs; // available groups
	for(int i = 0; i < (int) all.size(); ++i) {
		if(in[i] == 0) {
			ggs.insert(i);
		}
	}
	{
		int me = gg[a];
		for(int i = 0; i < (int) inv[me].size(); ++i) {
			if(inv[me][i] == a) {
				swap(inv[me][i], inv[me].back());
				inv[me].pop_back();
			}
		}
	}

	auto rem = [&](int g) {
		g = gg[g];
		for(int x : all[g]) {
			for(int y : memo[x]) {
				if(gg[x] == gg[y]) {
					continue;
				}
				if(--in[gg[y]] == 0) {
					ggs.insert(gg[y]);
				}
				assert(in[gg[y]] >= 0);
			}
		}
		ggs.erase(g);
	};

	while(true) {
		int me = gg[a];
		if((int) ggs.size() == 1) {
			assert(ggs == set<int>{me});
			break;
		}
		auto it = ggs.find(me);
		if(it == ggs.begin()) {
			++it;
		}
		else {
			--it;
		}
		int he = *it;
		int b = inv[he].back();
		inv[he].pop_back();
		printf("? %d %d\n", a + 1, b + 1);
		fflush(stdout);
		int r;
		scanf("%d", &r);
		if(r == 1) {
			// from a to b, skip b
			rem(b);
		}
		else {
			assert(r == 0);
			// change a to b
			rem(a);
			a = b;
		}
	}
	printf("! %d\n", a + 1);
}


#include "bits/stdc++.h"
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

using ll = long long;

vector<vector<int>> all;

// SCC: https://www.geeksforgeeks.org/strongly-connected-components/
class Graph
{
    int V;    // No. of vertices
    list<int> *adj;    // An array of adjacency lists

    // Fills Stack with vertices (in increasing order of finishing
    // times). The top element of stack has the maximum finishing
    // time
    void fillOrder(int v, bool visited[], stack<int> &Stack);

    // A recursive function to print DFS starting from v
    void DFSUtil(int v, bool visited[]);
public:
    Graph(int V);
    void addEdge(int v, int w);

    // The main function that finds and prints strongly connected
    // components
    void printSCCs();

    // Function that returns reverse (or transpose) of this graph
    Graph getTranspose();
};

Graph::Graph(int _V)
{
    this->V = _V;
    adj = new list<int>[_V];
}

// A recursive function to print DFS starting from v
vector<int> last;
void Graph::DFSUtil(int v, bool visited[])
{
    // Mark the current node as visited and print it
    visited[v] = true;
    last.push_back(v);
    //~ cout << v << " ";

    // Recur for all the vertices adjacent to this vertex
    list<int>::iterator i;
    for (i = adj[v].begin(); i != adj[v].end(); ++i)
        if (!visited[*i])
            DFSUtil(*i, visited);
}

Graph Graph::getTranspose()
{
    Graph g(V);
    for (int v = 0; v < V; v++)
    {
        // Recur for all the vertices adjacent to this vertex
        list<int>::iterator i;
        for(i = adj[v].begin(); i != adj[v].end(); ++i)
        {
            g.adj[*i].push_back(v);
        }
    }
    return g;
}

void Graph::addEdge(int v, int w)
{
    adj[v].push_back(w); // Add w to v’s list.
}

void Graph::fillOrder(int v, bool visited[], stack<int> &Stack)
{
    // Mark the current node as visited and print it
    visited[v] = true;

    // Recur for all the vertices adjacent to this vertex
    list<int>::iterator i;
    for(i = adj[v].begin(); i != adj[v].end(); ++i)
        if(!visited[*i])
            fillOrder(*i, visited, Stack);

    // All vertices reachable from v are processed by now, push v
    Stack.push(v);
}

// The main function that finds and prints all strongly connected
// components
void Graph::printSCCs()
{
    stack<int> Stack;

    // Mark all the vertices as not visited (For first DFS)
    bool *visited = new bool[V];
    for(int i = 0; i < V; i++)
        visited[i] = false;

    // Fill vertices in stack according to their finishing times
    for(int i = 0; i < V; i++)
        if(visited[i] == false)
            fillOrder(i, visited, Stack);

    // Create a reversed graph
    Graph gr = getTranspose();

    // Mark all the vertices as not visited (For second DFS)
    for(int i = 0; i < V; i++)
        visited[i] = false;

    // Now process all vertices in order defined by Stack
    while (Stack.empty() == false)
    {
        // Pop a vertex from stack
        int v = Stack.top();
        Stack.pop();

        // Print Strongly connected component of the popped vertex
        if (visited[v] == false)
        {
			last.clear();
            gr.DFSUtil(v, visited);
            all.push_back(last);
            //~ cout << endl;
        }
    }
}

const int nax = 2e5 + 5;
int gg[nax];
vector<int> inv[nax];
int in[nax];
vector<int> memo[nax];

int main() {
	int n, m;
	scanf("%d%d", &n, &m);
	Graph gr(n);
	vector<pair<int,int>> edges;
	for(int i = 0; i < m; ++i) {
		int a, b;
		scanf("%d%d", &a, &b);
		--a;
		--b;
		gr.addEdge(a, b);
		edges.emplace_back(a, b);
		memo[a].push_back(b);
	}
	gr.printSCCs();
	debug() << imie(all);
	for(int i = 0; i < (int) all.size(); ++i) {
		inv[i] = all[i];
		for(int x : all[i]) {
			gg[x] = i;
		}
	}
	for(pair<int,int> p : edges) {
		if(gg[p.first] != gg[p.second]) {
			++in[gg[p.second]];
		}
	}
	int a = -1;
	for(int i = 0; i < n; ++i) {
		if(!in[gg[i]]) {
			a = i;
			break;
		}
	}
	assert(a != -1);
	set<int> ggs; // available groups
	for(int i = 0; i < (int) all.size(); ++i) {
		if(in[i] == 0) {
			ggs.insert(i);
		}
	}
	{
		int me = gg[a];
		for(int i = 0; i < (int) inv[me].size(); ++i) {
			if(inv[me][i] == a) {
				swap(inv[me][i], inv[me].back());
				inv[me].pop_back();
			}
		}
	}

	auto rem = [&](int g) {
		g = gg[g];
		if(!inv[g].empty()) {
			return;
		}
		for(int x : all[g]) {
			for(int y : memo[x]) {
				if(gg[x] == gg[y]) {
					continue;
				}
				if(--in[gg[y]] == 0) {
					ggs.insert(gg[y]);
				}
				assert(in[gg[y]] >= 0);
			}
		}
		ggs.erase(g);
	};

	while(true) {
		int me = gg[a];
		if((int) ggs.size() == 1) {
			assert(ggs == set<int>{me});
			break;
		}
		auto it = ggs.find(me);
		if(it == ggs.begin()) {
			++it;
		}
		else {
			--it;
		}
		int he = *it;
		int b = inv[he].back();
		inv[he].pop_back();
		printf("? %d %d\n", a + 1, b + 1);
		fflush(stdout);
		int r;
		scanf("%d", &r);
		if(r == 1) {
			// from a to b, skip b
			rem(b);
		}
		else {
			assert(r == 0);
			// change a to b
			rem(a);
			a = b;
		}
	}
	printf("! %d\n", a + 1);
}