#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c *x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug &operator<<(const c &) {
    return *this;
  }
};
void solve() {
  int n;
  cin >> n;
  vector<int> v(n), l(n);
  for (int &i : v) cin >> i;
  for (int &i : l) cin >> i;
  vector<int> un;
  for (int i = 0; i < n; i++) {
    if (!l[i]) un.push_back(v[i]);
  }
  int res = 0;
  sort(un.rbegin(), un.rend());
  for (int i = 0; i < n; i++) {
    if (!l[i]) {
      v[i] = un[res];
      res++;
    }
  }
  for (int i : v) cout << i << " ";
  cout << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tt;
  cin >> tt;
  while (tt--) {
    solve();
  }
  return 0;
}
