#include <bits/stdc++.h>
using namespace std;
ostream& operator<<(ostream& str, const pair<long long, long long>& p) {
  str << p.first << " " << p.second;
  return str;
}
template <typename T>
void join(T& arr, string sep) {
  bool first = true;
  for (auto t : arr) {
    if (first) {
      first = false;
      cout << t;
    } else {
      cout << sep << t;
    }
  }
  cout << "\n";
}
const long long md = 1e9 + 7;
long long MOD(long long x) {
  long long r = x % md;
  if (r < 0) {
    r += md;
  }
  return r;
}
void solve() {
  long long n;
  cin >> n;
  vector<long long> res;
  vector<long long> a(n);
  for (long long i = 0; i < (long long)(n); ++i) {
    cin >> a[i];
  }
  long long sm = accumulate(a.begin(), a.end(), 0);
  vector<long long> b(n);
  vector<long long> c;
  for (long long i = 0; i < (long long)(n); ++i) {
    cin >> b[i];
    if (!b[i]) {
      c.push_back(a[i]);
    }
  }
  sort(c.rbegin(), c.rend());
  long long j = 0;
  res.resize(n);
  for (long long i = 0; i < (long long)(n); ++i) {
    if (b[i]) {
      res[i] = a[i];
    } else {
      assert(j < c.size());
      res[i] = c[j++];
    }
  }
  join(res, " ");
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt = 1;
  cin >> tt;
  for (int t = 1; t <= tt; t++) {
    solve();
  }
  return 0;
}
