#include <bits/stdc++.h>
using namespace std;
int mod = 1000000007;
long long int gcd(long long int a, long long int b) {
  return b ? gcd(b, a % b) : a;
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
long long int bpow(long long int a, long long int b) {
  long long int res = 1;
  while (b > 0) {
    if (b & 1) res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}
long long int fact(long long int n) {
  if ((n == 0) || (n == 1))
    return 1;
  else
    return (n * fact(n - 1)) % mod;
}
void go() {}
void solve(vector<long long int> &ans, vector<long long int> lock,
           vector<long long int> pos, vector<bool> vis, int pr_sum) {}
int main() {
  go();
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  std::cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<long long int> v(n), neg, pos, ans(n), lock(n);
    for (int i = 0; i < n; i++) {
      cin >> v[i];
    }
    for (int i = 0; i < n; i++) cin >> lock[i];
    for (int i = 0; i < n; i++) {
      if (lock[i] == 0) {
        pos.push_back(v[i]);
      } else
        ans[i] = v[i];
    }
    sort(pos.rbegin(), pos.rend());
    int j = 0;
    for (int i = 0; i < n; i++) {
      if (lock[i] == 0) {
        ans[i] = pos[j];
        j++;
      }
    }
    for (auto i : ans) cout << i << " ";
    cout << "\n";
  }
}
