def det(A): return ( A[0][0] * A[1][1] * A[2][2] + A[0][1] * A[1][2] * A[2][0] + A[0][2] * A[1][0] * A[2][1] - A[0][2] * A[1][1] * A[2][0] - A[0][1] * A[1][0] * A[2][2] - A[0][0] * A[1][2] * A[2][1] ) def solve(): n, k = map(int, input().split()) P = [list(map(int, input().split())) for _ in range(3)] A = list(map(int, input().split())) S = input() x = det(P) if x == 0: return None x = abs(x) x %= 606 dp = [[False] * 606 for _ in range(n + 1)] dp[0][x] = True for i, a in enumerate(A): for j in range(606): dp[i + 1][j] |= dp[i][j] dp[i + 1][j * (a + 1) % 606] |= dp[i][j] dp2 = [[0] * 606 for _ in range(n + 1)] for i in range(606): if i % 6 != 0: dp2[-1][i] = 1 elif i >= 6 * k: dp2[-1][i] = 2 else: dp2[-1][i] = 0 for i in range(n - 1, -1, -1): a = A[i] s = S[i] for j in range(606): if not dp[i][j]: continue if s == "K": dp2[i][j] = max(dp2[i + 1][j], dp2[i + 1][j * (a + 1) % 606]) else: dp2[i][j] = min(dp2[i + 1][j], dp2[i + 1][j * (a + 1) % 606]) if dp2[0][x] == 2: return True elif dp2[0][x] == 1: return None else: return False for _ in range(int(input())): res = solve() if res is None: print("D") elif res: print("K") else: print("P")