#include using namespace std; int st1[220000], st2[220000], aux1[220000], aux2[220000]; void build(int a, int b, int index) { if (a == b) { st1[index] = st2[index] = 0; aux1[index] = aux2[index] = 0; } else { build(a, (a+b)/2, 2*index); build((a+b)/2+1, b, 2*index+1); st1[index] = st2[index] = 0; aux1[index] = aux2[index] = 0; } } void update1(int a, int b, int l, int r, int index, int val) { if (a > r or b < l) return; else if (l <= a and b <= r) { aux1[index] += val; } else { if (aux1[index] != 0) { aux1[2*index] += aux1[index]; aux1[2*index+1] += aux1[index]; aux1[index] = 0; } update1(a, (a+b)/2, l, r, 2*index, val); update1((a+b)/2+1, b, l, r, 2*index+1, val); st1[index] = max(st1[2*index]+aux1[2*index], st1[2*index+1]+aux1[2*index+1]); } } void update2(int a, int b, int l, int r, int index, int val) { if (a > r or b < l) return; else if (l <= a and b <= r) { aux2[index] += val; } else { if (aux2[index] != 0) { aux2[2*index] += aux2[index]; aux2[2*index+1] += aux2[index]; aux2[index] = 0; } update2(a, (a+b)/2, l, r, 2*index, val); update2((a+b)/2+1, b, l, r, 2*index+1, val); st2[index] = max(st2[2*index]+aux2[2*index], st2[2*index+1]+aux2[2*index+1]); } } int query1(int a, int b, int l, int r, int index) { if (a > r or b < l) return 0; else if (l <= a and b <= r) { return (st1[index]+aux1[index]); } else { if (aux1[index] != 0) { aux1[2*index] += aux1[index]; aux1[2*index+1] += aux1[index]; aux1[index] = 0; } return max(query1(a, (a+b)/2, l, r, 2*index), query1((a+b)/2+1, b, l, r, 2*index+1)); } } int query2(int a, int b, int l, int r, int index) { if (a > r or b < l) return 0; else if (l <= a and b <= r) { return (st2[index]+aux2[index]); } else { if (aux2[index] != 0) { aux2[2*index] += aux2[index]; aux2[2*index+1] += aux2[index]; aux2[index] = 0; } return max(query2(a, (a+b)/2, l, r, 2*index), query2((a+b)/2+1, b, l, r, 2*index+1)); } } vector minimumZooNumbers(int m, int n, vector t, vector s, vector d) { vector< vector > inc_1(m+100); vector< vector > inc_2(m+100); int dp[m+100]; memset(dp, 0, sizeof(dp)); build(1, m, 1); for (int i = 0; i < t.size(); i++) { if (t[i] == 'E' or t[i] == 'C') { if (s[i] > d[i]) continue; inc_1[d[i]].push_back(s[i]); } else { if (s[i] > d[i]) continue; inc_2[d[i]].push_back(s[i]); } } for (int i = 1; i <= m; i++) { sort(inc_1[i].begin(), inc_1[i].end()); sort(inc_2[i].begin(), inc_2[i].end()); } dp[0] = 0; for (int i = 1; i <= m; i++) { dp[i] = dp[i-1]; for (int j = inc_1[i].size()-1; j >= 0; j--) { update1(1, m, 1, inc_1[i][j], 1, 1); } for (int j = inc_2[i].size()-1; j >= 0; j--) { update2(1, m, 1, inc_2[i][j], 1, 1); } dp[i] = max(dp[i-1], max(query1(1, m, 1, i-1, 1), query2(1, m, 1, i-1, 1))); update1(1, m, i, i, 1, dp[i]); update2(1, m, i, i, 1, dp[i]); //cout< ans; int curr = 1; for (int i = 1; i <= m; i++) { while (dp[i] >= curr) { curr++; ans.push_back(i); } } int ext = n-ans.size(); for (int i = 0; i < ext; i++) ans.push_back(-1); return ans; } int main() { int cases; cin >> cases; for(int a0 = 0; a0 < cases; a0++){ int m; int n; cin >> m >> n; vector t(n); for(int t_i = 0; t_i < n; t_i++){ cin >> t[t_i]; } vector s(n); for(int s_i = 0; s_i < n; s_i++){ cin >> s[s_i]; } vector d(n); for(int d_i = 0; d_i < n; d_i++){ cin >> d[d_i]; } vector result = minimumZooNumbers(m, n, t, s, d); for (ssize_t i = 0; i < result.size(); i++) { cout << result[i] << (i != result.size() - 1 ? " " : ""); } cout << endl; } return 0; }