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