#include using namespace std; using ll = long long; static const ll INF = (ll)4e18; 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> A[i]; vector L(2*N+5,-1), R(2*N+5,-1); vector> dp(2*N+5); vector cur(N); iota(cur.begin(), cur.end(), 0); int id = N; for(int i=0;i dfs = [&](int v){ if(v < N){ dp[v]={1,1,1}; return; } dfs(L[v]); dfs(R[v]); array res={0,0,0}; char C[3]={'R','P','S'}; for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ char w=win(C[i],C[j]); int t=(w=='R'?0:w=='P'?1:2); res[t]+=dp[L[v]][i]*dp[R[v]][j]; if(res[t]>INF) res[t]=INF; } } dp[v]=res; }; dfs(root); auto solve = [&](int target)->string{ function go = [&](int v,ll k)->string{ if(v < N){ string s="RPS"; for(char c:s){ if(k==1) return string(1,c); k--; } } char ord[3]={'R','P','S'}; for(char c:ord){ ll cnt=0; for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ char w=win("RPS"[i],"RPS"[j]); int t=(w=='R'?0:w=='P'?1:2); if(t==target) cnt+=dp[L[v]][i]*dp[R[v]][j]; } } if(k>cnt) k-=cnt; else return go(L[v],k)+go(R[v],k); } return "-1"; }; if(dp[root][target]