#include <bits/stdc++.h>
using namespace std;
const long long inf = LLONG_MAX;
const long long mod = 1e9 + 7;
void solve() {
  long long n;
  cin >> n;
  vector<long long> a(n), l(n), u;
  for (long long i = (0); i < (n); i++) cin >> a[i];
  for (long long i = (0); i < (n); i++) {
    cin >> l[i];
    if (l[i] == 0) {
      u.push_back(a[i]);
    }
  }
  sort(u.begin(), u.end(), greater<long long>());
  long long idx = 0;
  for (long long i = (0); i < (n); i++) {
    if (l[i] == 0) {
      a[i] = u[idx++];
    }
    cout << a[i] << " ";
  }
  cout << "\n";
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) solve();
  return 0;
}
