#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const int inf = 0x3f3f3f3f;
long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = 10 * x + ch - '0';
    ch = getchar();
  }
  return f * x;
}
int a[N], c[N], b[N];
int main() {
  int T = read();
  while (T--) {
    int n = read();
    for (int i = 1; i <= n; i++) a[i] = read();
    int tot = 0;
    for (int i = 1; i <= n; i++) {
      c[i] = read();
      if (c[i]) continue;
      b[++tot] = a[i];
    }
    sort(b + 1, b + tot + 1);
    for (int i = 1; i <= n; i++) {
      if (c[i])
        printf("%d ", a[i]);
      else
        printf("%d ", b[tot--]);
    }
    printf("\n");
  }
  return 0;
}
