#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  vector<long long> v(n);
  for (auto &x : v) cin >> x;
  vector<long long> lc(n);
  for (long long &x : lc) cin >> x;
  vector<long long> sor;
  for (long long i = 0; i < n; ++i) {
    if (lc[i] == 0) {
      sor.push_back(v[i]);
    }
  }
  if (((long long)sor.size()) == 0) {
    for (auto x : v) cout << x << " ";
    cout << endl;
  } else {
    sort(sor.rbegin(), sor.rend());
    long long j = 0;
    for (long long i = 0; i < n; ++i) {
      if (lc[i] == 0) {
        v[i] = sor[j];
        ++j;
      }
      cout << v[i] << " ";
    }
    cout << endl;
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t = 1;
  cin >> t;
  for (long long i = 0; i < t; ++i) {
    solve();
  }
  return 0;
}
