#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename... T>
void rd(T&... args) {
  ((cin >> args), ...);
}
template <typename... T>
void wr(T... args) {
  ((cout << args << " "), ...);
  cout << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int tt;
  cin >> tt;
  while (tt--) {
    int n;
    cin >> n;
    vector<int> a(n), l(n);
    for (auto& i : a) cin >> i;
    for (auto& i : l) cin >> i;
    vector<int> tmp;
    for (int i = 0; i < n; i++) {
      if (!l[i]) {
        tmp.push_back(a[i]);
      }
    }
    sort(tmp.begin(), tmp.end());
    for (int i = 0; i < n; i++) {
      if (l[i])
        cout << a[i] << ' ';
      else {
        cout << tmp.back() << ' ';
        tmp.pop_back();
      }
    }
    cout << '\n';
  }
  return 0;
}
