from collections import deque def solve(): N,K = list(map(int,input().split())) p11, p12, p13 = list(map(int,input().split())) p21, p22, p23 = list(map(int,input().split())) p31, p32, p33 = list(map(int,input().split())) A = list(map(int,input().split())) S = input() V6 = p11 * p22 * p33 + p12*p23*p31 + p13*p21*p32 - p11*p23*p32 - p12*p21*p33 - p13*p22*p31 V6 = abs(V6) if V6 == 0: print("D") return DP = [0]*606 for i in range(606): if i%6 == 0: if i//6 >= K: DP[i] = 1 else: DP[i] = 2 for i in range(N-1,-1,-1): a = A[i] DP1 = [0]*606 if S[i] == "K": for j in range(606): nxt1 = j nxt2 = j * (1+a) % 606 if DP[nxt1] == 1 or DP[nxt2] == 1: DP1[j] = 1 elif DP[nxt1] == 2 and DP[nxt2] == 2: DP1[j] = 2 else: DP1[j] = 0 else: for j in range(606): nxt1 = j nxt2 = j * (1+a) % 606 if DP[nxt1] == 2 or DP[nxt2] == 2: DP1[j] = 2 elif DP[nxt1] == 1 and DP[nxt2] == 1: DP1[j] = 1 else: DP1[j] = 0 DP = DP1 #print(V6) #print(DP) if DP[V6%606] == 1: print("K") elif DP[V6%606] == 2: print("P") else: print("D") T = int(input()) for i in range(T): solve()