#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) { cerr << "\n"; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << setw(2) << *it << " = " << setw(2) << a << " ";
  err(++it, args...);
}
void solve() {
  int n;
  cin >> n;
  int a[n], status[n], res[n];
  memset(res, 0, sizeof res);
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) cin >> status[i];
  for (int i = 0; i < n; i++)
    if (status[i]) res[i] = a[i];
  vector<int> v;
  for (int i = 0; i < n; i++) {
    if (status[i] == 0) v.push_back(a[i]);
  }
  sort(v.rbegin(), v.rend());
  int idx = 0;
  for (int i = 0; i < n; i++)
    if (status[i] == 0) res[i] = v[idx++];
  for (int i = 0; i < n; i++) cout << res[i] << " ";
  cout << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int test;
  cin >> test;
  while (test--) solve();
  return 0;
}
