#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  vector<long long> a(n), l(n), unlocked;
  for (long long i = 0; i < n; i++) cin >> a[i];
  for (long long i = 0; i < n; i++) cin >> l[i];
  for (long long i = 0; i < n; i++)
    if (!l[i]) unlocked.push_back(a[i]);
  sort(unlocked.rbegin(), unlocked.rend());
  for (long long i = 0, j = 0; i < n; i++) {
    if (l[i])
      cout << a[i] << " ";
    else
      cout << unlocked[j++] << " ";
  }
  cout << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
}
