#include <bits/stdc++.h>
using namespace std;
void no() { cout << "NO" << endl; }
void yes() { cout << "YES" << endl; }
long long int l = 0;
vector<vector<long long int> > dp(21, vector<long long int>(21, -1));
long long int cal(long long int a, long long int b) {
  if (dp[a][b] != -1) return dp[a][b];
  if (a == b) return 1;
  if (b == 0) return 1;
  if (b == 1) return a;
  return dp[a][b] = cal(a - 1, b) + cal(a - 1, b - 1);
}
class comp {
 public:
  bool operator()(pair<long long int, long long int> a,
                  pair<long long int, long long int> b) {
    return 1;
  }
};
map<long long int, vector<long long int> > m;
set<long long int> ans;
void dfs(vector<long long int>& v, long long int i) {
  v[i] = 1;
  for (auto it : m[i]) {
    if (v[it] == 1) {
      ans.emplace(it);
      continue;
    }
    dfs(v, it);
  }
  v[i] = 0;
}
void solve() {
  long long int n;
  cin >> n;
  vector<long long int> a(n), b(n), c;
  for (long long int i = 0; i < n; i++) cin >> a[i];
  for (long long int i = 0; i < n; i++) cin >> b[i];
  for (long long int i = 0; i < n; i++)
    if (!b[i]) c.push_back(a[i]);
  sort(c.rbegin(), c.rend());
  long long int j = 0;
  for (long long int i = 0; i < n; i++) {
    if (b[i])
      cout << a[i] << ' ';
    else
      cout << c[j++] << ' ';
  }
  cout << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t = 1;
  cin >> t;
  while (t--) solve();
  return 0;
}
