#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const int INF = 1e9 + 5;
const int NMAX = 1e7 + 5;
int x[4] = {-1, 0, 0, 1};
int y[4] = {0, -1, 1, 0};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int tc;
  cin >> tc;
  while (tc--) {
    int n;
    cin >> n;
    int a[n + 1], l[n + 1], b[n + 1], m = 0;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) {
      cin >> l[i];
      if (!l[i]) b[++m] = a[i];
    }
    sort(b + 1, b + m + 1, greater<int>());
    m = 0;
    for (int i = 1; i <= n; i++)
      if (!l[i]) a[i] = b[++m];
    int pml = 0, re = 0;
    for (int i = 1; i <= n; i++) {
      pml += a[i];
      cout << a[i] << ' ';
    }
    cout << '\n';
  }
  return 0;
}
