#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  int a[n], cond[n];
  for (int i = 0; i < n; ++i) cin >> a[i];
  vector<int> x;
  for (int i = 0; i < n; ++i) {
    cin >> cond[i];
    if (cond[i] == 0) {
      x.push_back(a[i]);
    }
  }
  sort(x.begin(), x.end());
  reverse(x.begin(), x.end());
  int j = 0;
  for (int i = 0; i < n; ++i) {
    if (cond[i] == 0) {
      a[i] = x[j++];
    }
  }
  for (int i = 0; i < n; ++i) cout << a[i] << " ";
  cout << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
}
