#include <bits/stdc++.h>
using namespace std;
int main() {
  int tc;
  cin >> tc;
  while (tc--) {
    int n;
    cin >> n;
    int arr[n], brr[n];
    vector<int> Operate;
    for (auto &k : arr) cin >> k;
    for (auto &k : brr) cin >> k;
    for (int j = 0; j < n; j++) {
      if (brr[j] == 0) Operate.emplace_back(arr[j]);
    }
    sort(Operate.begin(), Operate.end(), greater<int>());
    int k = -1;
    for (int i = 0; i < n; i++) {
      if (brr[i] == 0) {
        k++;
        arr[i] = Operate[k];
      }
    }
    for (int k : arr) cout << k << " ";
    cout << endl;
  }
}
