#include using namespace std; typedef unsigned long long u64; const u64 CAP = (u64)2e18 + 5; u64 smul(u64 a, u64 b){ if(!a||!b) return 0; if(a > CAP/b) return CAP; u64 r=a*b; return r>CAP?CAP:r; } u64 sadd(u64 a, u64 b){ u64 s=a+b; if(s>CAP||s t; void init(int n_){n=n_; t.assign(n+2,0);} void upd(int i,int v){for(;i<=n;i+=i&-i) t[i]+=v;} int kth(int k){int p=0; for(int i=17;i>=0;i--){int q=p+(1<>T; while(T--){ int N; u64 K; cin>>N>>K; vector A(N-1); for(auto&x:A) cin>>x; int tot=2*N; vector LC(tot,-1), RC(tot,-1); vector> cnt(tot); for(int i=0;i na(N+2); for(int i=1;i<=N;i++) na[i]=i-1; for(int i=0;i w; }; int tg[3]={1,0,2}; const char* ch="PRS"; for(int ti=0;ti<3;ti++){ int t=tg[ti]; if(cnt[root][t] st; array w0={0,0,0}; w0[t]=1; st.push_back({root,0,0,w0}); int rv=-1; while(!st.empty()){ F&f=st.back(); if(LC[f.v]==-1){ for(int c=0;c<3;c++){ if(Kc<=f.w[c]){ans.push_back(ch[c]); rv=c; break;} Kc-=f.w[c]; } st.pop_back(); continue; } if(f.ph==0){ array wL={0,0,0}; auto&cr=cnt[RC[f.v]]; for(int l=0;l<3;l++) for(int r=0;r<3;r++) if(l!=r){ int v=W_[l][r]; wL[l]=sadd(wL[l],smul(cr[r],f.w[v])); } int ch2=LC[f.v]; f.ph=1; st.push_back({ch2,0,0,wL}); } else if(f.ph==1){ f.lv=rv; array wR={0,0,0}; for(int r=0;r<3;r++) if(r!=f.lv){ int v=W_[f.lv][r]; wR[r]=f.w[v]; } int ch2=RC[f.v]; f.ph=2; st.push_back({ch2,0,0,wR}); } else { rv=W_[f.lv][rv]; st.pop_back(); } } cout<