#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int a;
    cin >> a;
    int arr1[a];
    int arr2[a];
    for (int i = 0; i < a; i++) cin >> arr1[i];
    for (int i = 0; i < a; i++) cin >> arr2[i];
    int r = 0;
    for (int i = 0; i < a; i++) {
      for (int j = i + 1; j < a; j++) {
        if (arr2[i] == 0) {
          if (arr1[i] < arr1[j] && arr2[j] == 0) {
            r = arr1[i];
            arr1[i] = arr1[j];
            arr1[j] = r;
          }
        }
      }
    }
    for (int i = 0; i < a; i++) cout << arr1[i] << " ";
    cout << endl;
  }
}
