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