#include <bits/stdc++.h>
#pragma GCC optimize("O3")
long long fact(int i) {
  if (i <= 1)
    return 1;
  else
    return i * fact(i - 1);
}
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<pair<int, int>> arr;
    int arr1[n];
    int arr2[n];
    for (int i = 0; i < n; i++) {
      cin >> arr1[i];
    }
    for (int i = 0; i < n; i++) {
      cin >> arr2[i];
    }
    for (int i = 0; i < n; i++) {
      arr.push_back(make_pair(arr1[i], arr2[i]));
    }
    vector<int> v;
    for (int i = 0; i < n; i++) {
      if (arr[i].second == 0) {
        v.push_back(arr[i].first);
      }
    }
    sort(v.begin(), v.end(), greater<int>());
    int c = 0;
    for (int i = 0; i < n; i++) {
      if (arr[i].second == 0) {
        arr[i].first = v[c];
        c++;
      }
    }
    for (int i = 0; i < n; i++) {
      cout << arr[i].first << " ";
    }
    cout << endl;
  }
}
