#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, i, j;
  cin >> n;
  int arr[n], locked[n];
  for (i = 0; i < n; i++) cin >> arr[i];
  for (i = 0; i < n; i++) cin >> locked[i];
  vector<int> unlocked;
  for (i = 0; i < n; i++)
    if (!locked[i]) unlocked.push_back(arr[i]);
  sort(unlocked.begin(), unlocked.end());
  j = unlocked.size() - 1;
  for (i = 0; i < n; i++) {
    if (!locked[i]) arr[i] = unlocked[j--];
  }
  for (i = 0; i < n; i++) cout << arr[i] << " ";
  cout << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
