#include <bits/stdc++.h>
using namespace std;
int a[105], l[105];
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    for (int i = 1; i <= n; ++i) cin >> l[i];
    deque<int> v;
    for (int i = 1; i <= n; ++i) {
      if (l[i] == 1) continue;
      v.push_back(a[i]);
    }
    sort(v.begin(), v.end());
    for (int i = 1; i <= n; ++i) {
      if (l[i] == 1) continue;
      a[i] = v[v.size() - 1];
      v.pop_back();
    }
    for (int i = 1; i <= n; ++i) cout << a[i] << ' ';
    cout << endl;
  }
  return 0;
}
