#include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef unsigned int ui; typedef long double ld; typedef pair ii; typedef pair iii; ll MOD = 1e9; const ld E = 1e-9; #define null NULL #define ms(x) memset(x, 0, sizeof(x)) #ifndef LOCAL #define endl "\n" #endif #ifndef LONG_LONG_MAX #define LONG_LONG_MAX LLONG_MAX #endif #define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define _read(x) freopen(x, "r", stdin) #define _write(x) freopen(x, "w", stdout) #define files(x) _read(x ".in"); _write(x ".out") #define filesdatsol(x) _read(x ".DAT"); _write(x ".SOL") #define output _write("output.txt") #define input _read("input.txt") #define prev time_prev #ifndef M_PI #define M_PI acos(-1) #endif #define remove tipa_remove #define next tipa_next #define left tipa_left #define right tipa_right #define mod % MOD #define y1 hello_world unsigned char ccc; bool _minus = false; template inline T sqr(T t) { return (t * t); } inline void read(ll &n) { n = 0; _minus = false; while (true) { ccc = getchar(); if (ccc == ' ' || ccc == '\n') break; if (ccc == '-') { _minus = true; continue; } n = n * 10 + ccc - '0'; } if (_minus) n *= -1; } inline bool read(int &n) { n = 0; _minus = false; while (true) { ccc = getchar(); if (ccc == ' ' || ccc == '\n') { if (ccc == '\n') return true; break; } if (ccc == '-') { _minus = true; continue; } n = n * 10 + ccc - '0'; } if (_minus) n *= -1; return false; } char wwww[19]; int kkkk; inline void write(ll y) { long long x = y; kkkk = 0; if (x < 0) { putchar('-'); x *= -1; } if (!x) wwww[++kkkk] = '0'; else while (x) { ++kkkk; wwww[kkkk] = char(x % 10 + '0'); x /= 10; } for (int i = kkkk; i >= 1; --i) putchar(wwww[i]); } #ifdef LOCAL #define __DEBUG #endif #ifdef __DEBUG #define dbg if(1) #else #define dbg if(0) #endif const int MAX = 5e4 + 10; int dp[MAX]; int e1[MAX], e2[MAX]; vector v1[MAX], v2[MAX]; char C[MAX]; int L[MAX], R[MAX]; int ans[MAX]; struct segment_tree{ private: int n, *t, *y; void upd(int v, int tl, int tr, int l, int r, int val){ if(l <= tl && tr <= r){ t[v] += val; y[v] += val; return; } if(tr < l || r < tl) return; int x = (tl + tr) >> 1; upd(v << 1, tl, x, l, r, val); upd(v << 1 | 1, x + 1, tr, l, r, val); t[v] = max(t[v << 1], t[v << 1 | 1]) + y[v]; } int get_max(int v, int tl, int tr, int l, int r){ if(l <= tl && tr <= r){ return t[v]; } if(tr < l || r < tl) return 0; int x = (tl + tr) >> 1; return max(get_max(v << 1, tl, x, l, r), get_max(v << 1 | 1, x + 1, tr, l, r)) + y[v]; } public: segment_tree(int n){ this->n = n; n++; t = new int[n * 4]; y = new int[n * 4]; for(int i = 0; i < n * 4; i++){ t[i] = y[i] = 0; } } void upd(int l, int r, int val){ upd(1, 1, n, l, r, val); } int get_max(int l, int r){ return get_max(1, 1, n, l, r); } }; int g1[MAX], g2[MAX]; void solve(){ int m, n; cin >> m >> n; ms(dp); segment_tree tree1(m); segment_tree tree2(m); for(int i = 0; i <= m; i++){ v1[i].clear(); v2[i].clear(); e1[i] = e2[i] = 0; g1[i] = g2[i] = 0; } for(int i = 1; i <= n; i++){ cin >> C[i]; } for(int i = 1; i <= n; i++){ cin >> L[i]; } for(int i = 1; i <= n; i++){ cin >> R[i]; } for(int i = 1; i <= n; i++){ if(L[i] > R[i]) continue; if(C[i] == 'D' || C[i] == 'M'){ v1[R[i]].push_back(L[i]); }else{ v2[R[i]].push_back(L[i]); } } for(int i = 1; i <= m; i++){ dp[i] = max(dp[i], dp[i - 1]); if(v1[i].empty() && v2[i].empty()){ continue; } for(int a : v1[i]){ tree1.upd(1, a, 1); e1[a]++; g1[a] = i; } for(int a : v2[i]){ tree2.upd(1, a, 1); e2[a]++; g2[a] = i; } dp[i] = max(dp[i], tree1.get_max(1, i)); dp[i] = max(dp[i], tree2.get_max(1, i)); tree1.upd(i, i, dp[i]); tree2.upd(i, i, dp[i]); } for(int i = 1; i <= n; i++){ ans[i] = -1; } for(int i = m; i; i--){ ans[dp[i]] = i; } for(int i = n - 1; i; i--){ if(ans[i + 1] > 0 && (ans[i] > ans[i + 1] || ans[i] == -1)){ ans[i] = ans[i + 1]; } } for(int i = 1; i <= n; i++){ cout << ans[i] << " "; } cout << endl; } int main() { sync; srand((unsigned int) time(NULL)); cout.precision(10); cout << fixed; #ifdef LOCAL input; output; #else #endif int t; cin >> t; while(t--){ solve(); } }