#include <bits/stdc++.h>
using namespace std;
long long b[1000006], l[1000006];
int main() {
  int a;
  scanf("%d", &a);
  vector<long long> v;
  while (a--) {
    v.clear();
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%lld", &b[i]);
    }
    for (int i = 1; i <= n; i++) {
      scanf("%lld", &l[i]);
      if (l[i] == 0) v.push_back(b[i]);
    }
    sort(v.begin(), v.end());
    int j = v.size() - 1;
    for (int i = 1; i <= n; ++i) {
      if (l[i] == 0) {
        printf("%lld ", v[j]);
        j--;
      } else
        printf("%lld ", b[i]);
    }
    printf("\n");
  }
  return 0;
}
