#include #include using namespace std; int A[10101]; string S; int memo[10001][606]; int N,K; int dfs(int t,int d) { if(t==N) { if(d%6!=0)return 0; else if(d/6>=K)return 1; else return -1; } if(memo[t][d]!=0) { return memo[t][d]-2; } int x=dfs(t+1,d),y=dfs(t+1,d*(A[t]+1)%606); if(S[t]=='K') { if(x==1||y==1) { memo[t][d]=3; } else if(x==0||y==0) { memo[t][d]=2; } else { memo[t][d]=1; } } else { if(x==-1||y==-1) { memo[t][d]=1; } else if(x==0||y==0) { memo[t][d]=2; } else { memo[t][d]=3; } } return memo[t][d]-2; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T;cin>>T; for(;T--;) { cin>>N>>K; long P[3][3]; for(int i=0;i<3;i++)for(int j=0;j<3;j++)cin>>P[i][j]; for(int i=0;i>A[i]; cin>>S; long D=0; D+=P[0][0]*P[1][1]*P[2][2]; D+=P[0][1]*P[1][2]*P[2][0]; D+=P[0][2]*P[1][0]*P[2][1]; D-=P[0][2]*P[1][1]*P[2][0]; D-=P[0][0]*P[1][2]*P[2][1]; D-=P[0][1]*P[1][0]*P[2][2]; D=abs(D); if(D==0) { cout<<"D\n"; continue; } D%=606; for(int i=0;i