#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (auto &i : a) cin >> i;
  vector<long long> c(n);
  for (auto &i : c) cin >> i;
  long long tmp;
  bool b;
  for (int i = 0; i < n; i++) {
    b = true;
    for (int j = 0; j < n; j++) {
      if (b) {
        if (c[j] == 0) {
          b = false;
          tmp = j;
        }
      } else {
        if (c[j] == 0) {
          if (a[j] > a[tmp]) swap(a[j], a[tmp]);
          tmp = j;
        }
      }
    }
  }
  for (auto &i : a) cout << i << " ";
  cout << "\n";
}
int main() {
  long long t;
  cin >> t;
  while (t--) solve();
  return 0;
}
