#include <bits/stdc++.h>
#define int long long
using namespace std;
constexpr int maxn= 2e5 + 5 , inf= 0x3f3f3f3f , mod= 1e9 + 7 ;
int n, m, du[ maxn] , vis[ maxn] ;
int res, ans= 1 ;
int va[ maxn] , vb[ maxn] ;
vector< int > e[ maxn] ;
int t, a, b;
void dfs ( int u, int dd) {
if ( u!= dd) vis[ u] = 1 ;
else return ;
for ( auto v: e[ u] ) {
if ( ! vis[ v] ) {
dfs ( v, dd) ;
}
}
}
signed main ( ) {
cin>> t;
while ( t-- ) {
cin>> n>> m>> a>> b;
for ( int i= 1 ; i<= m; i++ ) {
int u, v;
cin>> u>> v;
e[ u] . push_back ( v) ;
e[ v] . push_back ( u) ;
}
dfs ( a, b) ;
int res1= 0 , res2= 0 ;
for ( int i= 1 ; i<= n; i++ ) {
if ( ! vis[ i] && i!= b) res1++ ;
}
for ( int i= 1 ; i<= n; i++ ) {
vis[ i] = 0 ;
}
dfs ( b, a) ;
for ( int i= 1 ; i<= n; i++ ) {
if ( ! vis[ i] && i!= a) res2++ ;
}
cout<< res1* res2<< endl;
for ( int i= 1 ; i<= n; i++ ) {
e[ i] . clear ( ) ;
vis[ i] = 0 ;
}
}
}
#include <bits/stdc++.h>
using namespace std;
constexpr int maxn= 1005 , inf= 0x3f3f3f3f , mod= 1e9 + 7 ;
constexpr int dx[ ] = { 1 , - 1 , 0 , 0 } ;
constexpr int dy[ ] = { 0 , 0 , 1 , - 1 } ;
int p[ maxn] , n, m, k;
int vis[ maxn] [ maxn] ;
string mp[ 1005 ] ;
int res[ maxn] ;
struct node{
int x, y, id;
} ;
void bfs ( ) {
queue< node> q[ 10 ] ;
for ( int i= 1 ; i<= n; i++ ) {
for ( int j= 1 ; j<= m; j++ ) {
if ( mp[ i] [ j] >= '1' && mp[ i] [ j] <= '9' ) {
int c= mp[ i] [ j] - '0' ;
q[ c] . push ( ( node) { i, j, c} ) ;
res[ c] ++ ;
vis[ i] [ j] = 1 ;
}
}
}
while ( 1 ) {
int flag= 1 ;
for ( int i= 1 ; i<= k; i++ ) {
if ( q[ i] . size ( ) ) flag= 0 ;
}
if ( flag) return ;
for ( int i= 1 ; i<= k; i++ ) {
if ( q[ i] . empty ( ) ) continue ;
int cs= p[ i] ;
while ( cs-- ) {
vector< node> sb;
if ( q[ i] . empty ( ) ) break ;
while ( ! q[ i] . empty ( ) ) {
auto t= q[ i] . front ( ) ;
q[ i] . pop ( ) ;
for ( int j= 0 ; j< 4 ; j++ ) {
int xx= t. x+ dx[ j] ;
int yy= t. y+ dy[ j] ;
if ( xx>= 1 && xx<= n&& yy>= 1 && yy<= m&& mp[ xx] [ yy] != '#' && ! vis[ xx] [ yy] ) {
res[ t. id] ++ ;
vis[ xx] [ yy] = t. id;
sb. push_back ( ( node) { xx, yy, t. id} ) ;
}
}
}
for ( auto g: sb) {
q[ i] . push ( g) ;
}
}
}
}
}
int main ( ) {
cin>> n>> m>> k;
for ( int i= 1 ; i<= k; i++ ) {
cin>> p[ i] ;
}
for ( int i= 1 ; i<= n; i++ ) {
cin>> mp[ i] ;
mp[ i] = " " + mp[ i] ;
}
bfs ( ) ;
for ( int i= 1 ; i<= k; i++ ) {
cout<< res[ i] << " " ;
}
return 0 ;
}