#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
int main() {
  cin.sync_with_stdio(false);
  int t, n;
  cin >> t;
  while (t--) {
    cin >> n;
    vector<int> a(n), l(n), b;
    for (auto i = (0); i < (n); ++i) cin >> a[i];
    for (auto i = (0); i < (n); ++i) {
      cin >> l[i];
      if (!l[i]) b.push_back(a[i]);
    }
    sort(b.begin(), b.end());
    for (auto i = (0); i < (n); ++i)
      if (!l[i]) a[i] = b.back(), b.pop_back();
    for (auto i = (0); i < (n); ++i) cout << a[i] << ' ';
    cout << '\n';
  }
}
