#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; int dp[2][2][101], ndp[2][2][101]; } int main() { ios::sync_with_stdio(false); cin.tie(0); int tt; cin >> tt; while (tt--) { int n, k; cin >> n >> k; ll p[3][3]{}; rep(i, 3) rep(j, 3) cin >> p[i][j]; VI a(n); rep(i, n) cin >> a[i]; string s; cin >> s; rep(x, 2) rep(y, 2) rep(z, 101) dp[x][y][z] = x || y ? 0 : z >= k ? 1 : -1; rrep(i, n) { if (s[i] == 'K') { rep(x, 2) rep(y, 2) { int v = a[i] + 1; int px = x, py = y; if (px && v % 2 == 0) v /= 2, px = 0; if (py && v % 3 == 0) v /= 3, py = 0; rep(z, 101) ndp[x][y][z] = max(dp[x][y][z], dp[px][py][(z * v) % 101]); } } else { rep(x, 2) rep(y, 2) { int v = a[i] + 1; int px = x, py = y; if (px && v % 2 == 0) v /= 2, px = 0; if (py && v % 3 == 0) v /= 3, py = 0; rep(z, 101) ndp[x][y][z] = min(dp[x][y][z], dp[px][py][(z * v) % 101]); } } swap(dp, ndp); } ll v = p[0][0] * p[1][1] * p[2][2] + p[0][1] * p[1][2] * p[2][0] + p[0][2] * p[1][0] * p[2][1] - p[0][0] * p[1][2] * p[2][1] - p[0][1] * p[1][0] * p[2][2] - p[0][2] * p[1][1] * p[2][0]; int x = 1, y = 1; if (v % 2 == 0) v /= 2, x = 0; if (v % 3 == 0) v /= 3, y = 0; v = abs(v); int ans = dp[x][y][v % 101]; cout << (v == 0 || ans == 0 ? 'D' : "PK"[ans > 0]) << '\n'; } }