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