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