#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long arr[n];
    for (long long i = 0; i < n; i++) {
      cin >> arr[i];
    }
    long long l[n];
    for (long long i = 0; i < n; i++) cin >> l[i];
    vector<long long> pos, neg, real;
    for (long long i = 0; i < n; i++) {
      if (l[i] == 0) {
        pos.push_back(arr[i]);
      }
    }
    sort(pos.begin(), pos.end(), greater<long long>());
    for (long long i = 0; i < pos.size(); i++) {
      real.push_back(pos[i]);
    }
    for (long long i = 0, j = 0; i < n; i++) {
      if (l[i] == 0) {
        arr[i] = real[j];
        j++;
      }
    }
    for (long long x : arr) cout << x << " ";
    cout << "\n";
  }
  return 0;
}
