#include <bits/stdc++.h>
using namespace std;
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    int b[n];
    vector<int> c;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
      cin >> b[i];
    }
    for (int i = 0; i < n; i++) {
      if (!b[i]) c.push_back(a[i]);
    }
    sort(c.rbegin(), c.rend());
    int j = 0;
    for (int i = 0; i < n; i++) {
      if (b[i])
        cout << a[i] << ' ';
      else
        cout << c[j++] << ' ';
    }
    cout << '\n';
  }
  return 0;
}
