#include #include #include // #include #define gc getchar//_unlocked #define pc putchar//_unlocked #define ll long long #define ld long double #define pb push_back #define mp make_pair #define pp pair #define ppl pair #define bigint boost::multiprecision::cpp_int #define finp ios_base::sync_with_stdio(0);cin.tie(0); #define bc __builtin_popcountll #define afor(i,a,b) for(int i=a;i<=b;++i) #define bfor(i,a,b) for(int i=a;i>=b;--i) #define vi vector #define vpp vector #define vll vector using namespace std; using namespace __gnu_pbds; char putnb[20]; void putn(ll n) {if(!n)pc('0');if(n<0)pc('-'),n=0-n;int pi=0;while(n)putnb[pi++]=(n%10)+'0',n/=10;while(pi)pc(putnb[--pi]);} void sci(int *x) {register char c = gc();*x = 0;for(; (c<48)||(c>57);c = gc());for(; (c>47)&&(c<58);c = gc())*x = (int)((((*x)<<1) + ((*x)<<3)) + c - 48);} void scll(ll *x) {register char c = gc();*x = 0;for(; (c<48)||(c>57);c = gc());for(; (c>47)&&(c<58);c = gc())*x = (ll)((((*x)<<1) + ((*x)<<3)) + c - 48);} ll fp(ll a,ll b,ll c) {if(b==0)return 1%c; if(b==1)return a%c; ll ret=fp(a,b/2,c); ret=(ret*ret)%c; if(b&1)ret=(ret*a)%c; return ret;} const ll mod=1e9 +7; const ll mod2=1999999973; const ll inf=1e18; const int infs=1e9 + 1000; const int N=100000; const long double PI = acos(-1); template using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; // EC or DM int t; int m,n; char s[N+5]; vi st[5][N+5]; int dp[N+5]; int t0[N*6 + 5];//range update,range max query int t1[N*6 + 5]; int lzy0[N*6 + 5]; int lzy1[N*6 + 5]; void update(int tr[],int lzy[],int node,int a,int b,int i,int j,int v) { if(a>b || i>j)return; tr[node]+=lzy[node]; if(a < b) { lzy[node*2]+= lzy[node]; lzy[node*2 + 1]+= lzy[node]; } lzy[node] = 0; if(a>j || b=i && b<=j) { tr[node]+=v; if(a < b) { lzy[node*2]+=v; lzy[node*2 + 1]+=v; } return; } int mid = (a + b)/2; update(tr,lzy,node*2,a,mid,i,j,v); update(tr,lzy,node*2 +1,mid+1,b,i,j,v); tr[node] = max(tr[node*2],tr[node*2 + 1]); } int query(int tr[],int lzy[],int node,int a,int b,int i,int j) { if(a>b || i>j)return 0; tr[node]+=lzy[node]; if(a < b) { lzy[node*2]+=lzy[node]; lzy[node*2 + 1]+=lzy[node]; } lzy[node] = 0; if(a>j || b=i && b<=j)return tr[node]; int mid = (a + b)/2; int r1 = query(tr,lzy,node*2,a,mid,i,j); int r2 = query(tr,lzy,node*2 +1,mid+1,b,i,j); return max(r1,r2); } int ss[N+5]; int ans[N+5]; int dp2[N+5]; int dp3[N+5]; int main() { finp; cin>>t; while(t--) { cin>>m>>n; //assert(m*n <= 1000000); assert(n <= 50000); afor(i,1,m)afor(j,0,1)st[j][i].clear(); afor(i,1,n)cin>>s[i]; afor(i,1,n)cin>>ss[i]; afor(i,1,n) { int des; cin>>des; assert(des>=1 && des<=m); assert(des != ss[i]); if(des > ss[i]) { if(s[i]=='E' || s[i]=='C') { st[0][des].pb(ss[i]); } else { st[1][des].pb(ss[i]); } } } afor(i,0,5*m + 2) { t0[i]=t1[i]=lzy0[i]=lzy1[i]=0; } afor(i,1,m) { dp[i] = dp[i-1]; for(auto j:st[0][i]) { update(t0,lzy0,1,1,m,1,j,1); //afor(jj,1,j)dp2[jj]++; } //afor(j,1,i-1)dp[i] = max(dp[i],dp2[j]); dp[i] = max(dp[i],query(t0,lzy0,1,1,m,1,i-1)); for(auto j:st[1][i]) { update(t1,lzy1,1,1,m,1,j,1); // afor(jj,1,j)dp3[jj]++; } //afor(j,1,i-1)dp[i] = max(dp[i],dp3[j]); dp[i] = max(dp[i],query(t1,lzy1,1,1,m,1,i-1)); update(t0,lzy0,1,1,m,i,i,dp[i]); update(t1,lzy1,1,1,m,i,i,dp[i]); //dp2[i] = dp[i]; //dp3[i] = dp[i]; } afor(i,1,n+1)ans[i] = infs; afor(i,1,m) { int x = dp[i]; ans[x] = min(ans[x],i); } bfor(x,n,1) { ans[x] = min(ans[x] , ans[x+1]); } afor(x,1,n) { if(ans[x] == infs)cout<<"-1 "; else cout<