// main.cpp // solve // // Created by Ahmed on 11/16/16. // Copyright © 2016 Abdellah. All rights reserved. // #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define f first #define s second #define pb push_back #define sz(a) (int)a.size() #define lp(i,a,n) for(int (i)=(a);(i)<=(int)(n);(i)++) #define lpd(i,n,a) for(int (i)=(n);(i)>=(a);--(i)) #define mp make_pair #define clr(a,b) memset(a,b,sizeof a) #define all(v) v.begin(),v.end() #define mod 1000000007 #define eps 1e-6 #define infi 1000000000 #define infll 4000000000000000005ll #define MX 1000000 #define X real() #define Y imag() #define polar(r,t) ((r)* exp(point(0,(t)))) #define length(a) hypot( (a).X , (a).Y ) #define angle(a) atan2( (a).Y , (a).X ) #define vec(a,b) ( (b) - (a) ) #define dot(a,b) (conj(a)*(b)).X #define cross(a,b) (conj(a)*(b)).Y #define lengthsqr(a) dot(a,a) #define reflect(V,M) (conj((V)/(M)) * (M)) #define normalize(a) ((a)/length(a)) #define ccw(a,b,c) cross(vec(a,b) , vec(a,c)) > -eps #define cosRule(a,b,c) (acos(((a)*(a)+(b)*(b)-(c)*(c))/(2*(a)*(b)))) #define cosDot(a,b) (acos(dot(a,b)/length(a)/length(b))) #define EQ(a,b) (fabs((a) - (b)) <= eps) /* equal to */ #define NE(a,b) (fabs((a) - (b)) > eps) /* not equal to */ #define LT(a,b) ((a) < (b) - eps) /* less than */ #define GT(a,b) ((a) > (b) + eps) /* greater than */ #define LE(a,b) ((a) <= (b) + eps) /* less than or equal to */ #define GE(a,b) ((a) >= (b) - eps) /* greater than or equal to */ #define mod1 100050001 #define mod2 100030001 #define base1 37 #define base2 31 #define que priority_queue, vector> , greater > > #define rotate(v,t) ((v)*exp(point(0,t))) #define rotateabout(v,t,a) (rotate(vec(a,v),t)+(a)) #define PI atan(1)*4 using namespace std; typedef long long ll; typedef pair pii; typedef pair pdd; typedef pair pll; typedef vector vi; typedef vector vll; typedef vector vvi; typedef set si; typedef map mii; typedef map mll; typedef unordered_map umll; typedef complex point; typedef pair line; typedef pair Circle; const int N = 50002; int t,m,n,dp[N][2],tree[4*N][2],lazy[4*N][2],type[N],s[N],d[N]; vvi g; void propagate(int i, int j, bool leaf){ tree[i][j] += lazy[i][j]; if(!leaf) lazy[i<<1][j] += lazy[i][j],lazy[(i<<1)|1][j] += lazy[i][j]; lazy[i][j] = 0; } void update(int i, int start, int end, int l, int r, int j, int val){ propagate(i, j, start == end); if(l <= start && r >= end){ lazy[i][j] += val; propagate(i, j, start == end); return; } if(r < start || l > end) return; int mid = (start+end)/2; update(i<<1, start, mid, l, r, j, val), update((i<<1)|1, mid+1, end, l, r, j, val); tree[i][j] = max(tree[i<<1][j], tree[(i<<1)|1][j]); } int query(int i, int start, int end, int l, int r, int j){ propagate(i, j, start == end); if(l <= start && r >= end) return tree[i][j]; if(r < start || l > end) return 0; int mid = (start+end)>>1; return max(query(i<<1,start,mid,l,r,j), query((i<<1)|1,mid+1,end,l,r,j)); } int main(){ scanf("%d",&t); while(t--){ g = vvi(N); scanf("%d%d",&m,&n); lp(i,1,n){ char c; scanf(" %c", &c); type[i] = c == 'D' or c == 'M' ? 0 : 1; } lp(i,1,n) scanf("%d",&s[i]); lp(i,1,n) scanf("%d",&d[i]), g[d[i]].pb(i); lp(i,1,m){ for(int idx : g[i]) if(s[idx] < i) update(1,1,m, 1, s[idx], !type[idx], 1); dp[i][0] = query(1,1,m, 1, i, 1); dp[i][1] = query(1,1,m, 1, i, 0); update(1,1,m, i, i, 0, dp[i][0]); update(1,1,m, i, i, 1, dp[i][1]); } vi ans; lp(i,1,m) ans.pb(max(dp[i][0], dp[i][1])); lp(i,1,n){ int x = (int)(lower_bound(all(ans), i) - ans.begin()) + 1; if(x == m+1) x = -1; printf("%d ", x); } printf("\n"); clr(tree, 0),clr(lazy, 0); } } /* */ //freopen("output.txt","w",stdout); //freopen("input.txt","r",stdin); //ios::sync_with_stdio(0);cin.tie(0);