#pragma GCC optimize("Ofast,no-stack-protector") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include #define ff(i, a, b) for(int i=(a); (i <= (b)); ++i) #define FF(i, a, b) for(int i=(a); (i >= (b)); --i) #define fin( v , a ) for(auto v : a ) #define ll long long #define random(a, b) (rand() % (b - a + 1) + a) #define pi 3.141592653589793238462643383279502884 #define INRA(a,u,v) {cout << "_\n"; ff(i,u,v) cout << a[i].x << " " << a[i].y << endl; cout << "|\n";} #define pb push_back #define mp make_pair #define y1 ahsclskajlasjdkl #define x1 akjadhaskdals using namespace std; // Fast IO ******************************************************************************************************** const int __BS = 4096; static char __bur[__BS + 16], *__er = __bur + __BS, *__ir = __er; template T readInt() { auto c = [&]() { if (__ir == __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; }; c(); while (*__ir && (*__ir < '0' || *__ir > '9') && *__ir != '-') ++__ir; c(); bool m = false; if (*__ir == '-') ++__ir, c(), m = true; T r = 0; while (*__ir >= '0' && *__ir <= '9') r = r * 10 + *__ir - '0', ++__ir, c(); ++__ir; return m ? -r : r; } static char __buw[__BS + 20], *__iw = __buw, *__ew = __buw + __BS; template void writeInt(T x, char endc = '\n') { if (x < 0) *__iw++ = '-', x = -x; if (x == 0) *__iw++ = '0'; char* s = __iw; while (x) { T t = x / 10; char c = x - 10 * t + '0'; *__iw++ = c; x = t; } char* f = __iw - 1; while (s < f) swap(*s, *f), ++s, --f; if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw; *__iw++ = endc; } template void writeStr(const T& str) { int i = 0; while (str[i]) { *__iw++ = str[i++]; if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw; } } struct __FL__ { ~__FL__() { if (__iw != __buw) cout.write(__buw, __iw - __buw); } }; static __FL__ __flushVar__; //******************************************************************************************************************* template void minimize(X &a, const Y &b) { a = (b void maximize(X &a, const Y &b) { a = (b>a) ? b : a; } const int M = 1000000000+7; const int MN = 5*10000+10; struct re { int t, x, y; }; bool cmp(re a, re b) { return a.y < b.y; } int t, n, m, it[2][MN*4], c[2][MN*4], f[MN]; re a[MN]; void upd(int t, int cs, int x, int y, int u, int v) { if (x>v || y=u && y<=v) c[t][cs]++; else { int mid = (x+y)/2; upd(t, cs*2, x, mid, u, v); upd(t, cs*2+1, mid+1, y, u, v); it[t][cs] = max(it[t][cs*2] + c[t][cs*2], it[t][cs*2+1] + c[t][cs*2+1]); } } void update(int t, int cs, int x, int y, int u, int g) { if (uy) return; if (x == y) it[t][cs] = g; else { int mid = (x+y)/2; update(t, cs*2, x, mid, u, g); update(t, cs*2+1, mid+1, y, u, g); it[t][cs] = max(it[t][cs*2] + c[t][cs*2], it[t][cs*2+1] + c[t][cs*2+1]); } } int getmax(int t, int cs, int x, int y, int u, int v) { if (u > y || v < x) return 0; if (x >= u && y <= v) return it[t][cs] + c[t][cs]; int mid = (x+y)/2; return max(getmax(t, cs*2, x, mid, u, v), getmax(t, cs*2+1, mid+1, y, u, v)) + c[t][cs]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); //freopen("checking.inp", "r" , stdin); //freopen("checking.out", "w", stdout); cin >> t; while (t--) { cin >> n >> m; ff(i, 1, m) { char ch; cin >> ch; if (ch == 'E' || ch == 'C') a[i].t = 0; else a[i].t = 1; } ff(i, 1, m) cin >> a[i].x; ff(i, 1, m) cin >> a[i].y; sort(a+1, a+1+m, cmp); //ff(i,1,m) cout << a[i].t << " " << a[i].x << " " << a[i].y << endl; memset(it, 0, sizeof(it)); memset(c, 0, sizeof(c)); int j = 1, tg0, tg1; ff(i, 1, n) { while (j <= m && a[j].y <= i) { if (a[j].x < a[j].y) upd(a[j].t, 1, 1, n, 1, a[j].x); j++; } tg0 = getmax(0, 1, 1, n, 1, i-1); tg1 = getmax(1, 1, 1, n, 1, i-1); f[i] = max(tg0, tg1); update(0, 1, 1, n, i, f[i]); update(1, 1, 1, n, i, f[i]); } j = 1; ff(i, 1, m) { while (j <= n && f[j] < i) j++; if (j>n) cout << -1 << " "; else cout << j << " "; } cout << endl; //ff(i,1,n) cout << f[i] << " "; cout << endl << endl; } } //This code belongs to S34vv1nd :3