#include <bits/stdc++.h>
using namespace std;
bool sortbysec(const pair<string, long long int> &a,
               const pair<string, long long int> &b) {
  return (a.second < b.second);
}
bool isPalindrome(string sp) {
  long long int n = sp.size();
  for (long long int k = 0; k < n; k++) {
    long long int i = n - k - 1;
    if (sp[k] != sp[i]) return false;
  }
  return true;
}
void solve() {
  long long int n;
  cin >> n;
  std::vector<long long int> v(n), v1(n), c;
  for (long long int i = 0; i < n; i++) {
    cin >> v[i];
  }
  for (long long int i = 0; i < n; i++) {
    cin >> v1[i];
    if (v1[i] == 0) {
      c.push_back(v[i]);
    }
  }
  sort(c.begin(), c.end());
  long long int j = 0;
  long long int k = c.size();
  for (long long int i = 0; i < n; i++) {
    if (v1[i] == 1)
      cout << v[i] << " ";
    else {
      cout << c[k - 1 - j] << " ";
      j++;
    }
  }
  cout << endl;
}
int main() {
  long long int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
