#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long a[n], lo[n];
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
    }
    vector<long long> v;
    for (long long i = 0; i < n; i++) {
      cin >> lo[i];
      if (lo[i] == 0) {
        v.push_back(a[i]);
      }
    }
    long long x = 0, j = 0;
    sort(v.begin(), v.end(), greater<long long>());
    deque<long long> dq;
    for (long long i = 0; i < n; i++) {
      if (lo[i] == 1) {
        x += a[i];
      } else {
        if (v[j] >= 0) {
          x += v[j];
          dq.push_back(v[j]);
        } else {
          if (x >= abs(v[j])) {
            x += v[j];
            dq.push_back(v[j]);
          } else {
            x += v[j];
            dq.push_front(v[j]);
          }
        }
        j++;
      }
    }
    vector<long long> p;
    deque<long long>::iterator it = dq.begin();
    for (long long i = 0; i < n; i++) {
      if (lo[i] == 1) {
        p.push_back(a[i]);
      } else {
        p.push_back(*it);
        it++;
      }
    }
    for (long long i = 0; i < n; i++) {
      cout << p[i] << " ";
    }
    cout << endl;
  }
  return 0;
}
