/*
* Copyright (c) 2024 Emon Thakur
* All rights reserved.
*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fr freopen("input52.txt","r",stdin);
//ofstream file("output52.txt");
char a[2001][2001];
int main()
{
ios::sync_with_stdio(false); cin.tie(nullptr);
//fr;
int t; cin >> t; while(t--)
{
ll n,m,x,y; cin >> n >> m >> x >> y;
vector<vector<pair<ll,ll>>> dp(n+2,vector<pair<ll,ll>>(m+2,{1e18,1e18}));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++) cin >> a[i][j];
}
dp[1][1] = {0,0};
for(int j=2;j<=m;j++)
{
if(a[1][j]=='#') break;
dp[1][j] = {dp[1][j-1].first + x , dp[1][j-1].first+y};
}
for(ll i=2;i<=n;i++)
{
for(ll j=1;j<=m;j++)
{
if(a[i][j]=='#') continue;
dp[i][j].first = min(dp[i][j-1].first+x , dp[i-1][j].second+y);
dp[i][j].second = min(dp[i][j-1].first+y , dp[i-1][j].second+x);
}
for(ll j=m;j>=1;j--)
{
if(a[i][j]=='#') continue;
dp[i][j].first = min({dp[i][j].first , dp[i][j+1].first+x, dp[i-1][j].second+y});
dp[i][j].second = min({dp[i][j].second , dp[i][j+1].first+y , dp[i-1][j].second+x});
}
}
ll ans = min(dp[n][m-1].first , dp[n-1][m].second);
(ans>=1e18)? file<<-1<<'\n': file<<ans<<'\n';
}
}