#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int a[n], b[n];
    vector<int> v;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
      cin >> b[i];
      if (b[i] == 0) v.push_back(a[i]);
    }
    sort(v.begin(), v.end(), greater<int>());
    for (int i = 0, j = 0; i < n; i++) {
      if (b[i] == 1)
        cout << a[i] << " ";
      else {
        cout << v[j] << " ";
        j++;
      }
    }
    cout << "\n";
  }
  return 0;
}
