#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 103;int a[N], l[N], b[N];
//将能动的从大到小开始放即可
int main()
{int t;cin >> t;while (t--){int n;cin >> n;for (int i = 1; i <= n; i++)cin >> a[i];for (int j = 1; j <= n; j++)cin >> l[j];int k = 0;for (int i = 1; i <= n; i++)if (l[i] == 0)b[k++] = a[i];sort(b, b + k);k = 0;for (int i = n; i>0; i--)if (l[i] == 0)a[i] = b[k++];for (int i = 1; i <= n; i++)cout << a[i] << ' ';cout << endl; }return 0;
}