#include <bits/stdc++.h>
using namespace std;
long long gcd(long long n1, long long n2) {
  if (n1 % n2 == 0) return n2;
  return gcd(n2, n1 % n2);
}
long long powmod(long long base, long long exponent) {
  long long ans = 1;
  while (exponent) {
    if (exponent & 1) ans = (ans * base) % 1000000007;
    base = (base * base) % 1000000007;
    exponent /= 2;
  }
  return ans;
}
long long max(long long a, long long b, long long c) {
  return max(a, max(b, c));
}
int max(int a, int b, int c) { return max(a, max(b, c)); }
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    int a[n], l[n];
    vector<int> cons;
    for (int i = 0; i < n; i = i + 1) {
      scanf("%d", &a[i]);
    }
    for (int i = 0; i < n; i = i + 1) {
      scanf("%d", &l[i]);
      if (l[i] == 0) cons.push_back(a[i]);
    }
    sort(cons.begin(), cons.end());
    reverse(cons.begin(), cons.end());
    for (int i = n - 1; i >= 0; i--) {
      if (l[i] == 0) {
        a[i] = cons.back();
        cons.pop_back();
      }
    }
    for (int i = 0; i < n; i = i + 1) cout << a[i] << " ";
    cout << '\n';
  }
}
