#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long a[n + 2];
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
    }
    vector<long long> v;
    for (long long i = 0; i < n; i++) {
      long long x;
      cin >> x;
      if (x == 0) {
        v.push_back(a[i]);
        a[i] = 1000000;
      }
    }
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());
    vector<long long> ans;
    for (long long i = 0, j = 0; i < n; i++) {
      if (a[i] < 1000000) {
        ans.push_back(a[i]);
      } else {
        ans.push_back(v[j]);
        j++;
      }
    }
    for (auto i : ans) cout << i << " ";
    cout << endl;
  }
  return 0;
}
