#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];
    vector<int> v;
    for (i = 0; i < n; i++) cin >> a[i];
    for (i = 0; i < n; i++) cin >> b[i];
    for (i = 0; i < n; i++) {
      if (!b[i]) v.push_back(a[i]);
    }
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());
    int k = 0;
    for (i = 0; i < n; i++) {
      if (!b[i]) {
        a[i] = v[k];
        k++;
      }
      cout << a[i] << " ";
    }
    cout << endl;
  }
}
