#include <bits/stdc++.h>
using namespace std;
void Fast() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
void File() {}
using ll = long long;
using ull = unsigned long long;
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
int dx[] = {0, 0, -1, 1, 1, 1, -1, -1};
int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
int main() {
  Fast(), File();
  int t$$t$;
  cin >> t$$t$;
  while (t$$t$--) {
    int n;
    cin >> n;
    vector<int> v(n), re;
    vector<bool> vis(n);
    bool can;
    for (int i = 0; i < n; i++) {
      cin >> v[i];
    }
    for (int i = 0; i < n; i++) {
      cin >> can;
      vis[i] = can;
      if (!can) {
        re.push_back(v[i]), v[i] = 0;
      }
    }
    sort(re.rbegin(), re.rend());
    for (int i = 0, idx = 0; i < n; i++) {
      if (idx < re.size() && !vis[i]) v[i] = re[idx], idx++;
    }
    for (auto a : v) cout << a << " ";
    cout << "\n";
  }
}
