#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  long long a[n];
  for (auto &val : a) {
    cin >> val;
  }
  int b[n];
  for (auto &val : b) {
    cin >> val;
  }
  vector<long long> arr;
  for (int i = 0; i < n; i++) {
    if (!b[i]) arr.push_back(a[i]);
  }
  sort(arr.begin(), arr.end());
  int m = arr.size();
  int ptr = m - 1;
  for (int i = 0; i < n; i++) {
    if (b[i] == 1)
      cout << a[i] << " ";
    else
      cout << arr[ptr--] << " ";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
    cout << "\n";
  }
  return 0;
}
