#include <bits/stdc++.h>
using namespace std;
const int N = 10000 + 5;
int op[N], n, l[N];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", op + i);
    for (int i = 1; i <= n; i++) scanf("%d", l + i);
    vector<int> vec;
    for (int i = 1; i <= n; i++) {
      if (!l[i]) vec.push_back(op[i]);
    }
    sort(vec.begin(), vec.end());
    int at = 0;
    for (int i = 1; i <= n; i++) {
      if (!l[i]) op[i] = vec[at++];
    }
    long long sum = 0, ps1 = 0;
    for (int i = 1; i <= n; i++) {
      sum += op[i];
      if (sum < 0) ps1 = i;
    }
    sort(vec.begin(), vec.end(), greater<int>());
    at = 0;
    for (int i = 1; i <= n; i++) {
      if (!l[i]) op[i] = vec[at++];
    }
    sum = 0;
    int ps2 = 0;
    for (int i = 1; i <= n; i++) {
      sum += op[i];
      if (sum < 0) ps2 = i;
    }
    if (ps1 < ps2) {
      sort(vec.begin(), vec.end());
    } else {
      sort(vec.begin(), vec.end(), greater<int>());
    }
    at = 0;
    for (int i = 1; i <= n; i++) {
      if (!l[i]) op[i] = vec[at++];
    }
    for (int i = 1; i <= n; i++) printf("%d ", op[i]);
    printf("\n");
  }
  return 0;
}
