#include<bits/stdc++.h>
using namespace std;
struct c{
int x;
double h;
}cl[10010];
bool mar(c s,c r){
if(s.h>r.h){
return s.h<r.h;
}
else{
return s.h>r.h;
}
}
int main(){
int T;
cin>>T;
while(T--){
int stu;
cin>>stu;
for(int i=0;i<stu;i++){
cin>>cl[i].x;
}
for(int i=0;i<stu;i++){
cin>>cl[i].h;
}
sort(cl,cl+stu,mar);
for(int i=0;i<stu;i++){
if(cl[i].x==0){
cout<<cl[i].h<<" ";
}
}
cout<<endl;
for(int i=0;i<stu;i++){
if(cl[i].x==1){
cout<<cl[i].h<<" ";
}
}
cout<<endl;
}
return 0;
}