#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll n , m , a , b;
int main(){
	// ll n , m ;
	// int a , b;
	ll arr[n+100];
	cin >> n >> m;
	ll ans = 0;
	for(int i = 1 ; i <= n ; i++)
		cin >> arr[i];
	for(int  i = 1 ; i <= m ; i++){
		cin >> a >> b;
	ans = ans + min( arr[a] , arr[b] );
	}
	cout << ans <<endl;

}


#include <iostream>
using namespace std;
#define ll long long int
ll n , m , a , b;
int main(){
	//ios::sync_with_stdio(false);
	ll arr[n+100];
	scanf("%lld %lld", &n, &m);
	ll ans = 0;
	for(int i = 1 ; i <= n ; i++){
		scanf("%lld", &arr[i]);
	}
	for(int  i = 1 ; i <= m ; i++){
		scanf("%lld %lld", &a, &b);
		ans = ans + min( arr[a] , arr[b] );
	}
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll n , m , a , b;
int main(){
	ios::sync_with_stdio(false);
	ll arr[n+100];
	cin >> n >> m;
	ll ans = 0;
	for(int i = 1 ; i <= n ; i++)
		cin >> arr[i];
	for(int  i = 1 ; i <= m ; i++){
		cin >> a >> b;
	ans = ans + min( arr[a] , arr[b] );
	}
	cout << ans <<endl;

}


#include<bits/stdc++.h>
using namespace std;
const int N = 1001;
stack<int>order;
int n,m;
vector<int>G[N];
int val[N];
int mark[N];
int vis[N];
void dfs(int x)
{
	if(vis[x])return;
	vis[x]=1;
	for(auto k : G[x])
	{
		dfs(k);
	}
	order.push(x);
}
int main()
{
	cin>>n>>m;
	long long int ans=0;
	for(int i=1;i<=n;i++)cin>>val[i];
	while(m--)
	{
		int x,y;
		cin>>x>>y;
		if(val[x]>val[y])
		{
			G[x].push_back(y);
		}
		else
		{
			G[y].push_back(x);
		}
	}
	for(int i=1;i<=n;i++)
	{
		if(!vis[i])dfs(i);
	}
	while(!order.empty())
	{
		int cur=order.top();
		order.pop();
		mark[cur]=1;
		for(auto k:G[cur])
		{
			if(!mark[k])
			ans+=val[k];
		}
	}
	cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 1001;
stack<int>order;
int n,m;
vector<int>G[N];
int val[N];
int mark[N];
int vis[N];
void dfs(int x)
{
	if(vis[x])return;
	vis[x]=1;
	for(auto k : G[x])
	{
		dfs(k);
	}
	order.push(x);
}
int main()
{
	cin>>n>>m;
	long long int ans=0;
	for(int i=1;i<=n;i++)cin>>val[i];
	while(m--)
	{
		int x,y;
		cin>>x>>y;
		if(val[x]>val[y])
		{
			G[x].push_back(y);
		}
		else if(val[x]<val[y])
		{
			G[y].push_back(x);
		}
		else if(val[x]==val[y])
		{
			G[max(x,y)].push_back(min(x,y));
		}
	}
	for(int i=1;i<=n;i++)
	{
		if(!vis[i])dfs(i);
	}
	while(!order.empty())
	{
		int cur=order.top();
		order.pop();
		mark[cur]=1;
		for(auto k:G[cur])
		{
			if(!mark[k])
			ans+=val[k];
		}
	}
	cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 1001;
stack<int>order;
int n,m;
vector<int>G[N];
int val[N];
int mark[N];
int vis[N];
void dfs(int x)
{
	if(vis[x])return;
	vis[x]=1;
	for(auto k : G[x])
	{
		dfs(k);
	}
	order.push(x);
}
int main()
{
	cin>>n>>m;
	long long int ans=0;
	for(int i=1;i<=n;i++)cin>>val[i];
	while(m--)
	{
		int x,y;
		cin>>x>>y;
		if(val[x]>val[y])
		{
			G[x].push_back(y);
		}
		else if(val[x]<val[y])
		{
			G[y].push_back(x);
		}
		else if(val[x]==val[y])
		{
			G[min(x,y)].push_back(max(x,y));
		}
	}
	for(int i=1;i<=n;i++)
	{
		if(!vis[i])dfs(i);
	}
	while(!order.empty())
	{
		int cur=order.top();
		order.pop();
		mark[cur]=1;
		for(auto k:G[cur])
		{
			if(!mark[k])
			ans+=val[k];
		}
	}
	cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 1001;
stack<int>order;
int n,m;
vector<int>G[N];
int val[N];
int mark[N];
int vis[N];
void dfs(int x)
{
	if(vis[x])return;
	vis[x]=1;
	for(auto k : G[x])
	{
		dfs(k);
	}
	order.push(x);
}
int main()
{
	cin>>n>>m;
	long long int ans=0;
	for(int i=1;i<=n;i++)cin>>val[i];
	while(m--)
	{
		int x,y;
		cin>>x>>y;
		if(val[x]<val[y])
		{
			G[x].push_back(y);
		}
		else if(val[x]>val[y])
		{
			G[y].push_back(x);
		}
		else
		{
			int a=min(x,y);
			int b=max(x,y);
			G[a].push_back(b);
		}
	}
	for(int i=1;i<=n;i++)
	{
		if(!vis[i])dfs(i);
	}
	while(!order.empty())
	{
		int cur=order.top();
		order.pop();
		mark[cur]=1;
		for(auto k:G[cur])
		{
			if(!mark[k])
			ans+=val[k];
		}
	}
	cout<<ans<<endl;
}


//.cpp
#include <bits/stdc++.h>

using namespace std;

int x,y, n,m, mn, ans, w[1100], d[1100];
vector<int> v[1100];

int main(){
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=0; i<n; i++) cin>>w[i];
    for(int i=0; i<m; i++)
      cin>>x>>y,
      x--, y--,
      v[x].push_back(y),
      v[y].push_back(x);
    for(int i=0; i<n; i++)
      for(int j:v[i]) d[i] += w[j]-w[i];
    for(int k=0; k<n; k++) {
      mn = 0;
      for(int i=1; i<n; i++)
        if(d[mn]>d[i]) mn = i;
      for(int i:v[mn])
        d[i] -= w[mn]-w[i],
        ans += w[i];
      w[mn] = 0;
      d[mn] = 2e9;
    }
    cout<<ans<<endl;
    return 0;
}


//.cpp
#include <bits/stdc++.h>

using namespace std;

int n,m,x,y,ans,min,w[1100],d[1100];
vector<int> v[1100];

int main(){
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=1; i<=n; i++) cin>>w[i];
    for(int i=0; i<m; i++)
        cin>>x>>y,
        v[x].push_back(y),
        v[y].push_back(x);
    for(int i=1; i<=n; i++)
        for(int j=0; j<v[i].size(); j++) d[i] += w[v[i][j]];
    for(int j=0; j<n; j++) {
        int min = 1;
        for(int i=2; i<=n; i++)
            if(d[min]>d[i]) min = i;
        ans += d[min];
        for(int i=0; i<v[min].size(); i++) d[v[min][i]] -= w[min];
        d[min] = 1e9;
    }
    cout<<ans<<endl;
    return 0;
}


//.cpp
#include <bits/stdc++.h>

using namespace std;

long long x,y, n,m, mn, ans, w[1100], d[1100];
vector<long long> v[1100];

int main(){
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=0; i<n; i++) cin>>w[i];
    for(int i=0; i<m; i++)
      cin>>x>>y,
      x--, y--,
      v[x].push_back(y),
      v[y].push_back(x);
    for(int i=0; i<n; i++)
      for(int j:v[i]) d[i] += w[j]-w[i];
    for(int k=0; k<n; k++) {
      mn = 0;
      for(int i=1; i<n; i++)
        if(d[mn]>d[i]) mn = i;
      for(int i:v[mn]) {
        if(w[i]) d[i] -= w[mn]-w[i];
        ans += w[i];
      }
      w[mn] = 0;
      d[mn] = 2e18;
    }
    cout<<ans<<endl;
    return 0;
}


//.cpp
#include <bits/stdc++.h>

using namespace std;

int x,y, n,m, mn, ans, w[1100], d[1100];
vector<int> v[1100];

int main(){
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=0; i<n; i++) cin>>w[i];
    for(int i=0; i<m; i++)
      cin>>x>>y,
      x--, y--,
      v[x].push_back(y),
      v[y].push_back(x);
    for(int i=0; i<n; i++)
      for(int j:v[i]) d[i] += w[j]-w[i];
    for(int k=0; k<n; k++) {
      mn = 0;
      for(int i=1; i<n; i++)
        if(d[mn]>d[i]) mn = i;
      for(int i:v[mn]) {
        if(w[i]) d[i] -= w[mn]-w[i];
        ans += w[i];
      }
      w[mn] = 0;
      d[mn] = 2e9;
    }
    cout<<ans<<endl;
    return 0;
}


//.cpp
#include <bits/stdc++.h>

using namespace std;

long long x,y, n,m, ans, w[1100];

int main(){
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=0; i<n; i++) cin>>w[i];
    for(int i=0; i<m; i++)
      cin>>x>>y,
      x--, y--,
      ans += min(w[x],w[y]);
    cout<<ans<<endl;
    return 0;
}


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

int32_t main(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
        int n,m;
        cin >> n >> m;
        int i,a[n+1],ans=0;
        for(i=0; i<n; i++) cin >> a[i];
        for(i=0; i<m; i++){
            int x,y;
            cin >> x >> y;
            x--;
            y--;
            ans=ans+min(a[x],a[y]);
        }
        cout << ans << '\n';
}


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

#define int long long
#define mod 1000000007
#define Ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Test Ios; int tz; cin>>tz; while(tz--)

#define s(args...); { scan(args); }
#define sd(args...); int args; s(args);
#define sda(a,n); int n; cin>>n ; int a[n]; for(int32_t i = 0 ; i<n ; i++) cin>>a[i];
#define sa(a,n) int a[n]; for(int32_t i =0 ; i<n ; i++) cin>>a[i];
#define maxt(a,b,c) max(a,max(b,c));
#define mint(a,b,c) min(a,min(b,c));
void scan() {}
template<typename T, typename... Args>
void scan(T& a, Args&... args) {cin>>a; scan(args...);}
void sortt(int &a,int &b,int &c){if(b<a)swap(a,b);if(c<b)swap(b,c);if(b<a)swap(a,b);}

#define all(v) v.begin(), v.end()
#define mp make_pair
#define pra(a,n)        for(size_t zz=0;zz<n;zz++) cout<<a[zz]<<' '; cout<<'\n';
#define prv(a)          for(auto zz:a) cout<<a[zz]<<' '; cout<<'\n';
#define tr1(x)          cerr <<#x<<": "<<x<<'\n';
#define tr2(x, y)       cerr <<#x<<": "<<x<<" | "<<#y<<": "<<y<<'\n';
#define tr3(x, y, z)    cerr <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<'\n';
#define tr4(a, b, c, d) cerr <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<'\n';

int32_t main(){
//Test
{
    sd(n,m);
    sa(a,n);
    vector<int> v[n];
    int x,y;
    while(m--){
        cin>>x>>y;
        x--;
        y--;
        v[x].push_back(y);
        v[y].push_back(x);
    }

    int ans = 0;
    int del[n];
    memset(del,0,sizeof(del));

    for(int ii=0; ii<n;ii++){
        int lm = INT_MAX,li,lc=INT_MIN;
        for(int j = 0; j<n; j++){
            if(!del[j]){
                int c1 = 0 , c2 = 0;
                for(auto it:v[j]) if(!del[it]) {c1+=a[it]; c2+=a[j];}
                if(c2>lc || (c2==lc && c1<lm)){
                    lc = c2;
                    lm = c1;
                    li = j;
                }
                // else if(c2-c1==lc && c1<lm){
                //     lm = c1;
                //     li = j;
                // }
            }
        }
        //tr2(lm,li);
        ans+=lm;
        del[li] = 1;
    }

    cout<<ans<<'\n';
}}


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

#define int long long
#define mod 1000000007
#define Ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Test Ios; int tz; cin>>tz; while(tz--)

#define s(args...); { scan(args); }
#define sd(args...); int args; s(args);
#define sda(a,n); int n; cin>>n ; int a[n]; for(int32_t i = 0 ; i<n ; i++) cin>>a[i];
#define sa(a,n) int a[n]; for(int32_t i =0 ; i<n ; i++) cin>>a[i];
#define maxt(a,b,c) max(a,max(b,c));
#define mint(a,b,c) min(a,min(b,c));
void scan() {}
template<typename T, typename... Args>
void scan(T& a, Args&... args) {cin>>a; scan(args...);}
void sortt(int &a,int &b,int &c){if(b<a)swap(a,b);if(c<b)swap(b,c);if(b<a)swap(a,b);}

#define all(v) v.begin(), v.end()
#define mp make_pair
#define pra(a,n)        for(size_t zz=0;zz<n;zz++) cout<<a[zz]<<' '; cout<<'\n';
#define prv(a)          for(auto zz:a) cout<<a[zz]<<' '; cout<<'\n';
#define tr1(x)          cerr <<#x<<": "<<x<<'\n';
#define tr2(x, y)       cerr <<#x<<": "<<x<<" | "<<#y<<": "<<y<<'\n';
#define tr3(x, y, z)    cerr <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<'\n';
#define tr4(a, b, c, d) cerr <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<'\n';

int32_t main(){
//Test
{
    sd(n,m);
    sa(a,n);
    vector<int> v[n];
    int x,y;
    while(m--){
        cin>>x>>y;
        x--;
        y--;
        v[x].push_back(y);
        v[y].push_back(x);
    }

    int ans = 0;
    int del[n];
    memset(del,0,sizeof(del));

    for(int ii=0; ii<n;ii++){
        int lm = INT_MAX,li,lc=INT_MIN;
        for(int j = 0; j<n; j++){
            if(!del[j]){
                int c1 = 0 , c2 = 0;
                for(auto it:v[j]) if(!del[it]) {c1+=a[it]; c2+=a[j];}
                if(c2-c1>lc){
                    lm = c1;
                    li = j;
                }
            }
        }
        //tr2(lm,li);
        ans+=lm;
        del[li] = 1;
    }

    cout<<ans<<'\n';
}}


// Take care of mod becoming -ve

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define fro(i, s, e) for (auto i = s; i <= e; ++i)
#define int long long
#define ld long double
#define fr(i, n) for (auto i = 0; i < n; i++)
#define ff first
#define pf pop_front()
#define ss second
#define mp make_pair
#define vi std::vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define setbits(x) __builtin_popcountll(x)
#define zrbits(x) __builtin_ctzll(x)
#define vii std::vector<int, int>
#define tc    \
    int t;    \
    cin >> t; \
    while (t--)
#define FILE                          \
    freopen("input.txt", "r", stdin); \
    freopen("output.txt", "w", stdout)
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fio                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);
#define inp(a) \
    int a;     \
    cin >> a;
#define ps(x, y) fixed << setprecision(y) << x
#define mkarr(arr, n, type) type *arr = new type[n];
#define endl "\n"
#define rev(i, e, s) for (auto i = e; i >= s; --i)
const int mod = 1000000007;
#define min_pq priority_queue<int, std::vector<int>, greater<int>>
#define printclock cerr << "Time : " << 1000 * (ld)clock() / (ld)CLOCKS_PER_SEC << "ms\n";
using namespace std;
const int N = 100005;
const int inf = 1000000000000000000;
#define fit(s, x) memset(s, x, sizeof(s));
#define out(x) cout << x << "\n"
#define deb(x) cout << #x << x << "\n"
#define fr1(i, n) for (int i = 1; i <= n; i++)
#define pi 3.14159265358979323846264338327950
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    PBDS;
// Use __gcd(a,b) for gcd
// find(all(x),key) to get the iterator and its index is obtained by find(all(x),key)-x.begin()
// find_if(iterator_first,iterator_last,unary function) returns first iterator if any element corresponding to unary function exists and if no such element is found it retrurns the last iterator
/*For example:
find_if(all(x),isodd) for x being a vector{10,25,40,55}; *it returns 25*/
/*search(all(v1),all(v2),pred) searches for all the elements of v2 present in v1 based on pred function the iterator correspond
to the first element of sequence if it exists and if not then th elast iterator is obtained.*/
//use substr(size_t pos,size_t len) for the substrings
// iota(forward itr,backwrd itr,val) can be used for consecutive numbers
int power(int a, int b)
{
    long long ans = 1;
    while (b > 0)
    {
        if (b & 1LL)
        {
            ans = (ans * a) % mod;
        }
        a = (a * a) % mod;
        b = (b >> 1LL);
    }
    return ans;
}

vector<int>gr[N];
bool odd_cycle=0;
int vis[N]={0};
bool cmp(int a,int b){
    if(a<0 && b>=0){
        return true;
    }
    else if(a<0 && b<0){
        return abs(a)>=abs(b);
    }
    else if(a>=0 && b<0){
        return false;
    }
    return a>=b;
}
void solve(){
    int n;
    cin>>n;
    int a[n+1];
    fr1(i,n)cin>>a[i];
    int l[n+1];
    fr1(i,n)cin>>l[i];
    // if(l[1]==1 && a[1]>=0){
    //     fr1(i,n){
    //         cout<<a[i]<<" ";
    //     }
    //     cout<<"\n";
    //     return;
    // }
    // int b[n+1];
    // fr1(i,n){
    //     b[i]=a[i];
    // }
    // int pref1[n+1],pref2[n+1];
    // pref1[0]=0;
    // pref2[0]=0;
    vector<int>val;
    fr1(i,n){
        if(l[i]==0){
            val.push_back(a[i]);
        }
    }
    sort(all(val),greater<int>());
    // sort(all(val),cmp);
    // int m=val.size();
    // if(l[1]==0){
    //     int mn=0;
    //     int ind=0;
    //     fr(i,m){
    //         if(val[i]<0){
    //             ind=i;
    //             break;
    //         }
    //     }
    //     swap(val[0],val[ind]);
    // }
    int k=0;
    fr1(i,n){
        if(l[i]==0){
            a[i]=val[k++];
        }
    }
    fr1(i,n){
        cout<<a[i]<<" ";
    }
    cout<<"\n";
}
int32_t main()
{
    fio;
    tc{
        solve();
    }
    printclock;
    return 0;
}


// Take care of mod becoming -ve

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define fro(i, s, e) for (auto i = s; i <= e; ++i)
#define int long long
#define ld long double
#define fr(i, n) for (auto i = 0; i < n; i++)
#define ff first
#define pf pop_front()
#define ss second
#define mp make_pair
#define vi std::vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define setbits(x) __builtin_popcountll(x)
#define zrbits(x) __builtin_ctzll(x)
#define vii std::vector<int, int>
#define tc    \
    int t;    \
    cin >> t; \
    while (t--)
#define FILE                          \
    freopen("input.txt", "r", stdin); \
    freopen("output.txt", "w", stdout)
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fio                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);
#define inp(a) \
    int a;     \
    cin >> a;
#define ps(x, y) fixed << setprecision(y) << x
#define mkarr(arr, n, type) type *arr = new type[n];
#define endl "\n"
#define rev(i, e, s) for (auto i = e; i >= s; --i)
const int mod = 1000000007;
#define min_pq priority_queue<int, std::vector<int>, greater<int>>
#define printclock cerr << "Time : " << 1000 * (ld)clock() / (ld)CLOCKS_PER_SEC << "ms\n";
using namespace std;
const int N = 100005;
const int inf = 1000000000000000000;
#define fit(s, x) memset(s, x, sizeof(s));
#define out(x) cout << x << "\n"
#define deb(x) cout << #x << x << "\n"
#define fr1(i, n) for (int i = 1; i <= n; i++)
#define pi 3.14159265358979323846264338327950
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    PBDS;
// Use __gcd(a,b) for gcd
// find(all(x),key) to get the iterator and its index is obtained by find(all(x),key)-x.begin()
// find_if(iterator_first,iterator_last,unary function) returns first iterator if any element corresponding to unary function exists and if no such element is found it retrurns the last iterator
/*For example:
find_if(all(x),isodd) for x being a vector{10,25,40,55}; *it returns 25*/
/*search(all(v1),all(v2),pred) searches for all the elements of v2 present in v1 based on pred function the iterator correspond
to the first element of sequence if it exists and if not then th elast iterator is obtained.*/
//use substr(size_t pos,size_t len) for the substrings
// iota(forward itr,backwrd itr,val) can be used for consecutive numbers
int power(int a, int b)
{
    long long ans = 1;
    while (b > 0)
    {
        if (b & 1LL)
        {
            ans = (ans * a) % mod;
        }
        a = (a * a) % mod;
        b = (b >> 1LL);
    }
    return ans;
}

vector<int>gr[N];
bool odd_cycle=0;
int vis[N]={0};
bool cmp(int a,int b){
    if(a<0 && b>=0){
        return true;
    }
    else if(a<0 && b<0){
        return abs(a)>=abs(b);
    }
    else if(a>=0 && b<0){
        return false;
    }
    return a>=b;
}
void solve(){
    int n;
    cin>>n;
    int a[n+1];
    fr1(i,n)cin>>a[i];
    int l[n+1];
    fr1(i,n)cin>>l[i];
    if(l[1]==1 && a[1]>=0){
        fr1(i,n){
            cout<<a[i]<<" ";
        }
        cout<<"\n";
        return;
    }
    int b[n+1];
    fr1(i,n){
        b[i]=a[i];
    }
    int pref1[n+1],pref2[n+1];
    pref1[0]=0;
    pref2[0]=0;
    vector<int>val;
    fr1(i,n){
        if(l[i]==0){
            val.push_back(a[i]);
        }
    }
    sort(all(val),cmp);
    // int m=val.size();
    // if(l[1]==0){
    //     int mn=0;
    //     int ind=0;
    //     fr(i,m){
    //         if(val[i]<0){
    //             ind=i;
    //             break;
    //         }
    //     }
    //     swap(val[0],val[ind]);
    // }
    int k=0;
    fr1(i,n){
        if(l[i]==0){
            a[i]=val[k++];
        }
    }
    int k2=0;
    sort(all(val),greater<int>());
    fr1(i,n){
        if(l[i]==0){
            b[i]=val[k2++];
        }
    }
    int j1=0,j2=0;
    fr1(i,n){
        pref1[i]=pref1[i-1]+a[i];
        pref2[i]=pref2[i-1]+b[i];
        if(pref1[i]<0){
            j1=i;
        }
        if(pref2[i]<0){
            j2=i;
        }
    }
    if(j1<=j2){
        fr1(i, n)
        {
            cout << a[i] << " ";
        }
    }
    else{
        fr1(i, n)
        {
            cout << b[i] << " ";
        }
    }
    cout<<"\n";
}
int32_t main()
{
    fio;
    tc{
        solve();
    }
    printclock;
    return 0;
}


// Take care of mod becoming -ve

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define fro(i, s, e) for (auto i = s; i <= e; ++i)
#define int long long
#define ld long double
#define fr(i, n) for (auto i = 0; i < n; i++)
#define ff first
#define pf pop_front()
#define ss second
#define mp make_pair
#define vi std::vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define setbits(x) __builtin_popcountll(x)
#define zrbits(x) __builtin_ctzll(x)
#define vii std::vector<int, int>
#define tc    \
    int t;    \
    cin >> t; \
    while (t--)
#define FILE                          \
    freopen("input.txt", "r", stdin); \
    freopen("output.txt", "w", stdout)
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fio                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);
#define inp(a) \
    int a;     \
    cin >> a;
#define ps(x, y) fixed << setprecision(y) << x
#define mkarr(arr, n, type) type *arr = new type[n];
#define endl "\n"
#define rev(i, e, s) for (auto i = e; i >= s; --i)
const int mod = 1000000007;
#define min_pq priority_queue<int, std::vector<int>, greater<int>>
#define printclock cerr << "Time : " << 1000 * (ld)clock() / (ld)CLOCKS_PER_SEC << "ms\n";
using namespace std;
const int N = 100005;
const int inf = 1000000000000000000;
#define fit(s, x) memset(s, x, sizeof(s));
#define out(x) cout << x << "\n"
#define deb(x) cout << #x << x << "\n"
#define fr1(i, n) for (int i = 1; i <= n; i++)
#define pi 3.14159265358979323846264338327950
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    PBDS;
// Use __gcd(a,b) for gcd
// find(all(x),key) to get the iterator and its index is obtained by find(all(x),key)-x.begin()
// find_if(iterator_first,iterator_last,unary function) returns first iterator if any element corresponding to unary function exists and if no such element is found it retrurns the last iterator
/*For example:
find_if(all(x),isodd) for x being a vector{10,25,40,55}; *it returns 25*/
/*search(all(v1),all(v2),pred) searches for all the elements of v2 present in v1 based on pred function the iterator correspond
to the first element of sequence if it exists and if not then th elast iterator is obtained.*/
//use substr(size_t pos,size_t len) for the substrings
// iota(forward itr,backwrd itr,val) can be used for consecutive numbers
int power(int a, int b)
{
    long long ans = 1;
    while (b > 0)
    {
        if (b & 1LL)
        {
            ans = (ans * a) % mod;
        }
        a = (a * a) % mod;
        b = (b >> 1LL);
    }
    return ans;
}

vector<int>gr[N];
bool odd_cycle=0;
int vis[N]={0};

void solve(){
    int n;
    cin>>n;
    int a[n+1];
    fr1(i,n)cin>>a[i];
    int l[n+1];
    fr1(i,n)cin>>l[i];
    if(l[1]==1 && a[1]>=0){
        fr1(i,n){
            cout<<a[i]<<" ";
        }
        cout<<"\n";
        return;
    }
    vector<int>val;
    fr1(i,n){
        if(l[i]==0){
            val.push_back(a[i]);
        }
    }
    sort(all(val),greater<int>());
    int k=0;
    fr1(i,n){
        if(l[i]==0){
            a[i]=val[k++];
        }
    }
    fr1(i,n){
        cout<<a[i]<<" ";
    }
    cout<<"\n";
}
int32_t main()
{
    fio;
    tc{
        solve();
    }
    printclock;
    return 0;
}


// Take care of mod becoming -ve

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define fro(i, s, e) for (auto i = s; i <= e; ++i)
#define int long long
#define ld long double
#define fr(i, n) for (auto i = 0; i < n; i++)
#define ff first
#define pf pop_front()
#define ss second
#define mp make_pair
#define vi std::vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define setbits(x) __builtin_popcountll(x)
#define zrbits(x) __builtin_ctzll(x)
#define vii std::vector<int, int>
#define tc    \
    int t;    \
    cin >> t; \
    while (t--)
#define FILE                          \
    freopen("input.txt", "r", stdin); \
    freopen("output.txt", "w", stdout)
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fio                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);
#define inp(a) \
    int a;     \
    cin >> a;
#define ps(x, y) fixed << setprecision(y) << x
#define mkarr(arr, n, type) type *arr = new type[n];
#define endl "\n"
#define rev(i, e, s) for (auto i = e; i >= s; --i)
const int mod = 1000000007;
#define min_pq priority_queue<int, std::vector<int>, greater<int>>
#define printclock cerr << "Time : " << 1000 * (ld)clock() / (ld)CLOCKS_PER_SEC << "ms\n";
using namespace std;
const int N = 100005;
const int inf = 1000000000000000000;
#define fit(s, x) memset(s, x, sizeof(s));
#define out(x) cout << x << "\n"
#define deb(x) cout << #x << x << "\n"
#define fr1(i, n) for (int i = 1; i <= n; i++)
#define pi 3.14159265358979323846264338327950
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    PBDS;
// Use __gcd(a,b) for gcd
// find(all(x),key) to get the iterator and its index is obtained by find(all(x),key)-x.begin()
// find_if(iterator_first,iterator_last,unary function) returns first iterator if any element corresponding to unary function exists and if no such element is found it retrurns the last iterator
/*For example:
find_if(all(x),isodd) for x being a vector{10,25,40,55}; *it returns 25*/
/*search(all(v1),all(v2),pred) searches for all the elements of v2 present in v1 based on pred function the iterator correspond
to the first element of sequence if it exists and if not then th elast iterator is obtained.*/
//use substr(size_t pos,size_t len) for the substrings
// iota(forward itr,backwrd itr,val) can be used for consecutive numbers
int power(int a, int b)
{
    long long ans = 1;
    while (b > 0)
    {
        if (b & 1LL)
        {
            ans = (ans * a) % mod;
        }
        a = (a * a) % mod;
        b = (b >> 1LL);
    }
    return ans;
}

vector<int>gr[N];
bool odd_cycle=0;
int vis[N]={0};
bool cmp(int a,int b){
    if(a<0 && b>=0){
        return true;
    }
    else if(a<0 && b<0){
        return abs(a)>=abs(b);
    }
    else if(a>=0 && b<0){
        return false;
    }
    return a>=b;
}
void solve(){
    int n;
    cin>>n;
    int a[n+1];
    fr1(i,n)cin>>a[i];
    int l[n+1];
    fr1(i,n)cin>>l[i];
    // if(l[1]==1 && a[1]>=0){
    //     fr1(i,n){
    //         cout<<a[i]<<" ";
    //     }
    //     cout<<"\n";
    //     return;
    // }
    // int b[n+1];
    // fr1(i,n){
    //     b[i]=a[i];
    // }
    // int pref1[n+1],pref2[n+1];
    // pref1[0]=0;
    // pref2[0]=0;
    vector<int>val;
    fr1(i,n){
        if(l[i]==0){
            val.push_back(a[i]);
        }
    }
    sort(all(val),[](const auto& a,const auto& b){
           return a>=b?true:false;
    });
    int k=0;
    fr1(i,n){
        if(l[i]==0){
            a[i]=val[k++];
        }
    }
    fr1(i,n){
        cout<<a[i]<<" ";
    }
    cout<<"\n";
}
int32_t main()
{
    fio;
    tc{
        solve();
    }
    printclock;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 1e18
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define pb push_back
#define ff first
#define ss second
#define vll vector<ll>
#define pii pair<ll,ll>
#define sz(v) (v).size()
#define srt(v) sort(v.begin(),v.end())
#define ull unsigned long long
#define tell(a) cout<<a<<" "
#define el cout<<endl
#define rev(v) reverse(v.begin(),v.end())
#define rep(i,a,n) for(int i=a;i<n;i++)
#define ld long double
#define mem(a,b) memset(a,b,sizeof(a) )
#define M 2005
#define md 1000000007

ll power(ll x, ll y, ll p){ll res = 1;x = x % p;while (y > 0){   if (y & 1)res = (res*x) % p;y = y>>1;
x = (x*x) % p;}return res;}
//inline ll add(ll a,ll b){return (a+b)%md;}
//inline ll mul(ll a,ll b){return (a*b)%md;}
//ll f[M],iv[M];
//ll C(ll n,ll r){return mul(f[n],mul(iv[r],iv[n-r]));}
//void prep_fac(){
//    f[0]=1;
//    rep(i,1,M)f[i]=mul(i,f[i-1]);
//    iv[M-1]=power(f[M-1],md-2,md);
//    for(ll i=M-2;i>=0;--i)iv[i]=mul(i+1,iv[i+1]);
//}

int main()
{
	IOS;
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		ll a[n+1],b[n+1];
		ll sum=0,f=1;
		vll v;
		for(int i=0;i<n;i++){
		cin>>a[i+1],sum+=a[i+1];if(a[i+1]<0)f=0;}
		for(int j=0;j<n;j++){
			cin>>b[j+1];
			if(!b[j+1])v.pb(a[j+1]);
		}
		if(f||sum<0){
			for(int i=0;i<n;i++)cout<<a[i+1]<<" ";
			cout<<endl;
			continue;
		}
		srt(v);
		int j=v.size()-1;
		for(int i=0;i<n;i++)
		{
			if(!b[i+1]){
				cout<<v[j--]<<" ";
			}
			else
			cout<<a[i+1]<<" ";
		}
			cout<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 1e18
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define pb push_back
#define ff first
#define ss second
#define vll vector<ll>
#define pii pair<ll,ll>
#define sz(v) (v).size()
#define srt(v) sort(v.begin(),v.end())
#define ull unsigned long long
#define tell(a) cout<<a<<" "
#define el cout<<endl
#define rev(v) reverse(v.begin(),v.end())
#define rep(i,a,n) for(int i=a;i<n;i++)
#define ld long double
#define mem(a,b) memset(a,b,sizeof(a) )
#define M 2005
#define md 1000000007

ll power(ll x, ll y, ll p){ll res = 1;x = x % p;while (y > 0){   if (y & 1)res = (res*x) % p;y = y>>1;
x = (x*x) % p;}return res;}
//inline ll add(ll a,ll b){return (a+b)%md;}
//inline ll mul(ll a,ll b){return (a*b)%md;}
//ll f[M],iv[M];
//ll C(ll n,ll r){return mul(f[n],mul(iv[r],iv[n-r]));}
//void prep_fac(){
//    f[0]=1;
//    rep(i,1,M)f[i]=mul(i,f[i-1]);
//    iv[M-1]=power(f[M-1],md-2,md);
//    for(ll i=M-2;i>=0;--i)iv[i]=mul(i+1,iv[i+1]);
//}

int main()
{
	IOS;
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		ll a[n+1],b[n+1];
		ll sum=0,f=1;
		vll v;
		for(int i=0;i<n;i++){
		cin>>a[i+1],sum+=a[i+1];if(a[i+1]<0)f=0;}
		for(int j=0;j<n;j++){
			cin>>b[i+1];
			if(!b[i+1])v.pb(a[i+1]);
		}
		if(f||sum<0){
			for(int i=0;i<n;i++)cout<<a[i+1]<<" ";
			cout<<endl;
			continue;
		}
		srt(v);
		int j=0;
		for(int i=0;i<n;i++){
			if(!b[i+1])a[i+1]=v[j++];
		}
		for(int i=0;i<n;i++)
		cout<<a[i+1]<<" ";
		el;

	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 1e18
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define pb push_back
#define ff first
#define ss second
#define vll vector<ll>
#define pii pair<ll,ll>
#define sz(v) (v).size()
#define srt(v) sort(v.begin(),v.end())
#define ull unsigned long long
#define tell(a) cout<<a<<" "
#define el cout<<endl
#define rev(v) reverse(v.begin(),v.end())
#define rep(i,a,n) for(int i=a;i<n;i++)
#define ld long double
#define mem(a,b) memset(a,b,sizeof(a) )
#define M 2005
#define md 1000000007

ll power(ll x, ll y, ll p){ll res = 1;x = x % p;while (y > 0){   if (y & 1)res = (res*x) % p;y = y>>1;
x = (x*x) % p;}return res;}
//inline ll add(ll a,ll b){return (a+b)%md;}
//inline ll mul(ll a,ll b){return (a*b)%md;}
//ll f[M],iv[M];
//ll C(ll n,ll r){return mul(f[n],mul(iv[r],iv[n-r]));}
//void prep_fac(){
//    f[0]=1;
//    rep(i,1,M)f[i]=mul(i,f[i-1]);
//    iv[M-1]=power(f[M-1],md-2,md);
//    for(ll i=M-2;i>=0;--i)iv[i]=mul(i+1,iv[i+1]);
//}

int main()
{
	IOS;
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		ll a[n+1],b[n+1];
		ll sum=0,f=1;
		vll v;
		for(int i=0;i<n;i++){
		cin>>a[i+1],sum+=a[i+1];if(a[i+1]<0)f=0;}
		for(int j=0;j<n;j++){
			cin>>b[j+1];
			if(!b[j+1])v.pb(a[j+1]);
		}
		if(f||sum<0){
			for(int i=0;i<n;i++)cout<<a[i+1]<<" ";
			cout<<endl;
			continue;
		}
		srt(v);
		int j=n-1,i=0;
		int sum=0;
		for(int k=1;k<=n;k++){
			if(!b[k]){
				if(sum<0){
				if(sum+v[i]>0)sum+=v[i],i++,a[k]=v[i-1];
				else
				sum+=v[j],j--,a[k]=v[j+1];
				}
				else
				{
				if(sum+v[i]>0)sum+=v[i],i++,a[k]=v[i-1];
				else
				sum+=v[j],j--,a[k]=v[j+1];
				}
			}
			else
			sum+=a[k];

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


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 1e18
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define pb push_back
#define ff first
#define ss second
#define vll vector<ll>
#define pii pair<ll,ll>
#define sz(v) (v).size()
#define srt(v) sort(v.begin(),v.end())
#define ull unsigned long long
#define tell(a) cout<<a<<" "
#define el cout<<endl
#define rev(v) reverse(v.begin(),v.end())
#define rep(i,a,n) for(int i=a;i<n;i++)
#define ld long double
#define mem(a,b) memset(a,b,sizeof(a) )
#define M 2005
#define md 1000000007

ll power(ll x, ll y, ll p){ll res = 1;x = x % p;while (y > 0){   if (y & 1)res = (res*x) % p;y = y>>1;
x = (x*x) % p;}return res;}
//inline ll add(ll a,ll b){return (a+b)%md;}
//inline ll mul(ll a,ll b){return (a*b)%md;}
//ll f[M],iv[M];
//ll C(ll n,ll r){return mul(f[n],mul(iv[r],iv[n-r]));}
//void prep_fac(){
//    f[0]=1;
//    rep(i,1,M)f[i]=mul(i,f[i-1]);
//    iv[M-1]=power(f[M-1],md-2,md);
//    for(ll i=M-2;i>=0;--i)iv[i]=mul(i+1,iv[i+1]);
//}

int main()
{
	IOS;
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		ll a[n+1],b[n+1];
		ll sum=0,f=1;
		vll v;
		for(int i=0;i<n;i++){
		cin>>a[i+1],sum+=a[i+1];if(a[i+1]<0)f=0;}
		for(int j=0;j<n;j++){
			cin>>b[j+1];
			if(!b[j+1])v.pb(a[j+1]);
		}
		if(f||sum<0){
			for(int i=0;i<n;i++)cout<<a[i+1]<<" ";
			cout<<endl;
			continue;
		}
		srt(v);
		int j=0;
		for(int i=0;i<n;i++){
			if(!b[i+1])a[i+1]=v[j++];
		}
		for(int i=0;i<n;i++)
		cout<<a[i+1]<<" ";
		el;

	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 1e18
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define pb push_back
#define ff first
#define ss second
#define vll vector<ll>
#define pii pair<ll,ll>
#define sz(v) (v).size()
#define srt(v) sort(v.begin(),v.end())
#define ull unsigned long long
#define tell(a) cout<<a<<" "
#define el cout<<endl
#define rev(v) reverse(v.begin(),v.end())
#define rep(i,a,n) for(int i=a;i<n;i++)
#define ld long double
#define mem(a,b) memset(a,b,sizeof(a) )
#define M 2005
#define md 1000000007

ll power(ll x, ll y, ll p){ll res = 1;x = x % p;while (y > 0){   if (y & 1)res = (res*x) % p;y = y>>1;
x = (x*x) % p;}return res;}
//inline ll add(ll a,ll b){return (a+b)%md;}
//inline ll mul(ll a,ll b){return (a*b)%md;}
//ll f[M],iv[M];
//ll C(ll n,ll r){return mul(f[n],mul(iv[r],iv[n-r]));}
//void prep_fac(){
//    f[0]=1;
//    rep(i,1,M)f[i]=mul(i,f[i-1]);
//    iv[M-1]=power(f[M-1],md-2,md);
//    for(ll i=M-2;i>=0;--i)iv[i]=mul(i+1,iv[i+1]);
//}

int main()
{
	IOS;
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		ll a[n+1],b[n+1];
		ll sum=0,f=1;
		vll v;
		for(int i=0;i<n;i++){
		cin>>a[i+1],sum+=a[i+1];if(a[i+1]<0)f=0;}
		for(int j=0;j<n;j++){
			cin>>b[j+1];
			if(!b[j+1])v.pb(a[j+1]);
		}
		if(f||sum<0){
			for(int i=0;i<n;i++)cout<<a[i+1]<<" ";
			cout<<endl;
			continue;
		}
		srt(v);
		int j=n-1,i=0;
		 sum=0;
		for(int k=1;k<=n;k++){
			if(!b[k]){
				if(sum<0){
				if(sum+v[i]>0)sum+=v[i],i++,a[k]=v[i-1];
				else
				sum+=v[j],j--,a[k]=v[j+1];
				}
				else
				{
				if(sum+v[i]>0)sum+=v[i],i++,a[k]=v[i-1];
				else
				sum+=v[j],j--,a[k]=v[j+1];
				}
			}
			else
			sum+=a[k];

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


#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define si(n) scanf("%d",&n);
#define PI acos(-1.0)
#define sl(n) scanf("%lld",&n);
#define sf(n) scanf("%lf",&n);
#define ss(n) scanf("%s",n);
#define for0(i,n) for(ll i=0;i<n;i++)
#define for1(i,n) for(ll i=1;i<=n;i++)
#define forc(i,j,n) for(ll i=j;i<n;i++)
#define pb push_back
#define sor(n) sort(n.begin(),n.end())
#define sorcmp(n) sort(n.begin(),n.end(),cmp)
#define rev(n) reverse(n.begin(),n.end())
#define pi(x)  printf("%d",x);
#define pl(x)  printf("%lld",x);
#define pf(x)  printf("%.10lf",x);
#define ps(x)  printf("%s",x);
#define en() printf("\n");
#define random_shuffle(r...)random_shuffle(r,[](int _){return rand()%_;})
#define SQR(a) (a)*(a)
#define sp(x) fixed<<setprecision(x)
#define mod 1000000007
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define bug cout<<"Here"<<endl
void fastio(){ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}
template <typename T> T POW(T B,T P){ if(P==0) return 1; if(P&1) return B*POW(B,P-1);  else return SQR(POW(B,P/2));}
ll powmod(ll a, ll b){ll ans = 1;while(b){if(b&1) ans = ans*a%mod;a = a*a%mod;b >>= 1;}return ans;}

bool cmp(pii a,pii b)
{
    if(a.first<b.first ) return true;
    else return false;
}

int main()
{
	int t;
	cin>>t;
	while(t--)
    {
        int n;
        cin>>n;
        int arr[n];
        int ans[n];

        for(int i=0;i<n;i++) ans[i]=INT_MAX;

        for(int i=0;i<n;i++) cin>>arr[i];
        int brr[n];
        deque<int>v;
        vector<pii>res;
        for(int i=0;i<n;i++)
        {
            cin>>brr[i];

            if(brr[i]==0) v.pb(arr[i]);
            else
            {
                ans[i]=arr[i];
            }
        }

        sor(v);
        rev(v);
        int sum=0;
        int st=INT_MAX,ed=-1;
        for(int i=0;i<n;i++)
        {
            if(brr[i])
            {
                st=min(st,i);
                ed=max(ed,i);

                sum+=arr[i];

                if(i==n-1)
                {
                    res.pb({sum,ed});
                }

            }
            else
            {
                int ps=INT_MAX, ped=-1;

                if( ed==-1) continue;
                res.pb({sum,ed});
                //cout<<sum<<" "<<ed<<endl;
                sum=0;
                ed=-1;
            }
        }

        sorcmp(res);

        for(auto u:res)
        {
            ed=u.second+1;
            sum=u.first;
            int k=0;
           // cout<<ed<<" "<<sum<<endl;
           // cout<<"VEc : "<<endl;
           //// for(auto g:v) cout<<g<<" ";
           // cout<<endl;
            for(int i=ed;i<n;i++)
            {
                if(brr[i]==1) break;
                 else if(sum+v[k]<sum)
                {

                    break;
                }
                if(sum+v[k]>=0 )
                {
                    sum+=v[k];
                    v.pop_front();
                    break;
                }

                else
                {
                    sum+=v[k];
                    ans[i]=v[k];
                    v.pop_front();
                }
            }
        }

        int k=0;
       sor(v);
       rev(v);
        for(int i=0;i<n;i++)
        {
           // cout<<ans[i]<<" ";
            if(ans[i]==INT_MAX)
            {
                ans[i]=v[k];
                k++;
            }
        }
       // cout<<endl;

        for(auto u:ans) cout<<u<<" ";
        cout<<endl;

    }

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define si(n) scanf("%d",&n);
#define PI acos(-1.0)
#define sl(n) scanf("%lld",&n);
#define sf(n) scanf("%lf",&n);
#define ss(n) scanf("%s",n);
#define for0(i,n) for(ll i=0;i<n;i++)
#define for1(i,n) for(ll i=1;i<=n;i++)
#define forc(i,j,n) for(ll i=j;i<n;i++)
#define pb push_back
#define sor(n) sort(n.begin(),n.end())
#define sorcmp(n) sort(n.begin(),n.end(),cmp)
#define rev(n) reverse(n.begin(),n.end())
#define pi(x)  printf("%d",x);
#define pl(x)  printf("%lld",x);
#define pf(x)  printf("%.10lf",x);
#define ps(x)  printf("%s",x);
#define en() printf("\n");
#define random_shuffle(r...)random_shuffle(r,[](int _){return rand()%_;})
#define SQR(a) (a)*(a)
#define sp(x) fixed<<setprecision(x)
#define mod 1000000007
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define bug cout<<"Here"<<endl
void fastio(){ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}
template <typename T> T POW(T B,T P){ if(P==0) return 1; if(P&1) return B*POW(B,P-1);  else return SQR(POW(B,P/2));}
ll powmod(ll a, ll b){ll ans = 1;while(b){if(b&1) ans = ans*a%mod;a = a*a%mod;b >>= 1;}return ans;}

int main()
{
	int t;
	cin>>t;
	while(t--)
    {
        int n;
        cin>>n;
        int arr[n];
        int ans[n];

        for(int i=0;i<n;i++) ans[i]=INT_MAX;

        for(int i=0;i<n;i++) cin>>arr[i];
        int brr[n];
        deque<int>v;
        for(int i=0;i<n;i++)
        {
            cin>>brr[i];

            if(brr[i]==0) v.pb(arr[i]);
            else
            {
                ans[i]=arr[i];
            }
        }

        sor(v);
        rev(v);

        for(int i=0;i<n;i++)
        {
            if(brr[i])
            {
                if(arr[i]>=0) continue;
                int tot=0;
                int j;
                for( j=i;j<n;j++)
                {
                    if(brr[j]) tot+=arr[j];
                    else break;
                    if(j==n-1)
                    {
                        j=n;
                        break;
                    }
                }
                int k=j;
                if(tot>=0) { i=j-1; continue;}
                int temp=abs(tot);
                if(j==n)
                {
                    break;
                }

                int lst=0;

                for(int j=0;j<v.size();j++ )

                {
                    if(tot+v[j]>=0)
                    {
                        tot+=v[j];
                        lst=j;

                        break;
                    }
                    else if(tot+v[j]<tot) break;
                    else tot+=v[j],lst=j;

                    if(tot>=0 ) break;
                }

                lst++;

                while(lst--)
                {
                    ans[k]=v[0];
                    v.pop_front();
                    k++;
                }

            }
        }
        int k=0;
        for(int i=0;i<n;i++)
        {
            if(ans[i]==INT_MAX)
            {
                ans[i]=v[k];
                k++;
            }
        }

        for(auto u:ans) cout<<u<<" ";
        cout<<endl;

    }

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define si(n) scanf("%d",&n);
#define PI acos(-1.0)
#define sl(n) scanf("%lld",&n);
#define sf(n) scanf("%lf",&n);
#define ss(n) scanf("%s",n);
#define for0(i,n) for(ll i=0;i<n;i++)
#define for1(i,n) for(ll i=1;i<=n;i++)
#define forc(i,j,n) for(ll i=j;i<n;i++)
#define pb push_back
#define sor(n) sort(n.begin(),n.end())
#define sorcmp(n) sort(n.begin(),n.end(),cmp)
#define rev(n) reverse(n.begin(),n.end())
#define pi(x)  printf("%d",x);
#define pl(x)  printf("%lld",x);
#define pf(x)  printf("%.10lf",x);
#define ps(x)  printf("%s",x);
#define en() printf("\n");
#define random_shuffle(r...)random_shuffle(r,[](int _){return rand()%_;})
#define SQR(a) (a)*(a)
#define sp(x) fixed<<setprecision(x)
#define mod 1000000007
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define bug cout<<"Here"<<endl
void fastio(){ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}
template <typename T> T POW(T B,T P){ if(P==0) return 1; if(P&1) return B*POW(B,P-1);  else return SQR(POW(B,P/2));}
ll powmod(ll a, ll b){ll ans = 1;while(b){if(b&1) ans = ans*a%mod;a = a*a%mod;b >>= 1;}return ans;}

int main()
{
	int t;
	cin>>t;
	while(t--)
    {
        int n;
        cin>>n;
        int arr[n];
        int ans[n];

        for(int i=0;i<n;i++) ans[i]=INT_MAX;

        for(int i=0;i<n;i++) cin>>arr[i];
        int brr[n];
        deque<int>v;
        for(int i=0;i<n;i++)
        {
            cin>>brr[i];

            if(brr[i]==0) v.pb(arr[i]);
            else
            {
                ans[i]=arr[i];
            }
        }

        sor(v);
        rev(v);

        for(int i=0;i<n;i++)
        {
            if(brr[i])
            {
                if(arr[i]>=0) continue;
                int tot=0;
                int j;
                for( j=i;j<n;j++)
                {
                    if(brr[j]) tot+=arr[j];
                    else break;
                    if(j==n-1)
                    {
                        j=n;
                        break;
                    }
                }
                int k=j;
                if(tot>=0) { i=j-1; continue;}
                int temp=abs(tot);
                if(j==n)
                {
                    break;
                }

                int lst=0;

                for(int j=0;j<v.size();j++ )

                {
                    if(tot+v[j]>=0)
                    {
                        tot+=v[j];
                        lst=j;

                        break;
                    }
                    else if(tot+v[j]<tot) break;
                    else tot+=v[j],lst=j;
                }

                lst++;

                while(lst--)
                {
                    ans[k]=v[0];
                    v.pop_front();
                    k++;
                }

            }
        }
        int k=0;
        for(int i=0;i<n;i++)
        {
            if(ans[i]==INT_MAX)
            {
                ans[i]=v[k];
                k++;
            }
        }

        for(auto u:ans) cout<<u<<" ";
        cout<<endl;

    }

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define si(n) scanf("%d",&n);
#define PI acos(-1.0)
#define sl(n) scanf("%lld",&n);
#define sf(n) scanf("%lf",&n);
#define ss(n) scanf("%s",n);
#define for0(i,n) for(ll i=0;i<n;i++)
#define for1(i,n) for(ll i=1;i<=n;i++)
#define forc(i,j,n) for(ll i=j;i<n;i++)
#define pb push_back
#define sor(n) sort(n.begin(),n.end())
#define sorcmp(n) sort(n.begin(),n.end(),cmp)
#define rev(n) reverse(n.begin(),n.end())
#define pi(x)  printf("%d",x);
#define pl(x)  printf("%lld",x);
#define pf(x)  printf("%.10lf",x);
#define ps(x)  printf("%s",x);
#define en() printf("\n");
#define random_shuffle(r...)random_shuffle(r,[](int _){return rand()%_;})
#define SQR(a) (a)*(a)
#define sp(x) fixed<<setprecision(x)
#define mod 1000000007
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define bug cout<<"Here"<<endl
void fastio(){ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}
template <typename T> T POW(T B,T P){ if(P==0) return 1; if(P&1) return B*POW(B,P-1);  else return SQR(POW(B,P/2));}
ll powmod(ll a, ll b){ll ans = 1;while(b){if(b&1) ans = ans*a%mod;a = a*a%mod;b >>= 1;}return ans;}

bool cmp(pii a,pii b)
{
    if(a.first<b.first ) return true;
    else return false;
}

int main()
{
	int t;
	cin>>t;
	while(t--)
    {
        int n;
        cin>>n;
        int arr[n];
        int ans[n];

        for(int i=0;i<n;i++) ans[i]=INT_MAX;

        for(int i=0;i<n;i++) cin>>arr[i];
        int brr[n];
        deque<int>v;
        vector<pii>res;
        for(int i=0;i<n;i++)
        {
            cin>>brr[i];

            if(brr[i]==0) v.pb(arr[i]);
            else
            {
                ans[i]=arr[i];
            }
        }

        sor(v);
        rev(v);
        int k=0;
        for(int i=0;i<n;i++)
        {
            if(ans[i]==INT_MAX)
            {
                ans[i]=v[k++];
            }
        }

        for(auto u:ans)cout<<u<<" ";
        cout<<endl;

    }

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define si(n) scanf("%d",&n);
#define PI acos(-1.0)
#define sl(n) scanf("%lld",&n);
#define sf(n) scanf("%lf",&n);
#define ss(n) scanf("%s",n);
#define for0(i,n) for(ll i=0;i<n;i++)
#define for1(i,n) for(ll i=1;i<=n;i++)
#define forc(i,j,n) for(ll i=j;i<n;i++)
#define pb push_back
#define sor(n) sort(n.begin(),n.end())
#define sorcmp(n) sort(n.begin(),n.end(),cmp)
#define rev(n) reverse(n.begin(),n.end())
#define pi(x)  printf("%d",x);
#define pl(x)  printf("%lld",x);
#define pf(x)  printf("%.10lf",x);
#define ps(x)  printf("%s",x);
#define en() printf("\n");
#define random_shuffle(r...)random_shuffle(r,[](int _){return rand()%_;})
#define SQR(a) (a)*(a)
#define sp(x) fixed<<setprecision(x)
#define mod 1000000007
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define bug cout<<"Here"<<endl
void fastio(){ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}
template <typename T> T POW(T B,T P){ if(P==0) return 1; if(P&1) return B*POW(B,P-1);  else return SQR(POW(B,P/2));}
ll powmod(ll a, ll b){ll ans = 1;while(b){if(b&1) ans = ans*a%mod;a = a*a%mod;b >>= 1;}return ans;}

bool cmp(pii a,pii b)
{
    if(a.first<b.first ) return true;
    else return false;
}

int main()
{
	int t;
	cin>>t;
	while(t--)
    {
        int n;
        cin>>n;
        int arr[n];
        int ans[n];

        for(int i=0;i<n;i++) ans[i]=INT_MAX;

        for(int i=0;i<n;i++) cin>>arr[i];
        int brr[n];
        deque<int>v;
        vector<pii>res;
        for(int i=0;i<n;i++)
        {
            cin>>brr[i];

            if(brr[i]==0) v.pb(arr[i]);
            else
            {
                ans[i]=arr[i];
            }
        }

        sor(v);
        rev(v);
        int sum=0;
        int st=INT_MAX,ed=-1;
        for(int i=0;i<n;i++)
        {
            if(brr[i])
            {
                st=min(st,i);
                ed=max(ed,i);

                sum+=arr[i];

                if(i==n-1)
                {
                    res.pb({sum,ed});
                }

            }
            else
            {
                int ps=INT_MAX, ped=-1;

                if( ed==-1) continue;
                res.pb({sum,ed});
                //cout<<sum<<" "<<ed<<endl;
                sum=0;
                ed=-1;
            }
        }

        sorcmp(res);

        for(auto u:res)
        {
            ed=u.second+1;
            sum=u.first;
            int k=0;
            for(int i=ed;i<n;i++)
            {
                if(brr[i]==1) break;
                if(sum+v[k]>=0)
                {
                    sum+=v[k];
                    v.pop_front();
                    break;
                }
                if(sum+v[k]<sum)
                {
                    break;
                }
                else
                {
                    sum+=v[k];
                    ans[i]=v[k];
                    v.pop_front();
                }
            }
        }

        int k=0;
        sor(v);
        for(int i=0;i<n;i++)
        {
            if(ans[i]==INT_MAX)
            {
                ans[i]=v[k];
                k++;
            }
        }

        for(auto u:ans) cout<<u<<" ";
        cout<<endl;

    }

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define si(n) scanf("%d",&n);
#define PI acos(-1.0)
#define sl(n) scanf("%lld",&n);
#define sf(n) scanf("%lf",&n);
#define ss(n) scanf("%s",n);
#define for0(i,n) for(ll i=0;i<n;i++)
#define for1(i,n) for(ll i=1;i<=n;i++)
#define forc(i,j,n) for(ll i=j;i<n;i++)
#define pb push_back
#define sor(n) sort(n.begin(),n.end())
#define sorcmp(n) sort(n.begin(),n.end(),cmp)
#define rev(n) reverse(n.begin(),n.end())
#define pi(x)  printf("%d",x);
#define pl(x)  printf("%lld",x);
#define pf(x)  printf("%.10lf",x);
#define ps(x)  printf("%s",x);
#define en() printf("\n");
#define random_shuffle(r...)random_shuffle(r,[](int _){return rand()%_;})
#define SQR(a) (a)*(a)
#define sp(x) fixed<<setprecision(x)
#define mod 1000000007
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define bug cout<<"Here"<<endl
void fastio(){ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}
template <typename T> T POW(T B,T P){ if(P==0) return 1; if(P&1) return B*POW(B,P-1);  else return SQR(POW(B,P/2));}
ll powmod(ll a, ll b){ll ans = 1;while(b){if(b&1) ans = ans*a%mod;a = a*a%mod;b >>= 1;}return ans;}

bool cmp(pii a,pii b)
{
    if(a.first<b.first ) return true;
    else return false;
}

int main()
{
	int t;
	cin>>t;
	while(t--)
    {
        int n;
        cin>>n;
        int arr[n];
        int ans[n];

        for(int i=0;i<n;i++) ans[i]=INT_MAX;

        for(int i=0;i<n;i++) cin>>arr[i];
        int brr[n];
        deque<int>v;
        vector<pii>res;
        for(int i=0;i<n;i++)
        {
            cin>>brr[i];

            if(brr[i]==0) v.pb(arr[i]);
            else
            {
                ans[i]=arr[i];
            }
        }

        sor(v);
        rev(v);
        int sum=0;
        int st=INT_MAX,ed=-1;
        for(int i=0;i<n;i++)
        {
            if(brr[i])
            {
                st=min(st,i);
                ed=max(ed,i);

                sum+=arr[i];

                if(i==n-1)
                {
                    res.pb({sum,ed});
                }

            }
            else
            {
                int ps=INT_MAX, ped=-1;

                if( ed==-1) continue;
                res.pb({sum,ed});
                //cout<<sum<<" "<<ed<<endl;
                sum=0;
                ed=-1;
            }
        }

        sorcmp(res);

        for(auto u:res)
        {
            ed=u.second+1;
            sum=u.first;
            int k=0;
           // cout<<ed<<" "<<sum<<endl;
           // cout<<"VEc : "<<endl;
           //// for(auto g:v) cout<<g<<" ";
           // cout<<endl;
            for(int i=ed;i<n;i++)
            {
                if(brr[i]==1) break;
                  if(sum+v[k]<sum)
                {

                    break;
                }
                if(sum+v[k]>=0 )
                {
                    sum+=v[k];
                    ans[i]=v[k];
                    v.pop_front();
                    break;
                }

                else
                {
                    sum+=v[k];
                    ans[i]=v[k];
                    v.pop_front();
                }
            }
        }

        int k=0;
       sor(v);
       rev(v);
        for(int i=0;i<n;i++)
        {
           // cout<<ans[i]<<" ";
            if(ans[i]==INT_MAX)
            {
                ans[i]=v[k];
                k++;
            }
        }
       // cout<<endl;

        for(auto u:ans) cout<<u<<" ";
        cout<<endl;

    }

	return 0;
}


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

#define ll long long
#define pb push_back
#define pii pair<int, int>
#define sz size()

#ifdef LOCAL
template<class T1, class T2>
ostream &operator<<(ostream &os,const pair<T1,T2> &p){os<<'{'<<p.first<<", "<<p.second<<'}';return os;}
#define trace(...) __f(#__VA_ARGS__,__VA_ARGS__)
template<typename Arg1>void __f(const char* name,Arg1&& arg1){cerr<<name<<" : "<<arg1<<"\n";}
template<typename Arg1,typename... Args>void __f(const char* names,Arg1&& arg1,Args&&... args)
{const char* comma=strchr(names+1,',');cerr.write(names,comma-names)<<" : "<<arg1<<" , ";__f(comma+1, args...);}
#define traceloop(x,a){cerr<<#a<<": ";for(ll _=0;_<x;_++)cerr<<a[_]<<" ";cerr<<"\n";}
#else
#define trace(...) 69
#define traceloop(...) 69
#endif

int main()
{
	ios::sync_with_stdio(0);
	int i, j, k, n, t;
	cin >> t;
	while(t--)
	{
		cin >> n;
		vector<int> a(n), b(n), take;
		for(i=0;i<n;++i)
			cin >> a[i];
		for(i=0;i<n;++i)
		{
			cin >> b[i];
			if(b[i] == 0)
				take.pb(a[i]);
		}

		sort(take.begin(), take.end());
		vector<int> ans;
		ans = a;

		int best = n;
		int m = take.sz;
		for(j=0;j<m;++j)
		{
			k = 0;
			vector<int> temp(n);
			for(i=0;i<n;++i)
			{
				if(b[i])
					temp[i] = a[i];
				else
				{
					temp[i] = take[(k + j) % m];
					k++;
				}
			}

			int curr = n;
			int pre = 0;
			for(i=0;i<n;++i)
			{
				pre += temp[i];
				if(pre < 0)
					curr = i;
			}

			trace(j, curr);
			traceloop(n, temp);

			if(curr == n)
				curr = 0;
			if(curr < best)
			{
				ans = temp;
				best = curr;
			}
		}

		for(auto x : ans)
			cout << x << " ";
		cout << "\n";
	}

	return 0;
}


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

#define ll long long
#define pb push_back
#define pii pair<int, int>
#define sz size()

int main()
{
	ios::sync_with_stdio(0);
	int i, j, k, n, t;
	cin >> t;
	while(t--)
	{
		cin >> n;
		vector<int> a(n), b(n), ans(n), take;
		for(i=0;i<n;++i)
			cin >> a[i];
		for(i=0;i<n;++i)
		{
			cin >> b[i];
			if(b[i] == 0)
				take.pb(a[i]);
		}

		sort(take.begin(), take.end());

		j = 0;
		for(i=0;i<n;++i)
		{
			if(b[i])
				ans[i] = a[i];
			else
				ans[i] = take[j++];
			cout << ans[i] << " ";
		}
		cout << "\n";
	}

	return 0;
}


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

#define ll long long
#define pb push_back
#define pii pair<int, int>
#define sz size()

int main()
{
	ios::sync_with_stdio(0);
	int i, j, k, n, t;
	cin >> t;
	while(t--)
	{
		cin >> n;
		vector<int> a(n), b(n), ans(n), take;
		for(i=0;i<n;++i)
			cin >> a[i];
		for(i=0;i<n;++i)
		{
			cin >> b[i];
			if(b[i] == 0)
				take.pb(a[i]);
		}

		sort(take.rbegin(), take.rend());

		j = 0;
		for(i=0;i<n;++i)
		{
			if(b[i])
				ans[i] = a[i];
			else
				ans[i] = take[j++];
			cout << ans[i] << " ";
		}
		cout << "\n";
	}

	return 0;
}


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

#define ll long long
#define pb push_back
#define pii pair<int, int>
#define sz size()

#ifdef LOCAL
template<class T1, class T2>
ostream &operator<<(ostream &os,const pair<T1,T2> &p){os<<'{'<<p.first<<", "<<p.second<<'}';return os;}
#define trace(...) __f(#__VA_ARGS__,__VA_ARGS__)
template<typename Arg1>void __f(const char* name,Arg1&& arg1){cerr<<name<<" : "<<arg1<<"\n";}
template<typename Arg1,typename... Args>void __f(const char* names,Arg1&& arg1,Args&&... args)
{const char* comma=strchr(names+1,',');cerr.write(names,comma-names)<<" : "<<arg1<<" , ";__f(comma+1, args...);}
#define traceloop(x,a){cerr<<#a<<": ";for(ll _=0;_<x;_++)cerr<<a[_]<<" ";cerr<<"\n";}
#else
#define trace(...) 69
#define traceloop(...) 69
#endif

int main()
{
	ios::sync_with_stdio(0);
	int i, j, k, n, t;
	cin >> t;
	while(t--)
	{
		cin >> n;
		vector<int> a(n), b(n), take;
		for(i=0;i<n;++i)
			cin >> a[i];
		for(i=0;i<n;++i)
		{
			cin >> b[i];
			if(b[i] == 0)
				take.pb(a[i]);
		}

		sort(take.begin(), take.end());
		vector<int> ans;
		ans = a;

		int best = n;
		int m = take.sz;
		for(j=0;j<=m;++j)
		{
			k = (j == 0 ? m-1 : 0);
			vector<int> temp(n);
			for(i=0;i<n;++i)
			{
				if(b[i])
					temp[i] = a[i];
				else
				{
					temp[i] = take[k];

					if(k < j)
						k++;

					if(k == j)
						k = m;

					if(k >= j)
						k--;
				}
			}

			// trace(j);
			// traceloop(n, temp);

			int curr = n;
			int pre = 0;
			for(i=0;i<n;++i)
			{
				pre += temp[i];
				if(pre < 0)
					curr = i;
			}

			if(curr == n)
				curr = 0;
			if(curr < best)
			{
				ans = temp;
				best = curr;
			}
		}

		for(auto x : ans)
			cout << x << " ";
		cout << "\n";
	}

	return 0;
}


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

#define ll long long
#define pb push_back
#define pii pair<int, int>
#define sz size()

#ifdef LOCAL
template<class T1, class T2>
ostream &operator<<(ostream &os,const pair<T1,T2> &p){os<<'{'<<p.first<<", "<<p.second<<'}';return os;}
#define trace(...) __f(#__VA_ARGS__,__VA_ARGS__)
template<typename Arg1>void __f(const char* name,Arg1&& arg1){cerr<<name<<" : "<<arg1<<"\n";}
template<typename Arg1,typename... Args>void __f(const char* names,Arg1&& arg1,Args&&... args)
{const char* comma=strchr(names+1,',');cerr.write(names,comma-names)<<" : "<<arg1<<" , ";__f(comma+1, args...);}
#define traceloop(x,a){cerr<<#a<<": ";for(ll _=0;_<x;_++)cerr<<a[_]<<" ";cerr<<"\n";}
#else
#define trace(...) 69
#define traceloop(...) 69
#endif

int main()
{
	ios::sync_with_stdio(0);
	int i, j, k, n, t;
	cin >> t;
	while(t--)
	{
		cin >> n;
		vector<int> a(n), b(n), take;
		for(i=0;i<n;++i)
			cin >> a[i];
		for(i=0;i<n;++i)
		{
			cin >> b[i];
			if(b[i] == 0)
				take.pb(a[i]);
		}

		sort(take.begin(), take.end());
		vector<int> ans;
		ans = a;

		j = (int)take.sz - 1;
		vector<int> lol(n);
		for(i=0;i<n;++i)
			lol[i] = (b[i] ? a[i] : take[j--]);

		int lollast = -1;
		int lolpre = 0;
		for(i=0;i<n;++i)
		{
			lolpre += lol[i];
			if(lolpre < 0)
				lollast = i;
		}

		if(lollast == -1)
		{
			for(auto x : lol)
				cout << x << " ";
			cout << "\n";
			continue;
		}

		for(i=n-1;i>=0;--i)
		{
			vector<int> temp(n);
			k = 0;
			for(j=0;j<=i;++j)
			{
				if(b[j])
					temp[j] = a[j];
				else
					temp[j] = take[k++];
			}
			k = (int)take.sz - 1;
			for(j=i+1;j<n;++j)
			{
				if(b[j])
					temp[j] = a[j];
				else
					temp[j] = take[k--];
			}

			int last = 0;
			int pre = 0;
			for(j=0;j<n;++j)
			{
				pre += temp[j];
				if(pre < 0)
					last = j;
			}

			if(last == i)
				ans = temp;
		}

		for(auto x : ans)
			cout << x << " ";
		cout << "\n";
	}

	return 0;
}
// LOL?


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

#define ll long long
#define pb push_back
#define pii pair<int, int>
#define sz size()

#ifdef LOCAL
template<class T1, class T2>
ostream &operator<<(ostream &os,const pair<T1,T2> &p){os<<'{'<<p.first<<", "<<p.second<<'}';return os;}
#define trace(...) __f(#__VA_ARGS__,__VA_ARGS__)
template<typename Arg1>void __f(const char* name,Arg1&& arg1){cerr<<name<<" : "<<arg1<<"\n";}
template<typename Arg1,typename... Args>void __f(const char* names,Arg1&& arg1,Args&&... args)
{const char* comma=strchr(names+1,',');cerr.write(names,comma-names)<<" : "<<arg1<<" , ";__f(comma+1, args...);}
#define traceloop(x,a){cerr<<#a<<": ";for(ll _=0;_<x;_++)cerr<<a[_]<<" ";cerr<<"\n";}
#else
#define trace(...) 69
#define traceloop(...) 69
#endif

int main()
{
	ios::sync_with_stdio(0);
	int i, j, k, n, t;
	cin >> t;
	while(t--)
	{
		cin >> n;
		vector<int> a(n), b(n), take;
		for(i=0;i<n;++i)
			cin >> a[i];
		for(i=0;i<n;++i)
		{
			cin >> b[i];
			if(b[i] == 0)
				take.pb(a[i]);
		}

		sort(take.begin(), take.end());
		vector<int> ans;
		ans = a;

		for(i=n-1;i>=0;--i)
		{
			vector<int> temp(n);
			k = 0;
			for(j=0;j<=i;++j)
			{
				if(b[j])
					temp[j] = a[j];
				else
					temp[j] = take[k++];
			}
			k = (int)take.sz - 1;
			for(j=i+1;j<n;++j)
			{
				if(b[j])
					temp[j] = a[j];
				else
					temp[j] = take[k--];
			}

			int last = 0;
			int pre = 0;
			for(j=0;j<n;++j)
			{
				pre += temp[j];
				if(pre < 0)
					last = j;
			}

			if(last == i)
				ans = temp;
		}

		for(auto x : ans)
			cout << x << " ";
		cout << "\n";
	}

	return 0;
}


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

#define ll long long
#define pb push_back
#define pii pair<int, int>
#define sz size()

#ifdef LOCAL
template<class T1, class T2>
ostream &operator<<(ostream &os,const pair<T1,T2> &p){os<<'{'<<p.first<<", "<<p.second<<'}';return os;}
#define trace(...) __f(#__VA_ARGS__,__VA_ARGS__)
template<typename Arg1>void __f(const char* name,Arg1&& arg1){cerr<<name<<" : "<<arg1<<"\n";}
template<typename Arg1,typename... Args>void __f(const char* names,Arg1&& arg1,Args&&... args)
{const char* comma=strchr(names+1,',');cerr.write(names,comma-names)<<" : "<<arg1<<" , ";__f(comma+1, args...);}
#define traceloop(x,a){cerr<<#a<<": ";for(ll _=0;_<x;_++)cerr<<a[_]<<" ";cerr<<"\n";}
#else
#define trace(...) 69
#define traceloop(...) 69
#endif

int main()
{
	ios::sync_with_stdio(0);
	int i, j, k, n, t;
	cin >> t;
	while(t--)
	{
		cin >> n;
		vector<int> a(n), b(n), take;
		for(i=0;i<n;++i)
			cin >> a[i];
		for(i=0;i<n;++i)
		{
			cin >> b[i];
			if(b[i] == 0)
				take.pb(a[i]);
		}

		sort(take.begin(), take.end());
		vector<int> ans;
		ans = a;

		j = (int)take.sz - 1;
		vector<int> lol(n);
		for(i=0;i<n;++i)
			lol[i] = (b[i] ? a[i] : take[j--]);

		int lollast = -1;
		int lolpre = 0;
		for(i=0;i<n;++i)
		{
			lolpre += lol[i];
			if(lolpre < 0)
				lollast = i;
		}

		if(lollast == -1)
		{
			for(auto x : lol)
				cout << x << " ";
			cout << "\n";
			continue;
		}

		int best = n;
		for(i=n-1;i>=0;--i)
		{
			vector<int> temp(n);
			k = 0;
			for(j=0;j<=i;++j)
			{
				if(b[j])
					temp[j] = a[j];
				else
					temp[j] = take[k++];
			}
			k = (int)take.sz - 1;
			for(j=i+1;j<n;++j)
			{
				if(b[j])
					temp[j] = a[j];
				else
					temp[j] = take[k--];
			}

			int last = 0;
			int pre = 0;
			for(j=0;j<n;++j)
			{
				pre += temp[j];
				if(pre < 0)
					last = j;
			}

			if(last < best)
				ans = temp, best = last;
		}

		for(auto x : ans)
			cout << x << " ";
		cout << "\n";
	}

	return 0;
}


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

#define ll long long
#define pb push_back
#define pii pair<int, int>
#define sz size()

#ifdef LOCAL
template<class T1, class T2>
ostream &operator<<(ostream &os,const pair<T1,T2> &p){os<<'{'<<p.first<<", "<<p.second<<'}';return os;}
#define trace(...) __f(#__VA_ARGS__,__VA_ARGS__)
template<typename Arg1>void __f(const char* name,Arg1&& arg1){cerr<<name<<" : "<<arg1<<"\n";}
template<typename Arg1,typename... Args>void __f(const char* names,Arg1&& arg1,Args&&... args)
{const char* comma=strchr(names+1,',');cerr.write(names,comma-names)<<" : "<<arg1<<" , ";__f(comma+1, args...);}
#define traceloop(x,a){cerr<<#a<<": ";for(ll _=0;_<x;_++)cerr<<a[_]<<" ";cerr<<"\n";}
#else
#define trace(...) 69
#define traceloop(...) 69
#endif

int main()
{
	ios::sync_with_stdio(0);
	int i, j, k, n, t;
	cin >> t;
	while(t--)
	{
		cin >> n;
		vector<int> a(n), b(n), take;
		for(i=0;i<n;++i)
			cin >> a[i];
		for(i=0;i<n;++i)
		{
			cin >> b[i];
			if(b[i] == 0)
				take.pb(a[i]);
		}

		sort(take.begin(), take.end());
		vector<int> ans;
		ans = a;

		j = (int)take.sz - 1;
		vector<int> lol(n);
		for(i=0;i<n;++i)
			lol[i] = (b[i] ? a[i] : take[j--]);

		int lollast = -1;
		int lolpre = 0;
		for(i=0;i<n;++i)
		{
			lolpre += lol[i];
			if(lolpre < 0)
				lollast = i;
		}

		if(lollast == -1)
		{
			for(auto x : lol)
				cout << x << " ";
			cout << "\n";
			continue;
		}

		for(i=n-1;i>=0;--i)
		{
			vector<int> temp(n);
			k = 0;
			for(j=0;j<=i;++j)
			{
				if(b[j])
					temp[j] = a[j];
				else
					temp[j] = take[k++];
			}
			k = (int)take.sz - 1;
			for(j=i+1;j<n;++j)
			{
				if(b[j])
					temp[j] = a[j];
				else
					temp[j] = take[k--];
			}

			int last = 0;
			int pre = 0;
			for(j=0;j<n;++j)
			{
				pre += temp[j];
				if(pre < 0)
					last = j;
			}

			if(last == i)
				ans = temp;
		}

		for(auto x : ans)
			cout << x << " ";
		cout << "\n";
	}

	return 0;
}


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

#define ll long long
#define pb push_back
#define pii pair<int, int>
#define sz size()

#ifdef LOCAL
template<class T1, class T2>
ostream &operator<<(ostream &os,const pair<T1,T2> &p){os<<'{'<<p.first<<", "<<p.second<<'}';return os;}
#define trace(...) __f(#__VA_ARGS__,__VA_ARGS__)
template<typename Arg1>void __f(const char* name,Arg1&& arg1){cerr<<name<<" : "<<arg1<<"\n";}
template<typename Arg1,typename... Args>void __f(const char* names,Arg1&& arg1,Args&&... args)
{const char* comma=strchr(names+1,',');cerr.write(names,comma-names)<<" : "<<arg1<<" , ";__f(comma+1, args...);}
#define traceloop(x,a){cerr<<#a<<": ";for(ll _=0;_<x;_++)cerr<<a[_]<<" ";cerr<<"\n";}
#else
#define trace(...) 69
#define traceloop(...) 69
#endif

int main()
{
	ios::sync_with_stdio(0);
	int i, j, k, n, t;
	cin >> t;
	while(t--)
	{
		cin >> n;
		vector<int> a(n), b(n), take;
		for(i=0;i<n;++i)
			cin >> a[i];
		for(i=0;i<n;++i)
		{
			cin >> b[i];
			if(b[i] == 0)
				take.pb(a[i]);
		}

		sort(take.begin(), take.end());
		vector<int> ans;
		ans = a;

		int best = n;
		int m = take.sz;
		for(j=0;j<m;++j)
		{
			k = 0;
			vector<int> temp(n);
			for(i=0;i<n;++i)
			{
				if(b[i])
					temp[i] = a[i];
				else
				{
					temp[i] = take[(k + j) % m];
					k++;
				}
			}

			int curr = n;
			int pre = 0;
			for(i=0;i<n;++i)
			{
				pre += temp[i];
				if(pre < 0)
					curr = pre;
			}

			if(curr == n)
				curr = 0;
			if(curr < best)
			{
				ans = temp;
				best = curr;
			}
		}

		for(auto x : ans)
			cout << x << " ";
		cout << "\n";
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	int i,j,k,l,m,n,t,x,y,z,l1;
	cin>>t;
	while(t--) {
		cin>>n;
		int a[n+1],b[n+1];
		vector<int> v1;
		for(i=0;i<n;i++){
			cin>>a[i];

		}
		for(i=0;i<n;i++){
			cin>>b[i];
			if(b[i]==0)
				v1.push_back(a[i]);
		}
		sort(v1.begin(), v1.end());
		cout<<"v1="<<v1.size()<<endl;
		j=v1.size()-1;
		for(i=0;i<n;i++){
			if(b[i]==0)
				cout<<v1[j--]<<" ";
			else
				cout<<a[i]<<" ";
		}
		cout<<endl;

	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	int i,j,k,l,m,n,t,x,y,z,l1;
	cin>>t;
	while(t--) {
		cin>>n;
		int a[n+1],b[n+1];
		vector<int> v1;
		for(i=0;i<n;i++){
			cin>>a[i];

		}
		for(i=0;i<n;i++){
			cin>>b[i];
			if(a[i]>0 && b[i]==0)
				v1.push_back(a[i]);
		}
		sort(v1.begin(), v1.end());
		j=v1.size()-1;
		for(i=0;i<n;i++){
			if(b[i]==0)
				cout<<v1[j--]<<" ";
			else
				cout<<a[i]<<" ";
		}
		cout<<endl;

	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	int i,j,k,l,m,n,t,x,y,z,l1;
	cin>>t;
	while(t--) {
		cin>>n;
		int a[n+1],b[n+1];
		vector<int> v1;
		for(i=0;i<n;i++){
			cin>>a[i];
			if(a[i]>0)
				v1.push_back(a[i]);
		}
		for(i=0;i<n;i++)
			cin>>b[i];
		sort(v1.begin(), v1.end());
		j=v1.size()-1;
		for(i=0;i<n;i++){
			if(b[i]==0)
				cout<<v1[j--]<<" ";
			else
				cout<<a[i]<<" ";
		}
		cout<<endl;

	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	int i,j,k,l,m,n,t,x,y,z,l1;
	cin>>t;
	while(t--) {
		cin>>n;
		int a[n+1],b[n+1];
		vector<int> v1;
		for(i=0;i<n;i++){
			cin>>a[i];
			if(a[i]>0)
				v1.push_back(a[i]);
		}
		for(i=0;i<n;i++)
			cin>>b[i];
		sort(v1.begin(), v1.end());
		j=0;
		for(i=0;i<n;i++){
			if(b[i]==0)
				cout<<v1[j++]<<" ";
			else
				cout<<a[i]<<" ";
		}
		cout<<endl;

	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	int i,j,k,l,m,n,t,x,y,z,l1;
	cin>>t;
	while(t--) {
		cin>>n;
		int a[n+1],b[n+1];
		vector<int> v1;
		for(i=0;i<n;i++){
			cin>>a[i];

		}
		for(i=0;i<n;i++){
			cin>>b[i];
			if(b[i]==0)
				v1.push_back(a[i]);
		}
		sort(v1.begin(), v1.end());
		j=v1.size()-1;
		for(i=0;i<n;i++){
			if(b[i]==0)
				cout<<v1[j--]<<" ";
			else
				cout<<a[i]<<" ";
		}
		cout<<endl;

	}
}


#include<bits/stdc++.h>
using namespace std;
#define lc "\n"
#define fast_io ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(0)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define int long long
#define c(a,n) for(size_t i = 0; i < n; i++) cin >> a[i];
#define ffor(n) for(size_t i = 0; i < n; i++)
#define asort(a) sort(a.begin(), a.end())
#define rsort(a) sort(a.begin(), a.end(), greater<int>())

typedef vector<int> vi;
typedef vector<float> vf;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<long long> vll;
typedef map<string,int> msi;
typedef map<int,int> mii;
typedef unordered_map<string,int> umsi;

int32_t main()
{
    fast_io;
    int t;
    cin >> t;
    while(t--)
    {
        int n, k, l = 0, un = 0;
        cin >> n;
        vi a(n), pos(n), pre(n);
        for(int i = 0; i < n; i++)
            cin >> a[i];
        for(int i = 0; i < n; i++)
        {
            cin >> pos[i];
            if(pos[i] == 0)
                un++;
            else
                l++;
        }
        pre[0] = a[0];
        for(int i = 1; i < n; i++)
            pre[i] = pre[i-1] + a[i];
        int curmax = 0;
        for(int i = n-1; i >= 0; i--)
            if(pre[i] < 0)
            {
                curmax = i;
                break;
            }
        if(curmax == 0 || un == 0)
        {
            for(int i = 0; i < n; i++)
                cout << a[i] << " ";
            cout << lc;
            continue;
        }
        else
        {
            vi ans;
            for(int i = 0; i < n; i++)
                if(pos[i] == 0)
                    ans.pb(a[i]);
            sort(ans.begin(), ans.end());
            for(int i = 0; i < n; i++)
            {
                if(pos[i] == 1)
                {
                    ans.pb(0);
                    for(int j = i+1; j < ans.size(); j++)
                        ans[j] = ans[j-1];
                    ans[i] = a[i];
                }
            }
            for(auto i : ans)
                cout << i << " ";
            cout << lc;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define lc "\n"
#define fast_io ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(0)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define int long long
#define c(a,n) for(size_t i = 0; i < n; i++) cin >> a[i];
#define ffor(n) for(size_t i = 0; i < n; i++)
#define asort(a) sort(a.begin(), a.end())
#define rsort(a) sort(a.begin(), a.end(), greater<int>())

typedef vector<int> vi;
typedef vector<float> vf;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<long long> vll;
typedef map<string,int> msi;
typedef map<int,int> mii;
typedef unordered_map<string,int> umsi;

int32_t main()
{
    fast_io;
    int t;
    cin >> t;
    while(t--)
    {
        int n, k, l = 0, un = 0;
        cin >> n;
        vi a(n), pos(n), pre(n);
        for(int i = 0; i < n; i++)
            cin >> a[i];
        for(int i = 0; i < n; i++)
            cin >> pos[i];

        vi ans;
        for(int i = 0; i < n; i++)
            if(pos[i] == 0)
                ans.pb(a[i]);

        rsort(ans);
        int k = 0;

        for(int i = 0; i < n; i++)
            if(!pos[i])
                a[i] = ans[k++];

        for(auto i : a)
            cout << i << " ";

        cout << lc;

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define lc "\n"
#define fast_io ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(0)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define int long long
#define c(a,n) for(size_t i = 0; i < n; i++) cin >> a[i];
#define ffor(n) for(size_t i = 0; i < n; i++)
#define asort(a) sort(a.begin(), a.end())
#define rsort(a) sort(a.begin(), a.end(), greater<int>())

typedef vector<int> vi;
typedef vector<float> vf;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<long long> vll;
typedef map<string,int> msi;
typedef map<int,int> mii;
typedef unordered_map<string,int> umsi;

int32_t main()
{
    fast_io;
    int t;
    cin >> t;
    while(t--)
    {
        int n, k, l = 0, un = 0;
        cin >> n;
        vi a(n+1), pos(n+1), pre(n+1);
        for(int i = 1; i <= n; i++)
            cin >> a[i];
        for(int i = 1; i <= n; i++)
        {
            cin >> pos[i];
            if(pos[i] == 0)
                un++;
            else
                l++;
        }
        for(int i = 1; i < n; i++)
            pre[i] = pre[i-1] + a[i];
        int curmax = 0;
        for(int i = n; i > 0; i--)
            if(pre[i] < 0)
            {
                curmax = i;
                break;
            }
        if(curmax == 1 || un == 0 || curmax == 0)
        {
            for(int i = 1; i <= n; i++)
                cout << a[i] << " ";
            cout << lc;
            continue;
        }
        else
        {
            vi ans;
            for(int i = 1; i <= n; i++)
                if(pos[i] == 0)
                    ans.pb(a[i]);
            sort(ans.begin(), ans.end());
            for(int i = 1; i <= n; i++)
            {
                if(pos[i] == 1)
                {
                    ans.pb(0);
                    for(int j = i+1; j < ans.size(); j++)
                        ans[j] = ans[j-1];
                    ans[i] = a[i];
                }
            }
            for(auto i : ans)
                cout << i << " ";
            cout << lc;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define lc "\n"
#define fast_io ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(0)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define int long long
#define c(a,n) for(size_t i = 0; i < n; i++) cin >> a[i];
#define ffor(n) for(size_t i = 0; i < n; i++)
#define asort(a) sort(a.begin(), a.end())
#define rsort(a) sort(a.begin(), a.end(), greater<int>())

typedef vector<int> vi;
typedef vector<float> vf;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<long long> vll;
typedef map<string,int> msi;
typedef map<int,int> mii;
typedef unordered_map<string,int> umsi;

int32_t main()
{
    fast_io;
    int t;
    cin >> t;
    while(t--)
    {
        int n, k, l = 0, un = 0;
        cin >> n;
        vi a(n), pos(n), pre(n);
        for(int i = 0; i < n; i++)
            cin >> a[i];
        for(int i = 0; i < n; i++)
        {
            cin >> pos[i];
            if(pos[i] == 0)
                un++;
            else
                l++;
        }
        pre[0] = a[0];
        for(int i = 1; i < n; i++)
            pre[i] = pre[i-1] + a[i];
        int curmax = 0;
        for(int i = n-1; i >= 0; i--)
            if(pre[i] < 0)
            {
                curmax = i;
                break;
            }
        if(curmax == 0 || un == 0)
        {
            for(int i = 0; i < n; i++)
                cout << a[i] << " ";
            cout << lc;
            continue;
        }
        else
        {
            vi ans;
            for(int i = 0; i < n; i++)
                if(pos[i] == 0)
                    ans.pb(a[i]);
            sort(ans.begin(), ans.end());
            for(int i = 0; i < n; i++)
            {
                if(pos[i] == 1)
                {
                    ans.pb(0);
                    for(int j = ans.size()-1; j > i; j--)
                        ans[j] = ans[j-1];
                    ans[i] = a[i];
                }
            }
            for(auto i : ans)
                cout << i << " ";
            cout << lc;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define lc "\n"
#define fast_io ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(0)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define int long long
#define c(a,n) for(size_t i = 0; i < n; i++) cin >> a[i];
#define ffor(n) for(size_t i = 0; i < n; i++)
#define asort(a) sort(a.begin(), a.end())
#define rsort(a) sort(a.begin(), a.end(), greater<int>())

typedef vector<int> vi;
typedef vector<float> vf;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<long long> vll;
typedef map<string,int> msi;
typedef map<int,int> mii;
typedef unordered_map<string,int> umsi;

int32_t main()
{
    fast_io;
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        vi a(n), pos(n), pre(n);
        for(int i = 0; i < n; i++)
            cin >> a[i];
        for(int i = 0; i < n; i++)
            cin >> pos[i];

        vi ans;
        for(int i = 0; i < n; i++)
            if(pos[i] == 0)
                ans.pb(a[i]);

        rsort(ans);
        int k = 0;

        for(int i = 0; i < n; i++)
            if(!pos[i])
                a[i] = ans[k++];

        for(auto i : a)
            cout << i << " ";

        cout << lc;

    }
    return 0;
}


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

typedef long long ll ;

#define debug(x)               cerr << #x << " : " << x << endl

const int maxn = 1e2 + 5 ;

ll t ,n ,b ,arr[maxn] ,arr1[maxn] ,arr2[maxn] ,mark[maxn] ,j1 ,j2 ,sum;
vector <int> srt ;

void solve(){
	cin >> n ;
	for (int i = 0 ; i < n ; i++){
		cin >> arr[i] ;
	}
	for (int i = 0 ; i < n ; i++){
		cin >> mark[i] ;
		if (!mark[i]){
			srt.push_back(arr[i]) ;
		}
	}
	sort(srt.begin() ,srt.end()) ;
	for (int i = 0 ,p = 0; i < n ; i++){
		if (mark[i]){
			arr1[i] = arr[i] ;
		}
		else{
			arr1[i] = srt[p] ;
			p++ ;
		}
		sum += arr1[i] ;
		if (sum < 0){
			j1 = i ;
		}
	}
	sum = 0 ;
	reverse(srt.begin() ,srt.end()) ;
	for (int i = 0 ,p = 0; i < n ; i++){
		if (mark[i]){
			arr2[i] = arr[i] ;
		}
		else{
			arr2[i] = srt[p] ;
			p++ ;
		}
		sum += arr2[i] ;
		if (sum < 0){
			j2 = i ;
		}
	}
	if (j1 < j2){
		for (int i = 0 ; i < n ; i++){
			cout << arr1[i] << " " ;
		}
		cout << "\n" ;
	}
	else {
		for (int i = 0 ; i < n ; i++){
			cout << arr2[i] << " " ;
		}
		cout << "\n" ;
	}
	srt.clear() ;
}

int main (){
	cin >> t ;
	while (t--){
		solve() ;
	}
	return 0 ;
}


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

typedef long long ll ;

#define debug(x)               cerr << #x << " : " << x << endl

const int maxn = 1e2 + 5 ;

ll t ,n ,b ,arr[maxn] ,arr1[maxn] ,arr2[maxn] ,mark[maxn] ,j1 ,j2 ,sum;
vector <int> srt ;

void solve(){
	cin >> n ;
	for (int i = 0 ; i < n ; i++){
		cin >> arr[i] ;
	}
	for (int i = 0 ; i < n ; i++){
		cin >> mark[i] ;
		if (!mark[i]){
			srt.push_back(arr[i]) ;
		}
	}
	sort(srt.begin() ,srt.end()) ;
	for (int i = 0 ,p = 0; i < n ; i++){
		if (mark[i]){
			arr1[i] = arr[i] ;
		}
		else{
			arr1[i] = srt[p] ;
			p++ ;
		}
		sum += arr1[i] ;
		if (sum < 0){
			j1 = i ;
		}
	}
	sum = 0 ;
	reverse(srt.begin() ,srt.end()) ;
	for (int i = 0 ,p = 0; i < n ; i++){
		if (mark[i]){
			arr2[i] = arr[i] ;
		}
		else{
			arr2[i] = srt[p] ;
			p++ ;
		}
		sum += arr2[i] ;
		if (sum < 0){
			j2 = i ;
		}
	}
	debug(j1) ;
	debug(j2) ;
	if (j1 < j2){
		for (int i = 0 ; i < n ; i++){
			cout << arr1[i] << " " ;
		}
		cout << "\n" ;
	}
	else {
		for (int i = 0 ; i < n ; i++){
			cout << arr2[i] << " " ;
		}
		cout << "\n" ;
	}
}

int main (){
	cin >> t ;
	while (t--){
		solve() ;
	}
	return 0 ;
}


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

typedef long long ll ;

#define debug(x)               cerr << #x << " : " << x << endl

const int maxn = 1e2 + 5 ;

ll t ,n ,b ,arr[maxn] ,arr1[maxn] ,arr2[maxn] ,mark[maxn] ,j1 ,j2 ,sum;
vector <int> srt ;

void solve(){
	cin >> n ;
	for (int i = 0 ; i < n ; i++){
		cin >> arr[i] ;
	}
	for (int i = 0 ; i < n ; i++){
		cin >> mark[i] ;
		if (!mark[i]){
			srt.push_back(arr[i]) ;
		}
	}
	sort(srt.begin() ,srt.end()) ;
	for (int i = 0 ,p = 0; i < n ; i++){
		if (mark[i]){
			arr1[i] = arr[i] ;
		}
		else{
			arr1[i] = srt[p] ;
			p++ ;
		}
		sum += arr1[i] ;
		if (sum < 0){
			j1 = i ;
		}
	}
	sum = 0 ;
	reverse(srt.begin() ,srt.end()) ;
	for (int i = 0 ,p = 0; i < n ; i++){
		if (mark[i]){
			arr2[i] = arr[i] ;
		}
		else{
			arr2[i] = srt[p] ;
			p++ ;
		}
		sum += arr2[i] ;
		if (sum < 0){
			j2 = i ;
		}
	}
	if (j1 < j2){
		for (int i = 0 ; i < n ; i++){
			cout << arr1[i] << " " ;
		}
		cout << "\n" ;
	}
	else {
		for (int i = 0 ; i < n ; i++){
			cout << arr2[i] << " " ;
		}
		cout << "\n" ;
	}
}

int main (){
	cin >> t ;
	while (t--){
		solve() ;
	}
	return 0 ;
}


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

typedef long long ll ;

#define debug(x)               cerr << #x << " : " << x << endl

const int maxn = 1e2 + 5 ;

ll t ,n ,b ,arr[maxn] ,arr1[maxn] ,arr2[maxn] ,mark[maxn] ,j1 ,j2 ,sum;
vector <int> srt ;

void solve(){
	cin >> n ;
	for (int i = 0 ; i < n ; i++){
		cin >> arr[i] ;
	}
	for (int i = 0 ; i < n ; i++){
		cin >> mark[i] ;
		if (!mark[i]){
			srt.push_back(arr[i]) ;
		}
	}
	sort(srt.begin() ,srt.end()) ;
	for (int i = 0 ,p = 0; i < n ; i++){
		if (mark[i]){
			arr1[i] = arr[i] ;
		}
		else{
			arr1[i] = srt[p] ;
			p++ ;
		}
		sum += arr1[i] ;
		if (sum < 0){
			j1 = i ;
		}
	}
	sum = 0 ;
	reverse(srt.begin() ,srt.end()) ;
	for (int i = 0 ,p = 0; i < n ; i++){
		if (mark[i]){
			arr2[i] = arr[i] ;
		}
		else{
			arr2[i] = srt[p] ;
			p++ ;
		}
		sum += arr2[i] ;
		if (sum < 0){
			j2 = i ;
		}
	}
	sum = 0 ;
	if (j1 < j2){
		for (int i = 0 ; i < n ; i++){
			cout << arr1[i] << " " ;
		}
		cout << "\n" ;
	}
	else {
		for (int i = 0 ; i < n ; i++){
			cout << arr2[i] << " " ;
		}
		cout << "\n" ;
	}
	j1 = 0 ;
	j2 = 0 ;
	srt.clear() ;
}

int main (){
	cin >> t ;
	while (t--){
		solve() ;
	}
	return 0 ;
}


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

typedef long long ll ;

#define debug(x)               cerr << #x << " : " << x << endl

const int maxn = 1e2 + 5 ;

ll t ,n ,b ,arr[maxn] ,arr1[maxn] ,arr2[maxn] ,mark[maxn] ,j1 ,j2 ,sum;
vector <int> srt ;

void solve(){
	cin >> n ;
	for (int i = 0 ; i < n ; i++){
		cin >> arr[i] ;
	}
	for (int i = 0 ; i < n ; i++){
		cin >> mark[i] ;
		if (!mark[i]){
			srt.push_back(arr[i]) ;
		}
	}
	sort(srt.begin() ,srt.end()) ;
	for (int i = 0 ,p = 0; i < n ; i++){
		if (mark[i]){
			arr1[i] = arr[i] ;
		}
		else{
			arr1[i] = srt[p] ;
			p++ ;
		}
		sum += arr1[i] ;
		if (sum < 0){
			j1 = i ;
		}
	}
	sum = 0 ;
	reverse(srt.begin() ,srt.end()) ;
	for (int i = 0 ,p = 0; i < n ; i++){
		if (mark[i]){
			arr2[i] = arr[i] ;
		}
		else{
			arr2[i] = srt[p] ;
			p++ ;
		}
		sum += arr2[i] ;
		if (sum < 0){
			j2 = i ;
		}
	}
	sum = 0 ;
	if (j1 < j2){
		for (int i = 0 ; i < n ; i++){
			cout << arr1[i] << " " ;
		}
		cout << "\n" ;
	}
	else {
		for (int i = 0 ; i < n ; i++){
			cout << arr2[i] << " " ;
		}
		cout << "\n" ;
	}
	srt.clear() ;
}

int main (){
	cin >> t ;
	while (t--){
		solve() ;
	}
	return 0 ;
}


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

int main()
{
    int t;
    while(t--)
    {
        int n;
        int arr[n];
        int check[n];
        int i = 0;
        while(i < n){
            cin>>arr[i];
            i++;
        }
        i =0;
        vector<int> a;
        while(i < n){
            cin>>check[i];
            if(check[i] == 0)
                a.push_back(arr[i]);
            i++;
        }
        sort(a.begin(),a.end(),greater<int>);
        int j = 0;

        for(int i = 0 ; i < n ; i++){
            if(check[i] == 1)
                cout<<arr[i]<<" ";
            else
                cout<<a[j++]<<" ";
        }
        cout<<endl;
    }
}


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

int main()
{
    int t;
    while(t--)
    {
        ll n;
        cin >> n;
        ll arr[n];
        bool check[n];
        int i = 0;
        while(i < n){
            cin>>arr[i];
            i++;
        }
        i =0;
        vector<int> a;
        while(i < n){
            cin>>check[i];
            if(check[i] == 0)
                a.push_back(arr[i]);
            i++;
        }
        sort(a.begin(),a.end());
        reverse(a.begin(),a.end());
        int j = 0;

        for(int i = 0 ; i < n ; i++){
           if(check[i]==0)
            {
                arr[i] = a[j];
                j++;
            }
            cout << arr[i] << " ";
        }
        cout<<endl;
    }
}


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

int main()
{
    int t;
    while(t--)
    {
        int n;
        int arr[n];
        int check[n];
        int i = 0;
        while(i < n){
            cin>>arr[i];
            i++;
        }
        i =0;
        vector<int> a;
        while(i < n){
            cin>>check[i];
            if(check[i] == 0)
                a.push_back(arr[i]);
            i++;
        }
        sort(a.begin(),a.end(),greater<int>());
        int j = 0;

        for(int i = 0 ; i < n ; i++){
            if(check[i] == 1)
                cout<<arr[i]<<" ";
            else
                cout<<a[j++]<<" ";
        }
        cout<<endl;
    }
}


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

int main()
{
    int t;
    while(t--)
    {
        int n;
        cin>>n;
        int arr[n];
        int check[n];
        int i = 0;
        while(i < n){
            cin>>arr[i];
            i++;
        }
        i =0;
        vector<int> a;
        while(i < n){
            cin>>check[i];
            if(check[i] == 0)
                a.push_back(arr[i]);
            i++;
        }
        sort(a.begin(),a.end(),greater<int>());
        int j = 0;

        for(int i = 0 ; i < n ; i++){
            if(check[i] == 1)
                cout<<arr[i]<<" ";
            else
                cout<<a[j++]<<" ";
        }
        cout<<endl;
    }
}


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

int main()
{
    int t;
    while(t--)
    {
        int n;
        cin>>n;
        int arr[n];
        int check[n];
        int i = 0;
        while(i < n){
            cin>>arr[i];
            i++;
        }
        i =0;
        vector<int> a;
        while(i < n){
            cin>>check[i];
            if(check[i] == 0)
                a.push_back(arr[i]);
            i++;
        }
        sort(a.begin(),a.end(),greater<int>());
        int j = 0;

        for(int i = 0 ; i < n ; i++){
            if(check[i] == 1)
                cout<<arr[i]<<' ';
            else
                cout<<a[j++]<<' ';
        }
        cout<<endl;
    }
}


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

int main()
{
    int t;
    while(t--)
    {
        int n;
        cin>>n;
        int arr[n];
        int check[n];
        int i = 0;
        while(i < n){
            cin>>arr[i];
            i++;
        }
        i =0;
        vector<int> a;
        while(i < n){
            cin>>check[i];
            if(check[i] == 0)
                a.push_back(arr[i]);
            i++;
        }
        sort(a.begin(),a.end());
        reverse(a.begin(),a.end());
        int j = 0;

        for(int i = 0 ; i < n ; i++){
           if(check[i]==0)
            {
                arr[i] = a[j];
                j++;
            }
            cout << arr[i] << " ";
        }
        cout<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i,n) for(int i=0;i<n;i++)
int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    cin >> t;
    while(t--){
    	ll n;
    	cin >> n;
    	vector<int> a(n),l(n),b;

    	f(i,n)cin >> a[i];
    	f(i,n)cin >> l[i];
    	f(i,n){
    		if(l[i]==0)b.push_back(a[i]);
    	}
    	sort(b.begin(),b.end(), greater<int>());
    	ll k=0;
    	for(int i=0;i<n;i++){
    		if(l[i]==0)l[i] = b[k++];
    		else l[i]=a[i];
    		cout << l[i] << " ";
    	}
    	cout << endl;

    }

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll
int main()
{
    int t;
    while(t--)
    {
        ll n;
        cin >> n;
        ll arr[n];
        bool check[n];
        int i = 0;
        while(i < n){
            cin>>arr[i];
            i++;
        }
        i =0;
        vector<int> a;
        while(i < n){
            cin>>check[i];
            if(check[i] == 0)
                a.push_back(arr[i]);
            i++;
        }
        sort(a.begin(),a.end());
        reverse(a.begin(),a.end());
        int j = 0;

        for(int i = 0 ; i < n ; i++){
           if(check[i]==0)
            {
                arr[i] = a[j];
                j++;
            }
            cout << arr[i] << " ";
        }
        cout<<endl;
    }
}


#include <bits/stdc++.h>
typedef long long int ll;
#define endl '\n'
using namespace std;
int main(){
    ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--){
            ll n;
            cin>>n;
         ll a[n],c[n];
         for(ll i=0;i<n;i++)cin>>a[i];
         for(ll i=0;i<n;i++)cin>>c[i];
         vector<ll>x;
         for(ll i=0;i<n;i++){
            if(c[i]==0)x.push_back(a[i]);
         }
         sort(x.begin(),x.end());
         reverse(x.begin(),x.end());
         deque<ll>id;
         for(ll i=0;i<n;i++){
            if(c[i]==0)id.push_back(i);
         }
         ll s=0;
         ll l=0,r=x.size()-1;
         while(id.size()>0){
            if(s==0){
                a[id.front()]=x[l++];
                id.pop_front();
            }
            else{
                a[id.back()]=x[l++];
                id.pop_back();
            }
            s=1-s;
         }
         for(ll i=0;i<n;i++)cout<<a[i]<<" ";
         cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long int ll;
#define endl '\n'
using namespace std;
int main(){
    ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--){
            ll n;
            cin>>n;
         ll a[n],c[n];
         for(ll i=0;i<n;i++)cin>>a[i];
         for(ll i=0;i<n;i++)cin>>c[i];
         vector<ll>x;
         for(ll i=0;i<n;i++){
            if(c[i]==0)x.push_back(a[i]);
         }
         sort(x.begin(),x.end());
         reverse(x.begin(),x.end());
         for(ll i=0;i<n;i++){
            if(c[i])cout<<a[i]<<" ";
            else{
                cout<<x.back()<<" ";
                x.pop_back();
            }
         }
         cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long int ll;
#define endl '\n'
using namespace std;
int main(){
    ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--){
            ll n;
            cin>>n;
         ll a[n],c[n];
         for(ll i=0;i<n;i++)cin>>a[i];
         for(ll i=0;i<n;i++)cin>>c[i];
         vector<ll>x;
         for(ll i=0;i<n;i++){
            if(c[i]==0)x.push_back(a[i]);
         }
         sort(x.begin(),x.end());
         deque<ll>id;
         for(ll i=0;i<n;i++){
            if(c[i]==0)id.push_back(i);
         }
         ll s=0;
         ll l=0,r=x.size()-1;
         while(id.size()>0){
            if(s==0){
                a[id.front()]=x[l++];
                id.pop_front();
            }
            else{
                a[id.back()]=x[l++];
                id.pop_back();
            }
            s=1-s;
         }
         for(ll i=0;i<n;i++)cout<<a[i]<<" ";
         cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long int ll;
#define endl '\n'
using namespace std;
int main(){
    ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--){
            ll n;
            cin>>n;
         ll a[n],c[n];
         for(ll i=0;i<n;i++)cin>>a[i];
         for(ll i=0;i<n;i++)cin>>c[i];
         vector<ll>x;
         for(ll i=0;i<n;i++){
            if(c[i]==0)x.push_back(a[i]);
         }
         sort(x.begin(),x.end());
         reverse(x.begin(),x.end());
         deque<ll>id;
         for(ll i=0;i<n;i++){
            if(c[i]==0)id.push_back(i);
         }
         ll s=0;
         ll l=0,r=x.size()-1;
         while(id.size()>0){
            if(s==1){
                a[id.front()]=x[l++];
                id.pop_front();
            }
            else{
                a[id.back()]=x[l++];
                id.pop_back();
            }
            s=1-s;
         }
         for(ll i=0;i<n;i++)cout<<a[i]<<" ";
         cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long int ll;
#define endl '\n'
using namespace std;
int main(){
    ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--){
            ll n;
            cin>>n;
         ll a[n],c[n];
         ll sum=0;
         for(ll i=0;i<n;i++)cin>>a[i];
         for(ll i=0;i<n;i++)cin>>c[i];
         vector<ll>x;
         for(ll i=0;i<n;i++){
            sum+=a[i];
            if(c[i]==0)x.push_back(a[i]);
         }
         sort(x.begin(),x.end());
         for(ll i=n-1;i>=0;i--){
            if(c[i])sum-=a[i];
            else{

                    a[i]=x[0];
                    sum-=x[0];
                    x.erase(x.begin());
            }
         }
         for(ll i=0;i<n;i++){
            cout<<a[i]<<" ";
         }
         cout<<endl;

    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long int ll;
#define endl '\n'
using namespace std;
int main(){
    ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--){
            ll n;
            cin>>n;
         ll a[n],c[n];
         for(ll i=0;i<n;i++)cin>>a[i];
         for(ll i=0;i<n;i++)cin>>c[i];
         vector<ll>x;
         for(ll i=0;i<n;i++){
            if(c[i]==0)x.push_back(a[i]);
         }
         sort(x.begin(),x.end());
         reverse(x.begin(),x.end());
         ll s=0;
         for(ll i=0;i<n;i++){
            if(c[i])cout<<a[i]<<" ";
            else{
                    if(s==0){
                cout<<x.back()<<" ";
                x.pop_back();}
                else{
                    cout<<x[0]<<" ";
                    x.erase(x.begin());
                }
                s^=1;
            }
         }
         cout<<endl;
    }
    return 0;
}


//#define LOCAL
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mem(a, b) memset(a,b,sizeof(a))
#define sz(a) (int)a.size()
#define INF 0x3f3f3f3f
#define DNF 0x7f
#define DBG printf("this is a input\n")
#define fi first
#define se second
#define mk(a, b) make_pair(a,b)
#define pb push_back
#define LF putchar('\n')
#define SP putchar(' ')
#define p_queue priority_queue
#define CLOSE ios::sync_with_stdio(0); cin.tie(0)

template<typename T>
void read(T &x) {x = 0;char ch = getchar();ll f = 1;while(!isdigit(ch)){if(ch == '-')f *= -1;ch = getchar();}while(isdigit(ch)){x = x * 10 + ch - 48; ch = getchar();}x *= f;}
template<typename T, typename... Args>
void read(T &first, Args& ... args) {read(first);read(args...);}
template<typename T>
void write(T arg) {T x = arg;if(x < 0) {putchar('-'); x =- x;}if(x > 9) {write(x / 10);}putchar(x % 10 + '0');}
template<typename T, typename ... Ts>
void write(T arg, Ts ... args) {write(arg);if(sizeof...(args) != 0) {putchar(' ');write(args ...);}}
using namespace std;

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

const int N = 20005;
int T;
int a[N] , b[N];
vector <int> x, y;
int main (void)
{
    read (T);
    while (T --)
    {
        x.clear() , y.clear();
        int n;
        read (n);
        for (int i = 1 ; i <= n ; i ++)
            read(a[i]);
        for (int i = 1 ; i <= n ; i ++)
            read (b[i]);
        for (int i = 1 ; i <= n ; i ++) {
            if (b[i] == 0) {
                if (a[i] < 0)
                    x.pb(a[i]);
                else
                    y.pb(a[i]);
            }
        }
        sort (y.begin() , y.end());
        int curx = 0 , cury = y.size() - 1;
        for (int i = 1 ; i <= n ; i ++)
        {
            if (b[i] == 0 && curx < x.size())
                a[i] = x[curx] , curx ++, b[i] = 1;
        }
        for (int i = 1 ; i <= n ; i ++)
        {
            if (b[i] == 0 && cury >= 0)
                a[i] = y[cury] , cury -- , b[i] = 0;
        }
        for (int i = 1 ; i<= n ; i ++)
            cout << a[i] << ' ';
        cout << endl;

    }
}