#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long inf = 2e18;
const int sz = 256;
const int max_k = 20;
void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  vector<int> check(n);
  for (int i = 0; i < n; ++i) cin >> check[i];
  vector<int> temp;
  for (int i = 0; i < n; ++i)
    if (!check[i]) temp.push_back(a[i]);
  sort(temp.begin(), temp.end(), greater<int>());
  int j = 0;
  for (int i = 0; i < n; ++i) {
    if (!check[i]) a[i] = temp[j++];
  }
  for (int i = 0; i < (int)a.size(); ++i) cout << a[i] << " ";
  cout << "\n";
  ;
}
void input() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int t = 0;
  cin >> t;
  while (t--) {
    solve();
  }
}
int main() {
  input();
  return 0;
}
