#include #define all(x) x.begin(), x.end() #define MOD 101 using namespace std; using ll = long long; ll powmod(ll a, ll n) { ll ret = 1; while(n){ if(n & 1){ ret *= a; ret %= MOD; } n >>= 1; a *= a; a %= MOD; } return ret; } const ll INV2 = powmod(2, MOD - 2); const ll INV3 = powmod(3, MOD - 2); ll det(vector >& P){ ll ret = 0; for(int i = 0; i < 3; ++i){ ret += P[i][0] * P[(i + 1) % 3][1] * P[(i + 2) % 3][2]; ret -= P[i][2] * P[(i + 1) % 3][1] * P[(i + 2) % 3][0]; } return ret; } char solve(int N, int K, vector >& P, vector& a, string& s){ ll d = abs(det(P)); if(d == 0){ return 'D'; } vector > > dp(N + 1, vector >(MOD, vector(4, -1))); for(int j = 0; j < MOD; ++j){ dp[N][j][3] = (j >= K) ? 1 : 0; } for(int i = N - 1; i >= 0; --i){ vector hands = (s[i] == 'K') ? vector({1, -1, 0}) : vector({0, -1, 1}); int val = a[i] + 1; bool val_div2 = (val % 2 == 0), val_div3 = (val % 3 == 0); for(int j = 0; j < MOD; ++j){ for(int k = 0; k < 4; ++k){ vector results = { dp[i + 1][j][k], dp[i + 1][j * val % MOD][k | val_div2 | (val_div3 << 1)] }; for(auto& x : hands){ if(find(all(results), x) == results.end()){ continue; } dp[i][j][k] = x; break; } } } } bool div2 = (d % 2 == 0), div3 = (d % 3 == 0); int stat_init = dp[0][d % MOD * INV2 * INV3 % MOD][div2 | (div3 << 1)]; if(stat_init == -1){ return 'D'; } return (stat_init == 1) ? 'K' : 'P'; } int main(){ int t; cin >> t; for(int i = 0; i < t; ++i){ int N, K; cin >> N >> K; vector > P(3, vector(3)); for(auto& v : P){ cin >> v[0] >> v[1] >> v[2]; } vector a(N); for(auto& x : a){ cin >> x; } string s; cin >> s; cout << solve(N, K, P, a, s) << endl; } return 0; }