#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int arr[n];
    int l[n];
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
    }
    for (int i = 0; i < n; i++) {
      cin >> l[i];
    }
    vector<int> temp;
    for (int i = 0; i < n; i++) {
      if (l[i] == 0) {
        temp.push_back(arr[i]);
      }
    }
    sort(temp.begin(), temp.end(), greater<int>());
    int i = 0;
    int j = 0;
    int m = temp.size();
    while (i < n && j < m) {
      if (l[i] == 1) {
        i++;
      } else {
        arr[i] = temp[j];
        j++;
        i++;
      }
    }
    for (int i = 0; i < n; i++) {
      cout << arr[i] << " ";
    }
    cout << endl;
  }
  return 0;
}
