#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  vector<long long> second(n);
  for (long long i = 0; i < n; ++i) cin >> second[i];
  vector<bool> bl(n);
  for (long long i = 0; i < n; ++i) {
    long long x;
    cin >> x;
    bl[i] = x;
  }
  vector<long long> a;
  for (long long i = 0; i < n; ++i) {
    if (!bl[i]) a.push_back(second[i]);
  }
  sort(a.begin(), a.end());
  reverse(a.begin(), a.end());
  long long l = 0;
  vector<long long> ns(n);
  for (long long i = 0; i < n; ++i) ns[i] = second[i];
  for (long long i = 0; i < n; ++i) {
    if (bl[i]) continue;
    ns[i] = a[l];
    l++;
  }
  for (long long i = 0; i < n; ++i) {
    if (bl[i])
      cout << second[i] << ' ';
    else
      cout << ns[i] << ' ';
  }
  cout << '\n';
}
signed main() {
  long long q;
  cin >> q;
  for (long long i = 0; i < q; ++i) {
    solve();
  }
}
