import sys input = sys.stdin.readline INF = 10**18 # R=0, P=1, S=2 def solve(): T = int(input()) for _ in range(T): N, K = map(int, input().split()) A = list(map(int, input().split())) # --- Build tree --- left = [-1] * (2 * N) right = [-1] * (2 * N) nodes = list(range(N)) # current active nodes ptr = N # next new node index for i in range(N - 1): a = A[i] - 1 L = nodes[a] R = nodes[a + 1] left[ptr] = L right[ptr] = R # replace two nodes with new node nodes[a:a+2] = [ptr] ptr += 1 root = nodes[0] # --- DP counts --- cnt = [[0, 0, 0] for _ in range(ptr)] def dfs(u): if left[u] == -1: cnt[u] = [1, 1, 1] return dfs(left[u]) dfs(right[u]) L, R = left[u], right[u] # R: (R, S) cnt[u][0] = min(INF, cnt[L][0] * cnt[R][2]) # P: (P, R) cnt[u][1] = min(INF, cnt[L][1] * cnt[R][0]) # S: (S, P) cnt[u][2] = min(INF, cnt[L][2] * cnt[R][1]) dfs(root) # --- reconstruction --- def build(u, target, k): if left[u] == -1: return "RPS"[target] L, R = left[u], right[u] if target == 0: # R tL, tR = 0, 2 elif target == 1: # P tL, tR = 1, 0 else: # S tL, tR = 2, 1 right_count = cnt[R][tR] # determine indices left_index = (k - 1) // right_count + 1 right_index = (k - 1) % right_count + 1 return build(L, tL, left_index) + build(R, tR, right_index) # --- output for R, P, S --- for t in range(3): if cnt[root][t] < K: print(-1) else: print(build(root, t, K)) if __name__ == "__main__": solve()