#include #ifndef LOCAL #define cerr dolor_sit_amet #endif #define mp make_pair #define sz(x) ((int)((x).size())) #define X first #define Y second #define ALL(x) (x).begin(), (x).end() using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair < int , int > ipair; typedef pair < ll , ll > lpair; const int IINF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3fll; const double DINF = numeric_limits::infinity(); const int MOD = 1000000007; const double EPS = 1e-9; const double PI = acos(-1.0); const int DX[] = { 1, 0, -1, 0, 1, -1, 1, -1}; const int DY[] = { 0, 1, 0, -1, 1, -1, -1, 1}; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll sqr(ll x) { return x*x; } ll sqr(int x) { return (ll)x*x; } double sqr(double x) { return x*x; } ld sqr(ld x) { return x*x; } mt19937 mmtw(960172); ll rnd(ll x, ll y) { static uniform_int_distribution d; return d(mmtw) % (y - x + 1) + x; } // ========================================================================= // const int N = 1 << 16; struct Animal { int l, r; int t; } a[N]; int n; struct SegTree { int tmax[N*2-1]; int tadd[N*2-1]; void clear() { memset(tmax, 0, sizeof(tmax)); memset(tadd, 0, sizeof(tadd)); } void add(int l, int r, int v, int c = 0, int cl = 0, int cr = N - 1) { if (cl > r || l > cr) return; if (l <= cl && cr <= r) { tmax[c] += v; tadd[c] += v; return; } int c1 = c*2+1, c2 = c*2+2; tmax[c1] += tadd[c]; tadd[c1] += tadd[c]; tmax[c2] += tadd[c]; tadd[c2] += tadd[c]; tadd[c] = 0; add(l, r, v, c1, cl, (cl+cr)/2); add(l, r, v, c2, (cl+cr)/2+1, cr); tmax[c] = max(tmax[c1], tmax[c2]); } int getMax(int l, int r, int c = 0, int cl = 0, int cr = N - 1) { if (cl > r || l > cr) return 0; if (l <= cl && cr <= r) { return tmax[c]; } int c1 = c*2+1, c2 = c*2+2; tmax[c1] += tadd[c]; tadd[c1] += tadd[c]; tmax[c2] += tadd[c]; tadd[c2] += tadd[c]; tadd[c] = 0; return max(getMax(l, r, c1, cl, (cl+cr)/2), getMax(l, r, c2, (cl+cr)/2+1, cr)); } } ff[2]; int d[N]; int rs[N]; void solve() { int n0; scanf("%*d%d", &n); n0 = n; for (int i = 0; i < n; ++i) { char cc[4]; scanf("%s", cc); if (cc[0] == 'E' || cc[0] == 'C') a[i].t = 0; else a[i].t = 1; } for (int i = 0; i < n; ++i) { scanf("%d", &a[i].l); ++a[i].l; } for (int i = 0; i < n; ++i) { scanf("%d", &a[i].r); } for (int i = 0; i < n; ++i) { if (a[i].l > a[i].r) { a[i] = a[--n]; --i; continue; } } sort(a, a + n, [](Animal const& x, Animal const& y) { return x.r < y.r; }); for (int i = 0; i < n; ++i) rs[i] = a[i].r; ff[0].clear(); ff[1].clear(); for (int i = 0; i < n; ++i) { int m = lower_bound(rs, rs + n, a[i].l) - rs; ff[a[i].t^1].add(0, m, 1); d[i] = ff[a[i].t^1].getMax(0, m); ff[a[i].t].add(i+1, i+1, d[i]); } int cc = 1; for (int i = 0; i < n; ++i) { while (cc <= d[i]) { ++cc; cout << rs[i] << " "; } } while (cc <= n0) { ++cc; cout << "-1 "; } cout << "\n"; } int main() { ios::sync_with_stdio(false); int t; scanf("%d", &t); while (t--) solve(); return 0; }