#include <bits/stdc++.h>
using namespace std;
long long t, n, a[200020], b[200020];
vector<int> unlckd;
int main() {
  ios::sync_with_stdio(false);
  cin >> t;
  while (t--) {
    unlckd.clear();
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) {
      cin >> b[i];
      if (b[i] == 0) unlckd.push_back(a[i]);
    }
    sort(unlckd.begin(), unlckd.end());
    reverse(unlckd.begin(), unlckd.end());
    int j = 0;
    for (int i = 0; i < n; i++) {
      if (b[i] == 0)
        cout << unlckd[j++] << " ";
      else
        cout << a[i] << " ";
    }
    cout << "\n";
  }
  return 0;
}
