#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, i;
    cin >> n;
    int a[n], b[n];
    for (i = 0; i < n; i++) cin >> a[i];
    for (i = 0; i < n; i++) cin >> b[i];
    vector<int> v, v1;
    for (i = 0; i < n; i++) {
      if (b[i] == 0) v.push_back(a[i]);
    }
    auto it = v.begin();
    sort(v.begin(), v.end(), greater<int>());
    for (i = 0; i < n; i++) {
      if (b[i] == 1)
        cout << a[i] << " ";
      else {
        cout << *it << " ";
        it++;
      }
    }
    cout << endl;
  }
  return 0;
}
