#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    vector<int> vec;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    };
    int l[n];
    map<int, int> mpp;
    for (int i = 0; i < n; i++) {
      cin >> l[i];
      if (l[i] == 0) {
        mpp[i] = 0;
        vec.push_back(a[i]);
      } else {
        mpp[i] = 1;
      }
    }
    int j = 0;
    sort(vec.rbegin(), vec.rend());
    for (int i = 0; i < n; i++) {
      if (mpp[i] == 0) {
        cout << vec[j] << " ";
        j++;
      } else {
        cout << a[i] << " ";
      }
    }
    cout << endl;
  }
  return 0;
}
