#include <bits/stdc++.h>
using namespace std;
template <class T>
T nrem(T x, T y) {
  return ((x % y) + y) % y;
}
const long long M = 1e9 + 7;
long long mod(long long x) { return ((x % M + M) % M); }
void in() {}
void solve() {
  long long n;
  cin >> n;
  long long a[n], l[n];
  for (int i = 0; i < n; ++i) cin >> a[i];
  for (int i = 0; i < n; ++i) cin >> l[i];
  vector<int> pos;
  for (int i = 0; i < n; ++i)
    if (!l[i]) pos.push_back(a[i]);
  sort(pos.begin(), pos.end(), greater<int>());
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (l[i])
      cout << a[i] << " ";
    else
      cout << pos[cnt++] << " ";
  }
  cout << "\n";
}
int main() {
  in();
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
}
