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