1 /*
2 模拟 简单
3 res[i][j]=mat[i][j]+d[sum];
4 其中sum=mat[i][j]+i,j其他四个方向的值
5 */
6 #include<stdio.h>
7 #include<string.h>
8 #include<stdlib.h>
9 #include<algorithm>
10 #include<iostream>
11 #include<queue>
12 //#include<map>
13 #include<math.h>
14 using namespace std;
15 typedef long long ll;
16 //typedef __int64 int64;
17 const int maxn = 25;
18 const int inf = 0x7fffffff;
19 const int pi=acos(-1.0);
20 int mat[ maxn ][ maxn ],res[ maxn ][ maxn ];
21 int d[ maxn ];
22 const int dx[]={0,0,-1,1};
23 const int dy[]={1,-1,0,0};
24
25 void solve(){
26 for( int i=1;i<=20;i++ ){
27 for( int j=1;j<=20;j++ ){
28 int x,y,sum=0;
29 sum=mat[ i ][ j ];
30 for( int k=0;k<4;k++){
31 x=i+dx[ k ];
32 y=j+dy[ k ];
33 if( x<1||x>20||y<1||y>20)continue;
34 sum+=mat[ x ][ y ];
35 }
36 res[ i ][ j ]=d[ sum ]+mat[ i ][ j ];
37 if( res[ i ][ j ]<0 ) res[ i ][ j ]=0;
38 else if( res[ i ][ j ]>3 ) res[ i ][ j ]=3;
39 }
40 }
41 return ;
42 }
43
44 int main(){
45 int ca;
46 scanf("%d",&ca);
47 for(int c=1;c<=ca;c++){
48 if(c!=1)printf("\n");
49 int n;
50 scanf("%d",&n);
51 memset( mat,0,sizeof(mat) );
52 memset( res,0,sizeof(res) );
53 for( int i=0;i<16;i++ ) scanf("%d",&d[ i ]);
54 for( int i=1;i<=20;i++ )
55 for( int j=1;j<=20;j++ )
56 scanf("%d",&mat[ i ][ j ]);
57 for( int t=1;t<=n;t++ ){
58 solve();
59 memcpy( mat,res,sizeof(res) );
60 }
61 for( int i=1;i<=20;i++ ){
62 for( int j=1;j<=20;j++ ){
63 if( res[ i ][ j ]==0 ) printf(".");
64 else if( res[ i ][ j ]==1 ) printf("!");
65 else if( res[ i ][ j ]==2 ) printf("X");
66 else if( res[ i ][ j ]==3 ) printf("#");
67 }
68 printf("\n");
69 }
70 }
71 return 0;
72 }