#pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include using namespace std; const int TREE_SIZE = 500 * 1000; template struct RIPLILPEEP { int n; vector t; vector d; int h; void apply(int p, T value) { t[p] += value; if (p < n) d[p] += value; } void recalc(int p) { while (p > 1) p >>= 1, t[p] = max(t[p<<1], t[p<<1|1]) + d[p]; } void push(int p) { for (int s = h; s > 0; --s) { int i = p >> s; if (d[i] != 0) { apply(i<<1, d[i]); apply(i<<1|1, d[i]); d[i] = 0; } } } void inc(int l, int r, T value) { l += n, r += n; int l0 = l, r0 = r; for (; l < r; l >>= 1, r >>= 1) { if (l & 1) apply(l++, value); if (r & 1) apply(--r, value); } recalc(l0); recalc(r0 - 1); } T query(int l, int r) { l += n, r += n; push(l); push(r - 1); T res = numeric_limits::min(); for (; l < r; l >>= 1, r >>= 1) { if (l & 1) res = max(res, t[l++]); if (r & 1) res = max(t[--r], res); } return res; } RIPLILPEEP(int n): n(n), t(n << 1, 0), h(32 - __builtin_clz(n)), d(n, 0) {} }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n, m; cin >> m >> n; vector>> v(m + 1); vector type(n); vector source(n), destination(n); for (auto &value: type) cin >> value; for (auto &value: source) cin >> value; for (auto &value: destination) cin >> value; for (int i = 0; i < n; i++) { if (source[i] > destination[i]) continue; v[destination[i]].emplace_back(type[i], source[i]); } RIPLILPEEP dp_dm(m + 5), dp_ec(m + 5); vector cnt_destination(26, 0); int ptr = 0; vector ans(n + 1, -1); for (int i = 1; i <= m; i++) { for (auto x: v[i]) { cnt_destination[x.first - 'A']++; if (x.first == 'E' || x.first == 'C') { if (x.second < m) dp_dm.inc(x.second + 1, m + 1, -1); } else { if (x.second < m) dp_ec.inc(x.second + 1, m + 1, -1); } } int ec_i = dp_dm.query(0, i); ec_i += cnt_destination['E' - 'A'] + cnt_destination['C' - 'A']; dp_ec.inc(i, i + 1, ec_i); int dm_i = dp_ec.query(0, i); dm_i += cnt_destination['D' - 'A'] + cnt_destination['M' - 'A']; dp_dm.inc(i, i + 1, dm_i); while (ptr < max(ec_i, dm_i)) ans[++ptr] = i; } for (int i = 1; i <= n; i++) cout << ans[i] << " "; cout << "\n"; } return 0; }