#include <bits/stdc++.h>
using namespace std;
long long arr[200007], brr[200007], crr[200007];
char att[1000], m;
string g, h, v, x;
int main() {
  long long a, b, c = 0, d = 0, e = 0, f = 0, n = 0, i, j, k, l = -1, t,
                  ans = 0, sum = 0, m, x, y, w, r;
  cin >> t;
  while (t--) {
    vector<long long> v1;
    d = 0, e = 0, f = 0, l = 0, n = 0;
    cin >> a;
    for (i = 0; i < a; i++) cin >> arr[i];
    for (i = 0; i < a; i++) cin >> brr[i];
    for (i = 0; i < a; i++)
      if (brr[i] == 0) v1.push_back(arr[i]);
    sort(v1.begin(), v1.end());
    reverse(v1.begin(), v1.end());
    while (1) {
      if (brr[d] == 0) {
        arr[d] = v1[e];
        e++;
      }
      d++;
      if (d == a) break;
    }
    for (i = 0; i < a; i++) {
      if (i != 0) cout << " ";
      cout << arr[i];
    }
    cout << endl;
  }
  return 0;
}
