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