#include using namespace std; using ll = long long; const ll INF = (ll)1e18; struct Node { int left = -1, right = -1; ll cnt[3]; // 0=R,1=P,2=S }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; ll K; cin >> N >> K; vector A(N - 1); for (int i = 0; i < N - 1; i++) cin >> A[i]; vector nodes; nodes.reserve(2 * N); // create leaves vector cur; for (int i = 0; i < N; i++) { Node nd; nd.cnt[0] = nd.cnt[1] = nd.cnt[2] = 1; nodes.push_back(nd); cur.push_back((int)nodes.size() - 1); } // build tree for (int i = 0; i < N - 1; i++) { int pos = A[i] - 1; int L = cur[pos]; int R = cur[pos + 1]; Node nd; nd.left = L; nd.right = R; // R = (R,S), P = (P,R), S = (S,P) nd.cnt[0] = min(INF, nodes[L].cnt[0] * nodes[R].cnt[2]); nd.cnt[1] = min(INF, nodes[L].cnt[1] * nodes[R].cnt[0]); nd.cnt[2] = min(INF, nodes[L].cnt[2] * nodes[R].cnt[1]); nodes.push_back(nd); int id = (int)nodes.size() - 1; // replace in sequence cur.erase(cur.begin() + pos, cur.begin() + pos + 2); cur.insert(cur.begin() + pos, id); } int root = cur[0]; function build = [&](int u, int target, ll k) -> string { if (nodes[u].left == -1) { if (target == 0) return "R"; if (target == 1) return "P"; return "S"; } int L = nodes[u].left; int R = nodes[u].right; int needL, needR; if (target == 0) { // R needL = 0; needR = 2; } else if (target == 1) { // P needL = 1; needR = 0; } else { // S needL = 2; needR = 1; } ll rightCnt = nodes[R].cnt[needR]; rightCnt = min(rightCnt, INF); ll leftIdx = (k - 1) / rightCnt + 1; ll rightIdx = (k - 1) % rightCnt + 1; return build(L, needL, leftIdx) + build(R, needR, rightIdx); }; for (int t = 0; t < 3; t++) { if (nodes[root].cnt[t] < K) { cout << -1 << '\n'; } else { cout << build(root, t, K) << '\n'; } } } return 0; }