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