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

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


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

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


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
#include <cassert>
//#include <unordered_set>
//#include <unordered_map>
#define ll              long long
#define pii             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
#define forn(i, n)      for(int i = 0; i < int(n); i++)
using namespace std;
int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-6;
const ll mod = 1e9 + 7;

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
inline ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}
void exgcd(ll A, ll B, ll& x, ll& y)
{
    if (B) exgcd(B, A % B, y, x), y -= A / B * x; else x = 1, y = 0;
}
inline int qpow(int x, ll n) {
    int r = 1;
    while (n > 0) {
        if (n & 1) r = 1ll * r * x % mod;
        n >>= 1; x = 1ll * x * x % mod;
    }
    return r;
}
inline int inv(int x) {
    return qpow(x, mod - 2);
}
ll lcm(ll a, ll b)
{
    return a * b / gcd(a, b);
}

/**********************************************************/
const int N = 200 + 5;

int main()
{
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int T;
    cin >> T;
    while (T--)
    {
        int n;
        cin >> n;
        vector<ll> a(n + 1), b(n + 1);
        vector<int> lock(n + 1);
        rep(i, 1, n)
            cin >> a[i],b[i]=a[i];
        rep(i, 1, n)
            cin >> lock[i];
        rep(i, 1, n)
        {
            rep(j, i, n)
            {
                if (a[i] < a[j] && lock[i]==0 &&lock[j]==0)
                    swap(a[i], a[j]);
            }
        }
        ll sum = 0, fg = 0;
        rep(i, 1, n)
        {
            sum += a[i];
            if (sum < 0)
                fg = 1;
        }
        if (!fg)
        {
            rep(i, 1, n)
                cout << a[i] << " ";
            cout << endl;
            continue;
        }
        rep(i, 1, n)
        {
            rep(j, i, n)
            {
                if (b[i] > b[j] && lock[i] == 0 && lock[j] == 0)
                    swap(b[i], b[j]);
            }
        }
        rep(i, 1, n)
            cout << b[i] << " ";
        cout << endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
#include <cassert>
//#include <unordered_set>
//#include <unordered_map>
#define ll              long long
#define pii             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
#define forn(i, n)      for(int i = 0; i < int(n); i++)
using namespace std;
int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-6;
const ll mod = 1e9 + 7;

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
inline ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}
void exgcd(ll A, ll B, ll& x, ll& y)
{
    if (B) exgcd(B, A % B, y, x), y -= A / B * x; else x = 1, y = 0;
}
inline int qpow(int x, ll n) {
    int r = 1;
    while (n > 0) {
        if (n & 1) r = 1ll * r * x % mod;
        n >>= 1; x = 1ll * x * x % mod;
    }
    return r;
}
inline int inv(int x) {
    return qpow(x, mod - 2);
}
ll lcm(ll a, ll b)
{
    return a * b / gcd(a, b);
}

/**********************************************************/
const int N = 200 + 5;

int main()
{
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int T;
    cin >> T;
    while (T--)
    {
        int n;
        vector<ll> a(n + 1), b(n + 1), p1(n + 1), p2(n + 1);
        vector<int> lock(n + 1);
        rep(i, 1, n)
            cin >> a[i],b[i]=a[i];
        rep(i, 1, n)
            cin >> lock[i];
        rep(i, 1, n)
        {
            rep(j, i, n)
            {
                if (a[i] < a[j] && lock[i]==0 &&lock[j]==0)
                    swap(a[i], a[j]);
            }
        }
        rep(i, 1, n)
            p1[i] = p1[i - 1] + a[i];
        if (p1[n] >= 0)
        {
            rep(i, 1, n)
                cout << a[i] << " ";
            cout << endl;
            continue;
        }
        rep(i, 1, n)
        {
            rep(j, i, n)
            {
                if (b[i] > b[j] && lock[i] == 0 && lock[j] == 0)
                    swap(b[i], b[j]);
            }
        }
        rep(i, 1, n)
        {
            rep(i, 1, n)
                cout << b[i] << " ";
            cout << endl;
        }
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
#include <cassert>
//#include <unordered_set>
//#include <unordered_map>
#define ll              long long
#define pii             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
#define forn(i, n)      for(int i = 0; i < int(n); i++)
using namespace std;
int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-6;
const ll mod = 1e9 + 7;

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
inline ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}
void exgcd(ll A, ll B, ll& x, ll& y)
{
    if (B) exgcd(B, A % B, y, x), y -= A / B * x; else x = 1, y = 0;
}
inline int qpow(int x, ll n) {
    int r = 1;
    while (n > 0) {
        if (n & 1) r = 1ll * r * x % mod;
        n >>= 1; x = 1ll * x * x % mod;
    }
    return r;
}
inline int inv(int x) {
    return qpow(x, mod - 2);
}
ll lcm(ll a, ll b)
{
    return a * b / gcd(a, b);
}

/**********************************************************/
const int N = 5000 + 5;

int dp[N][N];

int main()
{
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int T;
    cin >> T;
    while (T--)
    {
        int n;
        vector<int> lock(n+1),a(n + 1);
        rep(i, 1, n)
            cin >> a[i];
        rep(i, 1, n)
            cin >> lock[i];
        rep(i, 1, n)
        {
            rep(j, i, n)
            {
                if (a[i] < a[j] && lock[i] == 0 && lock[j] == 0)
                    swap(a[j], a[i]);
            }
        }
        rep(i, 1, n)
        {
            cout << a[i] << " ";
        }
        cout << endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
#include <cassert>
//#include <unordered_set>
//#include <unordered_map>
#define ll              long long
#define pii             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
#define forn(i, n)      for(int i = 0; i < int(n); i++)
using namespace std;
int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-6;
const ll mod = 1e9 + 7;

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
inline ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}
void exgcd(ll A, ll B, ll& x, ll& y)
{
    if (B) exgcd(B, A % B, y, x), y -= A / B * x; else x = 1, y = 0;
}
inline int qpow(int x, ll n) {
    int r = 1;
    while (n > 0) {
        if (n & 1) r = 1ll * r * x % mod;
        n >>= 1; x = 1ll * x * x % mod;
    }
    return r;
}
inline int inv(int x) {
    return qpow(x, mod - 2);
}
ll lcm(ll a, ll b)
{
    return a * b / gcd(a, b);
}

/**********************************************************/
const int N = 200 + 5;

int main()
{
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int T;
    cin >> T;
    while (T--)
    {
        int n;
        cin >> n;
        vector<ll> a(n + 1), b(n + 1);
        vector<int> lock(n + 1);
        rep(i, 1, n)
            cin >> a[i],b[i]=a[i];
        rep(i, 1, n)
            cin >> lock[i];
        rep(i, 1, n)
        {
            rep(j, i, n)
            {
                if (a[i] < a[j] && lock[i]==0 &&lock[j]==0)
                    swap(a[i], a[j]);
            }
        }
        ll sum = 0, fg = 0;
        rep(i, 1, n)
        {
            sum += a[i];
            if (sum < 0)
                fg = 1;
        }
        if (!fg)
        {
            rep(i, 1, n)
                cout << a[i] << " ";
            cout << endl;
            continue;
        }
        rep(i, 1, n)
        {
            rep(j, i, n)
            {
                if (b[i] > b[j] && lock[i] == 0 && lock[j] == 0)
                    swap(b[i], b[j]);
            }
        }
        rep(i, 1, n)
        {
            rep(i, 1, n)
                cout << b[i] << " ";
            cout << endl;
        }
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
#include <cassert>
//#include <unordered_set>
//#include <unordered_map>
#define ll              long long
#define pii             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
#define forn(i, n)      for(int i = 0; i < int(n); i++)
using namespace std;
int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-6;
const ll mod = 1e9 + 7;

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
inline ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}
void exgcd(ll A, ll B, ll& x, ll& y)
{
    if (B) exgcd(B, A % B, y, x), y -= A / B * x; else x = 1, y = 0;
}
inline int qpow(int x, ll n) {
    int r = 1;
    while (n > 0) {
        if (n & 1) r = 1ll * r * x % mod;
        n >>= 1; x = 1ll * x * x % mod;
    }
    return r;
}
inline int inv(int x) {
    return qpow(x, mod - 2);
}
ll lcm(ll a, ll b)
{
    return a * b / gcd(a, b);
}

/**********************************************************/
const int N = 200 + 5;

int main()
{
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int T;
    cin >> T;
    while (T--)
    {
        int n;
        cin >> n;
        vector<ll> a(n + 1), b(n + 1), p1(n + 1), p2(n + 1);
        vector<int> lock(n + 1);
        rep(i, 1, n)
            cin >> a[i],b[i]=a[i];
        rep(i, 1, n)
            cin >> lock[i];
        rep(i, 1, n)
        {
            rep(j, i, n)
            {
                if (a[i] < a[j] && lock[i]==0 &&lock[j]==0)
                    swap(a[i], a[j]);
            }
        }
        rep(i, 1, n)
            p1[i] = p1[i - 1] + a[i];
        if (p1[n] >= 0)
        {
            rep(i, 1, n)
                cout << a[i] << " ";
            cout << endl;
            continue;
        }
        rep(i, 1, n)
        {
            rep(j, i, n)
            {
                if (b[i] > b[j] && lock[i] == 0 && lock[j] == 0)
                    swap(b[i], b[j]);
            }
        }
        rep(i, 1, n)
        {
            rep(i, 1, n)
                cout << b[i] << " ";
            cout << endl;
        }
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
#include <cassert>
//#include <unordered_set>
//#include <unordered_map>
#define ll              long long
#define pii             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
#define forn(i, n)      for(int i = 0; i < int(n); i++)
using namespace std;
int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-6;
const ll mod = 1e9 + 7;

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
inline ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}
void exgcd(ll A, ll B, ll& x, ll& y)
{
    if (B) exgcd(B, A % B, y, x), y -= A / B * x; else x = 1, y = 0;
}
inline int qpow(int x, ll n) {
    int r = 1;
    while (n > 0) {
        if (n & 1) r = 1ll * r * x % mod;
        n >>= 1; x = 1ll * x * x % mod;
    }
    return r;
}
inline int inv(int x) {
    return qpow(x, mod - 2);
}
ll lcm(ll a, ll b)
{
    return a * b / gcd(a, b);
}

/**********************************************************/
const int N = 5000 + 5;

int dp[N][N];

int main()
{
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int T;
    cin >> T;
    while (T--)
    {
        int n;
        cin >> n;
        vector<int> lock(n+1),a(n + 1);
        rep(i, 1, n)
            cin >> a[i];
        rep(i, 1, n)
            cin >> lock[i];
        rep(i, 1, n)
        {
            rep(j, i, n)
            {
                if (a[i] < a[j] && lock[i] == 0 && lock[j] == 0)
                    swap(a[j], a[i]);
            }
        }
        rep(i, 1, n)
        {
            cout << a[i] << " ";
        }
        cout << endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
#include <cassert>
//#include <unordered_set>
//#include <unordered_map>
#define ll              long long
#define pii             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
#define forn(i, n)      for(int i = 0; i < int(n); i++)
using namespace std;
int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-6;
const ll mod = 1e9 + 7;

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
inline ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}
void exgcd(ll A, ll B, ll& x, ll& y)
{
    if (B) exgcd(B, A % B, y, x), y -= A / B * x; else x = 1, y = 0;
}
inline int qpow(int x, ll n) {
    int r = 1;
    while (n > 0) {
        if (n & 1) r = 1ll * r * x % mod;
        n >>= 1; x = 1ll * x * x % mod;
    }
    return r;
}
inline int inv(int x) {
    return qpow(x, mod - 2);
}
ll lcm(ll a, ll b)
{
    return a * b / gcd(a, b);
}

/**********************************************************/
const int N = 200 + 5;

int main()
{
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int T;
    cin >> T;
    while (T--)
    {
        int n;
        cin >> n;
        vector<ll> a(n + 1), b(n + 1), p1(n + 1);
        vector<int> lock(n + 1);
        rep(i, 1, n)
            cin >> a[i],b[i]=a[i];
        rep(i, 1, n)
            cin >> lock[i];
        rep(i, 1, n)
        {
            rep(j, i, n)
            {
                if (a[i] < a[j] && lock[i]==0 &&lock[j]==0)
                    swap(a[i], a[j]);
            }
        }
        ll sum = 0, fg = 0;
        rep(i, 1, n)
        {
            sum += a[i];
            if (sum < 0)
                fg = 1;
        }
        if (fg)
        {
            rep(i, 1, n)
                cout << a[i] << " ";
            cout << endl;
            continue;
        }
        rep(i, 1, n)
        {
            rep(j, i, n)
            {
                if (b[i] > b[j] && lock[i] == 0 && lock[j] == 0)
                    swap(b[i], b[j]);
            }
        }
        rep(i, 1, n)
        {
            rep(i, 1, n)
                cout << b[i] << " ";
            cout << endl;
        }
    }
    return 0;
}


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

typedef long long ll;
typedef long double ld;
#define umap unordered_map
#define size(a) int((a).size())
#define present(c, x) (c.find(x) != c.end())
#define printVerdict(verdict) cout << (verdict ? "YES": "NO") << '\n'
#define printDecimal(d) printf("%.9f\n", d)
#define inrange(val, start, end) (val >= start && val <= end)
#define var(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

const ll mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;

template <class T1, class T2, class T3>
void printTuple(tuple<T1, T2, T3> t) { cout << get<0>(t) << " " << get<1>(t) << " " << get<2>(t) << '\n'; }
template <class T1, class T2>
void printPair(pair<T1, T2> p) { cout << p.first << " " << p.second  << '\n'; }
template <class T>
void printArray(vector<T> arr) { for (int i = 0; i<size(arr); i++) {cout << arr[i] << " ";} cout << '\n'; }

int main() {
	std::ios_base::sync_with_stdio(false);
	cin.tie(0);
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		vector<int> a(n);
		for (int i = 0; i<n; i++) cin >> a[i];
		int first_unlocked = -1;
		vector<int> unlocked, idx;
		for (int i = 0; i<n; i++) {
			int temp; cin >> temp;
			if (!temp) {
				unlocked.push_back(a[i]);
				idx.push_back(i);
				if (first_unlocked == -1) first_unlocked = i;
			}
		}
		if (first_unlocked != -1) {
			vector<int> pref(n);
			sort(unlocked.begin(), unlocked.end());
			for (int i = 0; i<size(unlocked); i++) a[idx[i]]= unlocked[i];
			pref[0] = a[0];
			int order = (pref[0] < 0 ? 0 : -1);
			for (int i= 1; i<n; i++) {
				pref[i] = pref[i-1] + a[i];
				if (pref[i] < 0) order = i;
			}

			reverse(unlocked.begin(), unlocked.end());
			for (int i = 0; i<size(unlocked); i++) a[idx[i]] = unlocked[i];
			pref[0] = a[0];
			int order2 = (pref[0] < 0 ? 0 : -1);
			for (int i = 1; i<n; i++) {
				pref[i] = pref[i-1] + a[i];
				if (pref[i] < 0) order2 = i;
			}
			if (order < order2) {
				reverse(unlocked.begin(), unlocked.end());
				for (int i = 0; i<size(unlocked); i++) a[idx[i]] = unlocked[i];
			}
		}
		printArray(a);
	}

}


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

typedef long long ll;
typedef long double ld;
#define umap unordered_map
#define size(a) int((a).size())
#define present(c, x) (c.find(x) != c.end())
#define printVerdict(verdict) cout << (verdict ? "YES": "NO") << '\n'
#define printDecimal(d) printf("%.9f\n", d)
#define inrange(val, start, end) (val >= start && val <= end)
#define var(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;

template <class T1, class T2, class T3>
void printTuple(tuple<T1, T2, T3> t) { cout << get<0>(t) << " " << get<1>(t) << " " << get<2>(t) << '\n'; }
template <class T1, class T2>
void printPair(pair<T1, T2> p) { cout << p.first << " " << p.second  << '\n'; }
template <class T>
void printArray(vector<T> arr) { for (int i = 0; i<size(arr); i++) {cout << arr[i] << " ";} cout << '\n'; }

int main() {
	std::ios_base::sync_with_stdio(false);
	cin.tie(0);
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		vector<int> a(n);
		int first_unlocked = -1;
		for (int i = 0; i<n; i++) cin >> a[i];
		vector<int> unlocked, idx;
		for (int i = 0; i<n; i++) {
			int temp; cin >> temp;
			if (!temp) {
				unlocked.push_back(a[i]);
				idx.push_back(i);
			}
		}
		sort(unlocked.rbegin(), unlocked.rend());
		for (int i = 0; i<size(unlocked); i++) a[idx[i]] = unlocked[i];
		printArray(a);
	}
}


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

typedef long long ll;
typedef long double ld;
#define umap unordered_map
#define size(a) int((a).size())
#define present(c, x) (c.find(x) != c.end())
#define printVerdict(verdict) cout << (verdict ? "YES": "NO") << '\n'
#define printDecimal(d) printf("%.9f\n", d)
#define inrange(val, start, end) (val >= start && val <= end)
#define var(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

const ll mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;

template <class T1, class T2, class T3>
void printTuple(tuple<T1, T2, T3> t) { cout << get<0>(t) << " " << get<1>(t) << " " << get<2>(t) << '\n'; }
template <class T1, class T2>
void printPair(pair<T1, T2> p) { cout << p.first << " " << p.second  << '\n'; }
template <class T>
void printArray(vector<T> arr) { for (int i = 0; i<size(arr); i++) {cout << arr[i] << " ";} cout << '\n'; }

int main() {
	std::ios_base::sync_with_stdio(false);
	cin.tie(0);
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		vector<int> a(n);
		for (int i = 0; i<n; i++) cin >> a[i];
		int first_unlocked = -1;
		vector<int> unlocked, idx;
		for (int i = 0; i<n; i++) {
			int temp; cin >> temp;
			if (!temp) {
				unlocked.push_back(a[i]);
				idx.push_back(i);
				if (first_unlocked == -1) first_unlocked = i;
			}
		}
		if (first_unlocked != -1) {
			vector<int> pref(n);
			sort(unlocked.begin(), unlocked.end());
			for (int i = 0; i<size(unlocked); i++) a[idx[i]]= unlocked[i];
			pref[0] = a[0];
			int order = (pref[0] < 0 ? 0 : 1e9);
			for (int i= 1; i<n; i++) {
				pref[i] = pref[i-1] + a[i];
				if (pref[i] < 0) order = i;
			}

			reverse(unlocked.begin(), unlocked.end());
			for (int i = 0; i<size(unlocked); i++) a[idx[i]] = unlocked[i];
			pref[0] = a[0];
			int order2 = (pref[0] < 0 ? 0 : 1e9);
			for (int i = 1; i<n; i++) {
				pref[i] = pref[i-1] + a[i];
				if (pref[i] < 0) order2 = i;
			}
			if (order < order2) {
				reverse(unlocked.begin(), unlocked.end());
				for (int i = 0; i<size(unlocked); i++) a[idx[i]] = unlocked[i];
			}
		}
		printArray(a);
	}

}


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

typedef long long ll;
typedef long double ld;
#define umap unordered_map
#define size(a) int((a).size())
#define present(c, x) (c.find(x) != c.end())
#define printVerdict(verdict) cout << (verdict ? "YES": "NO") << '\n'
#define printDecimal(d) printf("%.9f\n", d)
#define inrange(val, start, end) (val >= start && val <= end)
#define var(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

const ll mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;

template <class T1, class T2, class T3>
void printTuple(tuple<T1, T2, T3> t) { cout << get<0>(t) << " " << get<1>(t) << " " << get<2>(t) << '\n'; }
template <class T1, class T2>
void printPair(pair<T1, T2> p) { cout << p.first << " " << p.second  << '\n'; }
template <class T>
void printArray(vector<T> arr) { for (int i = 0; i<size(arr); i++) {cout << arr[i] << " ";} cout << '\n'; }

int main() {
	std::ios_base::sync_with_stdio(false);
	cin.tie(0);
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		vector<int> a(n);
		for (int i = 0; i<n; i++) cin >> a[i];
		int first_unlocked = -1;
		vector<int> unlocked, idx;
		for (int i = 0; i<n; i++) {
			int temp; cin >> temp;
			if (!temp) {
				unlocked.push_back(a[i]);
				idx.push_back(i);
				if (first_unlocked == -1) first_unlocked = i;
			}
		}
		if (first_unlocked != -1) {
			int before_unlocked = 0, after_unlocked = 0;
			for (int i = 0; i<first_unlocked; i++) before_unlocked += a[i];
			for (int i = first_unlocked; i<n; i++) after_unlocked += a[i];
			if (before_unlocked >= 0 || after_unlocked < 0) sort(unlocked.begin(), unlocked.end());
			else sort(unlocked.rbegin(), unlocked.rend());
			for (int i = 0; i<size(unlocked); i++) a[idx[i]] = unlocked[i];
		}
		printArray(a);
	}

}


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

typedef long long ll;
typedef long double ld;
#define umap unordered_map
#define size(a) int((a).size())
#define present(c, x) (c.find(x) != c.end())
#define printVerdict(verdict) cout << (verdict ? "YES": "NO") << '\n'
#define printDecimal(d) printf("%.9f\n", d)
#define inrange(val, start, end) (val >= start && val <= end)
#define var(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;

template <class T1, class T2, class T3>
void printTuple(tuple<T1, T2, T3> t) { cout << get<0>(t) << " " << get<1>(t) << " " << get<2>(t) << '\n'; }
template <class T1, class T2>
void printPair(pair<T1, T2> p) { cout << p.first << " " << p.second  << '\n'; }
template <class T>
void printArray(vector<T> arr) { for (int i = 0; i<size(arr); i++) {cout << arr[i] << " ";} cout << '\n'; }

int main() {
	std::ios_base::sync_with_stdio(false);
	cin.tie(0);
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		vector<int> a(n);
		int first_unlocked = -1;
		for (int i = 0; i<n; i++) cin >> a[i];
		vector<int> unlocked, idx;
		for (int i = 0; i<n; i++) {
			int temp; cin >> temp;
			if (!temp) {
				unlocked.push_back(a[i]);
				idx.push_back(i);
				if (first_unlocked == -1) first_unlocked = i;
			}
		}
		vector<int> pref(n);
		pref[0] = a[0];

		for (int i = 1; i< first_unlocked && first_unlocked != -1; i++) pref[i] = pref[i-1] + a[i];
		if (first_unlocked != -1 && pref[first_unlocked-1] < 0) sort(unlocked.rbegin(), unlocked.rend());
		else if (first_unlocked != -1) sort(unlocked.begin(), unlocked.end());
		for (int i = 0; i<size(unlocked); i++) a[idx[i]] = unlocked[i];
		printArray(a);
	}
}


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

typedef long long ll;
typedef long double ld;
#define umap unordered_map
#define size(a) int((a).size())
#define present(c, x) (c.find(x) != c.end())
#define printVerdict(verdict) cout << (verdict ? "YES": "NO") << '\n'
#define printDecimal(d) printf("%.9f\n", d)
#define inrange(val, start, end) (val >= start && val <= end)
#define var(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

const ll mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;

template <class T1, class T2, class T3>
void printTuple(tuple<T1, T2, T3> t) { cout << get<0>(t) << " " << get<1>(t) << " " << get<2>(t) << '\n'; }
template <class T1, class T2>
void printPair(pair<T1, T2> p) { cout << p.first << " " << p.second  << '\n'; }
template <class T>
void printArray(vector<T> arr) { for (int i = 0; i<size(arr); i++) {cout << arr[i] << " ";} cout << '\n'; }

int main() {
	std::ios_base::sync_with_stdio(false);
	cin.tie(0);
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		vector<int> a(n);
		for (int i = 0; i<n; i++) cin >> a[i];
		int first_unlocked = -1;
		vector<int> unlocked, idx;
		for (int i = 0; i<n; i++) {
			int temp; cin >> temp;
			if (!temp) {
				unlocked.push_back(a[i]);
				idx.push_back(i);
				if (first_unlocked == -1) first_unlocked = i;
			}
		}
		if (first_unlocked != -1) {
			int before_unlocked = 0, after_unlocked = 0;
			for (int i = 0; i<first_unlocked; i++) before_unlocked += a[i];
			for (int i = first_unlocked; i<n; i++) after_unlocked += a[i];
			if (before_unlocked >= 0) {
				sort(unlocked.begin(), unlocked.end());
				for (int i = 0; i<size(unlocked); i++) a[idx[i]] = unlocked[i];
			}
			else if (after_unlocked >= 0){
				sort(unlocked.rbegin(), unlocked.rend());
				for (int i= 0; i<size(unlocked); i++) a[idx[i]] = unlocked[i];
			}
			else {
				sort(unlocked.begin(), unlocked.end());
				int curr_ptr = 0;
				while (!unlocked.empty() && after_unlocked < 0) {
					a[idx[curr_ptr]] = unlocked[0];
					after_unlocked -= unlocked[0];
					unlocked.erase(unlocked.begin());
					curr_ptr++;
				}
				sort(unlocked.rbegin(), unlocked.rend());
				for (int i = curr_ptr; i<size(idx); i++) a[idx[i]] = unlocked[i-curr_ptr];
			}
		}
		printArray(a);
	}

}


#include<bits/stdc++.h>
    using namespace std;
  #define int long long int
    #define endl "\n"
    #define MOD 1000000007
    #define mod 1000000007
    #define M 1000000007

    #define pb push_back
    #define take(a,b,c) for(int b=0;b<c;b++) cin>>a[b]
  //  #define mp make_pair
  #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  #define mx 300005
  #define fiint(a,b) memset(a,b,sizeof(a))
  #define bitcount __builtin_popcount
  #define fori(i,a,b) for(int i=a;i<b;i++)
  #define ford(i,a,b) for(int i=a;i>=b;i--)
  #define debug(x) cout<<x<<endl;
  #define cases(t) int t; cin>>t; while(t--)
  #define inf1 INT_MAX
  #define all(a) a.begin(),a.end()
  #define vec vector<int>
  #define pii pair<int,int>
  #define plii pair<int,int>
  #define pint pair<int,int>
  #define ff first
  #define ss second
  #define lb lower_bound
  #define ub upper_bound
  #define bs binary_search
  #define sz(x) (int)x.size()

int powerFunction(int x,int y){
    int res = 1;
    int p = MOD;
    x = x % p;
    while (y > 0){
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
bool prime[10000001];
int P=MOD-2;
int fact[10000001]={0};

void sieveFunction(int maxLimit){
    memset(prime, true, sizeof(prime));
    prime[0] = prime[1]=false;
    for(int i=2; maxLimit >= i ; i++){
        if(prime[i]){
            for(int j=2*i ; maxLimit >= j ; j+=i)
                prime[j]=false;
        }
    }
}

void factorialFunction(int maxLimit){
    fact[0]=1;
    for(int i=1 ; i <= maxLimit ; i++)
        fact[i]=(fact[i-1]*i)%MOD;
    return;
}

int nCr(int n,int r){

		 int a=fact[n];

		int b=powerFunction(fact[n-r],mod-2);

		int c=powerFunction(fact[r],mod-2);

		 return (a*(b%mod* c%mod)%mod)%mod;
}

signed  main()
{
    cases(t)
    {
        int n ;cin>> n;
        vec v(n);
        vec loc(n);
        map<pair<int,int>,int>m;
        for(int i = 0 ; i < n ;i++)
        cin>>v[i];
        for(int i = 0 ; i < n ;i++)
        {cin>>loc[i]; if(loc[i] == 0 )m[{v[i],i}] = i ; }

       while(m.size() > 0)
       {
           auto it = m.end();
           it--;
           for(int i = 0  ;i < (*it).ss; i++)
           {
               if(loc[i] == 0  && v[i] < (*it).ff.ff )
               {
                   if(m.find({v[i],i}) !=m.end() )
                   {
                       m[{v[i] , (*it).ss }] = (*it).ss;
                       swap(v[i] , v[(*it).ss]);
                       m.erase({v[i] , i});
                   }
                   else
                   {
                       swap(v[i], v[(*it).ss]);
                   }
               }

           }
             m.erase(it);
       }

        for(auto it : v)
        cout<<it<<" ";
        cout<<endl;

    }

}


#include<bits/stdc++.h>
    using namespace std;
  #define int long long int
    #define endl "\n"
    #define MOD 1000000007
    #define mod 1000000007
    #define M 1000000007

    #define pb push_back
    #define take(a,b,c) for(int b=0;b<c;b++) cin>>a[b]
  //  #define mp make_pair
  #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  #define mx 300005
  #define fiint(a,b) memset(a,b,sizeof(a))
  #define bitcount __builtin_popcount
  #define fori(i,a,b) for(int i=a;i<b;i++)
  #define ford(i,a,b) for(int i=a;i>=b;i--)
  #define debug(x) cout<<x<<endl;
  #define cases(t) int t; cin>>t; while(t--)
  #define inf1 INT_MAX
  #define all(a) a.begin(),a.end()
  #define vec vector<int>
  #define pii pair<int,int>
  #define plii pair<int,int>
  #define pint pair<int,int>
  #define ff first
  #define ss second
  #define lb lower_bound
  #define ub upper_bound
  #define bs binary_search
  #define sz(x) (int)x.size()

int powerFunction(int x,int y){
    int res = 1;
    int p = MOD;
    x = x % p;
    while (y > 0){
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
bool prime[10000001];
int P=MOD-2;
int fact[10000001]={0};

void sieveFunction(int maxLimit){
    memset(prime, true, sizeof(prime));
    prime[0] = prime[1]=false;
    for(int i=2; maxLimit >= i ; i++){
        if(prime[i]){
            for(int j=2*i ; maxLimit >= j ; j+=i)
                prime[j]=false;
        }
    }
}

void factorialFunction(int maxLimit){
    fact[0]=1;
    for(int i=1 ; i <= maxLimit ; i++)
        fact[i]=(fact[i-1]*i)%MOD;
    return;
}

int nCr(int n,int r){

		 int a=fact[n];

		int b=powerFunction(fact[n-r],mod-2);

		int c=powerFunction(fact[r],mod-2);

		 return (a*(b%mod* c%mod)%mod)%mod;
}

signed  main()
{
    cases(t)
    {
        int n ;cin>> n;
        vec v(n);
        vec loc(n);
  //  map<pair<int,int>,int>m;
        for(int i = 0 ; i < n ;i++)
        cin>>v[i];
        for(int i = 0 ; i < n ;i++)
        {cin>>loc[i];  }

    //    while(m.size() > 0)
    //    {
    //        auto it = m.end();
    //        it--;
    //        for(int i = 0  ;i < (*it).ss; i++)
    //        {
    //            if(loc[i] == 0  && v[i] < (*it).ff.ff )
    //            {
    //                if(m.find({v[i],i}) !=m.end() )
    //                {
    //                    m[{v[i] , (*it).ss }] = (*it).ss;
    //                    swap(v[i] , v[(*it).ss]);
    //                    m.erase({v[i] , i});
    //                }
    //                else
    //                {
    //                    swap(v[i], v[(*it).ss]);
    //                }
    //            }

    //        }
    //          m.erase(it);
    //    }

       for(int i = n-1 ; i>=0 ; i--)
       {

            if(v[i] > 0 &&  loc[i] == 0 )
            {
                for(int j = 0 ; j < i ;j++)
                {
                    if(loc[j] ==  0 && v[i] > v[j]  )
                    {
                        if(v[i] > 0 )
                        {
                            swap(v[i] , v[j]);
                            j++;
                        }
                        else swap(v[i] , v[j]);
                        break;
                    }
                }

            }

       }

        for(auto it : v)
        cout<<it<<" ";
        cout<<endl;

    }

}


#include<bits/stdc++.h>
    using namespace std;
  #define int long long int
    #define endl "\n"
    #define MOD 1000000007
    #define mod 1000000007
    #define M 1000000007

    #define pb push_back
    #define take(a,b,c) for(int b=0;b<c;b++) cin>>a[b]
  //  #define mp make_pair
  #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  #define mx 300005
  #define fiint(a,b) memset(a,b,sizeof(a))
  #define bitcount __builtin_popcount
  #define fori(i,a,b) for(int i=a;i<b;i++)
  #define ford(i,a,b) for(int i=a;i>=b;i--)
  #define debug(x) cout<<x<<endl;
  #define cases(t) int t; cin>>t; while(t--)
  #define inf1 INT_MAX
  #define all(a) a.begin(),a.end()
  #define vec vector<int>
  #define pii pair<int,int>
  #define plii pair<int,int>
  #define pint pair<int,int>
  #define ff first
  #define ss second
  #define lb lower_bound
  #define ub upper_bound
  #define bs binary_search
  #define sz(x) (int)x.size()

int powerFunction(int x,int y){
    int res = 1;
    int p = MOD;
    x = x % p;
    while (y > 0){
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
bool prime[10000001];
int P=MOD-2;
int fact[10000001]={0};

void sieveFunction(int maxLimit){
    memset(prime, true, sizeof(prime));
    prime[0] = prime[1]=false;
    for(int i=2; maxLimit >= i ; i++){
        if(prime[i]){
            for(int j=2*i ; maxLimit >= j ; j+=i)
                prime[j]=false;
        }
    }
}

void factorialFunction(int maxLimit){
    fact[0]=1;
    for(int i=1 ; i <= maxLimit ; i++)
        fact[i]=(fact[i-1]*i)%MOD;
    return;
}

int nCr(int n,int r){

		 int a=fact[n];

		int b=powerFunction(fact[n-r],mod-2);

		int c=powerFunction(fact[r],mod-2);

		 return (a*(b%mod* c%mod)%mod)%mod;
}

signed  main()
{
    cases(t)
    {
        int n ;cin>> n;
        vec v(n);
        vec loc(n);
    map<pair<int,int>,int>m;
        for(int i = 0 ; i < n ;i++)
        cin>>v[i];
        for(int i = 0 ; i < n ;i++)
        {cin>>loc[i];  }

       while(m.size() > 0)
       {
           auto it = m.end();
           it--;
           for(int i = 0  ;i < (*it).ss; i++)
           {
               if(loc[i] == 0  && v[i] < (*it).ff.ff )
               {
                   if(m.find({v[i],i}) !=m.end() )
                   {
                       m[{v[i] , (*it).ss }] = (*it).ss;
                       swap(v[i] , v[(*it).ss]);
                       m.erase({v[i] , i});
                   }
                   else
                   {
                       swap(v[i], v[(*it).ss]);
                   }
               }

           }
             m.erase(it);
       }

       for(int i = n-1 ; i>=0 ; i--)
       {

            if(v[i] > 0 &&  loc[i] == 0 )
            {
                for(int j = 0 ; j < i ;j++)
                {
                    if(loc[j] ==  0 && v[i] > v[j]  )
                    {
                        if(v[j] > 0 )
                        {
                            swap(v[i] , v[j]);
                            i++;
                        }
                        else swap(v[i] , v[j]);
                        break;
                    }
                }

            }

       }

        for(auto it : v)
        cout<<it<<" ";
        cout<<endl;

    }

}


#include<bits/stdc++.h>
    using namespace std;
  #define int long long int
    #define endl "\n"
    #define MOD 1000000007
    #define mod 1000000007
    #define M 1000000007

    #define pb push_back
    #define take(a,b,c) for(int b=0;b<c;b++) cin>>a[b]
  //  #define mp make_pair
  #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  #define mx 300005
  #define fiint(a,b) memset(a,b,sizeof(a))
  #define bitcount __builtin_popcount
  #define fori(i,a,b) for(int i=a;i<b;i++)
  #define ford(i,a,b) for(int i=a;i>=b;i--)
  #define debug(x) cout<<x<<endl;
  #define cases(t) int t; cin>>t; while(t--)
  #define inf1 INT_MAX
  #define all(a) a.begin(),a.end()
  #define vec vector<int>
  #define pii pair<int,int>
  #define plii pair<int,int>
  #define pint pair<int,int>
  #define ff first
  #define ss second
  #define lb lower_bound
  #define ub upper_bound
  #define bs binary_search
  #define sz(x) (int)x.size()

int powerFunction(int x,int y){
    int res = 1;
    int p = MOD;
    x = x % p;
    while (y > 0){
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
bool prime[10000001];
int P=MOD-2;
int fact[10000001]={0};

void sieveFunction(int maxLimit){
    memset(prime, true, sizeof(prime));
    prime[0] = prime[1]=false;
    for(int i=2; maxLimit >= i ; i++){
        if(prime[i]){
            for(int j=2*i ; maxLimit >= j ; j+=i)
                prime[j]=false;
        }
    }
}

void factorialFunction(int maxLimit){
    fact[0]=1;
    for(int i=1 ; i <= maxLimit ; i++)
        fact[i]=(fact[i-1]*i)%MOD;
    return;
}

int nCr(int n,int r){

		 int a=fact[n];

		int b=powerFunction(fact[n-r],mod-2);

		int c=powerFunction(fact[r],mod-2);

		 return (a*(b%mod* c%mod)%mod)%mod;
}

signed  main()
{
    cases(t)
    {
        int n ;cin>> n;
        vec v(n);
        vec loc(n);
        map<pair<int,int>,int>m;
        for(int i = 0 ; i < n ;i++)
        cin>>v[i];
        for(int i = 0 ; i < n ;i++)
        {cin>>loc[i]; if(loc[i] == 0 )m[{v[i],i}] = i ; }

       while(m.size() > 0)
       {
           auto it = m.end();
           it--;
           for(int i = 0  ;i < (*it).ss; i++)
           {
               if(loc[i] == 0  && v[i] < (*it).ff.ff )
               {
                   if(m.find({v[i],i}) !=m.end() )
                   {
                       m[{v[i] , (*it).ss }] = (*it).ss;
                       swap(v[i] , v[(*it).ss]);
                       m.erase({v[i] , i});
                   }
                   else
                   {
                       swap(v[i], v[(*it).ss]);
                   }
                   break;
               }

           }
             m.erase(it);
       }

        for(auto it : v)
        cout<<it<<" ";
        cout<<endl;

    }

}


#include<bits/stdc++.h>
    using namespace std;
  #define int long long int
    #define endl "\n"
    #define MOD 1000000007
    #define mod 1000000007
    #define M 1000000007

    #define pb push_back
    #define take(a,b,c) for(int b=0;b<c;b++) cin>>a[b]
  //  #define mp make_pair
  #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  #define mx 300005
  #define fiint(a,b) memset(a,b,sizeof(a))
  #define bitcount __builtin_popcount
  #define fori(i,a,b) for(int i=a;i<b;i++)
  #define ford(i,a,b) for(int i=a;i>=b;i--)
  #define debug(x) cout<<x<<endl;
  #define cases(t) int t; cin>>t; while(t--)
  #define inf1 INT_MAX
  #define all(a) a.begin(),a.end()
  #define vec vector<int>
  #define pii pair<int,int>
  #define plii pair<int,int>
  #define pint pair<int,int>
  #define ff first
  #define ss second
  #define lb lower_bound
  #define ub upper_bound
  #define bs binary_search
  #define sz(x) (int)x.size()

int powerFunction(int x,int y){
    int res = 1;
    int p = MOD;
    x = x % p;
    while (y > 0){
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
bool prime[10000001];
int P=MOD-2;
int fact[10000001]={0};

void sieveFunction(int maxLimit){
    memset(prime, true, sizeof(prime));
    prime[0] = prime[1]=false;
    for(int i=2; maxLimit >= i ; i++){
        if(prime[i]){
            for(int j=2*i ; maxLimit >= j ; j+=i)
                prime[j]=false;
        }
    }
}

void factorialFunction(int maxLimit){
    fact[0]=1;
    for(int i=1 ; i <= maxLimit ; i++)
        fact[i]=(fact[i-1]*i)%MOD;
    return;
}

int nCr(int n,int r){

		 int a=fact[n];

		int b=powerFunction(fact[n-r],mod-2);

		int c=powerFunction(fact[r],mod-2);

		 return (a*(b%mod* c%mod)%mod)%mod;
}

signed  main()
{
    cases(t)
    {
        int n ;cin>> n;
        vec v(n);
        vec loc(n);
  //  map<pair<int,int>,int>m;
        for(int i = 0 ; i < n ;i++)
        cin>>v[i];
        for(int i = 0 ; i < n ;i++)
        {cin>>loc[i];  }

    //    while(m.size() > 0)
    //    {
    //        auto it = m.end();
    //        it--;
    //        for(int i = 0  ;i < (*it).ss; i++)
    //        {
    //            if(loc[i] == 0  && v[i] < (*it).ff.ff )
    //            {
    //                if(m.find({v[i],i}) !=m.end() )
    //                {
    //                    m[{v[i] , (*it).ss }] = (*it).ss;
    //                    swap(v[i] , v[(*it).ss]);
    //                    m.erase({v[i] , i});
    //                }
    //                else
    //                {
    //                    swap(v[i], v[(*it).ss]);
    //                }
    //            }

    //        }
    //          m.erase(it);
    //    }

       for(int i = n-1 ; i>=0 ; i--)
       {

            if(v[i] > 0 &&  loc[i] == 0 )
            {
                for(int j = 0 ; j < i ;j++)
                {
                    if(loc[j] ==  0 && v[i] > v[j]  )
                    {
                        if(v[j] > 0 )
                        {
                            swap(v[i] , v[j]);
                            i++;
                        }
                        else swap(v[i] , v[j]);
                        break;
                    }
                }

            }

       }

        for(auto it : v)
        cout<<it<<" ";
        cout<<endl;

    }

}


#include<bits/stdc++.h>
    using namespace std;
  #define int long long int
    #define endl "\n"
    #define MOD 1000000007
    #define mod 1000000007
    #define M 1000000007

    #define pb push_back
    #define take(a,b,c) for(int b=0;b<c;b++) cin>>a[b]
  //  #define mp make_pair
  #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  #define mx 300005
  #define fiint(a,b) memset(a,b,sizeof(a))
  #define bitcount __builtin_popcount
  #define fori(i,a,b) for(int i=a;i<b;i++)
  #define ford(i,a,b) for(int i=a;i>=b;i--)
  #define debug(x) cout<<x<<endl;
  #define cases(t) int t; cin>>t; while(t--)
  #define inf1 INT_MAX
  #define all(a) a.begin(),a.end()
  #define vec vector<int>
  #define pii pair<int,int>
  #define plii pair<int,int>
  #define pint pair<int,int>
  #define ff first
  #define ss second
  #define lb lower_bound
  #define ub upper_bound
  #define bs binary_search
  #define sz(x) (int)x.size()

int powerFunction(int x,int y){
    int res = 1;
    int p = MOD;
    x = x % p;
    while (y > 0){
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
bool prime[10000001];
int P=MOD-2;
int fact[10000001]={0};

void sieveFunction(int maxLimit){
    memset(prime, true, sizeof(prime));
    prime[0] = prime[1]=false;
    for(int i=2; maxLimit >= i ; i++){
        if(prime[i]){
            for(int j=2*i ; maxLimit >= j ; j+=i)
                prime[j]=false;
        }
    }
}

void factorialFunction(int maxLimit){
    fact[0]=1;
    for(int i=1 ; i <= maxLimit ; i++)
        fact[i]=(fact[i-1]*i)%MOD;
    return;
}

int nCr(int n,int r){

		 int a=fact[n];

		int b=powerFunction(fact[n-r],mod-2);

		int c=powerFunction(fact[r],mod-2);

		 return (a*(b%mod* c%mod)%mod)%mod;
}

signed  main()
{
    cases(t)
    {
        int n ;cin>> n;
        vec v(n);
        vec loc(n);
    map<pair<int,int>,int>m;
        for(int i = 0 ; i < n ;i++)
        cin>>v[i];
        for(int i = 0 ; i < n ;i++)
        {cin>>loc[i];  }

       while(m.size() > 0)
       {
           auto it = m.end();
           it--;
           for(int i = 0  ;i < (*it).ss; i++)
           {
               if(loc[i] == 0  && v[i] < (*it).ff.ff )
               {
                   if(m.find({v[i],i}) !=m.end() )
                   {
                       m[{v[i] , (*it).ss }] = (*it).ss;
                       swap(v[i] , v[(*it).ss]);
                       m.erase({v[i] , i});
                   }
                   else
                   {
                       swap(v[i], v[(*it).ss]);
                   }
               }

           }
             m.erase(it);
       }

       for(int i = n-1 ; i>=0 ; i--)
       {

            if(v[i] > 0 &&  loc[i] == 0 )
            {
                for(int j = 0 ; j < i ;j++)
                {
                    if(loc[j] ==  0 && v[i] > v[j]  )
                    {
                        if(v[j] > 0 )
                        {
                            swap(v[i] , v[j]);
                           // i++;
                        }
                        else swap(v[i] , v[j]);
                       // break;
                    }
                }

            }

       }

        for(auto it : v)
        cout<<it<<" ";
        cout<<endl;

    }

}


#include<bits/stdc++.h>
    using namespace std;
  #define int long long int
    #define endl "\n"
    #define MOD 1000000007
    #define mod 1000000007
    #define M 1000000007

    #define pb push_back
    #define take(a,b,c) for(int b=0;b<c;b++) cin>>a[b]
  //  #define mp make_pair
  #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  #define mx 300005
  #define fiint(a,b) memset(a,b,sizeof(a))
  #define bitcount __builtin_popcount
  #define fori(i,a,b) for(int i=a;i<b;i++)
  #define ford(i,a,b) for(int i=a;i>=b;i--)
  #define debug(x) cout<<x<<endl;
  #define cases(t) int t; cin>>t; while(t--)
  #define inf1 INT_MAX
  #define all(a) a.begin(),a.end()
  #define vec vector<int>
  #define pii pair<int,int>
  #define plii pair<int,int>
  #define pint pair<int,int>
  #define ff first
  #define ss second
  #define lb lower_bound
  #define ub upper_bound
  #define bs binary_search
  #define sz(x) (int)x.size()

int powerFunction(int x,int y){
    int res = 1;
    int p = MOD;
    x = x % p;
    while (y > 0){
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
bool prime[10000001];
int P=MOD-2;
int fact[10000001]={0};

void sieveFunction(int maxLimit){
    memset(prime, true, sizeof(prime));
    prime[0] = prime[1]=false;
    for(int i=2; maxLimit >= i ; i++){
        if(prime[i]){
            for(int j=2*i ; maxLimit >= j ; j+=i)
                prime[j]=false;
        }
    }
}

void factorialFunction(int maxLimit){
    fact[0]=1;
    for(int i=1 ; i <= maxLimit ; i++)
        fact[i]=(fact[i-1]*i)%MOD;
    return;
}

int nCr(int n,int r){

		 int a=fact[n];

		int b=powerFunction(fact[n-r],mod-2);

		int c=powerFunction(fact[r],mod-2);

		 return (a*(b%mod* c%mod)%mod)%mod;
}

signed  main()
{
    cases(t)
    {
        int n ;cin>> n;
        vec v(n);
        vec loc(n);
    //map<pair<int,int>,int>m;
        for(int i = 0 ; i < n ;i++)
        cin>>v[i];
        for(int i = 0 ; i < n ;i++)
        {cin>>loc[i];  }

    //    while(m.size() > 0)
    //    {
    //        auto it = m.end();
    //        it--;
    //        for(int i = 0  ;i < (*it).ss; i++)
    //        {
    //            if(loc[i] == 0  && v[i] < (*it).ff.ff )
    //            {
    //                if(m.find({v[i],i}) !=m.end() )
    //                {
    //                    m[{v[i] , (*it).ss }] = (*it).ss;
    //                    swap(v[i] , v[(*it).ss]);
    //                    m.erase({v[i] , i});
    //                }
    //                else
    //                {
    //                    swap(v[i], v[(*it).ss]);
    //                }
    //            }

    //        }
    //          m.erase(it);
    //    }

       for(int i = n-1 ; i>=0 ; i--)
       {

            if(v[i] > 0 &&  loc[i] == 0 )
            {
                for(int j = 0 ; j < i ;j++)
                {
                    if(loc[j] ==  0 && v[i] > v[j]  )
                    {
                        if(v[j] > 0 )
                        {
                            swap(v[i] , v[j]);
                           // i++;
                        }
                        else swap(v[i] , v[j]);
                       // break;
                    }
                }

            }

       }

        for(auto it : v)
        cout<<it<<" ";
        cout<<endl;

    }

}


#include<bits/stdc++.h>
    using namespace std;
  #define int long long int
    #define endl "\n"
    #define MOD 1000000007
    #define mod 1000000007
    #define M 1000000007

    #define pb push_back
    #define take(a,b,c) for(int b=0;b<c;b++) cin>>a[b]
  //  #define mp make_pair
  #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  #define mx 300005
  #define fiint(a,b) memset(a,b,sizeof(a))
  #define bitcount __builtin_popcount
  #define fori(i,a,b) for(int i=a;i<b;i++)
  #define ford(i,a,b) for(int i=a;i>=b;i--)
  #define debug(x) cout<<x<<endl;
  #define cases(t) int t; cin>>t; while(t--)
  #define inf1 INT_MAX
  #define all(a) a.begin(),a.end()
  #define vec vector<int>
  #define pii pair<int,int>
  #define plii pair<int,int>
  #define pint pair<int,int>
  #define ff first
  #define ss second
  #define lb lower_bound
  #define ub upper_bound
  #define bs binary_search
  #define sz(x) (int)x.size()

int powerFunction(int x,int y){
    int res = 1;
    int p = MOD;
    x = x % p;
    while (y > 0){
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
bool prime[10000001];
int P=MOD-2;
int fact[10000001]={0};

void sieveFunction(int maxLimit){
    memset(prime, true, sizeof(prime));
    prime[0] = prime[1]=false;
    for(int i=2; maxLimit >= i ; i++){
        if(prime[i]){
            for(int j=2*i ; maxLimit >= j ; j+=i)
                prime[j]=false;
        }
    }
}

void factorialFunction(int maxLimit){
    fact[0]=1;
    for(int i=1 ; i <= maxLimit ; i++)
        fact[i]=(fact[i-1]*i)%MOD;
    return;
}

int nCr(int n,int r){

		 int a=fact[n];

		int b=powerFunction(fact[n-r],mod-2);

		int c=powerFunction(fact[r],mod-2);

		 return (a*(b%mod* c%mod)%mod)%mod;
}

signed  main()
{
    cases(t)
    {
        int n ;cin>> n;
        vec v(n);
        vec loc(n);
    //map<pair<int,int>,int>m;
        vec ch;
        for(int i = 0 ; i < n ;i++)
        cin>>v[i];
        for(int i = 0 ; i < n ;i++)
        {cin>>loc[i];   if(loc[i] == 0 ) ch.pb(v[i]); }
            int idx = sz(ch) - 1;
            sort(all(ch));
        for(int i = 0 ; i < n ;i++)
        if(loc[i] == 0)
        v[i] = ch[idx--];

        for(auto it : v)
        cout<<it<<" ";
        cout<<endl;

    }

}


#include<bits/stdc++.h>
    using namespace std;
  #define int long long int
    #define endl "\n"
    #define MOD 1000000007
    #define mod 1000000007
    #define M 1000000007

    #define pb push_back
    #define take(a,b,c) for(int b=0;b<c;b++) cin>>a[b]
  //  #define mp make_pair
  #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  #define mx 300005
  #define fiint(a,b) memset(a,b,sizeof(a))
  #define bitcount __builtin_popcount
  #define fori(i,a,b) for(int i=a;i<b;i++)
  #define ford(i,a,b) for(int i=a;i>=b;i--)
  #define debug(x) cout<<x<<endl;
  #define cases(t) int t; cin>>t; while(t--)
  #define inf1 INT_MAX
  #define all(a) a.begin(),a.end()
  #define vec vector<int>
  #define pii pair<int,int>
  #define plii pair<int,int>
  #define pint pair<int,int>
  #define ff first
  #define ss second
  #define lb lower_bound
  #define ub upper_bound
  #define bs binary_search
  #define sz(x) (int)x.size()

int powerFunction(int x,int y){
    int res = 1;
    int p = MOD;
    x = x % p;
    while (y > 0){
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
bool prime[10000001];
int P=MOD-2;
int fact[10000001]={0};

void sieveFunction(int maxLimit){
    memset(prime, true, sizeof(prime));
    prime[0] = prime[1]=false;
    for(int i=2; maxLimit >= i ; i++){
        if(prime[i]){
            for(int j=2*i ; maxLimit >= j ; j+=i)
                prime[j]=false;
        }
    }
}

void factorialFunction(int maxLimit){
    fact[0]=1;
    for(int i=1 ; i <= maxLimit ; i++)
        fact[i]=(fact[i-1]*i)%MOD;
    return;
}

int nCr(int n,int r){

		 int a=fact[n];

		int b=powerFunction(fact[n-r],mod-2);

		int c=powerFunction(fact[r],mod-2);

		 return (a*(b%mod* c%mod)%mod)%mod;
}

signed  main()
{
    cases(t)
    {
        int n ;cin>> n;
        vec v(n);
        vec loc(n);
  //  map<pair<int,int>,int>m;
        for(int i = 0 ; i < n ;i++)
        cin>>v[i];
        for(int i = 0 ; i < n ;i++)
        {cin>>loc[i];  }

    //    while(m.size() > 0)
    //    {
    //        auto it = m.end();
    //        it--;
    //        for(int i = 0  ;i < (*it).ss; i++)
    //        {
    //            if(loc[i] == 0  && v[i] < (*it).ff.ff )
    //            {
    //                if(m.find({v[i],i}) !=m.end() )
    //                {
    //                    m[{v[i] , (*it).ss }] = (*it).ss;
    //                    swap(v[i] , v[(*it).ss]);
    //                    m.erase({v[i] , i});
    //                }
    //                else
    //                {
    //                    swap(v[i], v[(*it).ss]);
    //                }
    //            }

    //        }
    //          m.erase(it);
    //    }

       for(int i = n-1 ; i>=0 ; i--)
       {

            if(v[i] > 0 &&  loc[i] == 0 )
            {
                for(int j = 0 ; j < i ;j++)
                {
                    if(loc[j] ==  0 && v[i] > v[j]  )
                    {
                        if(v[i] > 0 )
                        {
                            swap(v[i] , v[j]);
                            i++;
                        }
                        else swap(v[i] , v[j]);
                        break;
                    }
                }

            }

       }

        for(auto it : v)
        cout<<it<<" ";
        cout<<endl;

    }

}


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define FAST  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#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 int
#define ar array
#define pb push_back
#define fi(a,b) for(ll i=a;i<(b);i++)
#define fj(a,b) for(ll j=a;j<(b);j++)
#define fk(a,b) for(ll k=a;k<(b);k++)
//hash base 177013

int main()
{
	FAST;
	//#ifndef ONLINE_JUDGE
	//	freopen("input.txt","r",stdin);
	//	freopen("output.txt","w",stdout);
	//#endif
	ll testcase;
	cin>>testcase;
	while(testcase--)
	{
		int n;
		cin>>n;
		vector<int> a(n),b(n);
		fi(0,n) cin>>a[i];
		fi(0,n) cin>>b[i];
		set<int> s;
		int sum_pos=0,sum_neg=0;
		fi(0,n){
			if(a[i]>0)
				sum_pos+=a[i];
			else
				sum_neg+=abs(a[i]);
			if(b[i]==0)
			{
				s.insert(a[i]);
			}
		}
		if(s.size()==0)
		{
			for(auto i:a)
			{
				cout<<i<<' ';
			}
			cout<<'\n';
			continue;
		}
		//if(sum_pos>sum_neg)
		//{
			//auto it=--s.end();
			//fi(0,n)
			//{
				//if(b[i]==0)
				//{
					//a[i]=*it;
					//it--;
				//}
			//}
		//}
		//else
		{
			auto it=s.begin();
			fi(0,n)
			{
				if(b[i]==0)
				{
					a[i]=*it;
					it++;
				}
			}
		}
		for(auto i:a)
		{
			cout<<i<<' ';
		}
		cout<<'\n';

	}
}


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define FAST  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#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 int
#define ar array
#define pb push_back
#define fi(a,b) for(ll i=a;i<(b);i++)
#define fj(a,b) for(ll j=a;j<(b);j++)
#define fk(a,b) for(ll k=a;k<(b);k++)
//hash base 177013

int main()
{
	FAST;
	//#ifndef ONLINE_JUDGE
	//	freopen("input.txt","r",stdin);
	//	freopen("output.txt","w",stdout);
	//#endif
	ll testcase;
	cin>>testcase;
	while(testcase--)
	{
		int n;
		cin>>n;
		vector<int> a(n),b(n);
		fi(0,n) cin>>a[i];
		fi(0,n) cin>>b[i];
		set<int> s;
		int sum_pos=0,sum_neg=0;
		fi(0,n){
			if(a[i]>0)
				sum_pos+=a[i];
			else
				sum_neg+=abs(a[i]);
			if(b[i]==0)
			{
				s.insert(a[i]);
			}
		}
		if(s.size()==0)
		{
			for(auto i:a)
			{
				cout<<i<<' ';
			}
			cout<<'\n';
			continue;
		}
		if(sum_pos>=sum_neg)
		{
			auto it=--s.end();
			fi(0,n)
			{
				if(b[i]==0)
				{
					a[i]=*it;
					it--;
				}
			}
		}
		else
		{
			auto it=s.begin();
			fi(0,n)
			{
				if(b[i]==0)
				{
					a[i]=*it;
					it++;
				}
			}
		}
		for(auto i:a)
		{
			cout<<i<<' ';
		}
		cout<<'\n';

	}
}


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define FAST  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#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 int
#define ar array
#define pb push_back
#define fi(a,b) for(ll i=a;i<(b);i++)
#define fj(a,b) for(ll j=a;j<(b);j++)
#define fk(a,b) for(ll k=a;k<(b);k++)
//hash base 177013

int main()
{
	FAST;
	//#ifndef ONLINE_JUDGE
	//	freopen("input.txt","r",stdin);
	//	freopen("output.txt","w",stdout);
	//#endif
	ll testcase;
	cin>>testcase;
	while(testcase--)
	{
		int n;
		cin>>n;
		vector<int> a(n),b(n);
		fi(0,n) cin>>a[i];
		fi(0,n) cin>>b[i];
		set<int> s;
		int sum_pos=0,sum_neg=0;
		fi(0,n){
			if(b[i]==0)
			{
				if(a[i]>0)
					sum_pos+=a[i];
				else
					sum_neg+=abs(a[i]);
				s.insert(a[i]);
			}
		}
		if(s.size()==0)
		{
			for(auto i:a)
			{
				cout<<i<<' ';
			}
			cout<<'\n';
			continue;
		}
		if(sum_pos<=sum_neg)
		{
			auto it=--s.end();
			fi(0,n)
			{
				if(b[i]==0)
				{
					a[i]=*it;
					it--;
				}
			}
		}
		else
		{
			auto it=s.begin();
			fi(0,n)
			{
				if(b[i]==0)
				{
					a[i]=*it;
					it++;
				}
			}
		}
		for(auto i:a)
		{
			cout<<i<<' ';
		}
		cout<<'\n';

	}
}


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define FAST  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#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 int
#define ar array
#define pb push_back
#define fi(a,b) for(ll i=a;i<(b);i++)
#define fj(a,b) for(ll j=a;j<(b);j++)
#define fk(a,b) for(ll k=a;k<(b);k++)
//hash base 177013

int main()
{
	FAST;
	//#ifndef ONLINE_JUDGE
	//	freopen("input.txt","r",stdin);
	//	freopen("output.txt","w",stdout);
	//#endif
	ll testcase;
	cin>>testcase;
	while(testcase--)
	{
		int n;
		cin>>n;
		vector<int> a(n),b(n);
		fi(0,n) cin>>a[i];
		fi(0,n) cin>>b[i];
		set<int> s;
		int sum_pos=0,sum_neg=0;
		fi(0,n){
			if(b[i]==0)
			{
				s.insert(a[i]);
				if(a[i]>0)
					sum_pos+=a[i];
				else
					sum_neg+=a[i];
			}
		}
		if(s.size()==0)
		{
			for(auto i:a)
			{
				cout<<i<<' ';
			}
			cout<<'\n';
			continue;
		}
		int index=n;
		if(abs(sum_neg)>=sum_pos)
		{
			auto it=s.begin();
			fi(0,n)
			{
				if(b[i]==0)
				{
					a[i]=*it;
					sum_neg-=*it;
					s.erase(it);
					it++;
				}
				if(it==s.end()) break;
				if(sum_pos>=abs(sum_neg))
				{
					index=i;
					break;
				}
			}
		}
		//for(auto i:s)
		//{
			//cout<<i<<' ';
		//}
		//cout<<'\n';
		//cout<<index<<'\n';
		if(s.size()==0||index==n)
		{
			for(auto i:a){
				cout<<i<<' ';
			}
			cout<<'\n';
			continue;
		}
		auto it=--s.end();
		fi(index+1,n)
		{
			if(b[i]==0)
			{
				a[i]=*it;
				if(it==s.begin()) break;
				it--;
			}
		}
		for(auto i:a)
		{
			cout<<i<<' ';
		}
		cout<<'\n';

	}
}


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define FAST  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#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 int
#define ar array
#define pb push_back
#define fi(a,b) for(ll i=a;i<(b);i++)
#define fj(a,b) for(ll j=a;j<(b);j++)
#define fk(a,b) for(ll k=a;k<(b);k++)
//hash base 177013

int main()
{
	FAST;
	//#ifndef ONLINE_JUDGE
	//	freopen("input.txt","r",stdin);
	//	freopen("output.txt","w",stdout);
	//#endif
	ll testcase;
	cin>>testcase;
	while(testcase--)
	{
		int n;
		cin>>n;
		vector<int> a(n),b(n);
		fi(0,n) cin>>a[i];
		fi(0,n) cin>>b[i];
		multiset<int> s;
		int sum_pos=0,sum_neg=0;
		fi(0,n){
			if(b[i]==0)
			{
				s.insert(a[i]);
				if(a[i]>0)
					sum_pos+=a[i];
				else
					sum_neg+=a[i];
			}
		}
		if(s.size()==0)
		{
			for(auto i:a)
			{
				cout<<i<<' ';
			}
			cout<<'\n';
			continue;
		}
		int index=n;
		if(abs(sum_neg)>=sum_pos)
		{
			auto it=s.begin();
			fi(0,n)
			{
				if(b[i]==0)
				{
					a[i]=*it;
					sum_neg-=*it;
					s.erase(it);
					it++;
				}
				if(it==s.end()) break;
				if(sum_pos>=abs(sum_neg))
				{
					index=i;
					break;
				}
			}
		}
		//for(auto i:s)
		//{
			//cout<<i<<' ';
		//}
		//cout<<'\n';
		//cout<<index<<'\n';
		if(s.size()==0||index==n)
		{
			for(auto i:a){
				cout<<i<<' ';
			}
			cout<<'\n';
			continue;
		}
		auto it=--s.end();
		fi(index+1,n)
		{
			if(b[i]==0)
			{
				a[i]=*it;
				if(it==s.begin()) break;
				it--;
			}
		}
		for(auto i:a)
		{
			cout<<i<<' ';
		}
		cout<<'\n';

	}
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
  int n=0;
int tab[1000];
scanf("%d",&n);
int max=0;
int pos=n-1;
int pos1=0;
for (int i = 0; i < n; i++) {
  scanf("%d",&tab[i]);
}
for (int i = 0; i <n; i++) {
  if(abs(tab[i]-tab[(i+1)%n])<max){
    pos=i ;
    pos1=i+1;
  }
}
if(n)
printf("%d %d",pos+1,pos1+1);
  return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
  int n=0;
int tab[1000];
scanf("%d",&n);
int max=0;
int pos;
int pos1;
for (int i = 0; i < n; i++) {
  scanf("%d",&tab[i]);
}
for (int i = 0; i <n; i++) {
  if(i==0){
    max=abs(tab[0]-tab[1]);
    pos=0;
    pos1=1;
  }
  if(abs(tab[i]-tab[(i+1)%n])<max){
    max=abs(tab[i]-tab[(i+1)%n]);
    pos=i ;
    pos1=i+1;
  }
}

if(n)
printf("%d %d",pos%n+1,pos1%n+1);
  return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
  int n,max,pos,pos1;
int tab[1000];
scanf("%d",&n);
max=abs(tab[0]-tab[n-1]);
pos=n-1;
pos1=0;
for (int i = 0; i < n; i++) {
  scanf("%d",&tab[i]);
}
for (int i = 0; i < n; i++) {
  for (int j =i;j <n; j++) {
    if(abs(tab[i]-tab[j])>=max&&j!=i){
      pos=i ;
      pos1=j;
    }
  }
}
if(n)
printf("%d %d",pos+1,pos1+1);
  return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
  int n=0;
int tab[1000];
scanf("%d",&n);
int max=abs(tab[0]-tab[n-1]);
int pos=n-1;
int pos1=0;
for (int i = 0; i < n; i++) {
  scanf("%d",&tab[i]);
}
for (int i = 0; i < n; i++) {
  for (int j =i;j <n; j++) {
    if(abs(tab[i]-tab[j])>=max&&j!=i){
      pos=i ;
      pos1=j;
    }
  }
}
if(n)
printf("%d %d",pos,pos1);
  return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
  int n=0;
int tab[1000];
scanf("%d",&n);
int max=abs(tab[0]-tab[n-1]);
int pos=n-1;
int pos1=0;
for (int i = 0; i < n; i++) {
  scanf("%d",&tab[i]);
}
for (int i = 0; i < n; i++) {
  for (int j =i;j <n; j++) {
    if(abs(tab[i]-tab[j])>=max&&j!=i){
      pos=i ;
      pos1=j;
    }
  }
}
if(n)
printf("%d %d",pos+1,pos1+1);
  return 0;
}


#include<stdio.h>
#include<stdlib.h>
int main()
{
    int j,i,n,*a,m=100000,end,start,f=0;
    scanf("%d",&n);
    a=(int*)malloc(sizeof(int)*n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(abs(a[i]-a[j])<m)
            {
                m=abs(a[i]-a[j]);
                start=i;
                end=j;
            }
            if (m==0)
            {
                f=1;
                break;
            }
        }
        if (f==1)
        {
            break;
        }
    }
    free(a);
    printf("%d %d",start+1,end+1);
    return 0;
}


#include<stdio.h>
#include<stdlib.h>
int main()
{
    int n,a[100],i,m,end,start,f=0;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    m=abs(a[n-1]-a[0]);
    start=0;
    end=n-1;
    if(m!=0)
    {
        for(i=0;i<n-1;i++)
        {
            if (abs(a[i]-a[i+1])<m)
            {
                m=abs(a[i]-a[i+1]);
                start=i;
                end=i+1;
            }
            if(m==0)
            {
                break;
            }
        }
    }
    printf("%d %d",start+1,end+1);
    return 0;
}


#include<stdio.h>
#include<stdlib.h>
int main()
{
	int n;
	scanf("%d",&n);
	int i,a[n],j,k=0,min=0,p1,p2;
	for(i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
	}
	if(n==2)
	{
		min=abs(a[i]-a[j]);
		printf("1 2");
		return 0;
	}
	else
	{
		j=1;
		for(i=1;i<=n;i++)
		{
			j=i+1;
			if(i==1&&j==2)
			{
				min=abs(a[i]-a[j]);
				p1=i;
				p2=j;
			}
			if(i==n)
			{
				j=1;
			}
			else
			{
			}
				k=abs(a[i]-a[j]);
				if(k<min)
				{min=k;
				p1=i;
				p2=j;
				}

		}
		printf("%d",min);
		printf("%d %d",p1,p2);
		return 0;
	}
}


#include<stdio.h>
#include<stdlib.h>
int main()
{
	int n;
	scanf("%d",&n);
	int i,a[n],j,k=0,min=0,p1,p2;
	for(i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
	}
	if(n==2)
	{
		min=abs(a[i]-a[j]);
		printf("1 2");
		return 0;
	}
	else
	{
		j=1;
		for(i=1;i<=n;i++)
		{
			j=i+1;
			if(i==1&&j==2)
			{
				min=abs(a[i]-a[j]);
				p1=i;
				p2=j;
			}
			if(i==n)
			{
				j=1;
			}
			else
			{
			}
				k=abs(a[i]-a[j]);
				if(k<min)
				{min=k;
				p1=i;
				p2=j;
				}

		}

		printf("%d %d",p1,p2);
		return 0;
	}
}


#include <stdio.h>
#include <math.h>
int main()
{
    int a[106],n,minn=1001,minx,miny;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=0;i<n;i++)
        if(minn>abs(a[i%n+1]-a[(i+1)%n+1]))
        {
            minn=abs(a[i%n+1]-a[(i+1)%n+1]);
            minx=i%n+1;
            miny=(i+1)%n+1;
        }
    printf("%d %d\n",minx,miny);
    return 0;
}


#include<stdio.h>
#include<stdlib.h>
void fun(int [],int ,int *,int *);
void fun(int a[],int n,int *x,int *y)
{
    int ans,i;
    ans=abs(a[0]-a[n-1]);
    *x=n-1;
    *y=0;
    for(i=1;i<n;i++)
    {
        if(abs(a[i]-a[i-1])<ans)
        {
            *x=i-1;
            *y=i;
        }
    }
}
int main()
{
    int a[105];
    int n,i,x,y;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    fun(a,n,&x,&y);
    printf("%d %d\n",x+1,y+1);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
int main () {
    int n,arr[100],diff,min = 1001,index1,index2,i,j;
    scanf("%d",&n);
    for(i = 0; i < n ; i++)
        scanf("%d",&arr[i]);
    for(i = 0; i < n-1; i++)
        for(j = i+1 ; j < n ;j++)
        {
            diff = abs(arr[j] - arr[i]);
            if (diff < min)
            {
                min = diff;
                index1 = i;
                index2 = j;
            }
        }
    printf("%d %d",index1+1,index2+1);
    return 0;

}


#include <stdio.h>
int main()
{
	int n,i,min1 = 1001,min2 = 1001;
	scanf("%d",&n);
	int a[n];
	int index1,index2;
	for(i = 0 ; i < n ;i++)
	{
		scanf("%d",&a[i]);
		if(a[i] <= min2)
			if(a[i] < min1)
			{
				min1 = a[i];
				index1 = i;
			}
			else
			{
				min2 = a[i];
				index2 = i;
			}
	}
	printf("%d %d",index1 + 1,index2+1);
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
int main () {
    int n,arr[100],diff,min = 1001,index1,index2,i,j;
    scanf("%d",&n);
    for(i = 0; i < n ; i++)
        scanf("%d",&arr[i]);
    for(i = 1; i < n; i++)
   {
       diff = abs(arr[i] - arr[i-1]);
       if(diff < min)
        {
            min = diff;
            index1 = i+1;
            index2 = i;
        }
   }
   diff = abs(arr[n-1] - arr[0]);
   if (diff < min)
   {
       index1 = 1;
       index2 = n;
   }

   printf("%d %d",index1,index2);
    return 0;

}


#include<stdio.h>
int main()
{
	int n;
	int i,j;int b;int min=9999999;int k,l;
	scanf("%d",&n);
	int a[n];
	for(i=0;i<n;i++)scanf("%d",&a[i]);
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			if(i!=j&&(abs(i-j)==1||abs(i-j)==n-1)){
				b=abs(a[i]-a[j]);
				if(min>b){
				    min=b;
				    k=i+1;l=j+1;
				}
			}

		}
	}

	//printf("%d\n",min);
	printf("%d %d\n",k,l);
	return 0;
}


#include<stdio.h>
int main()
{
	int n;
	int i,j;int b;int min=9999999;int k,l;
	scanf("%d",&n);
	int a[n];
	for(i=0;i<n;i++)scanf("%d",&a[i]);
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			if(i!=j){
				b=abs(a[i]-a[j]);
				if(min>b){
				    min=b;
				    k=i+1;l=j+1;
				}
			}
		}
	}
	printf("%d %d",k,l);
	return 0;
}


#include<stdio.h>
int main()
{
	int n,i,min,diff,flag=0,min_index=1,min_index1;
	int a[100];
	scanf("%d",&n);
	min_index1=n;
	for(i=1;i<=n;i++)
		scanf("%d",&a[i]);
	min=a[n]-a[1];
	if(min<0)
	min=min*(-1);
	for(i=1;i<=n;i++)
	{
		diff=a[i]-a[i+1];
		if(diff<0)
		diff=diff*(-1);
		if(diff<=min)
		{
			min=diff;

			flag=1;
			min_index=i;
			min_index1=i+1;

		}

	}

	printf("%d %d",min_index,min_index1);

	return 0;

}


#include<stdio.h>
int main()
{
	int n,i,min,diff,flag=0,min_index=0,min_index1;
	int a[100];
	scanf("%d",&n);
	min_index1=n;
	for(i=1;i<=n;i++)
		scanf("%d",&a[i]);
	min=a[n]-a[1];
	if(min<0)
	min=min*(-1);
	for(i=1;i<=n;i++)
	{
		diff=a[i]-a[i+1];
		if(diff<0)
		diff=diff*(-1);
		if(diff<=min)
		{
			min=diff;

			flag=1;
			min_index=i;
			min_index1=i+1;

		}

	}

	printf("%d %d",min_index,min_index1);

	return 0;

}


#include<stdio.h>
#include<math.h>
#include <stdlib.h>
#define INT_MAX 2147483647
int  main()
{
    int n;
    scanf("%d", &n);

    int arr[n];

    for (int i = 0; i < n; i++)
    {
        scanf("%d", &arr[i]);
    }

    int min = INT_MAX;
    int a, b = -1;
    for (int i = 0; i < n; i++)
    {
        if (i == n - 1)
        {
            if (abs(arr[i] - arr[0]) < min)
            {
                min = abs(arr[i] - arr[0]);
                a = i + 1;
                b = 1;
            }
        }
        else
        {
            if (abs(arr[i] - arr[i + 1]) < min)
            {
                min = abs(arr[i] - arr[i + 1]);
                a = i + 1;
                b = i + 2;
            }
        }
    }
    printf("%d %d\n", a,b);

}


#include<stdio.h>
int main()
{
      int n;
      scanf("%d",&n);
      int a[n],i;
      for(i=0;i<n;i++) scanf("%d",&a[i]);
      int x,y=10000,b,first,second;
      for(b=1,i=0;b<n;b++)
      {
            x=abs(a[i]-a[i+1]);
            if(x<y)
            {
                  y=x;
                  first=i+1;
                  second=i+2;
            }
            i++;
      }
      x=abs(a[0]-a[n-1]);
      if(x<y)
      {
            first=1;
            second=n;
      }
      printf("%d %d\n",first,second);

      return 0;
}


#include<stdio.h>
int main(void)
{
    int i,j,k,n,dif,max,min,last=1000;
    scanf("%d",&n);
    int base[n],ans1,ans2;
    for(i=0;i<n;i++)
    {
        scanf("%d",&base[i]);
    }
    for(i=0;i<n;i++)
    {
        if(i+1==n)
        {
            j=0;
            k=i;
        }
        else
        {
            j=i+1;
            k=i;
        }
        max=base[j]>base[k]?base[j]:base[k];
        min=base[j]<base[k]?base[j]:base[k];
        dif=max-min;
        if(dif<last)
        {
            ans1=j+1;
            ans2=k+1;
            last=dif;
        }
        if(dif==0)
            break;
    }
    printf("%d %d\n",ans1,ans2);
    return 0;
}


#include<stdio.h>
int main(void)
{
    int i,j,k=0,x=0,y=0,n;
    scanf("%d",&n);
    int min,base[n],innovated[n],ans1,ans2;
    for(i=0;i<n;i++)
    {
        scanf("%d",&base[i]);
        innovated[i]=base[i];
    }
    for(i=0;i<n;i++)
    {
        min=base[0];
        for(j=i+1;j<n;j++)
            min=min<base[j]?min:base[j];
        for(j=i+1;j<n;j++)
        {
            if(base[j]==min)
            {
                base[j]=base[i];
                base[i]=min;
                break;
            }
        }
    }
    for(i=1,j=0,min=1000;i<n;i++,j++)
    {
        if(min>base[i]-base[i-1])
            k=j;
        if(base[i]-base[i-1]==0)
            break;
    }
    for(i=0;i<n;i++)
    {
        if(base[k]==innovated[i]&&x==0)
        {
            ans1=i+1;
            x++;
        }
        else if(base[k+1]==innovated[i]&&y==0)
        {
            ans2=i+1;
            y++;
        }
    }
    printf("%d %d\n",ans1,ans2);
    return 0;
}


#include <stdio.h>

int main() {
	int n;
	scanf("%d", &n);
	getchar();

	int a[n], i, min_diff = 10000, diff, loc_1, loc_2;
	for(i = 0; i < n; i++) {
		scanf("%d", &a[i]);
		getchar();
	}
	for(i = 0; i < n; i++) {
		diff = a[(i + 1) % n] - a[i % n];
		if(diff < 0) {
			diff *= -1;
		}
		if(diff < min_diff) {
			min_diff = diff;
			loc_1 = (i % n) + 1;
			loc_2 = ((i + 1) % n) + 1;
		}
	}

	printf("%d %d\n", loc_1, loc_2);

	return 0;
}


let inputString = "";
let currentLine = 0;
process.stdin.on("data", (data) => {
  inputString += data;
});
process.stdin.on("end", function () {
  inputString = inputString
    .trim()
    .split("\n")
    .map((str) => str.trim());
  main();
});
function readLine() {
  return inputString[currentLine++];
}
function main() {
  let len = +readLine();
  const arr = readLine()
    .split(" ")
    .map((n) => parseInt(n));
  let [min, minIndex] = [Math.abs(arr[len - 1] - arr[0]), [len, 1]];

  for (let i = 0; i < len - 1; i++) {
    const diff = Math.abs(arr[i] - arr[i + 1]);
    if (diff < min) {
      min = diff;
      minIndex = [i + 1, i + 2];
    }
  }
  console.log(`${minIndex[0]} ${minIndex[1]}`);
}


const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});
let c = 0;

rl.on('line', (d) => {
  if (c === 0) {
    c++;
    return;
  }

  const arr = d.split(' ').map(Number);
  let best = Infinity;
  let x = Infinity;
  let y = Infinity;

  for (let i = 0; i < arr.length - 1; i++) {
    for (let j = i + 1; j < arr.length; j++) {
      const current = Math.abs(arr[i] - arr[j]);

      if (current < best) {
        best = current;
        x = i + 1;
        y = j + 1;
      }
    }
  }

  console.log(x, y);

  c++;
});


const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});
let c = 0;

rl.on('line', (d) => {
  if (c === 0) {
    c++;
    return;
  }

  const arr = d.split(' ').map(Number);
  let best = Math.abs(arr[0] - arr[arr.length - 1]);
  let x = 1;
  let y = arr.length;

  for (let i = 0; i < arr.length - 1; i++) {
    const current = Math.abs(arr[i] - arr[i + 1]);

    if (current < best) {
      best = current;
      x = i + 1;
      y = i + 2;
    }
  }

  console.log(x, y);

  c++;
});


let inputString = '';
let currentLine = 0;
process.stdin.on('data', data => {
	inputString += data;
});
process.stdin.on('end', function () {
	inputString = inputString
		.trim()
		.split('\n')
		.map(str => str.trim());
	main();
});

function readLine() {
	return inputString[currentLine++];
}

function multiply(a, b) {
	a = a.toString(10).split('').reverse();
	b = b.toString(10).split('').reverse();

	if (a.length < b.length) {
		[a, b] = [b, a];
	}

	let res = Array(a.length + b.length).fill(0);
	for (let i = 0, lb = b.length; i < lb; ++i) {
		let carry = 0;
		let scarry = 0;
		for (let j = 0, la = a.length; j < la; ++j) {
			let m = b[i] * a[j];
			m += carry;

			if (m > 9) {
				let [c, d] = m.toString(10).split('');
				res[i + j] += (scarry >> 0) + (d >> 0);
				carry = c >> 0;
			} else {
				res[i + j] += (scarry >> 0) + m;
				carry = 0;
			}

			if (res[i + j] > 9) {
				let [c, d] = res[i + j].toString(10).split('');
				res[i + j] = d >> 0;
				scarry = c;
			} else {
				scarry = 0;
			}
		}
		if (carry > 0) res[a.length + i] = carry;
		if (scarry > 0) res[a.length + i] = scarry;
	}

	res.reverse();
	let mulVal = '';
	let flag = false;
	res.forEach(d => {
		if (!flag && d !== 0) flag = true;
		if (flag) mulVal += d;
	});
	return mulVal;
}

function main() {
	let n = readLine() >> 0;
	let h = readLine()
		.split(' ')
		.map(x => x >> 0);
	let d = Math.abs(h[0] - h[n - 1]);
	let x = 1,
		y = n;
	for (let i = 1; i < n; ++i) {
		let diff = Math.abs(h[i] - h[i - 1]);
		if (diff <= d) {
			d = diff;
			x = i;
			y = i + 1;
		}
	}
	console.log(x, y);
}


process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', (inputStd) => {
    inputString += inputStd;
});
process.stdin.on('end', function () {
    inputString = inputString
        .trim()
        .split('\n')
        .map((str) => {
            return str.trim();
        });
    main();
});
function readLine() {
    return inputString[currentLine++];
}
function main() {
    let n = parseInt(readLine());
    let heights = readLine().split(' ').map(Number);
    let differences = Math.min();
    let pairs = [];
    for (let i = 0; i < heights.length; i++) {
        let diff = Math.abs(heights[i] - heights[i + 1]);
        if (diff < differences) {
            differences = diff;
            pairs.push([i + 1, i + 2]);
        }
    }
    if (Math.abs(heights[0] - heights[heights.length - 1]) < differences) {
        console.log(1, heights.length);
        return;
    }
    console.log(...pairs[pairs.length - 1]);
}


process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', (inputStd) => {
    inputString += inputStd;
});
process.stdin.on('end', function () {
    inputString = inputString
        .trim()
        .split('\n')
        .map((str) => {
            return str.trim();
        });
    main();
});
function readLine() {
    return inputString[currentLine++];
}
function main() {
    let n = parseInt(readLine());
    let heights = readLine().split(' ').map(Number);
    let differences = Math.min();
    let pairs = [];
    for (let i = 0; i < heights.length; i++) {
        let diff = Math.abs(heights[i] - heights[i + 1]);
        if (diff < differences) {
            differences = diff;
            pairs.push([i, i + 1]);
        }
    }
    if (Math.abs(heights[0] - heights[heights.length - 1]) < differences) {
        console.log(1, heights.length);
        return;
    }
    console.log(...pairs[pairs.length - 1]);
}


const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

var n = -1;
var reconnaissance = [];

rl.on('line', function (input) {

    if (n == -1)
        n = parseInt(input);
    else {
          reconnaissance = input.split(' ').map(item => {return parseInt(item);});
        //****************************************** DO IT HERE*/

        var min = Math.abs(reconnaissance[0] - reconnaissance[reconnaissance.length - 1]);
        var ans = "1 " + reconnaissance.length;

        for(var i = 0; i< n-1; i++)
        {
            var temp = Math.abs(reconnaissance[i] - reconnaissance[i + 1]);
            if (min > temp)
                {
                    min = temp; ans = "" + (i + 1) + " " + (i + 2);
                }

        }
            console.log(ans);

            rl.close();
            /*************************************** */

    }

})


let fs = require("fs");

let txt = fs.readFileSync(0, "utf-8").split(/[\r\n]/).filter(data => data.length > 0);
for (let i = 0; i < txt.length; i += 2) {
    doit(txt[i + 1].split(" ").filter(data => data.length > 0).map(data => data * 1));
}

function doit(tab) {

    let changes = [];
    for (let i = 0; i < tab.length - 1; i++) {
        changes.push(Math.abs(tab[i] - tab[i + 1]));
    }
    changes.push(Math.abs(tab[0] - tab[tab.length - 1]));
    let index = changes.indexOf(Math.min(...changes));
    let next=index+2;
    if(next>tab.length)next-=tab.length;

    console.log(index + 1, next);

}


let fs = require("fs");

let txt = fs.readFileSync(0, "utf-8").split(/[\r\n]/).filter(data => data.length > 0);
for (let i = 0; i < txt.length; i += 2) {
    doit(txt[i + 1].split(" ").filter(data => data.length > 0).map(data => data * 1));
}

function doit(tab) {

    let changes = [];
    for (let i = 0; i < tab.length - 1; i++) {
        changes.push(Math.abs(tab[i] - tab[i + 1]));
    }
    changes.push(Math.abs(tab[0] - tab[tab.length - 1]));
    let index = changes.indexOf(Math.min(...changes));
    console.log(index + 1, (index + 2) % tab.length);

}


using System;
using System.Linq;

namespace Task34A
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int[] container = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int temp,comparer = 1000;
            string ans = "";
            for (int i = 0; i < n - 1; i++)
            {
                temp = Math.Abs(container[i] - container[i+1]);
                if (temp < comparer)
                {
                    comparer = temp;
                    ans = (i + 1).ToString() + " " + (i + 2).ToString();
                }
            }

            temp = Math.Abs(container[0] - container[n-1]);
            if (temp < comparer)
                ans = "1 " + n;

            Console.WriteLine(ans);
            //Console.ReadKey();

        }
    }
}


using System;
using System.Collections.Generic;

namespace CF34A
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			int n = int.Parse (Console.ReadLine ());
			string[] parts = Console.ReadLine ().Split (new char[]{' '});

			List<int> list = new List<int> ();

			int max = 0;

			foreach (String s in parts) {
				int temp = int.Parse (s);

				if (temp > max)
					max = temp;

				list.Add (temp);
			}

			int x = 0;
			int y = 1;

			string result = "";

			while (x < list.Count) {
				while (y < list.Count) {
					if (Math.Abs (list [x] - list [y]) < max) {
						result = (x+1) + " " + (y+1);
						max = Math.Abs (list [x] - list [y]);
					}
					y++;
				}

				x++;
				y = x + 1;
			}

			//connecting dots
			if (Math.Abs (list [0] - list [list.Count - 1]) < max) {
				result = (list.Count) + " " + 1;
				max = Math.Abs (list [list.Count-1] - list [0]);
			}

			Console.WriteLine(result);
		}
	}
}


using System;
using System.Collections.Generic;

namespace CF34A
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			int n = int.Parse (Console.ReadLine ());
			string[] parts = Console.ReadLine ().Split (new char[]{' '});

			List<int> list = new List<int> ();

			int min = 0;

			foreach (String s in parts) {
				int temp = int.Parse (s);

				if (temp < min)
					min = temp;

				list.Add (temp);
			}

			int x = 0;
			int y = 1;

			string result = "";

			while (x < list.Count) {
				while (y < list.Count) {
					if (Math.Abs (list [x] - list [y]) < min) {
						result = (x+1) + " " + (y+1);
						min = Math.Abs (list [x] - list [y]);
					}
					y++;
				}

				x++;
				y = x + 1;
			}

			//connecting dots
			if (Math.Abs (list [0] - list [list.Count - 1]) < min) {
				result = (list.Count) + " " + 1;
				min = Math.Abs (list [list.Count-1] - list [0]);
			}

			Console.WriteLine(result);
		}
	}
}


using System;
using System.Collections.Generic;

namespace CF34A
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			int n = int.Parse (Console.ReadLine ());
			string[] parts = Console.ReadLine ().Split (new char[]{' '});

			List<int> list = new List<int> ();

			int min = -1;

			foreach (String s in parts) {
				int temp = int.Parse (s);
				list.Add (temp);
			}

			int x = 0;
			int y = 1;

			string result = "";

			while (x < list.Count) {
				while (y < list.Count) {
					if (min == -1)
					{
						result = (x+1) + " " + (y+1);
						min = Math.Abs (list [x] - list [y]);
					}
					else
					{
					if (Math.Abs (list [x] - list [y]) < min) {
						result = (x+1) + " " + (y+1);
						min = Math.Abs (list [x] - list [y]);
					}
					}
					y++;
				}

				x++;
				y = x + 1;
			}

			//connecting dots
			if (Math.Abs (list [0] - list [list.Count - 1]) < min) {
				result = (list.Count) + " " + 1;
				min = Math.Abs (list [list.Count-1] - list [0]);
			}

			Console.WriteLine(result);
		}
	}
}


using System;
using System.Collections.Generic;

namespace CF34A
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			int n = int.Parse (Console.ReadLine ());
			string[] parts = Console.ReadLine ().Split (new char[]{' '});

			List<int> list = new List<int> ();

			int min = -1;

			foreach (String s in parts) {
				int temp = int.Parse (s);
				list.Add (temp);
			}

			int x = 0;
			int y = 1;

			string result = "";

			while (x < list.Count && y < list.Count) {

					if (min == -1)
					{
						result = (x+1) + " " + (y+1);
						min = Math.Abs (list [x] - list [y]);
					}
					else
					{
					if (Math.Abs (list [x] - list [y]) < min) {
						result = (x+1) + " " + (y+1);
						min = Math.Abs (list [x] - list [y]);
					}
					}

				x++;
				y++;
			}

			//connecting dots
			if (Math.Abs (list [0] - list [list.Count - 1]) < min) {
				result = (list.Count) + " " + 1;
				min = Math.Abs (list [list.Count-1] - list [0]);
			}

			Console.WriteLine(result);
		}
	}
}


using System;
using System.Linq;

namespace codeforces
{
    class Program
    {
        static void Main(string[] args)
        {
            int t = int.Parse(Console.ReadLine());
            int[] nk = Console.ReadLine().Split().Select(x => int.Parse(x)).ToArray();
            var rez = Enumerable.Range(0, t).Select(x => Math.Abs(nk[x] - nk[(x + 1 == t ? 0 : x + 1)])).ToArray();
            int pos = Array.IndexOf(rez, rez.Min());
            Console.WriteLine((pos+1)+" "+((pos+1==t?0:pos+1)+1));
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace laboratornaya
{
    class Program
    {
        static void Main(string[] args)
        {

            int n = int.Parse(Console.ReadLine());
            var arr = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
            var ARR = new int[arr.Length];
            for (int i = 0; i < arr.Length; i++)
                ARR[i] = arr[i];
            Array.Sort(arr);
            int Minn = 1001;
            int q1 = 0, q2 = 0;
            for (int i = 0; i < n - 1; i++)
            {
                if (Math.Abs(arr[i+1] - arr[i]) < Minn)
                {
                    Minn = Math.Abs(arr[i + 1] - arr[i]);
                    q1 = arr[i]; q2 = arr[i + 1];
                    Console.WriteLine($"q  - {q1}, {q2}");
                }
            }
            Console.WriteLine($"{Array.IndexOf(ARR, q1) + 1}, {Array.IndexOf(ARR, q2, Array.IndexOf(ARR, q1) + 1) + 1}");

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace laboratornaya
{
    class Program
    {
        static void Main(string[] args)
        {

            int n = int.Parse(Console.ReadLine());
            var arr = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
            var ARR = new int[arr.Length];
            for (int i = 0; i < arr.Length; i++)
                ARR[i] = arr[i];
            Array.Sort(arr);
            int Minn = 1001;
            int q1 = 0, q2 = 0;
            for (int i = 0; i < n - 1; i++)
            {
                if (Math.Abs(arr[i+1] - arr[i]) < Minn)
                {
                    Minn = Math.Abs(arr[i + 1] - arr[i]);
                    q1 = arr[i]; q2 = arr[i + 1];

                }
            }
            Console.WriteLine($"{Array.IndexOf(ARR, q1) + 1} {Array.IndexOf(ARR, q2, Array.IndexOf(ARR, q1) + 1) + 1}");

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace laboratornaya
{
    class Program
    {
        static void Main(string[] args)
        {

            int n = int.Parse(Console.ReadLine());
            var arr = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
            var ARR = new int[arr.Length];
            for (int i = 0; i < arr.Length; i++)
                ARR[i] = arr[i];
            Array.Sort(arr);
            int Minn = 1001;
            int q1 = 0, q2 = 0;
            for (int i = 0; i < n - 1; i++)
            {
                if (Math.Abs(arr[i+1] - arr[i]) < Minn)
                {
                    Minn = Math.Abs(arr[i + 1] - arr[i]);
                    q1 = arr[i]; q2 = arr[i + 1];

                }
            }
            Console.WriteLine($"{Array.IndexOf(ARR, q1) + 1} {Array.IndexOf(ARR, q2, Array.IndexOf(ARR, q1)) + 1}");

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace laboratornaya
{
    class Program
    {
        static void Main(string[] args)
        {

            int n = int.Parse(Console.ReadLine());
            var arr = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
            var ARR = new int[arr.Length];
            for (int i = 0; i < arr.Length; i++)
                ARR[i] = arr[i];
            Array.Sort(arr);
            int Minn = 1001;
            int q1 = 0, q2 = 0;
            for (int i = 0; i < n - 1; i++)
            {
                if (Math.Abs(arr[i+1] - arr[i]) < Minn)
                {
                    Minn = Math.Abs(arr[i + 1] - arr[i]);
                    q1 = arr[i]; q2 = arr[i + 1];

                }
            }
            Console.WriteLine($"{Array.IndexOf(ARR, q1) + 1}, {Array.IndexOf(ARR, q2, Array.IndexOf(ARR, q1) + 1) + 1}");

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace laboratornaya
{
    class Program
    {
        static void Main(string[] args)
        {

            int n = int.Parse(Console.ReadLine());
            var arr = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
            int Minn = 1001;
            int q1 = 0, q2 = 0;
            for (int i = 0; i < n - 1; i++)
            {
                if (Math.Abs(arr[i+1] - arr[i]) < Minn)
                {
                    Minn = Math.Abs(arr[i + 1] - arr[i]);
                    q1 = i; q2 = i + 1;

                }
            }
            Console.WriteLine($"{q1} {q2}");

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace laboratornaya
{
    class Program
    {
        static void Main(string[] args)
        {

            int n = int.Parse(Console.ReadLine());
            var arr = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
            int Minn = 1001;
            int last = arr[0];
            int q1 = 0, q2 = 0;
            for (int i = 0; i < n; i++)
            {
                if (i == n - 1)
                {
                    if (Math.Abs(last - arr[i]) < Minn)
                    {
                        q1 = i + 1;
                        q2 = 1;
                    }

                    break;
                }
                if (Math.Abs(arr[i+1] - arr[i]) < Minn)
                {
                    Minn = Math.Abs(arr[i + 1] - arr[i]);
                    q1 = i + 1; q2 = i + 2;

                }
            }
            Console.WriteLine($"{q1} {q2}");

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace laboratornaya
{
    class Program
    {
        static void Main(string[] args)
        {
            /*int n = int.Parse(Console.ReadLine());
            var arr = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
            int q = arr[0];
            arr[0] = 0;
            int count = 0;
            while (true)
            {
                if (q > arr.Max())
                    break;
                for (int i = 1; i < n; i++)
                {
                    if (q <= arr[i])
                    {
                        count++;
                        arr[i]--;
                        q++;
                    }
                }
            }
            Console.WriteLine(count);*/
            int n = int.Parse(Console.ReadLine());
            var arr = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
            int Minn = 1001;
            int last = arr[0];
            int q1 = 0, q2 = 0;
            for (int i = 0; i < n; i++)
            {
                if (i == n - 1)
                {
                    if (Math.Abs(arr[i + 1] - arr[i]) < Minn)
                        q1 = i; q2 = i + 1;
                }
                else if (Math.Abs(arr[i+1] - arr[i]) < Minn)
                {
                    Minn = Math.Abs(arr[i + 1] - arr[i]);
                    q1 = i; q2 = i + 1;
                }
            }
            Console.WriteLine($"{q1} {q2}");

        }
    }
}