#include using namespace std; using ll = long long; const int L = 606; void solve() { int N, K; cin >> N >> K; std::vector A(N); string s; for (auto& a : A) cin >> a; cin >> s; int dp[L]; for (int i = 0; i < L; i ++) { if (i % 6) { dp[i] = 0; } else { dp[i] = (((i / 6) % 101) >= K ? 1 : -1); } } for (int i = N - 1; i >= 0; i --) { int dp2[L]; for (int p = 0; p < L; p ++) { int x = dp[p], y = dp[(p * (A[i] + 1)) % L]; dp2[p] = (s[i] == 'K' ? max(x, y) : min(x, y)); } for (int i = 0; i < L; i ++) dp[i] = dp2[i]; } int now; { ll pnow[3][3]; for (int i = 0; i < 3; i ++) { for (int j = 0; j < 3; j ++) { cin >> pnow[i][j]; } } ll r = 0; for (int i = 1; i < 3; i ++) { for (int ofs = 0; ofs < 3; ofs ++) { ll kj = 1; for (int j = 0; j < 3; j ++) { kj *= pnow[j][(ofs + i * j) % 3]; } r += kj * (i & 1 ? 1ll : -1ll); } } if (r == 0) { puts("D"); return; } now = abs(r) % L; } cout << (dp[now] == -1 ? "P" : (dp[now] ? "K" : "D")) << endl; } int main () { int T; cin >> T; while (T--) solve(); }