#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
void test() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (auto &a : A) cin >> a;
  vector<int> L(N);
  for (auto &l : L) cin >> l;
  vector<int> unlocked;
  for (int i = 0; i < N; i++)
    if (L[i] == 0) unlocked.push_back(i);
  vector<int> order = unlocked;
  sort(order.begin(), order.end(), [&](int x, int y) { return A[x] > A[y]; });
  vector<int> B = A;
  for (int i = 0; i < int(unlocked.size()); i++) B[unlocked[i]] = A[order[i]];
  for (auto k : B) {
    cout << k << " ";
  }
  cout << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int testcase;
  cin >> testcase;
  while (testcase--) {
    int n;
    cin >> n;
    vector<int> temp(n);
    for (auto &i : temp) {
      cin >> i;
    }
    vector<int> canmove;
    vector<int> lock(n);
    for (auto &k : lock) {
      cin >> k;
    }
    for (int i = 0; i < n; i++) {
      if (lock[i] == 0) {
        canmove.push_back(temp[i]);
      }
    }
    sort(canmove.begin(), canmove.end());
    int count = 0;
    for (int i = n - 1; i >= 0; i--) {
      if (lock[i] == 0) {
        temp[i] = canmove[count];
        count++;
      }
    }
    for (auto k : temp) {
      cout << k << " ";
    }
    cout << "\n";
  }
  return 0;
}
