import java.io.*;
import java.util.*;
public  class Main
{
     public static void main(String args[])
     {
		 Scanner sc=new Scanner(System.in);
          String str1=sc.next();
		  String str2=sc.next();
		  HashMap<String,Integer> hm=new HashMap<>();
		  for(int i=0;i<str1.length();i++)
		  {
			  hm.put(str1.substring(i,str1.length()),1);
		  }long ans=0;
		  for(int i=0;i<str2.length();i++)
		  {
			  if(hm.containsKey(str2.substring(i,str2.length())))
			  {
				  ans=str2.length()-(str2.substring(i)).length();
				  ans+=str1.length()-(str2.substring(i)).length();
				  break;
			  }
		  }
		  System.out.println(ans);
     }
}


#include <bits/stdc++.h>
using namespace std;

signed main() {
	ios_base::sync_with_stdio(0);
	int t;
	cin >> t;
	while(t--) {
		int n;
		cin >> n;
		int a[n];
		bool u[n];
		for (int i = 0;i < n;i++) {
			cin >> a[i];
		}
		for (int i = 0;i < n;i++) {
			cin >> u[i];
		}
		int p[n];
		vector<int> s;
		for (int i = 0;i < n;i++) {
			if (!u[i]) {
				s.push_back(a[i]);
			}
		}
		sort(s.begin(), s.end());
		int c = 0;
		for (int i = 0;i < n;i++) {
			if (!u[i]) {
				a[i] = s[c++];
			}
			cout << a[i] << ' ';
		}
		cout << '\n';
		/*
		p[0] = (u[0] ? a[0] : 0);
		for (int i = 1;i < n;i++) {
			p[i] = p[i - 1] + a[i];
		}
		int ans = 0;
		for (int i = 0; i < n;i++) {
			if (p[i] < 0) {
				ans = i;
			}
		}
		cout << ans << '\n';
		*/
	}
#ifdef LOCAL
	cout << endl;
	system("pause");
#endif
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

signed main() {
	ios_base::sync_with_stdio(0);
	int t;
	cin >> t;
	while(t--) {
		int n;
		cin >> n;
		int a[n];
		bool u[n];
		for (int i = 0;i < n;i++) {
			cin >> a[i];
		}
		for (int i = 0;i < n;i++) {
			cin >> u[i];
		}
		vector<int> s;
		for (int i = 0;i < n;i++) {
			if (!u[i]) {
				s.push_back(a[i]);
			}
		}
		sort(s.begin(), s.end());
		int p = 0;
		bool us[s.size()];
		for (int i = 0;i < n;i++) {
			if (u[i]) {
				p += a[i];
				cout << a[i] << ' ';
			}
			else {
				int k;
				for (int j = 0;j < s.size();j++) {
					if (!us[j]) {
						k = j;
						break;
					}
				}
				for (int j = 0;j < s.size();j++) {
					if (abs(s[j] + p) <= abs(s[k] + p) && !us[j]) {
						k = j;
					}
				}
				cout << s[k] << ' ';
				us[k] = 1;
			}
		}
		cout << '\n';
	}
#ifdef LOCAL
	cout << endl;
	system("pause");
#endif
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

signed main() {
	ios_base::sync_with_stdio(0);
	int t;
	cin >> t;
	while(t--) {
		int n;
		cin >> n;
		int a[n];
		bool u[n];
		for (int i = 0;i < n;i++) {
			cin >> a[i];
		}
		for (int i = 0;i < n;i++) {
			cin >> u[i];
		}
		vector<int> s;
		for (int i = 0;i < n;i++) {
			if (!u[i]) {
				s.push_back(a[i]);
			}
		}
		sort(s.begin(), s.end(), [](int a, int b){return a > b;});
		int c = 0;
		for (int i = 0;i < n;i++) {
			if (u[i]) {
				cout << a[i] << ' ';
			}
			else {
				cout << s[c+-] << ' ';
			}
		}
		cout << '\n';
	}
#ifdef LOCAL
	cout << endl;
	system("pause");
#endif
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

signed main() {
	ios_base::sync_with_stdio(0);
	int t;
	cin >> t;
	while(t--) {
		int n;
		cin >> n;
		int a[n];
		bool u[n];
		for (int i = 0;i < n;i++) {
			cin >> a[i];
		}
		for (int i = 0;i < n;i++) {
			cin >> u[i];
		}
		vector<int> s;
		for (int i = 0;i < n;i++) {
			if (!u[i]) {
				s.push_back(a[i]);
			}
		}
		sort(s.begin(), s.end());
		int p = 0;
		bool us[s.size()];
		for (int i = 0;i < s.size();i++) {
			us[i] = 0;
		}
		for (int i = 0;i < n;i++) {
			if (u[i]) {
				p += a[i];
				cout << a[i] << ' ';
			}
			else {
				int k;
				for (int j = 0;j < s.size();j++) {
					if (us[j] == 0) {
						k = j;
						break;
					}
				}
				for (int j = 0;j < s.size();j++) {
					if (abs(s[j] + p) <= abs(s[k] + p) && !us[j]) {
						k = j;
					}
				}
				cout << s[k] << ' ';
				us[k] = 1;
			}
		}
		cout << '\n';
	}
#ifdef LOCAL
	cout << endl;
	system("pause");
#endif
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

signed main() {
	ios_base::sync_with_stdio(0);
	int t;
	cin >> t;
	while(t--) {
		int n;
		cin >> n;
		int a[n];
		bool u[n];
		for (int i = 0;i < n;i++) {
			cin >> a[i];
		}
		for (int i = 0;i < n;i++) {
			cin >> u[i];
		}
		vector<int> s;
		for (int i = 0;i < n;i++) {
			if (!u[i]) {
				s.push_back(a[i]);
			}
		}
		sort(s.begin(), s.end());
		int p = 0;
		bool us[s.size()];
		for (int i = 0;i < n;i++) {
			if (u[i]) {
				p += a[i];
				cout << a[i] << ' ';
			}
			else {
				int k;
				for (int j = 0;j < s.size();j++) {
					if (us[j] == 0) {
						k = j;
						break;
					}
				}
				for (int j = 0;j < s.size();j++) {
					if (abs(s[j] + p) <= abs(s[k] + p) && !us[j]) {
						k = j;
					}
				}
				cout << s[k] << ' ';
				us[k] = 1;
			}
		}
		cout << '\n';
	}
#ifdef LOCAL
	cout << endl;
	system("pause");
#endif
	return 0;
}


/*
    Stay motivated and keep working hard
*/
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define fr first
#define sc second
#define sz(v) ((int)(v).size())
#define mp make_pair
#define all(a) a.begin(),a.end()
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repd(i,a,p) for(int i=a-1;i>=p;i--)
#define M 1000000007
#define vi vector<int>
#define vp vector<pair<int,int> >
#define ml map<int,int>
#define MPI 3.1415926536
#define in insert
using namespace std;
//priority_queue is best for traversing elements when it is needed in a sorted order
//dsu is pel concept
int n,m,k=0,t,i,j,x,s=0;string s1;
const int N=500000;
bool cmp(pair<pair<int,int>,int>a , pair<pair<int,int>,int>b)
{
    return a.fr.fr>b.fr.fr;
}
signed main()
{
cin>>t;
while(t--)
{
  cin>>n;
  vi v(n),v2(n),v3;
  for(i=0;i<n;i++)
  cin>>v[i];
  for(i=0;i<n;i++)
  {
      cin>>v2[i];
      if(v2[i]==0)
      v3.pb(v[i]);
  }
  s=0;
  if(v2[0]==1)
  {
      int j=0;
      sort(all(v3),greater<int>());
      for(i=0;i<n;i++)
      {
          if(v2[i]==1)
          continue;
          v[i]=v3[j];
          j++;
      }
      for(auto it:v)cout<<it<<" ";
  }
  else
  {
     sort(all(v3));
     for(i=0;i<n;i++)
     {
         if(v3[i]<0)
         continue;
         else
         break;
     }
     if(i!=0)
     {
         v[0]=v3[i-1];
         v3.erase(v3.begin()+i-1,v3.begin()+i);
         sort(all(v3),greater<int>());
         j=0;
         for(i=1;i<n;i++)
         {
             if(v2[i]==1)
             continue;
             v[i]=v3[j];
             j++;
         }
         for(auto it:v)
         cout<<it<<" ";
     }
     else
     {
         for(auto it:v)cout<<it<<" ";
     }}
     cout<<"\n";
}
}


/*
    Stay motivated and keep working hard
*/
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define fr first
#define sc second
#define sz(v) ((int)(v).size())
#define mp make_pair
#define all(a) a.begin(),a.end()
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repd(i,a,p) for(int i=a-1;i>=p;i--)
#define M 1000000007
#define vi vector<int>
#define vp vector<pair<int,int> >
#define ml map<int,int>
#define MPI 3.1415926536
#define in insert
using namespace std;
//priority_queue is best for traversing elements when it is needed in a sorted order
//dsu is pel concept
int n,m,k=0,t,i,j,x,s=0;string s1;
const int N=500000;
bool cmp(pair<pair<int,int>,int>a , pair<pair<int,int>,int>b)
{
    return a.fr.fr>b.fr.fr;
}
signed main()
{
cin>>t;
while(t--)
{
  cin>>n;
  vi v(n),v2(n),v3;
  for(i=0;i<n;i++)
  cin>>v[i];
  for(i=0;i<n;i++)
  {
      cin>>v2[i];
      if(v2[i]==0)
      v3.pb(v[i]);
  }
  s=0;
      int j=0;
      sort(all(v3),greater<int>());
      for(i=0;i<n;i++)
      {
          if(v2[i]==1)
          continue;
          v[i]=v3[j];
          j++;
      }
      for(auto it:v)cout<<it<<" ";
     cout<<"\n";
}
}


/*
    Stay motivated and keep working hard
*/
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define fr first
#define sc second
#define sz(v) ((int)(v).size())
#define mp make_pair
#define all(a) a.begin(),a.end()
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repd(i,a,p) for(int i=a-1;i>=p;i--)
#define M 1000000007
#define vi vector<int>
#define vp vector<pair<int,int> >
#define ml map<int,int>
#define MPI 3.1415926536
#define in insert
using namespace std;
//priority_queue is best for traversing elements when it is needed in a sorted order
//dsu is pel concept
int n,m,k=0,t,i,j,x,s=0;string s1;
const int N=500000;
bool cmp(pair<pair<int,int>,int>a , pair<pair<int,int>,int>b)
{
    return a.fr.fr>b.fr.fr;
}
signed main()
{
cin>>t;
while(t--)
{
  cin>>n;
  vi v(n),v2(n),v3;
  for(i=0;i<n;i++)
  cin>>v[i];
  for(i=0;i<n;i++)
  {
      cin>>v2[i];
      if(v2[i]==0)
      v3.pb(v[i]);
  }
  s=0;
  if(v2[0]==1)
  {
      int j=0;
      sort(all(v3),greater<int>());
      for(i=0;i<n;i++)
      {
          if(v2[i]==1)
          continue;
          v[i]=v3[j];
          j++;
      }
      for(auto it:v)cout<<it<<" ";
  }
  else
  {
     sort(all(v3));
     for(i=0;i<n;i++)
     {
         if(v2[i]==1)
         continue;
         v[i]=v3[j];
         j++;
     }
     for(auto it:v)cout<<it<<" ";
     }
     cout<<"\n";
}
}


/*
    Stay motivated and keep working hard
*/
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define fr first
#define sc second
#define sz(v) ((int)(v).size())
#define mp make_pair
#define all(a) a.begin(),a.end()
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repd(i,a,p) for(int i=a-1;i>=p;i--)
#define M 1000000007
#define vi vector<int>
#define vp vector<pair<int,int> >
#define ml map<int,int>
#define MPI 3.1415926536
#define in insert
using namespace std;
//priority_queue is best for traversing elements when it is needed in a sorted order
//dsu is pel concept
int n,m,k=0,t,i,j,x,s=0;string s1;
const int N=500000;
bool cmp(pair<pair<int,int>,int>a , pair<pair<int,int>,int>b)
{
    return a.fr.fr>b.fr.fr;
}
signed main()
{
cin>>t;
while(t--)
{
  cin>>n;
  vi v(n),v2(n),v3;
  for(i=0;i<n;i++)
  cin>>v[i];
  for(i=0;i<n;i++)
  {
      cin>>v2[i];
      if(v2[i]==0)
      v3.pb(v[i]);
  }
  s=0;
  if(v2[0]==1)
  {
      int j=0;
      sort(all(v3),greater<int>());
      for(i=0;i<n;i++)
      {
          if(v2[i]==1)
          continue;
          v[i]=v3[j];
          j++;
      }
      for(auto it:v)cout<<it<<" ";
  }
  else
  {
     sort(all(v3));
     j=0;
     v[0]=v3[0];
     v3.erase(v3.begin(),v3.begin()+1);
     sort(all(v3),greater<int>());
     for(i=1;i<n;i++)
     {
         if(v2[i]==1)
         continue;
         v[i]=v3[j];
         j++;
     }
     for(auto it:v)cout<<it<<" ";
     }
     cout<<"\n";
}
}


/*
    Stay motivated and keep working hard
*/
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define fr first
#define sc second
#define sz(v) ((int)(v).size())
#define mp make_pair
#define all(a) a.begin(),a.end()
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repd(i,a,p) for(int i=a-1;i>=p;i--)
#define M 1000000007
#define vi vector<int>
#define vp vector<pair<int,int> >
#define ml map<int,int>
#define MPI 3.1415926536
#define in insert
using namespace std;
//priority_queue is best for traversing elements when it is needed in a sorted order
//dsu is pel concept
int n,m,k=0,t,i,j,x,s=0;string s1;
const int N=500000;
bool cmp(pair<pair<int,int>,int>a , pair<pair<int,int>,int>b)
{
    return a.fr.fr>b.fr.fr;
}
signed main()
{
cin>>t;
while(t--)
{
  cin>>n;
  vi v(n),v2(n),v3;
  for(i=0;i<n;i++)
  cin>>v[i];
  for(i=0;i<n;i++)
  {
      cin>>v2[i];
      if(v2[i]==0)
      v3.pb(v[i]);
  }
  s=0;
  if(v2[0]==1)
  {
      int j=0;
      sort(all(v3),greater<int>());
      for(i=0;i<n;i++)
      {
          if(v2[i]==1)
          continue;
          v[i]=v3[j];
          j++;
      }
      for(auto it:v)cout<<it<<" ";
  }
  else
  {
     sort(all(v3));
     j=0;
     v[0]=v3[0];
     v3.erase(v3.begin(),v3.begin()+1);
     sort(all(v3),greater<int>());
     for(i=0;i<n;i++)
     {
         if(v2[i]==1)
         continue;
         v[i]=v3[j];
         j++;
     }
     for(auto it:v)cout<<it<<" ";
     }
     cout<<"\n";
}
}


/*
    Stay motivated and keep working hard
*/
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define fr first
#define sc second
#define sz(v) ((int)(v).size())
#define mp make_pair
#define all(a) a.begin(),a.end()
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repd(i,a,p) for(int i=a-1;i>=p;i--)
#define M 1000000007
#define vi vector<int>
#define vp vector<pair<int,int> >
#define ml map<int,int>
#define MPI 3.1415926536
#define in insert
using namespace std;
//priority_queue is best for traversing elements when it is needed in a sorted order
//dsu is pel concept
int n,m,k=0,t,i,j,x,s=0;string s1;
const int N=500000;
bool cmp(pair<pair<int,int>,int>a , pair<pair<int,int>,int>b)
{
    return a.fr.fr>b.fr.fr;
}
signed main()
{
cin>>t;
while(t--)
{
  cin>>n;
  vi v(n),v2(n),v3;
  for(i=0;i<n;i++)
  cin>>v[i];
  for(i=0;i<n;i++)
  {
      cin>>v2[i];
      if(v2[i]==0)
      v3.pb(v[i]);
  }
  s=0;
  if(v2[0]==1)
  {
      int j=0;
      sort(all(v3),greater<int>());
      for(i=0;i<n;i++)
      {
          if(v2[i]==1)
          continue;
          v[i]=v3[j];
          j++;
      }
      for(auto it:v)cout<<it<<" ";
  }
  else
  {
     sort(all(v3));
     j=0;
     for(i=0;i<n;i++)
     {
         if(v2[i]==1)
         continue;
         v[i]=v3[j];
         j++;
     }
     for(auto it:v)cout<<it<<" ";
     }
     cout<<"\n";
}
}


/*
    Stay motivated and keep working hard
*/
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define fr first
#define sc second
#define sz(v) ((int)(v).size())
#define mp make_pair
#define all(a) a.begin(),a.end()
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repd(i,a,p) for(int i=a-1;i>=p;i--)
#define M 1000000007
#define vi vector<int>
#define vp vector<pair<int,int> >
#define ml map<int,int>
#define MPI 3.1415926536
#define in insert
using namespace std;
//priority_queue is best for traversing elements when it is needed in a sorted order
//dsu is pel concept
int n,m,k=0,t,i,j,x,s=0;string s1;
const int N=500000;
bool cmp(pair<pair<int,int>,int>a , pair<pair<int,int>,int>b)
{
    return a.fr.fr>b.fr.fr;
}
signed main()
{
cin>>t;
while(t--)
{
  cin>>n;
  vi v(n),v2(n),v3;
  for(i=0;i<n;i++)
  cin>>v[i];
  for(i=0;i<n;i++)
  {
      cin>>v2[i];
      if(v2[i]==0)
      v3.pb(v[i]);
  }
  s=0;
  if(v2[0]==1)
  {
      int j=0;
      sort(all(v3),greater<int>());
      for(i=0;i<n;i++)
      {
          if(v2[i]==1)
          continue;
          v[i]=v3[j];
          j++;
      }
      for(auto it:v)cout<<it<<" ";
  }
  else
  {
     sort(all(v3));
     for(i=0;i<v3.size();i++)
     {
         if(v3[i]<0)
         continue;
         else
         break;
     }
     if(i!=0)
     {
         v[0]=v3[i-1];
         v3.erase(v3.begin()+i-1,v3.begin()+i);
         sort(all(v3),greater<int>());
         j=0;
         for(i=1;i<n;i++)
         {
             if(v2[i]==1)
             continue;
             v[i]=v3[j];
             j++;
         }
         for(auto it:v)
         cout<<it<<" ";
     }
     else
     {
         for(auto it:v)cout<<it<<" ";
     }}
     cout<<"\n";
}
}


#include <bits/stdc++.h>

using namespace std;
//#pragma GCC optimize(2)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ull unsigned long long
#define ll long long
#define rep(i, x, y) for(int i=x;i<=y;i++)
#define mms(x, n) memset(x, n, sizeof(x))
#define mmc(A, tree) memcpy(A, tree, sizeof(tree))
#define eps (1e-8)
#define PI (acos(-1.0))
#define INF (0x3f3f3f3f)
#define mod (ull)(1e9+7)
typedef pair<int, int> P;
const int N = 200;
ll a[N];
int lo[N];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    int T;
    scanf("%d", &T);
    while (T--) {

        set<int> st;
        int n;
        scanf("%d", &n);
        ll sum = 0;
        ll allpos = true;
        rep(i, 1, n) {
            scanf("%lld", &a[i]);
            sum += a[i];
            if (a[i] < 0) allpos = false;
        }

        rep(i, 1, n) {
            scanf("%d", &lo[i]);
            if (lo[i] == 1) st.insert(i);
        }

        if (allpos || sum < 0 || st.size() == n) {
            rep(i, 1, n) printf("%lld ", a[i]);
        } else {
            int vis[N];
            mms(vis, 0);
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n - 1 - i; j++) {
                    if (st.find(j) != st.end()) continue;
                    int k = 1;
                    while (j + k <= n && st.find(j + k) != st.end()) {
                        k += 1;
                    }
                    if (j + k > n) continue;
                    if (a[j] < a[j + k]) swap(a[j], a[j + k]);
                }
            }
            for (int i = 1; i <= n; i++) {
                if (st.find(i) != st.end()) {
                    printf("%lld ", a[i]);
                    vis[i] = 1;
                    continue;
                }
                for (int j = 1; j <= n; j++) {
                    if (!vis[j] && st.find(j) == st.end()) {
                        printf("%lld ", a[j]);
                        vis[j] = 1;
                        break;
                    }
                }
            }
        }
        printf("\n");
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
//#pragma GCC optimize(2)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ull unsigned long long
#define ll long long
#define rep(i, x, y) for(int i=x;i<=y;i++)
#define mms(x, n) memset(x, n, sizeof(x))
#define mmc(A, tree) memcpy(A, tree, sizeof(tree))
#define eps (1e-8)
#define PI (acos(-1.0))
#define INF (0x3f3f3f3f)
#define mod (ull)(1e9+7)
typedef pair<int, int> P;
const int N = 200;
ll a[N];
int lo[N];
int vis[N];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    int T;
    scanf("%d", &T);
    while (T--) {
        set<int> lc;
        mms(lo, 0);
        mms(a, 0);

        int n;
        scanf("%d", &n);
        int sum = 0;
        ll tmp = 0;
        int neg = 0, pos = 0;
        rep(i, 1, n) {
            scanf("%lld", &a[i]);
            tmp += a[i];
        }
        rep(i, 1, n) {
            scanf("%d", &lo[i]);
//            if (lo[i] == 1 && a[i] < 0) neg = i;
            sum += lo[i];
            if (lo[i] == 1) lc.insert(i);
            else {
                if (a[i] < 0) neg += 1;
                else pos += 1;
            }
        }
        if (sum == n || neg == 0 || tmp < 0) {
            rep(i, 1, n) {
                printf("%lld ", a[i]);
            }
        } else {
            mms(vis, 0);
            for (int i = 1; i <= n; i++) {
                if (lc.find(i) != lc.end()) {
                    vis[i] = 1;
                    printf("%lld ", a[i]);
//                    if (a[i] >= 0) pos -= 1;
                    continue;
                }
                for (int j = 1; j <= n; j++) {
                    if (vis[j] || lo[j]) continue;
                    if (pos) {
                        if (a[j] >= 0) {
                            printf("%lld ", a[j]);
                            vis[j] = 1;
                            pos -= 1;
                            break;
                        }
                    } else if (!vis[j]) {
                        printf("%lld ", a[j]);
                        vis[j] = 1;
                        break;
                    }
                }
            }
        }
        printf("\n");
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
//#pragma GCC optimize(2)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ull unsigned long long
#define ll long long
#define rep(i, x, y) for(int i=x;i<=y;i++)
#define mms(x, n) memset(x, n, sizeof(x))
#define mmc(A, tree) memcpy(A, tree, sizeof(tree))
#define eps (1e-8)
#define PI (acos(-1.0))
#define INF (0x3f3f3f3f)
#define mod (ull)(1e9+7)
typedef pair<int, int> P;
const int N = 200;
ll a[N];
int lo[N];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    int T;
    scanf("%d", &T);
    while (T--) {

        set<int> st;
        int n;
        scanf("%d", &n);
        ll sum = 0;
        ll allpos = true;
        rep(i, 1, n) {
            scanf("%lld", &a[i]);
            sum += a[i];
            if (a[i] < 0) allpos = false;
        }

        rep(i, 1, n) {
            scanf("%d", &lo[i]);
            if (lo[i] == 1) st.insert(i);
        }

//        if (allpos || sum < 0 || st.size() == n) {
//            rep(i, 1, n) printf("%lld ", a[i]);
//        } else {
            int vis[N];
            mms(vis, 0);
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n - 1 - i; j++) {
                    if (st.find(j) != st.end()) continue;
                    int k = 1;
                    while (j + k <= n && st.find(j + k) != st.end()) {
                        k += 1;
                    }
                    if (j + k > n) continue;
                    if (a[j] < a[j + k]) swap(a[j], a[j + k]);
                }
            }
            for (int i = 1; i <= n; i++) {
                if (st.find(i) != st.end()) {
                    printf("%lld ", a[i]);
                    vis[i] = 1;
                    continue;
                }
                for (int j = 1; j <= n; j++) {
                    if (!vis[j] && st.find(j) == st.end()) {
                        printf("%lld ", a[j]);
                        vis[j] = 1;
                        break;
                    }
                }
            }
//        }
        printf("\n");
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
//#pragma GCC optimize(2)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ull unsigned long long
#define ll long long
#define rep(i, x, y) for(int i=x;i<=y;i++)
#define mms(x, n) memset(x, n, sizeof(x))
#define mmc(A, tree) memcpy(A, tree, sizeof(tree))
#define eps (1e-8)
#define PI (acos(-1.0))
#define INF (0x3f3f3f3f)
#define mod (ull)(1e9+7)
typedef pair<int, int> P;

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    int T;
    scanf("%d", &T);
    while (T--) {
        int n;
        scanf("%d", &n);
        priority_queue<ll> pos, neg;
        ll a[n];
        rep(i, 0, n - 1) {
            scanf("%lld", &a[i]);
            if (a[i] >= 0) pos.push(a[i]);
            else neg.push(a[i]);
        }
        int b[n];
        rep(i, 0, n-1) scanf("%d", &b[i]);
        for (int i = 0; i < n; i++) {
            if (b[i]) printf("%lld ", a[i]);
            else if (!pos.empty()) {
                printf("%lld ", pos.top());
                pos.pop();
            } else if (!neg.empty()) {
                printf("%lld ", neg.top());
                neg.pop();
            }
        }
        printf("\n");
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
//#pragma GCC optimize(2)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ull unsigned long long
#define ll long long
#define rep(i, x, y) for(int i=x;i<=y;i++)
#define mms(x, n) memset(x, n, sizeof(x))
#define mmc(A, tree) memcpy(A, tree, sizeof(tree))
#define eps (1e-8)
#define PI (acos(-1.0))
#define INF (0x3f3f3f3f)
#define mod (ull)(1e9+7)
typedef pair<int, int> P;

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    int T;
    scanf("%d", &T);
    while (T--) {
        int n;
        scanf("%d", &n);
        priority_queue<ll> pos, neg;
        ll a[n];
        rep(i, 0, n - 1) {
            scanf("%lld", &a[i]);
        }
        int b[n];
        rep(i, 0, n - 1) {
            scanf("%d", &b[i]);
            if (b[i] == 0) {
                if (a[i] >= 0) pos.push(a[i]);
                else neg.push(a[i]);
            }
        }
        for (int i = 0; i < n; i++) {
            if (b[i]) printf("%lld ", a[i]);
            else if (!pos.empty()) {
                printf("%lld ", pos.top());
                pos.pop();
            } else if (!neg.empty()) {
                printf("%lld ", neg.top());
                neg.pop();
            }
        }
        printf("\n");
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
//#pragma GCC optimize(2)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ull unsigned long long
#define ll long long
#define rep(i, x, y) for(int i=x;i<=y;i++)
#define mms(x, n) memset(x, n, sizeof(x))
#define mmc(A, tree) memcpy(A, tree, sizeof(tree))
#define eps (1e-8)
#define PI (acos(-1.0))
#define INF (0x3f3f3f3f)
#define mod (ull)(1e9+7)
typedef pair<int, int> P;
const int N = 200;
ll a[N];
int lo[N];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    int T;
    scanf("%d", &T);
    while (T--) {

        set<int> st;
        int n;
        scanf("%d", &n);
        ll sum = 0;
        ll allpos = true;
        rep(i, 1, n) {
            scanf("%lld", &a[i]);
            sum += a[i];
            if (a[i] < 0) allpos = false;
        }

        rep(i, 1, n) {
            scanf("%d", &lo[i]);
            if (lo[i] == 1) st.insert(i);
        }

        if (allpos || sum < 0 || st.size() == n) {
            rep(i, 1, n) printf("%lld ", a[i]);
        } else {
            int vis[N];
            mms(vis, 0);
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n - 1 - i; j++) {
                    if (st.find(j) != st.end()) continue;
                    int k = 1;
                    while (j + k <= n && st.find(j + k) != st.end()) {
                        k += 1;
                    }
                    if (j + k > n) continue;
                    if (a[j] > a[j + k]) swap(a[j], a[j + k]);
                }
            }
            for (int i = 1; i <= n; i++) {
                if (st.find(i) != st.end()) {
                    printf("%lld ", a[i]);
                    vis[i] = 1;
                    continue;
                }
                for (int j = 1; j <= n; j++) {
                    if (!vis[j] && st.find(j) == st.end()) {
                        printf("%lld ", a[j]);
                        vis[j] = 1;
                        break;
                    }
                }
            }
        }
        printf("\n");
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
//#pragma GCC optimize(2)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ull unsigned long long
#define ll long long
#define rep(i, x, y) for(int i=x;i<=y;i++)
#define mms(x, n) memset(x, n, sizeof(x))
#define mmc(A, tree) memcpy(A, tree, sizeof(tree))
#define eps (1e-8)
#define PI (acos(-1.0))
#define INF (0x3f3f3f3f)
#define mod (ull)(1e9+7)
typedef pair<int, int> P;
const int N = 200;
ll a[N];
int lo[N];
int vis[N];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    int T;
    scanf("%d", &T);
    while (T--) {
        set<int> lc;
        mms(lo, 0);
        mms(a, 0);

        int n;
        scanf("%d", &n);
        int sum = 0;
        ll tmp = 0;
        int neg = 0, pos = 0;
        rep(i, 1, n) {
            scanf("%lld", &a[i]);
            tmp += a[i];
        }
        rep(i, 1, n) {
            scanf("%d", &lo[i]);
//            if (lo[i] == 1 && a[i] < 0) neg = i;
            sum += lo[i];
            if (lo[i] == 1) lc.insert(i);
            else {
                if (a[i] < 0) neg += 1;
                else pos += 1;
            }
        }
        if (sum == n || neg == 0 || tmp < 0) {
            rep(i, 1, n) {
                printf("%lld ", a[i]);
            }
        } else {
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n - i - 1; j++) {
                    if (lc.find(j) != lc.end() || lc.find(j + 1) != lc.end()) continue;
                    if (a[j] < a[j + 1]) {
                        swap(a[j], a[j + 1]);
                    }
                }
            }
            mms(vis, 0);
            for (int i = 1; i <= n; i++) {
                if (lc.find(i) != lc.end()) {
                    vis[i] = 1;
                    printf("%lld ", a[i]);
//                    if (a[i] >= 0) pos -= 1;
                    continue;
                }
                for (int j = 1; j <= n; j++) {
                    if (vis[j] || lo[j]) continue;
                    if (pos) {
                        if (a[j] >= 0) {
                            printf("%lld ", a[j]);
                            vis[j] = 1;
                            pos -= 1;
                            break;
                        }
                    } else if (!vis[j]) {
                        printf("%lld ", a[j]);
                        vis[j] = 1;
                        break;
                    }
                }
            }
        }
        printf("\n");
    }
    return 0;
}


#include<bits/stdc++.h>
#define Ant ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define f(i,n) for(int i=0;i<n;i++)
#define ull unsigned long long
#define ll long long
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define ns cout<<"IMPOSSIBLE"<<endl

#define mod 1000000007

using namespace std;

void solve()
{
	int n;
	cin>>n;
	int A[n];
	int x;
	f(i,n)
	cin>>A[i];

	// vector<int> unlocked;
	int mn=INT_MAX,mx=INT_MIN,mni=-1,mxi=-1;
	vector<bool> locked(n,false);
	f(i,n)
	{
		cin>>x;
		if(x==0)
		{
			if(A[i]<mn)
			{
				mni=i;mn=A[i];
			}
			if(A[i]>mx)
			{
				mxi=i;mx=A[i];
			}
		}

	}

	if(mni!=mxi)
	swap(A[mni],A[mxi]);

	// sort(unlocked.begin(),unlocked.end());
	// int j=0;
	// f(i,n)
	// 	if(locked[i])
	// 		continue;
	// 	else
	// 		A[i] = unlocked[j++];

	// // j=0;
	// // while(A[j]<0&&j<n)
	// // 	j++;
	// // cout<<j<<endl;
	// // if(j>0)

		f(i,n) cout<<A[i]<<" ";
		cout<<endl;

}
int main(int argc, char const *argv[])
{
	Ant;
	int t;
	cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
#define Ant ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define f(i,n) for(int i=0;i<n;i++)
#define ull unsigned long long
#define ll long long
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define ns cout<<"IMPOSSIBLE"<<endl

#define mod 1000000007

using namespace std;

void solve()
{
	int n;
	cin>>n;
	int A[n];
	int x;
	f(i,n)
	cin>>A[i];

	// vector<int> unlocked;
	int mn=INT_MAX,mx=INT_MIN,mni=n,mxi=-1;
	vector<bool> locked(n,false);
	f(i,n)
	{
		cin>>x;
		if(x==0)
		{
			mni=min(mn,i);
			if(A[i]>0)
			{
				mxi=i;mx=A[i]; break;
			}
		}

	}

	if(mni!=n&&mxi!=-1)
	swap(A[mni],A[mxi]);

	// sort(unlocked.begin(),unlocked.end());
	// int j=0;
	// f(i,n)
	// 	if(locked[i])
	// 		continue;
	// 	else
	// 		A[i] = unlocked[j++];

	// // j=0;
	// // while(A[j]<0&&j<n)
	// // 	j++;
	// // cout<<j<<endl;
	// // if(j>0)

		f(i,n) cout<<A[i]<<" ";
		cout<<endl;

}
int main(int argc, char const *argv[])
{
	Ant;
	int t;
	cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
#define Ant ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define f(i,n) for(int i=0;i<n;i++)
#define ull unsigned long long
#define ll long long
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define ns cout<<"IMPOSSIBLE"<<endl

#define mod 1000000007

using namespace std;

void solve()
{
	int n;
	cin>>n;
	int A[n];
	int x;
	f(i,n)
	cin>>A[i];

	vector<int> unlocked;
	vector<bool> locked(n,false);
	f(i,n)
	{
		cin>>x;
		if(x==1) locked[i] = true;
		else unlocked.pb(A[i]);
	}

	sort(unlocked.begin(),unlocked.end(),greater<int>());
	int j=0;
	f(i,n)
		if(locked[i])
			continue;
		else
			A[i] = unlocked[j++];

	// j=0;
	// while(A[j]<0&&j<n)
	// 	j++;
	// cout<<j<<endl;
	// if(j>0)

		f(i,n) cout<<A[i]<<" ";
		cout<<endl;

}
int main(int argc, char const *argv[])
{
	Ant;
	int t;
	cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
#define Ant ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define f(i,n) for(int i=0;i<n;i++)
#define ull unsigned long long
#define ll long long
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define ns cout<<"IMPOSSIBLE"<<endl

#define mod 1000000007

using namespace std;

void solve()
{
	int n;
	cin>>n;
	int A[n];
	int x;
	f(i,n)
	cin>>A[i];

	vector<int> unlocked;
	vector<bool> locked(n,false);
	f(i,n)
	{
		cin>>x;
		if(x==1) locked[i] = true;
		else unlocked.pb(A[i]);
	}

	sort(unlocked.begin(),unlocked.end());
	int j=0;
	f(i,n)
		if(locked[i])
			continue;
		else
			A[i] = unlocked[j++];

	// j=0;
	// while(A[j]<0&&j<n)
	// 	j++;
	// cout<<j<<endl;
	// if(j>0)

		f(i,n) cout<<A[i]<<" ";
		cout<<endl;

}
int main(int argc, char const *argv[])
{
	Ant;
	int t;
	cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
#define Ant ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define f(i,n) for(int i=0;i<n;i++)
#define ull unsigned long long
#define ll long long
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define ns cout<<"IMPOSSIBLE"<<endl

#define mod 1000000007

using namespace std;

void solve()
{
	int n;
	cin>>n;
	int A[n];
	int x;
	f(i,n)
	cin>>A[i];

	// vector<int> unlocked;
	int mni=n,mxi=-1;
	vector<bool> locked(n,false);
	f(i,n)
	{
		cin>>x;
		if(x==0)
		{
			mni=min(mni,i);
			if(A[i]>0)
			{
				mxi=i;
			}
		}

	}

	if(mni!=n&&mxi!=-1)
	swap(A[mni],A[mxi]);

	// sort(unlocked.begin(),unlocked.end());
	// int j=0;
	// f(i,n)
	// 	if(locked[i])
	// 		continue;
	// 	else
	// 		A[i] = unlocked[j++];

	// // j=0;
	// // while(A[j]<0&&j<n)
	// // 	j++;
	// // cout<<j<<endl;
	// // if(j>0)

	f(i,n) cout<<A[i]<<" ";
		cout<<endl;

}
int main(int argc, char const *argv[])
{
	Ant;
	int t;
	cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
#define Ant ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define f(i,n) for(int i=0;i<n;i++)
#define ull unsigned long long
#define ll long long
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define ns cout<<"IMPOSSIBLE"<<endl

#define mod 1000000007

using namespace std;

void solve()
{
	int n;
	cin>>n;
	int A[n];
	int x;
	f(i,n)
	cin>>A[i];

	vector<int> unlocked;
	vector<bool> locked(n,false);
	f(i,n)
	{
		cin>>x;
		if(x==1) locked[i] = true;
		else unlocked.pb(A[i]);
	}

	sort(unlocked.begin(),unlocked.end());
	int j=0;
	f(i,n)
		if(locked[i])
			continue;
		else
			A[i] = unlocked[j++];

	// j=0;
	// while(A[j]<0&&j<n)
	// 	j++;
	// cout<<j<<endl;
	// if(j>0)

		f(i,n) cout<<A[0]<<" " ;
		cout<<endl;

}
int main(int argc, char const *argv[])
{
	Ant;
	int t;
	cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF  = 0x3f3f3f3f;
const ll  mod  = 1e9 + 7;
const ll  N = 1e6 + 5;

int n,a[N],l[N];

void solve(){
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++) cin>>l[i];
    vector<int> V;
    for(int i=1;i<=n;i++) if(!l[i]) V.emplace_back(a[i]);
    sort(V.rbegin(), V.rend());
    ll sum=0,id=0,mi=INF;
    bool ok=true;
    for(int i=1;i<=n;i++){
        if(l[i]){
            sum+=a[i];
        }else{
            sum+=V[id++];
        }
        if(sum<0) {ok=false;}
        mi=min(mi,sum);
    }
    if(ok){
        id=0;
        for(int i=1;i<=n;i++){
            if(l[i]) cout<<a[i]<<" \n"[i==n];
            else cout<<V[id++]<<" \n"[i==n];
        }
        return;
    }
    id=V.size()-1;
    int pos=0;
    ll S=0;
    bool flag=false;
    for(int i=1;i<=n;i++){
        if(l[i]) S+=a[i];
        else{
            if(flag) S+=V[pos];
            else S+=V[id];
        }

        if(l[i]) cout<<a[i]<<" \n"[i==n];
        else {
            if(flag) cout<<V[pos++]<<" \n"[i==n];
            else cout<<V[id--]<<" \n"[i==n];
        }

        if(S<=mi) flag=true;
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;cin>>T;
    while(T--){
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF  = 0x3f3f3f3f;
const ll  mod  = 1e9 + 7;
const ll  N = 1e6 + 5;

int n,a[N],l[N];

void solve(){
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++) cin>>l[i];
    vector<int> V;
    for(int i=1;i<=n;i++) if(!l[i]) V.emplace_back(a[i]);
    sort(V.rbegin(), V.rend());
    ll sum=0,id=0,mi=INF;
    for(int i=1;i<=n;i++){
        if(l[i]){
            sum+=a[i];
        }else{
            sum+=V[id++];
        }
        mi=min(mi,sum);
    }
    if(mi>=0){
        id=0;
        for(int i=1;i<=n;i++){
            if(l[i]) cout<<a[i]<<" \n"[i==n];
            else cout<<V[id++]<<" \n"[i==n];
        }
        return;
    }
    id=0;
    for(int i=1;i<=n;i++){
        if(l[i]){
            cout<<a[i]<<" \n"[i==n];
        }
        else{
            cout<<V[id++]<<" \n"[i==n];
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;cin>>T;
    while(T--){
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF  = 0x3f3f3f3f;
const ll  mod  = 1e9 + 7;
const ll  N = 1e6 + 5;

int n,a[N],l[N];

void solve(){
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++) cin>>l[i];
    vector<int> V;
    for(int i=1;i<=n;i++) if(!l[i]) V.emplace_back(a[i]);
    sort(V.rbegin(), V.rend());
    ll sum=0,id=0,mi=INF;
    for(int i=1;i<=n;i++){
        if(l[i]){
            sum+=a[i];
        }else{
            sum+=V[id++];
        }
        mi=min(mi,sum);
    }
    if(mi>=0){
        id=0;
        for(int i=1;i<=n;i++){
            if(l[i]) cout<<a[i]<<" \n"[i==n];
            else cout<<V[id++]<<" \n"[i==n];
        }
        return;
    }
    id=0;
    for(int i=1;i<=n;i++){
        if(l[i]){
            cout<<a[i]<<" \n"[i==n];
        }
        else{
            cout<<a[id++]<<" \n"[i==n];
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;cin>>T;
    while(T--){
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF  = 0x3f3f3f3f;
const ll  mod  = 1e9 + 7;
const ll  N = 1e6 + 5;

int n,a[N],l[N];

void solve(){
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++) cin>>l[i];
    vector<int> V;
    for(int i=1;i<=n;i++) if(!l[i]) V.emplace_back(a[i]);
    sort(V.rbegin(), V.rend());
    ll sum=0,id=0;
    bool ok=true;
    for(int i=1;i<=n;i++){
        if(l[i]){
            sum+=a[i];
        }else{
            sum+=V[id++];
        }
        if(sum<0) {ok=false;break;}
    }
    if(ok){
        id=0;
        for(int i=1;i<=n;i++){
            if(l[i]) cout<<a[i]<<" \n"[i==n];
            else cout<<V[id++]<<" \n"[i==n];
        }
        return;
    }
    id=V.size()-1;
    for(int i=1;i<=n;i++){
        if(l[i]) cout<<a[i]<<" \n"[i==n];
        else cout<<V[id--]<<" \n"[i==n];
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;cin>>T;
    while(T--){
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF  = 0x3f3f3f3f;
const ll  mod  = 1e9 + 7;
const ll  N = 1e6 + 5;

int n,a[N],l[N];

void solve(){
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++) cin>>l[i];
    vector<int> V;
    for(int i=1;i<=n;i++) if(!l[i]) V.emplace_back(a[i]);
    sort(V.rbegin(), V.rend());
    ll sum=0,id=0;
    bool ok=true;
    for(int i=1;i<=n;i++){
        if(l[i]){
            sum+=a[i];
        }else{
            sum+=V[id++];
        }
        if(sum<0) {ok=false;}
    }
    if(ok){
        id=0;
        for(int i=1;i<=n;i++){
            if(l[i]) cout<<a[i]<<" \n"[i==n];
            else cout<<V[id++]<<" \n"[i==n];
        }
        return;
    }
    id=V.size()-1;
    int pos=0;
    ll S=0;
    bool flag=false;
    for(int i=1;i<=n;i++){
        if(l[i]) S+=a[i];
        else if(flag) S+=V[pos];
        else S+=V[id];
        if(l[i]) cout<<a[i]<<" \n"[i==n];
        else if(flag) cout<<V[pos++]<<" \n"[i==n];
        else cout<<V[id--]<<" \n"[i==n];
        if(S<=sum) flag=true;
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;cin>>T;
    while(T--){
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF  = 0x3f3f3f3f;
const ll  mod  = 1e9 + 7;
const ll  N = 1e6 + 5;

int n,a[N],l[N];

void solve(){
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++) cin>>l[i];
    vector<int> V;
    for(int i=1;i<=n;i++) if(!l[i]) V.emplace_back(a[i]);
    sort(V.rbegin(), V.rend());
    ll sum=0,id=0,mi=INF;
    for(int i=1;i<=n;i++){
        if(l[i]){
            sum+=a[i];
        }else{
            sum+=V[id++];
        }
        mi=min(mi,sum);
    }
    if(mi>=0){
        id=0;
        for(int i=1;i<=n;i++){
            if(l[i]) cout<<a[i]<<" \n"[i==n];
            else cout<<V[id++]<<" \n"[i==n];
        }
        return;
    }
    id=0;
    for(int i=1;i<=n;i++){
        if(l[i]){
            S+=a[i];
            cout<<a[i]<<" \n"[i==n];
        }
        else{
            cout<<a[id++]<<" \n"[i==n];
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;cin>>T;
    while(T--){
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF  = 0x3f3f3f3f;
const ll  mod  = 1e9 + 7;
const ll  N = 1e6 + 5;

int n,a[N],l[N];

void solve(){
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++) cin>>l[i];
    vector<int> V;
    for(int i=1;i<=n;i++) if(!l[i]) V.emplace_back(a[i]);
    sort(V.rbegin(), V.rend());
    ll sum=0,id=0;
    bool ok=true;
    for(int i=1;i<=n;i++){
        if(l[i]){
            sum+=a[i];
        }else{
            sum+=V[id++];
        }
        if(sum<0) {ok=false;break;}
    }
    if(ok){
        id=0;
        for(int i=1;i<=n;i++){
            if(l[i]) cout<<a[i]<<" \n"[i==n];
            else cout<<V[id++]<<" \n"[i==n];
        }
        return;
    }
    id=V.size()-1;
    int pos=0;
    sum=0;
    bool flag=false;
    for(int i=1;i<=n;i++){
        if(l[i]) sum+=a[i];
        else sum+=V[id];
        if(l[i]) cout<<a[i]<<" \n"[i==n];
        else if(!flag) cout<<V[id--]<<" \n"[i==n];
        else cout<<V[pos++]<<" \n"[i==n];
        if(sum<0) flag=true;
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;cin>>T;
    while(T--){
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define l long
#define m9 1000000007
#define pb push_back
#define mp make_pair

#define s second
const int c = 2*10e5;
const double pi = 3.14159265358979323846;
using namespace std;

int main()
{
    int t; cin>>t;
    while (t--) {
        l n; cin>>n;
        l a[n];
        for (l i=0; i<n; i++) {
            cin>>a[i];
        }
        l lo[n];
        vector<l>unlocked;

        for (l i=0; i<n; i++) {
            cin>>lo[i];
            if (lo[i]==0) {
                unlocked.pb(a[i]);
            }
        }
        sort(unlocked.begin(), unlocked.end());
        l j=0;
        for (l i=0; i<n; i++) {
            if (lo[i]==0) {
                a[i]=unlocked[j];
                j++;
            }

        }
        for (l i=0; i<n; i++) {
            cout<<a[i]<<" ";
        }
        cout<<endl;
    }

}


#include<bits/stdc++.h>
#define ll long long
#define l long
#define m9 1000000007
#define pb push_back
#define mp make_pair

#define s second
const int c = 2*10e5;
const double pi = 3.14159265358979323846;
using namespace std;

int main()
{
    int t; cin>>t;
    while (t--) {
        l n; cin>>n;
        l a[n];
        for (l i=0; i<n; i++) {
            cin>>a[i];
        }
        l lo[n];
        vector<l>unlocked;

        for (l i=0; i<n; i++) {
            cin>>lo[i];
            if (lo[i]==0) {
                unlocked.pb(a[i]);
            }
        }
        sort(unlocked.rbegin(), unlocked.rend());
        l j=0;
        for (l i=0; i<n; i++) {
            if (lo[i]==0) {
                a[i]=unlocked[j];
                j++;
            }

        }
        for (l i=0; i<n; i++) {
            cout<<a[i]<<" ";
        }
        cout<<endl;
    }

}


#include<bits/stdc++.h>
#define ll long long
#define l long
#define m9 1000000007
#define pb push_back
#define mp make_pair

#define s second
const int c = 2*10e5;
const double pi = 3.14159265358979323846;
using namespace std;

int main()
{
    int t; cin>>t;
    while (t--) {
        ll n; cin>>n;
        ll a[n];
        for (ll i=0; i<n; i++) {
            cin>>a[i];
        }
        int l[n];
        vector<int>unlocked;

        for (int i=0; i<n; i++) {
            cin>>l[i];
            if (l[i]==0) {
                unlocked.pb(a[i]);
            }
        }
        sort(unlocked.begin(), unlocked.end());
        int j=0;
        for (int i=0; i<n; i++) {
            if (l[i]==0) {
                a[i]=unlocked[j];
                j++;
            }
        }
        for (int i=0; i<n; i++) {
            cout<<a[i]<<" ";
        }
        cout<<endl;
    }

}


#include<bits/stdc++.h>
#define ll long long
#define l long
#define m9 1000000007
#define pb push_back
#define mp make_pair

#define s second
const int c = 2*10e5;
const double pi = 3.14159265358979323846;
using namespace std;

int main()
{
    int t; cin>>t;
    while (t--) {
        l n; cin>>n;
        l a[n];
        for (l i=0; i<n; i++) {
            cin>>a[i];
        }
        l lo[n];
        vector<l>unlocked;

        for (l i=0; i<n; i++) {
            cin>>lo[i];
            if (lo[i]==0) {
                unlocked.pb(a[i]);
            }
        }
        sort(unlocked.begin(), unlocked.end());
        l j=0;
        for (l i=0; i<n; i++) {
            if (lo[i]==0) {
                a[i]=unlocked[j];
            }
            j++;
        }
        for (l i=0; i<n; i++) {
            cout<<a[i]<<" ";
        }
        cout<<endl;
    }

}


#include<bits/stdc++.h>
#define ll long long
#define l long
#define m9 1000000007
#define pb push_back
#define mp make_pair

#define s second
const int c = 2*10e5;
const double pi = 3.14159265358979323846;
using namespace std;

int main()
{
    int t; cin>>t;
    while (t--) {
        int n; cin>>n;
        int a[n];
        for (int i=0; i<n; i++) {
            cin>>a[i];
        }
        int l[n];
        vector<int>unlocked;

        for (int i=0; i<n; i++) {
            cin>>l[i];
            if (l[i]==0) {
                unlocked.pb(a[i]);
            }
        }
        sort(unlocked.begin(), unlocked.end());
        int j=0;
        for (int i=0; i<n; i++) {
            if (l[i]==0) {
                a[i]=unlocked[j];
            }
            j++;
        }
        for (int i=0; i<n; i++) {
            cout<<a[i]<<" ";
        }
        cout<<endl;
    }

}


#include<bits/stdc++.h>
#define ll long long
#define l long
#define m9 1000000007
#define pb push_back
#define mp make_pair

#define s second
const int c = 2*10e5;
const double pi = 3.14159265358979323846;
using namespace std;

int main()
{
    int t; cin>>t;
    while (t--) {
        ll n; cin>>n;
        ll a[n];
        for (ll i=0; i<n; i++) {
            cin>>a[i];
        }
        int l[n];
        vector<int>unlocked;

        for (int i=0; i<n; i++) {
            cin>>l[i];
            if (l[i]==0) {
                unlocked.pb(a[i]);
            }
        }
        sort(unlocked.rbegin(), unlocked.rend());
        int j=0;
        for (int i=0; i<n; i++) {
            if (l[i]==0) {
                a[i]=unlocked[j];
                j++;
            }
        }
        for (int i=0; i<n; i++) {
            cout<<a[i]<<" ";
        }
        cout<<endl;
    }

}


#include<bits/stdc++.h>
#define ll long long
#define l long
#define m9 1000000007
#define pb push_back
#define mp make_pair

#define s second
const int c = 2*10e5;
const double pi = 3.14159265358979323846;
using namespace std;

int main()
{
    int t; cin>>t;
    while (t--) {
        int n; cin>>n;
        int a[n];
        for (int i=0; i<n; i++) {
            cin>>a[i];
        }
        int lo[n];
        vector<int>unlocked;

        for (int i=0; i<n; i++) {
            cin>>lo[i];
            if (lo[i]==0) {
                unlocked.pb(a[i]);
            }
        }
        sort(unlocked.begin(), unlocked.end());
        int j=0;
        for (int i=0; i<n; i++) {
            if (lo[i]==0) {
                a[i]=unlocked[j];
            }
            j++;
        }
        for (int i=0; i<n; i++) {
            cout<<a[i]<<" ";
        }
        cout<<endl;
    }

}


#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<vll> vvl;
typedef pair<ll, ll> pll;
typedef vector<pll> vpl;
typedef unordered_set<ll> usl;
typedef set<ll> sl;
typedef unordered_map<ll, ll> uml;
typedef map<ll, ll> ml;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;

#define all(v) v.begin() , v.end()
#define ceil(a, b) (a + b - 1) / b
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const ll inf = 1e9 + 7;
const ll mod = 998244353;
const double pie = 3.14159265358979323846;
const char nl = '\n';
const char spc = ' ';

template<typename Type>
istream &operator>>(istream &in, vector<Type> &v)
{
    ll n = v.size();
    for (int i = 0; i < n; i++)
        in >> v[i];
    return in;
}

template<typename Type>
ostream &operator<<(ostream &out, vector<Type> &v)
{
    for (auto value : v)
        out << value << ' ';
    out << nl;
    return out;
}

bool isPrime (ll n)
{
    if (n < 4)
        return true;
    if (n % 2 == 0)
        return false;
    ll i = 3;
    while (i * i <= n)
    {
        if (n % i == 0)
            return false;
        i += 2;
    }
    return true;
}
void solve()
{
    ll n;
    cin >> n;
    vll a(n);
    cin >> a;
    vll l(n);
    cin >> l;
    vll done(n);
    vll ans(n);
    for (ll i = 0; i < n; ++i)
        done[i] = l[i];
    ll i = 0;
    for (i = 0; i < n; ++i)
    {
        if (l[i])
            ans[i] = a[i];
        else
        {
            if (a[i] >= 0)
            {
                ans[i] = a[i];
                done[i] = 1;
            }
            else
            {
                ll flag = 0;
                for (ll j = i + 1; j < n; ++j)
                {
                    if (!done[j] and a[j] >= 0)
                    {
                        flag = 1;
                        ans[i] = a[j];
                        done[j] = 1;
                        break;
                    }
                }
                if (!flag)
                    break;
            }
        }
    }
    while (i < n)
    {
        if (l[i])
            ans[i] = a[i];
        else
        {   for (ll j = 0; j < n; ++j)
            {
                if (!done[j])
                {
                    ans[i] = a[j];
                    done[j] = 1;
                    break;
                }
            }
        }
        ++i;
    }
    cout << ans;
}
int main()
{
    IOS
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    ll t;
    //t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<vll> vvl;
typedef pair<ll, ll> pll;
typedef vector<pll> vpl;
typedef unordered_set<ll> usl;
typedef set<ll> sl;
typedef unordered_map<ll, ll> uml;
typedef map<ll, ll> ml;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;

#define all(v) v.begin() , v.end()
#define rall(v) v.rbegin() , v.rend()
#define ceil(a, b) (a + b - 1) / b
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const ll inf = 1e9 + 7;
const ll mod = 998244353;
const double pie = 3.14159265358979323846;
const char nl = '\n';
const char spc = ' ';

template<typename Type>
istream &operator>>(istream &in, vector<Type> &v)
{
    ll n = v.size();
    for (int i = 0; i < n; i++)
        in >> v[i];
    return in;
}

template<typename Type>
ostream &operator<<(ostream &out, vector<Type> &v)
{
    for (auto value : v)
        out << value << ' ';
    out << nl;
    return out;
}

bool isPrime (ll n)
{
    if (n < 4)
        return true;
    if (n % 2 == 0)
        return false;
    ll i = 3;
    while (i * i <= n)
    {
        if (n % i == 0)
            return false;
        i += 2;
    }
    return true;
}
void solve()
{
    ll n;
    cin >> n;
    vll a(n), lock(n);
    cin >> a >> lock;
    vll ans;
    for (ll i = 0; i < n; ++i)
    {
        if (!lock[i])
            ans.emplace_back(a[i]);
    }
    sort(rall(ans));
    for (ll i = 0; i < n; ++i)
    {
        if (lock[i])
            ans.emplace(ans.begin() + i, a[i]);
    }
    cout << ans;
}
int main()
{
    IOS
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    ll t;
    //t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<vll> vvl;
typedef pair<ll, ll> pll;
typedef vector<pll> vpl;
typedef unordered_set<ll> usl;
typedef set<ll> sl;
typedef unordered_map<ll, ll> uml;
typedef map<ll, ll> ml;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;

#define all(v) v.begin() , v.end()
#define ceil(a, b) (a + b - 1) / b
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const ll inf = 1e9 + 7;
const ll mod = 998244353;
const double pie = 3.14159265358979323846;
const char nl = '\n';
const char spc = ' ';

template<typename Type>
istream &operator>>(istream &in, vector<Type> &v)
{
    ll n = v.size();
    for (int i = 0; i < n; i++)
        in >> v[i];
    return in;
}

template<typename Type>
ostream &operator<<(ostream &out, vector<Type> &v)
{
    for (auto value : v)
        out << value << ' ';
    out << nl;
    return out;
}

bool isPrime (ll n)
{
    if (n < 4)
        return true;
    if (n % 2 == 0)
        return false;
    ll i = 3;
    while (i * i <= n)
    {
        if (n % i == 0)
            return false;
        i += 2;
    }
    return true;
}
void solve()
{
    ll n;
    cin >> n;
    vll a(n);
    cin >> a;
    vll l(n);
    cin >> l;
    vll done(n);
    vll ans(n);
    for (ll i = 0; i < n; ++i)
        done[i] = l[i];
    ll i = 0;
    for (i = 0; i < n; ++i)
    {
        if (l[i])
            ans[i] = a[i];
        else
        {
            if (a[i] >= 0)
            {
                ans[i] = a[i];
                done[i] = 1;
            }
            else
            {
                ll flag = 0;
                for (ll j = i + 1; j < n; ++j)
                {
                    if (!done[j] and a[j] >= 0)
                    {
                        flag = 1;
                        ans[i] = a[j];
                        a[j] = a[i];
                        done[j] = 1;
                        break;
                    }
                }
                if (!flag)
                    break;
            }
        }
    }
    while (i < n)
    {
        if (l[i])
            ans[i] = a[i];
        else
        {   for (ll j = 0; j < n; ++j)
            {
                if (!done[j])
                {
                    ans[i] = a[j];
                    done[j] = 1;
                    break;
                }
            }
        }
        ++i;
    }
    cout << ans;
}
int main()
{
    IOS
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    ll t;
    t = 1;
    //cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<vll> vvl;
typedef pair<ll, ll> pll;
typedef vector<pll> vpl;
typedef unordered_set<ll> usl;
typedef set<ll> sl;
typedef unordered_map<ll, ll> uml;
typedef map<ll, ll> ml;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;

#define all(v) v.begin() , v.end()
#define ceil(a, b) (a + b - 1) / b
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const ll inf = 1e9 + 7;
const ll mod = 998244353;
const double pie = 3.14159265358979323846;
const char nl = '\n';
const char spc = ' ';

template<typename Type>
istream &operator>>(istream &in, vector<Type> &v)
{
    ll n = v.size();
    for (int i = 0; i < n; i++)
        in >> v[i];
    return in;
}

template<typename Type>
ostream &operator<<(ostream &out, vector<Type> &v)
{
    for (auto value : v)
        out << value << ' ';
    out << nl;
    return out;
}

bool isPrime (ll n)
{
    if (n < 4)
        return true;
    if (n % 2 == 0)
        return false;
    ll i = 3;
    while (i * i <= n)
    {
        if (n % i == 0)
            return false;
        i += 2;
    }
    return true;
}
void solve()
{
    ll n;
    cin >> n;
    vll a(n), lock(n);
    cin >> a >> lock;
    vll ans;
    for (ll i = 0; i < n; ++i)
    {
        if (!lock[i])
            ans.emplace_back(a[i]);
    }
    sort(all(ans), greater<ll>());
    for (ll i = 0; i < n; ++i)
    {
        if (lock[i])
            ans.emplace(ans.begin() + i, a[i]);
    }
    cout << ans;
}
int main()
{
    IOS
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    ll t;
    //t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<vll> vvl;
typedef pair<ll, ll> pll;
typedef vector<pll> vpl;
typedef unordered_set<ll> usl;
typedef set<ll> sl;
typedef unordered_map<ll, ll> uml;
typedef map<ll, ll> ml;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;

#define all(v) v.begin() , v.end()
#define ceil(a, b) (a + b - 1) / b
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const ll inf = 1e9 + 7;
const ll mod = 998244353;
const double pie = 3.14159265358979323846;
const char nl = '\n';
const char spc = ' ';

template<typename Type>
istream &operator>>(istream &in, vector<Type> &v)
{
    ll n = v.size();
    for (int i = 0; i < n; i++)
        in >> v[i];
    return in;
}

template<typename Type>
ostream &operator<<(ostream &out, vector<Type> &v)
{
    for (auto value : v)
        out << value << ' ';
    out << nl;
    return out;
}

bool isPrime (ll n)
{
    if (n < 4)
        return true;
    if (n % 2 == 0)
        return false;
    ll i = 3;
    while (i * i <= n)
    {
        if (n % i == 0)
            return false;
        i += 2;
    }
    return true;
}
void solve()
{
    ll n;
    cin >> n;
    vll a(n);
    cin >> a;
    vll l(n);
    cin >> l;
    vll done(n);
    vll ans(n);
    for (ll i = 0; i < n; ++i)
        done[i] = l[i];
    ll i = 0;
    for (i = 0; i < n; ++i)
    {
        if (l[i])
            ans[i] = a[i];
        else
        {
            if (a[i] >= 0)
            {
                ans[i] = a[i];
                done[i] = 1;
            }
            else
            {
                ll flag = 0;
                for (ll j = i + 1; j < n; ++j)
                {
                    if (!done[j] and a[j] >= 0)
                    {
                        flag = 1;
                        ans[i] = a[j];
                        a[j] = a[i];
                        done[j] = 1;
                        break;
                    }
                }
                if (!flag)
                    break;
            }
        }
    }
    while (i < n)
    {
        if (l[i])
            ans[i] = a[i];
        else
        {   for (ll j = 0; j < n; ++j)
            {
                if (!done[j])
                {
                    ans[i] = a[j];
                    done[j] = 1;
                    break;
                }
            }
        }
        ++i;
    }
    cout << ans;
}
int main()
{
    IOS
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    ll t;
    //t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<vll> vvl;
typedef pair<ll, ll> pll;
typedef vector<pll> vpl;
typedef unordered_set<ll> usl;
typedef set<ll> sl;
typedef unordered_map<ll, ll> uml;
typedef map<ll, ll> ml;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;

#define all(v) v.begin() , v.end()
#define ceil(a, b) (a + b - 1) / b
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const ll inf = 1e9 + 7;
const ll mod = 998244353;
const double pie = 3.14159265358979323846;
const char nl = '\n';
const char spc = ' ';

template<typename Type>
istream &operator>>(istream &in, vector<Type> &v)
{
    ll n = v.size();
    for (int i = 0; i < n; i++)
        in >> v[i];
    return in;
}

template<typename Type>
ostream &operator<<(ostream &out, vector<Type> &v)
{
    for (auto value : v)
        out << value << ' ';
    out << nl;
    return out;
}

bool isPrime (ll n)
{
    if (n < 4)
        return true;
    if (n % 2 == 0)
        return false;
    ll i = 3;
    while (i * i <= n)
    {
        if (n % i == 0)
            return false;
        i += 2;
    }
    return true;
}
void solve()
{
    ll n;
    cin >> n;
    vll a(n), lock(n);
    cin >> a >> lock;
    vll ans;
    for (ll i = 0; i < n; ++i)
    {
        if (!lock[i])
            ans.emplace_back(a[i]);
    }
    sort(rbegin(), rend());
    for (ll i = 0; i < n; ++i)
    {
        if (lock[i])
            ans.emplace(ans.begin() + i, a[i]);
    }
    cout << ans;
}
int main()
{
    IOS
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    ll t;
    //t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}


// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define fr(a,b,c) for(a = b;a<=c;a++)
#define repr(a,b,c) for(a = b;a>=c;a--)
#define rep(i,n) for(i = 0;i<n;i++)
#define pb push_back
#define MOD 1000000007

ll power(ll x,ll y)
{
    x = x%MOD;
    if(x==0 || x==1)
        return x;
    ll res = 1;
    while(y>0)
    {
        if(y%2==1)
            res = (res*x)%MOD;
        y = y/2;
        x = (x*x)%MOD;
    }
    return res%MOD;
}

ll min(ll a,ll b)
{
    return (a<b)?a:b;
}
ll max(ll a,ll b)
{
    return (a>b)?a:b;
}

int main()
{
    fast;
    ll T,n,i,j,k;
    //T = 1;
    cin>>T;
    while(T--)
    {
        cin>>n;
        ll a[n],pos[n];
        rep(i,n)
            cin>>a[i];
        rep(i,n)
            cin>>pos[i];

        vector<ll> free_pos;
        vector<ll> free_neg;
        rep(i,n)
        {
            if(pos[i]==0)
            {
                if(a[i]>=0)
                    free_pos.pb(a[i]);
                else
                    free_neg.pb(a[i]);
            }
        }
        sort(free_pos.begin(),free_pos.end(),greater<ll>());
        sort(free_neg.begin(),free_neg.end());

        i = 0;
        j = 0;
        while(i<n && j<free_neg.size())
        {
            if(pos[i]==0)
                a[i] = free_neg[j++];
            i++;
        }
        k = 0;
        while(i<n && k<free_pos.size())
        {
            if(pos[i]==0)
                a[i] = free_pos[k++];
            i++;
        }

        rep(i,n)
            cout<<a[i]<<" ";
        cout<<"\n";
    }

    return 0;
}


// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define fr(a,b,c) for(a = b;a<c;a++)
#define rfr(a,b,c) for(a = b;a>c;a--)
#define rep(i,n) for(i = 0;i<n;i++)
#define repr(i,n) for(i = n-1;i>=0;i--)
#define ii pair<ll,ll>
#define first fi
#define second sec
#define vi vector<ll>
#define pb push_back
#define all(o) (o).begin(),(o).end()
const ll MOD = 1000000007;
const ll lim = 100001;

ll power(ll x,ll y){
x%=MOD; y%=(MOD-1); if(x==0||x==1) return x;
ll res = 1;
while(y>0)
{ if(y&1) res = (res*x)%MOD;
y = y>>1; x = (x*x)%MOD; }
return res%MOD; }

ll min(ll a,ll b)
{ return (a<b)?a:b; }
ll max(ll a,ll b)
{ return (a>b)?a:b; }

ll fact[lim],invFact[lim];
void factorial(){
    ll i;
    fact[0] = fact[1] = 1; fr(i,2,lim) fact[i] = (fact[i-1]*i)%MOD;
    invFact[lim-1] = power(fact[lim-1],MOD-2)%MOD;
    rfr(i,lim-1,0) invFact[i-1] = (i * power(fact[i],MOD-2))%MOD; }
ll nCr(ll n, ll r) { return (((fact[n]*invFact[r])%MOD)*invFact[n-r])%MOD; }

signed main()
{
    fast;
    //factorial();
    ll T,n,i,j,k;
    //T = 1;
    cin>>T;
    while(T--)
    {
        cin>>n;
        ll a[n],pos[n];
        rep(i,n)
            cin>>a[i];
        rep(i,n)
            cin>>pos[i];

        vector<ll> free_pos;
        vector<ll> free_neg;
        rep(i,n)
        {
            if(pos[i]==0)
            {
                if(a[i]>0)
                    free_pos.pb(a[i]);
                else
                    free_neg.pb(a[i]);
            }
        }
        sort(all(free_pos),greater<ll>());
        sort(all(free_neg),greater<ll>());

        i = 0;
        k = 0;
        while(i<n && k<free_pos.size())
        {
            if(pos[i]==0)
                a[i] = free_pos[k++];
            i++;
        }
        j = 0;
        while(i<n && j<free_neg.size())
        {
            if(pos[i]==0)
                a[i] = free_neg[j++];
            i++;
        }

        rep(i,n)
            cout<<a[i]<<" ";
        cout<<"\n";
    }

    return 0;
}


// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define fr(a,b,c) for(a = b;a<=c;a++)
#define repr(a,b,c) for(a = b;a>=c;a--)
#define rep(i,n) for(i = 0;i<n;i++)
#define pb push_back
#define MOD 1000000007

ll power(ll x,ll y)
{
    x = x%MOD;
    if(x==0 || x==1)
        return x;
    ll res = 1;
    while(y>0)
    {
        if(y%2==1)
            res = (res*x)%MOD;
        y = y/2;
        x = (x*x)%MOD;
    }
    return res%MOD;
}

ll min(ll a,ll b)
{
    return (a<b)?a:b;
}
ll max(ll a,ll b)
{
    return (a>b)?a:b;
}

int main()
{
    fast;
    ll T,n,i,j,k;
    //T = 1;
    cin>>T;
    while(T--)
    {
        cin>>n;
        ll a[n],pos[n];
        rep(i,n)
            cin>>a[i];
        rep(i,n)
            cin>>pos[i];

        vector<ll> free;
        rep(i,n)
        {
            if(pos[i]==0)
                free.pb(a[i]);
        }
        sort(free.begin(),free.end());
        k = 0;
        rep(i,n)
        {
            if(pos[i]==0)
                a[i] = free[k++];
        }
        rep(i,n)
            cout<<a[i]<<" ";
        cout<<"\n";
    }

    return 0;
}


// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define fr(a,b,c) for(a = b;a<c;a++)
#define rfr(a,b,c) for(a = b;a>c;a--)
#define rep(i,n) for(i = 0;i<n;i++)
#define repr(i,n) for(i = n-1;i>=0;i--)
#define ii pair<ll,ll>
#define first fi
#define second sec
#define vi vector<ll>
#define pb push_back
#define all(o) (o).begin(),(o).end()
const ll MOD = 1000000007;
const ll lim = 100001;

ll power(ll x,ll y){
x%=MOD; y%=(MOD-1); if(x==0||x==1) return x;
ll res = 1;
while(y>0)
{ if(y&1) res = (res*x)%MOD;
y = y>>1; x = (x*x)%MOD; }
return res%MOD; }

ll min(ll a,ll b)
{ return (a<b)?a:b; }
ll max(ll a,ll b)
{ return (a>b)?a:b; }

ll fact[lim],invFact[lim];
void factorial(){
    ll i;
    fact[0] = fact[1] = 1; fr(i,2,lim) fact[i] = (fact[i-1]*i)%MOD;
    invFact[lim-1] = power(fact[lim-1],MOD-2)%MOD;
    rfr(i,lim-1,0) invFact[i-1] = (i * power(fact[i],MOD-2))%MOD; }
ll nCr(ll n, ll r) { return (((fact[n]*invFact[r])%MOD)*invFact[n-r])%MOD; }

signed main()
{
    fast;
    //factorial();
    ll T,n,i,j,k;
    //T = 1;
    cin>>T;
    while(T--)
    {
        cin>>n;
        ll a[n],pos[n];
        rep(i,n)
            cin>>a[i];
        rep(i,n)
            cin>>pos[i];

        vector<ll> free_pos;
        vector<ll> free_neg;
        rep(i,n)
        {
            if(pos[i]==0)
            {
                if(a[i]>=0)
                    free_pos.pb(a[i]);
                else
                    free_neg.pb(a[i]);
            }
        }
        sort(all(free_pos),greater<ll>());
        sort(all(free_neg));

        k = 0;
        while(i<n && k<free_pos.size())
        {
            if(pos[i]==0)
                a[i] = free_pos[k++];
            i++;
        }
        i = 0;
        j = 0;
        while(i<n && j<free_neg.size())
        {
            if(pos[i]==0)
                a[i] = free_neg[j++];
            i++;
        }

        rep(i,n)
            cout<<a[i]<<" ";
        cout<<"\n";
    }

    return 0;
}


// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define fr(a,b,c) for(a = b;a<c;a++)
#define rfr(a,b,c) for(a = b;a>c;a--)
#define rep(i,n) for(i = 0;i<n;i++)
#define repr(i,n) for(i = n-1;i>=0;i--)
#define ii pair<ll,ll>
#define first fi
#define second sec
#define vi vector<ll>
#define pb push_back
#define all(o) (o).begin(),(o).end()
const ll MOD = 1000000007;
const ll lim = 100001;

ll power(ll x,ll y){
x%=MOD; y%=(MOD-1); if(x==0||x==1) return x;
ll res = 1;
while(y>0)
{ if(y&1) res = (res*x)%MOD;
y = y>>1; x = (x*x)%MOD; }
return res%MOD; }

ll min(ll a,ll b)
{ return (a<b)?a:b; }
ll max(ll a,ll b)
{ return (a>b)?a:b; }

ll fact[lim],invFact[lim];
void factorial(){
    ll i;
    fact[0] = fact[1] = 1; fr(i,2,lim) fact[i] = (fact[i-1]*i)%MOD;
    invFact[lim-1] = power(fact[lim-1],MOD-2)%MOD;
    rfr(i,lim-1,0) invFact[i-1] = (i * power(fact[i],MOD-2))%MOD; }
ll nCr(ll n, ll r) { return (((fact[n]*invFact[r])%MOD)*invFact[n-r])%MOD; }

signed main()
{
    fast;
    //factorial();
    ll T,n,i,j,k;
    //T = 1;
    cin>>T;
    while(T--)
    {
        cin>>n;
        ll a[n],pos[n];
        rep(i,n)
            cin>>a[i];
        rep(i,n)
            cin>>pos[i];

        vector<ll> free_pos;
        vector<ll> free_neg;
        rep(i,n)
        {
            if(pos[i]==0)
            {
                if(a[i]>0)
                    free_pos.pb(a[i]);
                else
                    free_neg.pb(a[i]);
            }
        }
        sort(all(free_pos),greater<ll>());
        sort(all(free_neg));

        i = 0;
        k = 0;
        while(i<n && k<free_pos.size())
        {
            if(pos[i]==0)
                a[i] = free_pos[k++];
            i++;
        }
        j = 0;
        while(i<n && j<free_neg.size())
        {
            if(pos[i]==0)
                a[i] = free_neg[j++];
            i++;
        }

        rep(i,n)
            cout<<a[i]<<" ";
        cout<<"\n";
    }

    return 0;
}


// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define fr(a,b,c) for(a = b;a<c;a++)
#define rfr(a,b,c) for(a = b;a>c;a--)
#define rep(i,n) for(i = 0;i<n;i++)
#define repr(i,n) for(i = n-1;i>=0;i--)
#define ii pair<ll,ll>
#define first fi
#define second sec
#define vi vector<ll>
#define pb push_back
#define all(o) (o).begin(),(o).end()
const ll MOD = 1000000007;
const ll lim = 100001;

ll power(ll x,ll y){
x%=MOD; y%=(MOD-1); if(x==0||x==1) return x;
ll res = 1;
while(y>0)
{ if(y&1) res = (res*x)%MOD;
y = y>>1; x = (x*x)%MOD; }
return res%MOD; }

ll min(ll a,ll b)
{ return (a<b)?a:b; }
ll max(ll a,ll b)
{ return (a>b)?a:b; }

ll fact[lim],invFact[lim];
void factorial(){
    ll i;
    fact[0] = fact[1] = 1; fr(i,2,lim) fact[i] = (fact[i-1]*i)%MOD;
    invFact[lim-1] = power(fact[lim-1],MOD-2)%MOD;
    rfr(i,lim-1,0) invFact[i-1] = (i * power(fact[i],MOD-2))%MOD; }
ll nCr(ll n, ll r) { return (((fact[n]*invFact[r])%MOD)*invFact[n-r])%MOD; }

signed main()
{
    fast;
    //factorial();
    ll T,n,i,j,k;
    //T = 1;
    cin>>T;
    while(T--)
    {
        cin>>n;
        ll a[n],pos[n];
        rep(i,n)
            cin>>a[i];
        rep(i,n)
            cin>>pos[i];

        vector<ll> free_pos;
        rep(i,n)
        {
            if(pos[i]==0)
                free_pos.pb(a[i]);
        }
        sort(all(free_pos),greater<ll>());

        i = 0;
        k = 0;
        while(i<n && k<free_pos.size())
        {
            if(pos[i]==0)
                a[i] = free_pos[k++];
            i++;
        }

        rep(i,n)
            cout<<a[i]<<" ";
        cout<<"\n";
    }

    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define mpi map<ll,ll>
#define vl vector<ll>
#define mpp make_pair
#define pll pair<ll,ll>
#define forn(i,n) for(ll i=0;i<n;i++)
#define ff first
#define ss second
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define inf       1000000000000
#define MOD       998244353

long long binpow(long long a, long long b, long long m)
{
    a %= m;
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}
void solve()
{
	ll n;
	cin>>n;
	vector<pll> v;
	ll a[n],b[n];
	forn(i,n)
	cin>>a[i];
	forn(i,n)
	{
		cin>>b[i];
		if(b[i]==0)
		v.pb({a[i],i});
	}
	sort(v.begin(),v.end());
	ll ans=0,sum=0,j=v.size()-1;
	forn(i,n)
	{
		sum+=a[i];
		if(b[i]==0)
		{
			sum-=a[i];
			sum+=v[j].ff;
			a[i]=v[j].ff;
			j--;
		}
	}
	forn(i,n)
	cout<<a[i]<<" ";
}

int main()
{
	fast
	//pre();
	ll t=1;
	cin>>t;
	while(t--)
	{
		solve();
		cout<<endl;
	}
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define mpi map<ll,ll>
#define vl vector<ll>
#define mpp make_pair
#define pll pair<ll,ll>
#define forn(i,n) for(ll i=0;i<n;i++)
#define ff first
#define ss second
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define inf       1000000000000
#define MOD       998244353

long long binpow(long long a, long long b, long long m)
{
    a %= m;
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}
void solve()
{
	ll n;
	cin>>n;
	vl v;
	ll a[n],b[n];
	forn(i,n)
	cin>>a[i];
	forn(i,n)
	{
		cin>>b[i];
		if(b[i]==0)
		v.pb(i);
	}
	sort(v.begin(),v.end());
	ll ans=0,sum=0,j=v.size()-1;
	forn(i,n)
	{
		sum+=a[i];
		if(sum>=0)
		{
			forn(i,n)
			cout<<a[i]<<" ";
			return;
		}
		if(b[i]==0)
		{
			if(j>=0 && a[v[j]]>a[i])
			{
				sum-=a[i];
				sum+=a[v[j]];
				swap(a[i],a[v[j]]);
				j--;
			}
		}
		if(sum>=0)
		{
			forn(i,n)
			cout<<a[i]<<" ";
			return;
		}
	}
	forn(i,n)
	cout<<a[i]<<" ";
}

int main()
{
	fast
	//pre();
	ll t=1;
	cin>>t;
	while(t--)
	{
		solve();
		cout<<endl;
	}
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define mpi map<ll,ll>
#define vl vector<ll>
#define mpp make_pair
#define pll pair<ll,ll>
#define forn(i,n) for(ll i=0;i<n;i++)
#define ff first
#define ss second
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define inf       1000000000000
#define MOD       998244353

long long binpow(long long a, long long b, long long m)
{
    a %= m;
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}
void solve()
{
	ll n;
	cin>>n;
	vl v;
	ll a[n],b[n];
	forn(i,n)
	cin>>a[i];
	forn(i,n)
	{
		cin>>b[i];
		if(b[i]==0)
		v.pb(i);
	}
	sort(v.begin(),v.end());
	ll ans=0,sum=0,j=v.size()-1;
	forn(i,n)
	{
		sum+=a[i];
		if(sum>=0)
		{
			forn(i,n)
			cout<<a[i]<<" ";
			return;
		}
		if(b[i]==0)
		{
			if(j>i && a[v[j]]>a[i])
			{
				sum-=a[i];
				sum+=a[v[j]];
				swap(a[i],a[v[j]]);
				j--;
			}
		}
		if(sum>=0)
		{
			forn(i,n)
			cout<<a[i]<<" ";
			return;
		}
	}
	forn(i,n)
	cout<<a[i]<<" ";
}

int main()
{
	fast
	//pre();
	ll t=1;
	cin>>t;
	while(t--)
	{
		solve();
		cout<<endl;
	}
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define mpi map<ll,ll>
#define vl vector<ll>
#define mpp make_pair
#define pll pair<ll,ll>
#define forn(i,n) for(ll i=0;i<n;i++)
#define ff first
#define ss second
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define inf       1000000000000
#define MOD       998244353

long long binpow(long long a, long long b, long long m)
{
    a %= m;
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}
void solve()
{
	ll n;
	cin>>n;
	vl v;
	ll a[n],b[n];
	forn(i,n)
	cin>>a[i];
	forn(i,n)
	{
		cin>>b[i];
		if(b[i]==0)
		v.pb(i);
	}
	sort(v.begin(),v.end());
	ll ans=0,sum=0,j=v.size()-1;
	forn(i,n)
	{
		sum+=a[i];
		if(sum>=0)
		{
			forn(i,n)
			cout<<a[i]<<" ";
			return;
		}
		if(b[i]==0)
		{
			if(j>=0 && v[j]>i && a[v[j]]>a[i])
			{
				sum-=a[i];
				sum+=a[v[j]];
				swap(a[i],a[v[j]]);
				j--;
			}
		}
		if(sum>=0)
		{
			forn(i,n)
			cout<<a[i]<<" ";
			return;
		}
	}
	forn(i,n)
	cout<<a[i]<<" ";
}

int main()
{
	fast
	//pre();
	ll t=1;
	cin>>t;
	while(t--)
	{
		solve();
		cout<<endl;
	}
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define mpi map<ll,ll>
#define vl vector<ll>
#define mpp make_pair
#define pll pair<ll,ll>
#define forn(i,n) for(ll i=0;i<n;i++)
#define ff first
#define ss second
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define inf       1000000000000
#define MOD       998244353

long long binpow(long long a, long long b, long long m)
{
    a %= m;
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}
void solve()
{
	ll n;
	cin>>n;
	vl v;
	ll a[n],b[n];
	forn(i,n)
	cin>>a[i];
	forn(i,n)
	{
		cin>>b[i];
		if(b[i]==0)
		v.pb(i);
	}
	sort(v.begin(),v.end());
	ll ans=0,sum=0,j=v.size()-1;
	forn(i,n)
	{
		sum+=a[i];
		if(sum>=0)
		{
			cout<<i+1;
			return;
		}
		if(b[i]==0)
		{
			if(j>=0 && a[v[j]]>a[i])
			{
				sum-=a[i];
				sum+=a[v[j]];
				swap(a[i],a[v[j]]);
				j--;
			}
		}
		if(sum>=0)
		{
			cout<<i+1;
			return;
		}
	}
	cout<<0;
}

int main()
{
	fast
	//pre();
	ll t=1;
	cin>>t;
	while(t--)
	{
		solve();
		cout<<endl;
	}
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define mpi map<ll,ll>
#define vl vector<ll>
#define mpp make_pair
#define pll pair<ll,ll>
#define forn(i,n) for(ll i=0;i<n;i++)
#define ff first
#define ss second
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define inf       1000000000000
#define MOD       998244353

long long binpow(long long a, long long b, long long m)
{
    a %= m;
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}
void solve()
{
	ll n;
	cin>>n;
	vector<ll> v;
	ll a[n],b[n];
	forn(i,n)
	cin>>a[i];
	forn(i,n)
	{
		cin>>b[i];
		if(b[i]==0)
		v.pb(a[i]);
	}
	sort(v.begin(),v.end());
	ll ans=0,sum=0,j=v.size()-1;
	forn(i,n)
	{
		sum+=a[i];
		if(sum>=0)
		{
			forn(i,n)
			cout<<a[i]<<" ";
			return;
		}
		if(b[i]==0)
		{
			sum-=a[i];
			sum+=v[j];
			a[i]=v[j];
			j--;
		}
		if(sum>=0)
		{
			forn(i,n)
			cout<<a[i]<<" ";
			return;
		}
	}
	forn(i,n)
	cout<<a[i]<<" ";
}

int main()
{
	fast
	//pre();
	ll t=1;
	cin>>t;
	while(t--)
	{
		solve();
		cout<<endl;
	}
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define mpi map<ll,ll>
#define vl vector<ll>
#define mpp make_pair
#define pll pair<ll,ll>
#define forn(i,n) for(ll i=0;i<n;i++)
#define ff first
#define ss second
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define inf       1000000000000
#define MOD       998244353

long long binpow(long long a, long long b, long long m)
{
    a %= m;
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}
void solve()
{
	ll n;
	cin>>n;
	vl v;
	ll a[n],b[n];
	forn(i,n)
	cin>>a[i];
	forn(i,n)
	{
		cin>>b[i];
		if(b[i]==0)
		v.pb(i);
	}
	sort(v.begin(),v.end());
	ll ans=0,sum=0,j=v.size()-1;
	forn(i,n)
	{
		sum+=a[i];
		if(sum>=0)
		{
			forn(i,n)
			cout<<a[i]<<" ";
			return;
		}
		if(b[i]==0)
		{
			if(v[j]>i && a[v[j]]>a[i])
			{
				sum-=a[i];
				sum+=a[v[j]];
				swap(a[i],a[v[j]]);
				j--;
			}
		}
		if(sum>=0)
		{
			forn(i,n)
			cout<<a[i]<<" ";
			return;
		}
	}
	forn(i,n)
	cout<<a[i]<<" ";
}

int main()
{
	fast
	//pre();
	ll t=1;
	cin>>t;
	while(t--)
	{
		solve();
		cout<<endl;
	}
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define re return
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rrep(i, n) for (ll i = n - 1; i >= 0; --i)
#define all(n) n.begin(), n.end()
#define rall(n) n.rbegin(), n.rend()
#define fi first
#define se second
#define sz(n) ll(n.size())
#define sqr(n) ll((n) * (n))
#define get(x) cout << #x << " : " << x << endl;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<int, int> pii;

void GO() {
#ifdef LOL
	freopen("input.txt", "r" , stdin);
	freopen("output.txt", "w", stdout);
#endif
    //ios_base::sync_with_stdio(0);
    //cin.tie(0), cout.tie(0);
}

const int INF = 2 * 1e9;
const ll mod = 1e9 + 7;
//var
int n;
vll a, l;
//
bool f(vll mas, bool t){
    ll sum = 0, j = 0, x = 1;
    vll ans(n);
    rep(i,n){
        if (!l[i]) sum += mas[j], ans[i] = mas[j], j++;
        else sum += a[i], ans[i] = a[i];
        if (sum < 0) x = 0;
    }
    if (t)
    {
        if (!x) re 0;
        for (auto &i: ans) cout << i << ' ';
        cout << '\n';
        re 1;
    }
    for (auto &i: ans) cout << i << ' ';
    cout << '\n';
}
void solve(){
    cin >> n;
    a.resize(n); for (auto &i: a) cin >> i;
    l.resize(n); for (auto &i: l) cin >> i;
    vll mas;
    rep(i, n) if (!l[i]) mas.push_back(a[i]);
    sort(rall(mas));
    if (f(mas, 1)) re;
    reverse(all(mas));
    f(mas, 0);
}
signed main() {
    GO(); int Q; cin >> Q;
    while(Q--) solve(); re 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define re return
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rrep(i, n) for (ll i = n - 1; i >= 0; --i)
#define all(n) n.begin(), n.end()
#define rall(n) n.rbegin(), n.rend()
#define fi first
#define se second
#define sz(n) ll(n.size())
#define sqr(n) ll((n) * (n))
#define get(x) cout << #x << " : " << x << endl;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<int, int> pii;

void GO() {
#ifdef LOL
	freopen("input.txt", "r" , stdin);
	freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
}

const int INF = 2 * 1e9;
const ll mod = 1e9 + 7;
//var
int n;
vll a, l;
//
bool f(vll mas, bool t){
    ll sum = 0, j = 0, x = 1;
    vll ans(n);
    rep(i,n){
        if (!l[i]) sum += mas[j], ans[i] = mas[j], j++;
        else sum += a[i], ans[i] = a[i];
        if (sum < 0) x = 0;
    }
    if (t)
    {
        if (!x) re 0;
        for (auto &i: ans) cout << i << ' ';
        cout << '\n';
        re 1;
    }
    for (auto &i: ans) cout << i << ' ';
    cout << '\n';
}
void solve(){
    cin >> n;
    a.resize(n); for (auto &i: a) cin >> i;
    l.resize(n); for (auto &i: l) cin >> i;
    vll mas;
    rep(i, n) if (!l[i]) mas.push_back(a[i]);
    sort(rall(mas));
    if (f(mas, 1)) re;
    reverse(all(mas));
    f(mas, 0);
}
signed main() {
    GO(); int Q; cin >> Q;
    while(Q--) solve(); re 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define re return
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rrep(i, n) for (ll i = n - 1; i >= 0; --i)
#define all(n) n.begin(), n.end()
#define rall(n) n.rbegin(), n.rend()
#define fi first
#define se second
#define sz(n) ll(n.size())
#define sqr(n) ll((n) * (n))
#define get(x) cout << #x << " : " << x << endl;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<int, int> pii;

void GO() {
#ifdef LOL
	freopen("input.txt", "r" , stdin);
	freopen("output.txt", "w", stdout);
#endif
    //ios_base::sync_with_stdio(0);
    //cin.tie(0), cout.tie(0);
}

const int INF = 2 * 1e9;
const ll mod = 1e9 + 7;
//var
int n;
vll a, l;
//
bool f(vll mas, bool t){
    ll sum = 0, j = 0, x = 1;
    vll ans(n);
    rep(i,n){
        if (!l[i]) sum += mas[j], ans[i] = mas[j], j++;
        else sum += a[i], ans[i] = a[i];
        if (sum < 0) x = 0;
    }
    if (t)
    {
        if (!x) re 0;
        for (auto &i: ans) cout << i << ' ';
        cout << '\n';
        re 1;
    }
    for (auto &i: ans) cout << i << ' ';
    cout << '\n'; re 0;
}
void solve(){
    cin >> n;
    a.resize(n); for (auto &i: a) cin >> i;
    l.resize(n); for (auto &i: l) cin >> i;
    vll mas;
    rep(i, n) if (!l[i]) mas.push_back(a[i]);
    if (sz(mas)) sort(rall(mas));
    if (f(mas, 1)) re;
    if (sz(mas)) reverse(all(mas));
    f(mas, 0);
}
signed main() {
    GO(); int Q; cin >> Q;
    while(Q--) solve(); re 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define re return
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rrep(i, n) for (ll i = n - 1; i >= 0; --i)
#define all(n) n.begin(), n.end()
#define rall(n) n.rbegin(), n.rend()
#define fi first
#define se second
#define sz(n) ll(n.size())
#define sqr(n) ll((n) * (n))
#define get(x) cout << #x << " : " << x << endl;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<int, int> pii;

void GO() {
#ifdef LOL
	freopen("input.txt", "r" , stdin);
	freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
}

const int INF = 2 * 1e9;
const ll mod = 1e9 + 7;
//var
int n;
vll a, l;
////
bool f(vll mas, bool t){
    ll sum = 0, j = 0, x = 1;
    vll ans(n);
    rep(i,n){
        if (!l[i]) sum += mas[j], ans[i] = mas[j], j++;
        else sum += a[i], ans[i] = a[i];
        if (sum < 0) x = 0;
    }
    if (t)
    {
        if (!x) re 0;
        for (auto &i: ans) cout << i << ' ';
        cout << '\n';
        re 1;
    }
    for (auto &i: ans) cout << i << ' ';
    cout << '\n';
}
void solve(){
    cin >> n;
    a.resize(n); for (auto &i: a) cin >> i;
    l.resize(n); for (auto &i: l) cin >> i;
    vll mas;
    rep(i, n) if (!l[i]) mas.push_back(a[i]);
    sort(rall(mas));
    if (f(mas, 1)) re;
    reverse(all(mas));
    f(mas, 0);
}
signed main() {
    GO(); int Q; cin >> Q;
    while(Q--) solve(); re 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define re return
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rrep(i, n) for (ll i = n - 1; i >= 0; --i)
#define all(n) n.begin(), n.end()
#define rall(n) n.rbegin(), n.rend()
#define fi first
#define se second
#define sz(n) ll(n.size())
#define sqr(n) ll((n) * (n))
#define get(x) cout << #x << " : " << x << endl;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<int, int> pii;

void GO() {
#ifdef LOL
	freopen("input.txt", "r" , stdin);
	freopen("output.txt", "w", stdout);
#endif
    //ios_base::sync_with_stdio(0);
    //cin.tie(0), cout.tie(0);
}

const int INF = 2 * 1e9;
const ll mod = 1e9 + 7;

//var
//

void solve(){
    int n; cin >> n;
    vll a(n); for (auto &i: a) cin >> i;
    vll l(n); for (auto &i: l) cin >> i;
    vll b; rep(i, n){ if (!l[i]) b.push_back(a[i]);}
    sort(rall(b)); int j = 0;
    rep(i, n){
        if (l[i]) cout << a[i] << ' ';
        else cout << b[j] << ' ', j++;
    }
    cout << '\n';
}
signed main() {
    GO(); int Q = 1; cin >> Q;
    while(Q--) solve(); re 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define re return
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rrep(i, n) for (ll i = n - 1; i >= 0; --i)
#define all(n) n.begin(), n.end()
#define rall(n) n.rbegin(), n.rend()
#define fi first
#define se second
#define sz(n) ll(n.size())
#define sqr(n) ll((n) * (n))
#define get(x) cout << #x << " : " << x << endl;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<int, int> pii;

void GO() {
#ifdef LOL
	freopen("input.txt", "r" , stdin);
	freopen("output.txt", "w", stdout);
#endif
    //ios_base::sync_with_stdio(0);
    //cin.tie(0), cout.tie(0);
}

const int INF = 2 * 1e9;
const ll mod = 1e9 + 7;
//var
int n;
vll a, l;
//
bool f(vll mas, bool t){
    ll sum = 0, j = 0, x = 1;
    vll ans(n);
    rep(i,n){
        if (!l[i]) sum += mas[j], ans[i] = mas[j], j++;
        else sum += a[i], ans[i] = a[i];
        if (sum < 0) x = 0;
    }
    if (t)
    {
        if (!x) re 0;
        for (auto &i: ans) cout << i << ' ';
        cout << '\n';
        re 1;
    }
    for (auto &i: ans) cout << i << ' ';
    cout << '\n';
}
void solve(){
    cin >> n;
    a.assign(n,0); for (auto &i: a) cin >> i;
    l.assign(n,0); for (auto &i: l) cin >> i;
    vll mas;
    rep(i, n) if (!l[i]) mas.push_back(a[i]);
    if (sz(mas))sort(rall(mas));
    if (f(mas, 1)) re;
    if (sz(mas)) reverse(all(mas));
    f(mas, 0);
}
signed main() {
    GO(); int Q; cin >> Q;
    while(Q--) solve(); re 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long

int main()
{
    ll p; cin >> p;
    while(p--)
    {
        int n; cin >> n;
        int a[n],l[n],pos=0,neg=0;
        for(int i=0;i<n;i++)
        {
            cin >> a[i];
        }
        for(int i=0;i<n;i++)
        {
            cin >> l[i];
        }
        vector<int>v;
        for(int i=0;i<n;i++)
        {
            if(l[i]==0)
            {
                v.push_back(a[i]);
            }
        }
        sort(v.begin(),v.end());
        int j=0;
        for(int i=0;i<n;i++)
        {
            if(l[i]==0)
            {
                cout << v[j++] << " ";
            }
            else
            {
                cout << a[i] << " ";
            }
        }
        cout << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long

int main()
{
    ll p; cin >> p;
    while(p--)
    {
        int n; cin >> n;
        int a[n],l[n],pos=0,neg=0;
        for(int i=0;i<n;i++)
        {
            cin >> a[i];
            if(a[i]>0)
            {
                pos+=a[i];
            }
            else
            {
                neg+=a[i];
            }
        }
        for(int i=0;i<n;i++)
        {
            cin >> l[i];
        }
        vector<int>v;
        for(int i=0;i<n;i++)
        {
            if(l[i]==0)
            {
                v.push_back(a[i]);
            }
        }
        sort(v.begin(),v.end());
        if(pos==abs(neg))
        {
            int temp=v[0];
            v[0]=v[v.size()-1];
            v[v.size()-1]=temp;
        }
        else if(pos>abs(neg))
        {
            reverse(v.begin(),v.end());
        }
        int cnt=0;
        for(int i=0;i<n;i++)
        {
            if(l[i]==0)
            {
                a[i]=v[cnt];
                cnt++;
            }
        }
        for(int i=0;i<n;i++)
        {
            cout << a[i] << " ";
        }
        cout << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long

int main()
{
    ll p; cin >> p;
    while(p--)
    {
        int n; cin >> n;
        int a[n],l[n],pos=0,neg=0;
        for(int i=0;i<n;i++)
        {
            cin >> a[i];
            if(a[i]>0)
            {
                pos+=a[i];
            }
            else
            {
                neg+=a[i];
            }
        }
        for(int i=0;i<n;i++)
        {
            cin >> l[i];
        }
        vector<int>v;
        int index;
        for(int i=0;i<n;i++)
        {
            if(l[i]==0)
            {
                v.push_back(a[i]);
            }
        }
        sort(v.begin(),v.end());
        for(int i=0;i<v.size();i++)
        {
            if(v[i]<0)
            {
                index=i;
            }
        }
        if(pos==abs(neg))
        {
            int temp=v[index];
            v[index]=v[v.size()-1];
            v[v.size()-1]=temp;
        }
        else if(pos>abs(neg))
        {
            reverse(v.begin(),v.end());
        }
        int cnt=0;
        for(int i=0;i<n;i++)
        {
            if(l[i]==0)
            {
                a[i]=v[cnt];
                cnt++;
            }
        }
        for(int i=0;i<n;i++)
        {
            cout << a[i] << " ";
        }
        cout << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long

int main()
{
    ll p; cin >> p;
    while(p--)
    {
        int n; cin >> n;
        int a[n],l[n],pos=0,neg=0;
        for(int i=0;i<n;i++)
        {
            cin >> a[i];
        }
        for(int i=0;i<n;i++)
        {
            cin >> l[i];
        }
        vector<int>v;
        for(int i=0;i<n;i++)
        {
            if(l[i]==0)
            {
                v.push_back(a[i]);
            }
        }
        sort(v.begin(),v.end());
        int cnt=0;
        for(int i=0;i<n;i++)
        {
            if(l[i]==0)
            {
                a[i]=v[cnt];
                cnt++;
            }
        }
        for(int i=0;i<n;i++)
        {
            cout << a[i] << " ";
        }
        cout << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long

int main()
{
    ll p; cin >> p;
    while(p--)
    {
        int n; cin >> n;
        int a[n],l[n],pos=0,neg=0;
        for(int i=0;i<n;i++)
        {
            cin >> a[i];
            if(a[i]>0)
            {
                pos+=a[i];
            }
            else
            {
                neg+=a[i];
            }
        }
        for(int i=0;i<n;i++)
        {
            cin >> l[i];
        }
        vector<int>v;
        for(int i=0;i<n;i++)
        {
            if(l[i]==0)
            {
                v.push_back(a[i]);
            }
        }
        sort(v.begin(),v.end());
        if(pos==abs(neg))
        {
            int temp=v[0];
            v[0]=v[v.size()-1];
            v[v.size()-1]=temp;
        }
        int cnt=0;
        for(int i=0;i<n;i++)
        {
            if(l[i]==0)
            {
                a[i]=v[cnt];
                cnt++;
            }
        }
        for(int i=0;i<n;i++)
        {
            cout << a[i] << " ";
        }
        cout << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long

int main()
{
    ll p; cin >> p;
    while(p--)
    {
        int n; cin >> n;
        int a[n],l[n];
        for(int i=0;i<n;i++)
        {
            cin >> a[i];
        }
        for(int i=0;i<n;i++)
        {
            cin >> l[i];
        }
        vector<int>v;
        for(int i=0;i<n;i++)
        {
            if(l[i]==0)
            {
                v.push_back(a[i]);
            }
        }
        sort(v.begin(),v.end());
        int cnt=0;
        for(int i=0;i<n;i++)
        {
            if(l[i]==0)
            {
                a[i]=v[cnt];
                cnt++;
            }
        }
        for(int i=0;i<n;i++)
        {
            cout << a[i] << " ";
        }
        cout << endl;
    }
}