#include <bits/stdc++.h>
using namespace std;
int a[100005], b[100005], n;
vector<int> c;
int main() {
  int T;
  cin >> T;
  while (T--) {
    scanf("%d", &n);
    c.clear();
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++) {
      scanf("%d", &b[i]);
      if (!b[i]) c.push_back(a[i]);
    }
    sort(c.begin(), c.end());
    int j = (int)c.size();
    for (int i = 1; i <= n; i++) {
      printf("%d%c", b[i] ? a[i] : c[--j], i == n ? '\n' : ' ');
    }
  }
}
