#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
long long mod_chef = 998244353;
long long row[] = {1, -1, 0, 0};
long long col[] = {0, 0, 1, -1};
long long dx[] = {-2, -1, 1, 2, -2, -1, 1, 2};
long long dy[] = {-1, -2, -2, -1, 1, 2, 2, 1};
long long rr[] = {1, -1, 0, 0, -1, 1, 1, -1};
long long cc[] = {0, 0, 1, -1, -1, 1, -1, 1};
long long power(long long a, long long b, long long m = mod) {
  long long ans = 1;
  a = a % m;
  while (b > 0) {
    if (b & 1) ans = (1ll * a * ans) % m;
    b >>= 1;
    a = (1ll * a * a) % m;
  }
  return ans;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return (a / gcd(a, b) * b); }
void solve() {
  long long n, y, k;
  cin >> n;
  long long arr[n];
  bool loc[n];
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
  }
  for (int i = 0; i < n; ++i) {
    cin >> loc[i];
  }
  vector<long long> store;
  for (int i = 0; i < n; ++i) {
    if (!loc[i]) store.push_back(arr[i]);
  }
  sort(store.begin(), store.end());
  int j = 0;
  for (int i = n - 1; i >= 0; --i) {
    if (loc[i]) continue;
    arr[i] = store[j];
    ++j;
  }
  for (int i = 0; i < n; ++i) {
    cout << arr[i] << " ";
  }
  cout << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
