import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private PrintWriter pw; private long mod = 1000000000; private StringBuilder ans_sb; private static int block; private void soln() { int t = nextInt(); while(t-- > 0) { int n = nextInt(); int m = nextInt(); int[] f = new int[m]; String[] s = nextLine().split(" "); for(int i=0;i[] rtol = new ArrayList[n+1]; for(int i=0;i<=n;i++) rtol[i] = new ArrayList<>(); ArrayList[] ltor = new ArrayList[n+1]; for(int i=0;i<=n;i++) ltor[i] = new ArrayList<>(); int[] sp = nextIntArray(m); int[] ep = nextIntArray(m); Segment[] tree = new Segment[2]; int[][] cnt = new int[n+1][2]; tree[0] = new Segment(n+1); tree[1] = new Segment(n+1); for(int i=0;i sp[i]) { rtol[ep[i]].add(new Pair(sp[i], i)); ltor[ep[i]].add(new Pair(ep[i], i)); } } int overall = 0; int k = 0; // tree[0].print(0, n, 0); for(int i=1;i<=n;i++) { boolean fll = false; if(i != 0) { cnt[i][0] += cnt[i-1][0]; cnt[i][1] += cnt[i-1][1]; } for(Pair p: ltor[i]) { cnt[p.st][f[p.idx]]++; } for(Pair p : rtol[i]) { //cnt[i][f[p.idx]]++; fll = true; if(f[p.idx] == 0) { //min1 = Math.max(p.st, min1); tree[1].update(p.st+1, n, -1, true); }else { //min2 = Math.max(min2, p.st); tree[0].update(p.st+1, n, -1, true); } //tree[0].print(0, n, 0); //tree[1].print(0, n, 0); } if(!fll) continue; int ans0 = tree[0].query(0, i - 1); int ans1 = tree[1].query(0, i - 1); //System.out.println(i+" "+ans0+" "+ans1+" "+(ans1+cnt[i][0])+" "+(ans0+cnt[i][1])); ans0 += cnt[i][1]; ans1 += cnt[i][0]; tree[0].update(i, i, ans1, false); //debug(tree[0].query(i, i)); tree[1].update(i, i, ans0, false); //debug(tree[1].query(i, i)); overall = Math.max(overall, Math.max(ans1, ans0)); while(k < overall) { k++; pw.print(i+" "); } } //tree[0].print(0, n, 0); //tree[1].print(0, n, 0); //debug(cnt); while(k < m) { k++; pw.print(-1+" "); } pw.println(); } } private class Pair{ int st; int idx; public Pair(int a,int b) { st = a; idx = b; } } public class Segment { private int[] tree; private int[] lazy; private long[] base; private int size; private int n; private class Node { private int ans; } public Segment(int n) { //this.base=arr; int x = (int) (Math.ceil(Math.log(n) / Math.log(2))); size = 2 * (int) Math.pow(2, x) - 1; tree = new int[size]; lazy = new int[size]; this.n = n; //this.set = set1; //build(0, 0, n - 1); } /*public void build(int id, int l, int r) { if (l == r) { tree[id] = new Node(); tree[id].l = l; tree[id].r = r; return; } int mid = (l + r) / 2; build(2 * id + 1, l, mid); build(2 * id + 2, mid + 1, r); tree[id] = merge(tree[2*id+1], tree[2*id+2], l, r); //System.out.println(l+" "+r+" "+tree[id].l+" "+tree[id].r+" "+tree[id].ans); }*/ public int merge(int n1, int n2, int l, int r) { /*Node ret = new Node(); if(n1 == null && n2 == null) return null; else if(n1 == null) { ret.ans = n2.ans; ret.l = n2.l; ret.r = n2.r; } else if(n2 == null) { ret.ans = n1.ans; ret.l = n1.l; ret.r = n1.r; } else { ret.l = n1.l; ret.r = n2.r; ret.ans = Math.max(n1.ans, n2.ans); }*/ int ret = Math.max(n1, n2); return ret; } public int query(int l, int r) { int ret = queryUtil(l, r, 0, 0, n - 1); return ret; } private int queryUtil(int x, int y, int id, int l, int r) { if (l > y || x > r) return -1000000; if (x <= l && r <= y) { //shift(id); return tree[id]; } int mid = l + (r - l) / 2; shift(id); int q1 = queryUtil(x, y, 2 * id + 1, l, mid); int q2 = queryUtil(x, y, 2 * id + 2, mid + 1, r); return merge(q1, q2, Math.max(l,x), Math.min(r, y)); } public void update(int x, int y, int r, boolean f) { update1(x, y, r, 0, 0, n-1, f); } private void update1(int x, int y, int colour, int id, int l, int r, boolean f) { //System.out.println(l+" "+r+" "+x); if (x > r || y < l) return; if (x <= l && r <= y) { if(f) { lazy[id] += colour; tree[id] += colour; }else { //lazy[id] = 0; tree[id] += colour; } return; } int mid = l + (r - l) / 2; shift(id); if(y<=mid) update1(x, y, colour, 2 * id + 1, l, mid, f); else if(x>mid) update1(x, y, colour, 2 * id + 2, mid + 1, r, f); else { update1(x, y, colour, 2 * id + 1, l, mid, f); update1(x, y, colour, 2 * id + 2, mid + 1, r, f); } tree[id] = merge(tree[2*id+1], tree[2*id+2], l, r); } /*public void print(int l ,int r,int id) { if(l==r) { if(tree[id] != null) { System.out.println(l+" "+r+" "+lazy[id]); debug(tree[id].ans); } return; } int mid = l + (r - l)/2; print(l,mid,2*id+1); print(mid+1,r,2*id+2); if(tree[id] != null) { System.out.println(l+" "+r+" "+lazy[id]); debug(tree[id].ans); } }*/ public void shift(int id) { if(lazy[id] != 0 ) { lazy[2*id + 1] += lazy[id]; lazy[2*id + 2] += lazy[id]; tree[2*id + 1] += lazy[id]; tree[2*id + 2] += lazy[id]; lazy[id] = 0; } } } private String solveEqn(long a, long b) { long x = 0, y = 1, lastx = 1, lasty = 0, temp; while (b != 0){ long q = a / b; long r = a % b; a = b; b = r; temp = x; x = lastx - q * x; lastx = temp; temp = y; y = lasty - q * y; lasty = temp; } return lastx+" "+lasty; } private class Node implements Comparable{ int node; int dist; @Override public int compareTo(Node arg0) { if(this.dist != arg0.dist) return (int) (this.dist - arg0.dist); return this.node - arg0.node; } public boolean equals(Object o){ if(o instanceof Node){ Node c = (Node)o; return this.node == c.node && this.dist == c.dist; } return false; } public String toString() { return this.node+", "+this.dist; } } private void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } private long pow(long a, long b, long c) { if (b == 0) return 1; long p = pow(a, b / 2, c); p = (p * p) % c; return (b % 2 == 0) ? p : (a * p) % c; } private long gcd(long n, long l) { if (l == 0) return n; return gcd(l, n % l); } public static void main(String[] args) throws Exception { new Thread(null, new Runnable() { @Override public void run() { new Main().solve(); } }, "1", 1 << 26).start(); } public StringBuilder solve(){ InputReader(System.in); /*try { InputReader(new FileInputStream("C:\\Users\\hardik\\Desktop\\in.txt")); } catch(FileNotFoundException e) {} */ pw = new PrintWriter(System.out); ans_sb = new StringBuilder(); soln(); pw.close(); //System.out.println(ans_sb); return ans_sb; } public void InputReader(InputStream stream1) { stream = stream1; } private boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } private int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } private int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } private long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } private String nextToken() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } private int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } private long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } private void pArray(int[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); return; } private void pArray(long[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); return; } private boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } private char nextChar() { int c = read(); while (isSpaceChar(c)) c = read(); char c1 = (char) c; while (!isSpaceChar(c)) c = read(); return c1; } private interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }