#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  int t, n;
  cin >> t;
  while (t--) {
    cin >> n;
    int a[n], l[n], p[n];
    vector<int> sorted;
    map<int, int> sortedPosToRealPos;
    int assgn = 0;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      p[i] = a[i];
    }
    for (int i = 0; i < n; i++) {
      cin >> l[i];
      if (l[i] == 0) {
        sorted.push_back(a[i]);
        sortedPosToRealPos[assgn] = i;
        assgn++;
      }
    }
    sort(sorted.begin(), sorted.end());
    reverse(sorted.begin(), sorted.end());
    for (int i = 0; i < sorted.size(); i++) {
      a[sortedPosToRealPos[i]] = sorted[i];
      p[sortedPosToRealPos[i]] = sorted[i];
    }
    int k = 0;
    for (int i = 1; i < n; i++) p[i] += p[i - 1];
    for (int i = 0; i < n; i++) {
      if (p[i] < 0) k = i;
      cout << a[i] << " ";
    }
    cout << "\n";
  }
  return 0;
}
