You are given an array $a$, consisting of $n$ integers.

Each position $i$ ($1 \le i \le n$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.

For example, let $a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$, the underlined positions are locked. You can obtain the following arrays:   $[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$;  $[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$;  $[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$;  $[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$;  and some others. 

Let $p$ be a sequence of prefix sums of the array $a$ after the rearrangement. So $p_1 = a_1$, $p_2 = a_1 + a_2$, $p_3 = a_1 + a_2 + a_3$, $\dots$, $p_n = a_1 + a_2 + \dots + a_n$.

Let $k$ be the maximum $j$ ($1 \le j \le n$) such that $p_j < 0$. If there are no $j$ such that $p_j < 0$, then $k = 0$.

Your goal is to rearrange the values in such a way that $k$ is minimum possible.

Output the array $a$ after the rearrangement such that the value $k$ for it is minimum possible. If there are multiple answers then print any of them.


-----Input-----

The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.

Then $t$ testcases follow.

The first line of each testcase contains a single integer $n$ ($1 \le n \le 100$) — the number of elements in the array $a$.

The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^5 \le a_i \le 10^5$) — the initial array $a$.

The third line of each testcase contains $n$ integers $l_1, l_2, \dots, l_n$ ($0 \le l_i \le 1$), where $l_i = 0$ means that the position $i$ is unlocked and $l_i = 1$ means that the position $i$ is locked.


-----Output-----

Print $n$ integers — the array $a$ after the rearrangement. Value $k$ (the maximum $j$ such that $p_j < 0$ (or $0$ if there are no such $j$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones.

If there are multiple answers then print any of them.


-----Example-----
Input
5
3
1 3 2
0 0 0
4
2 -3 4 -1
1 1 1 1
7
-8 4 -2 -6 4 7 1
1 0 0 0 1 1 0
5
0 1 -4 6 3
0 0 0 1 1
6
-1 7 10 4 -8 -1
1 0 0 0 0 1

Output
1 2 3
2 -3 4 -1
-8 -6 1 4 4 7 -2
-4 0 1 6 3
-1 4 7 -8 10 -1


-----Note-----

In the first testcase you can rearrange all values however you want but any arrangement will result in $k = 0$. For example, for an arrangement $[1, 2, 3]$, $p=[1, 3, 6]$, so there are no $j$ such that $p_j < 0$. Thus, $k = 0$.

In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.

In the third testcase the prefix sums for the printed array are $p = [-8, -14, -13, -9, -5, 2, 0]$. The maximum $j$ is $5$, thus $k = 5$. There are no arrangements such that $k < 5$.

In the fourth testcase $p = [-4, -4, -3, 3, 6]$.

In the fifth testcase $p = [-1, 3, 10, 2, 12, 11]$.

Difficulty: EASY
Tags: Sorting

Greedy algorithms
Skill types: Sorting

Greedy algorithms

Starter code:

Solutions:
for _ in range(int(input())):
	n = int(input())
	lst1 = list(map(int, input().split()))
	lst2 = list(map(int, input().split()))
	lst3 = []
	for i in range(0, n):
		if lst2[i] == 0:
			lst3.append(lst1[i])
	lst4 = []
	if len(lst3) != 0:
		a = max(lst3)
		d = lst3.index(a)
		nn = len(lst3)
	lst3.sort(reverse=True)
	j = 0
	for i in range(0, n):
		if lst2[i] == 0:
			print(lst3[j], end=' ')
			j += 1
		else:
			print(lst1[i], end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	a = [int(s) for s in input().split()]
	l = [bool(int(s)) for s in input().split()]
	aa = []
	for i in range(n):
		if not l[i]:
			aa.append(a[i])
	aa.sort(reverse=True)
	k = 0
	for i in range(n):
		if l[i]:
			print(a[i], end=' ')
		else:
			print(aa[k], end=' ')
			k += 1
	print()


from sys import stdin, stdout

def input():
	return stdin.readline().strip()

def ans(N, A, L):
	unlocked = []
	for i in range(N):
		if L[i] == 0:
			unlocked += [A[i]]
	unlocked = sorted(unlocked)[::-1]
	j = 0
	for i in range(N):
		if L[i] == 0:
			A[i] = unlocked[j]
			j += 1
	print(*A)
T = int(input())
for t in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	L = list(map(int, input().split()))
	assert N == len(A)
	assert N == len(L)
	ans(N, A, L)


def solver(lst, a, n):
	b = []
	for i in range(n):
		if lst[i] == 0:
			b.append(a[i])
	b.sort()
	p = -1
	for i in range(n):
		if lst[i] == 0:
			a[i] = b[p]
			p -= 1
	return a
for _ in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	l = [int(i) for i in input().split()]
	print(*solver(l, arr, n))


t = int(input())
for i in range(t):
	n = int(input())
	order = []
	lis = list(map(int, input().split()))
	pos = list(map(int, input().split()))
	fixed = [[lis[i], i] for i in range(len(lis)) if pos[i] == 1]
	for i in fixed:
		lis.remove(i[0])
	lis.sort(reverse=True)
	for i in fixed:
		lis.insert(i[1], i[0])
	print(' '.join(map(str, lis)))


def solve(l, A, n):
	B = []
	for i in range(n):
		if l[i] == 0:
			B.append(A[i])
	B.sort()
	p = -1
	for i in range(n):
		if l[i] == 0:
			A[i] = B[p]
			p -= 1
	print(' '.join(map(str, A)))
T = int(input())
for _ in range(T):
	n = int(input())
	arr = list(map(int, input().split()))
	l = list(map(int, input().split()))
	solve(l, arr, n)


def result():
	n = int(input())
	arr = list(map(int, input().split()))
	b = list(map(int, input().split()))
	pos = []
	for i in range(n):
		if b[i] == 1:
			pass
		else:
			pos.append(arr[i])
	pos.sort(reverse=True)
	arr_new = []
	j = 0
	for i in range(n):
		if b[i] == 1:
			arr_new.append(arr[i])
		else:
			arr_new.append(pos[j])
			j = j + 1
	for i in range(n):
		print(arr_new[i], '', end='')
t = int(input())
while t > 0:
	result()
	t = t - 1


import sys
input = sys.stdin.buffer.readline
INF = 10 ** 8
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	res = []
	for i in range(n):
		if l[i] == 0:
			res.append(a[i])
			a[i] = INF
	res.sort(reverse=True)
	idx = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = res[idx]
			idx += 1
	print(*a)


def negativePre():
	n = int(input())
	arr = list(map(int, input().split()))
	pos = list(map(int, input().split()))
	temp = []
	for i in range(n):
		if pos[i] == 0:
			temp.append(arr[i])
	temp.sort()
	count = len(temp) - 1
	for i in range(n):
		if pos[i] == 0:
			arr[i] = temp[count]
			count -= 1
	return arr
for _ in ' ' * int(input()):
	x = negativePre()
	print(*x)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	if 0 not in l:
		print(*a)
	else:
		jhola = []
		for i in range(n):
			if l[i] == 0:
				jhola.append(a[i])
				a[i] = 'g'
		jhola.sort()
		k = len(jhola) - 1
		for i in range(n):
			if l[i] == 0:
				print(jhola[k], end=' ')
				k = k - 1
			else:
				print(a[i], end=' ')
		print()


a = int(input())
for j in range(a):
	length = int(input())
	s = list(map(int, input().strip(' ').split(' ')))
	d = list(map(int, input().strip(' ').split(' ')))
	sd = []
	leng = 0
	for i in range(length):
		if d[i] == 0:
			sd.append(s[i])
			leng = leng + 1
	if leng == 0:
		for i in s:
			print(i, end=' ')
	else:
		sd.sort(reverse=True)
		count = 0
		for i in range(length):
			if d[i] != 1:
				s[i] = sd[count]
				count += 1
		for i in s:
			print(i, end=' ')
	print('')


R = lambda : map(int, input().split())
(t,) = R()
for _ in range(t):
	n = int(input())
	arr = list(R())
	l = list(R())
	s = []
	for i in range(n):
		if not l[i]:
			s.append(arr[i])
	s.sort()
	for i in range(n):
		if not l[i]:
			arr[i] = s.pop()
	print(*arr)


for t in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	L = list(map(int, input().split()))
	B = list(A)
	for i in range(n):
		if L[i] == 1:
			B[i] = -1 * (10 ** 5 + 1)
	B.sort(reverse=True)
	other = 0
	for j in range(n):
		if L[j] == 0:
			A[j] = B[other]
			other += 1
	print(*A)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = [True if x == 1 else False for x in map(int, input().split())]
	a_ = [x for (x, i) in zip(a, l) if not i]
	a_n = [x for x in a_ if x < 0]
	a_p = [x for x in a_ if x > 0]
	a_z = [x for x in a_ if x == 0]
	a_n.sort()
	a_p.sort()
	a_.sort(reverse=True)
	j = 0
	for (i, x) in enumerate(a):
		if not l[i]:
			a[i] = a_[j]
			j += 1
	print(*a)


import io
import os
from collections import Counter, defaultdict, deque

def solve(N, A, L):
	unlocked = []
	for (i, x) in enumerate(A):
		if not L[i]:
			unlocked.append(x)
	unlocked.sort()
	for i in range(N):
		if not L[i]:
			A[i] = unlocked.pop()
	return ' '.join(map(str, A))
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = int(input())
for tc in range(1, TC + 1):
	(N,) = [int(x) for x in input().split()]
	A = [int(x) for x in input().split()]
	L = [int(x) for x in input().split()]
	ans = solve(N, A, L)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	b = [a[i] for i in range(n) if l[i] == 0]
	b.sort(reverse=True)
	c = []
	k = 0
	for i in range(n):
		if l[i] == 0:
			c.append(b[k])
			k += 1
		else:
			c.append(a[i])
	print(*c)


import sys
import bisect as bi
import math
from collections import defaultdict as dd
import heapq
import itertools
input = sys.stdin.readline
import random
mo = 10 ** 9 + 7

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())
for _ in range(inin()):
	n = inin()
	l = ain()
	u = ain()
	te = []
	for i in range(n):
		if u[i] == 0:
			te += [l[i]]
	te.sort(reverse=True)
	c = 0
	for i in range(n):
		if u[i] == 0:
			l[i] = te[c]
			c += 1
	print(*l)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = [a[i] for i in range(n) if b[i] == 0]
	c.sort(reverse=True)
	j = 0
	for i in range(n):
		if b[i] == 0:
			a[i] = c[j]
			j += 1
	print(*a)


t = int(input())
for _ in range(t):
	n = int(input())
	m = [int(i) for i in input().split()]
	l = [int(i) for i in input().split()]
	b = []
	for i in range(n):
		if l[i] == 0:
			b.append(m[i])
	b = sorted(b)
	for i in range(n):
		if l[i] == 0:
			x = b.pop()
			m[i] = x
	print(*m)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	br = list(map(int, input().split()))
	li = []
	for i in range(n):
		if br[i] == 0:
			li.append(ar[i])
	li.sort(reverse=True)
	j = 0
	for i in range(n):
		if br[i] == 0:
			ar[i] = li[j]
			j += 1
	print(*ar)


import sys
import math

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def rinput():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))
mod = int(1000000000.0) + 7
for _ in range(iinput()):
	n = iinput()
	a = get_list()
	l = get_list()
	a2 = []
	for i in range(n):
		if l[i] == 0:
			a2.append(a[i])
	a2.sort(reverse=True)
	j = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = a2[j]
			j += 1
	print(*a)


import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from collections import deque

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = tuple(map(int, input().split()))
		l = tuple(map(int, input().split()))
		lock = deque()
		unlock = deque()
		for (ae, le) in zip(a, l):
			if le:
				lock.append(ae)
			else:
				unlock.append(ae)
		lock = deque(lock)
		unlock_p = [i for i in unlock if i >= 0]
		unlock_m = [i for i in unlock if i < 0]
		unlock_p.sort(reverse=True)
		unlock_m.sort(reverse=True)
		unlock_p = deque(unlock_p)
		unlock_m = deque(unlock_m)
		if sum(a) >= 0:
			res = deque()
			for le in l:
				if le:
					res.append(lock.popleft())
				elif unlock_p:
					res.append(unlock_p.popleft())
				else:
					res.append(unlock_m.popleft())
		else:
			res = deque()
			for le in l:
				if le:
					res.append(lock.popleft())
				elif unlock_m:
					res.append(unlock_m.popleft())
				else:
					res.append(unlock_p.popleft())
		print(*res, sep=' ')
main()


import sys

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')

def ceil(x, y=1):
	return int(-(-x // y))
import math
from collections import defaultdict
for _ in range(ri()):
	n = ri()
	a = ria()
	l = ria()
	z = []
	for i in range(n):
		if l[i] == 0:
			z.append(a[i])
	z.sort(reverse=True)
	j = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = z[j]
			j += 1
	wia(a)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	l = [int(x) for x in input().split()]
	unlocked = []
	for i in range(n):
		if l[i] == 0:
			unlocked.append(a[i])
	u_sort = sorted(unlocked)[::-1]
	j = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = u_sort[j]
			j += 1
	print(' '.join([str(x) for x in a]))


cases = int(input())
for t in range(cases):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	c = []
	for i in range(n):
		if l[i] == 0:
			c.append(a[i])
	c = sorted(c, reverse=True)
	v = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = c[v]
			v += 1
	print(*a)


n = int(input())
for j in range(n):
	input()
	arr = input().split()
	arr = list(map(int, arr))
	b = input().split()
	b = list(map(int, b))
	c = []
	for (i, num) in enumerate(arr):
		if b[i] == 0:
			c.append(num)
	c.sort(reverse=True)
	for (i, num) in enumerate(arr):
		if b[i] == 1:
			continue
		arr[i] = c[0]
		c.pop(0)
	print(' '.join(map(str, arr)))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	c = []
	for j in range(n):
		if l[j] == 0:
			c.append(a[j])
	c.sort(reverse=True)
	k = 0
	for j in range(n):
		if l[j] == 0:
			a[j] = c[k]
			k = k + 1
	print(' '.join(map(str, a)))


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	l = [int(i) for i in input().split()]
	_neg = 0
	_pos = 0
	for i in range(n):
		if a[i] < 0:
			_neg += a[i]
		else:
			_pos += a[i]
	if -_neg > _pos:
		flag = 1
	else:
		flag = 2
	if flag == 1:
		rearrange = []
		for i in range(n):
			if l[i] == 0:
				rearrange.append(a[i])
		rearrange.sort()
		ans = []
		ind = 0
		for i in range(n):
			if l[i] == 0:
				ans.append(rearrange[ind])
				ind += 1
			else:
				ans.append(a[i])
	else:
		rearrange = []
		for i in range(n):
			if l[i] == 0:
				rearrange.append(a[i])
		rearrange.sort(reverse=True)
		ans = []
		ind = 0
		for i in range(n):
			if l[i] == 0:
				ans.append(rearrange[ind])
				ind += 1
			else:
				ans.append(a[i])
	l = ''
	for i in range(n):
		ans[i] = str(ans[i])
	print(' '.join(ans))


cases = int(input())
for case in range(cases):
	length = int(input())
	ls = [int(x) for x in input().split()]
	mask = [int(x) for x in input().split()]
	openlist = [ls[k] for k in range(len(ls)) if not mask[k]]
	openlist.sort(reverse=True)
	newlist = []
	for index in range(len(mask)):
		if mask[index]:
			newlist.append(ls[index])
		else:
			newlist.append(openlist.pop(0))
	print(' '.join([str(val) for val in newlist]))


import sys
input = lambda : sys.stdin.readline().rstrip()
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	ua = []
	for i in range(n):
		if l[i] == 0:
			ua.append(a[i])
	ua.sort(reverse=True)
	idx = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = ua[idx]
			idx += 1
	print(*a)


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	p = [int(i) for i in input().split()]
	st = []
	for i in range(n):
		if p[i] == 0:
			st.append(a[i])
	st.sort()
	for i in range(n):
		if p[i] == 0:
			a[i] = st.pop()
	print(*a)


(z, zz) = (input, lambda : list(map(int, z().split())))
zzz = lambda : [int(i) for i in stdin.readline().split()]
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from string import *
from re import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac
from itertools import permutations as permu

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
num = 1
num = int(z())
for _ in range(num):
	n = int(z())
	arr = zzz()
	lst = zzz()
	ans = []
	ans2 = []
	for (i, j) in enumerate(arr):
		if lst[i] == 0:
			ans.append(j)
		else:
			ans2.append(j)
	ans = sorted(ans)
	l = 0
	r = len(ans) - 1
	k = 0
	t = 0
	s = 0
	for i in lst:
		if i == 0 and k == 0:
			print(ans[r], end=' ')
			r -= 1
		else:
			print(ans2[t], end=' ')
			t += 1
			k = 0
	print()


for _ in range(int(input())):
	n = int(input())
	l1 = list(map(int, input().split()))
	locks = list(map(int, input().split()))
	l2 = []
	c = 0
	for x in range(n):
		if locks[x] == 0:
			l2.append(l1[x])
	l2.sort(reverse=True)
	for x in range(n):
		if locks[x] == 0:
			l1[x] = l2[c]
			c += 1
	print(*l1)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	l = [0] * n
	ar = []
	for i in range(n):
		if b[i] == 0:
			ar.append(a[i])
	ar.sort(reverse=True)
	k = 0
	for i in range(n):
		if b[i] == 0:
			l[i] = ar[k]
			k += 1
		else:
			l[i] = a[i]
	for i in l:
		print(i, end=' ')
	print()


test = int(input())
for _ in range(test):
	n = int(input())
	arr = list(map(int, input().split()))
	lock = list(map(int, input().split()))
	temp = []
	for i in range(n):
		if lock[i] == 0:
			temp.append(arr[i])
	temp.sort(reverse=True)
	k = 0
	for i in range(n):
		if lock[i] == 0:
			arr[i] = temp[k]
			k += 1
	print(*arr)


from math import ceil
t = int(input())
answer = []
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	l = list(map(int, input().split(' ')))
	new_list = []
	for i in range(n):
		if not l[i]:
			new_list.append(a[i])
	new_list.sort(reverse=True)
	temp = []
	index1 = 0
	index2 = 0
	for i in range(n):
		if l[index1]:
			temp.append(str(a[index1]))
			index1 += 1
		else:
			temp.append(str(new_list[index2]))
			index2 += 1
			index1 += 1
	answer.append(' '.join(temp))
print('\n'.join(answer))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	fx = list(map(int, input().split()))
	pos = []
	neg = []
	sm = 0
	for i in range(n):
		if arr[i] < 0 and fx[i] == 0:
			neg.append(arr[i])
		elif fx[i] == 0:
			pos.append(arr[i])
	neg.sort()
	pos.sort()
	for i in range(n):
		if fx[i] == 1:
			continue
		if len(pos) > 0:
			arr[i] = pos.pop()
		else:
			arr[i] = neg.pop()
	print(*arr)


import sys

def get_ints():
	return list(map(int, sys.stdin.readline().strip().split()))

def fast_print(input):
	sys.stdout.write(str(input) + '\n')
t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = get_ints()
	locks = get_ints()
	unlocked = []
	for i in range(n):
		if locks[i] == 0:
			unlocked.append(arr[i])
	unlocked.sort()
	ans = []
	for i in range(n):
		if locks[i] == 1:
			ans.append(arr[i])
		else:
			ans.append(unlocked.pop())
	print(*ans)


from math import ceil
for q in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	s = sum(a)
	if s < 0:
		print(*a)
		continue
	tmp = []
	for i in range(n):
		if b[i] == 0:
			tmp.append(a[i])
	if len(tmp) != 0:
		tmp = sorted(tmp, reverse=True)
	ind = 0
	res = []
	for i in range(n):
		if b[i] == 0:
			res.append(tmp[ind])
			ind += 1
		else:
			res.append(a[i])
	print(*res)


from sys import stdin
T = int(stdin.readline().strip())
for caso in range(T):
	n = int(stdin.readline().strip())
	s = list(map(int, stdin.readline().strip().split()))
	s1 = list(map(int, stdin.readline().strip().split()))
	arr = []
	for i in range(len(s1)):
		if s1[i] == 0:
			arr.append(s[i])
	arr.sort()
	for i in range(len(s)):
		if s1[i] == 0:
			s[i] = arr[-1]
			arr.pop()
	k = 0
	x = 0
	for i in range(len(s1)):
		x += s[i]
		if x < 0:
			k = i + 1
			break
	print(*s)


import sys
input = sys.stdin.readline

def main():
	n = int(input())
	alst = list(map(int, input().split()))
	llst = list(map(int, input().split()))
	num = 10 ** 6
	ans = [num for _ in range(n)]
	lst = []
	for (i, (a, l)) in enumerate(zip(alst, llst)):
		if l:
			ans[i] = a
		else:
			lst.append(a)
	lst.sort(reverse=True)
	pos = 0
	for i in range(n):
		if ans[i] == num:
			ans[i] = lst[pos]
			pos += 1
	print(*ans)
for _ in range(int(input())):
	main()


t = int(input(''))
arr = []
arr1 = []
for i in range(t):
	input('')
	arr.append(list(map(int, input().split())))
	arr1.append(list(map(int, input().split())))
for i in range(t):
	a = arr[i]
	locked = arr1[i]
	n = len(a)
	bodlaimu = []
	for j in range(n):
		if locked[j] == 0:
			bodlaimu.append(a[j])
	bodlaimu.sort(reverse=True)
	for j in range(n):
		if locked[j] == 0:
			a[j] = bodlaimu[0]
			del bodlaimu[0]
	print(*a)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	(m, k) = ([], 0)
	total = sum(a)
	if 0 not in l:
		print(*a)
		continue
	for i in range(n):
		if l[i] == 0:
			m.append(a[i])
	m.append(total)
	m = sorted(m, reverse=True)
	z = m.index(total)
	ll = m[:z]
	rl = m[z + 1:]
	m = ll + rl
	for i in range(n):
		if l[i] == 0:
			a[i] = m[k]
			k += 1
	print(*a)


import os
from sys import stdin, stdout
from heapq import heappush, heappop

def solve(tc):
	n = int(stdin.readline().strip())
	sa = list(map(int, stdin.readline().split()))
	sl = list(map(int, stdin.readline().split()))
	cand = []
	for i in range(n):
		if sl[i] == 0:
			heappush(cand, sa[i])
	ans = sa
	for i in range(n - 1, -1, -1):
		if sl[i] == 0:
			ans[i] = heappop(cand)
	print(*ans)
tcs = 1
tcs = int(stdin.readline().strip())
tc = 1
while tc <= tcs:
	solve(tc)
	tc += 1


t = int(input())
while t > 0:
	n = int(input())
	x = [int(x) for x in input().split()]
	arr = [int(x) for x in input().split()]
	ans = []
	for i in range(n):
		if arr[i] == 0:
			ans.append(x[i])
			x[i] = None
	ans.sort()
	for i in range(n):
		if x[i] == None:
			x[i] = ans.pop()
	t -= 1
	print(*x)


from math import ceil
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = []
	for i in range(len(a)):
		if b[i] == 0:
			c.append(a[i])
	c.sort(reverse=True)
	k = 0
	for i in range(len(a)):
		if b[i] == 1:
			print(a[i], end=' ')
		else:
			print(c[k], end=' ')
			k = k + 1
	print()


for i in range(int(input())):
	n = int(input())
	a = [int(j) for j in input().split()]
	l = [int(j) for j in input().split()]
	b = []
	for j in range(n):
		if l[j] == 0:
			b.append(a[j])
	b = sorted(b)[::-1]
	k = 0
	for j in range(n):
		if l[j] == 0:
			a[j] = b[k]
			k = k + 1
	print(' '.join((str(j) for j in a)))


import sys
input = sys.stdin.readline
read = lambda : map(int, input().split())
read_float = lambda : map(float, input().split())

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def fact(x):
	val = 1
	while x > 0:
		val *= x
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // math.gcd(a, b)

def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	(prime[0], prime[1]) = (False, False)
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def phi(n):
	result = n
	p = 2
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			result = result * (1.0 - 1.0 / float(p))
		p = p + 1
	if n > 1:
		result = result * (1.0 - 1.0 / float(n))
	return int(result)

def is_prime(n):
	if n == 0:
		return False
	if n == 1:
		return True
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]

def factoriazation(x):
	res = []
	for i in range(2, int(x ** 0.5) + 1):
		while x % i == 0:
			res.append(i)
			x //= i
	if x != 1:
		res.append(x)
	return res

def factors(n):
	res = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			res.append(i)
			res.append(n // i)
	return list(set(res))

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()

def binary_search(low, high, w, h, n):
	while low < high:
		mid = low + (high - low) // 2
		if check(mid, w, h, n):
			low = mid + 1
		else:
			high = mid
	return low

def check(councils, a, k):
	summ = 0
	for x in a:
		summ += min(x, councils)
	return summ // councils >= k
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
from itertools import permutations
import math
import bisect as bis
import random
import sys

def solve():
	n = int(input())
	array = list(read())
	locks = list(read())
	unlocked = []
	for i in range(n):
		if locks[i] == 0:
			unlocked.append(array[i])
	if len(unlocked) == 0:
		print(*array)
		return
	unlocked.sort()
	for i in range(n):
		if locks[i] == 0:
			array[i] = unlocked[-1]
			unlocked.pop()
	print(*array)
for _ in range(int(input())):
	solve()


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
import threading
threading.stack_size(10 ** 8)
sys.setrecursionlimit(300000)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
mod = 10 ** 9 + 7
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a1 = a
	l = list(map(int, input().split()))
	m = list()
	pre = list()
	pre1 = list()
	c1 = 0
	c2 = 0
	for i in range(n):
		if l[i] == 0:
			m.append(a[i])
	m.sort()
	c = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = m[c]
			c += 1
		if i == 0:
			pre.append(a[i])
		else:
			pre.append(pre[-1] + a[i])
		if pre[i] < 0 and c1 == 0:
			c1 = i + 1
	m.sort(reverse=True)
	c = 0
	for i in range(n):
		if l[i] == 0:
			a1[i] = m[c]
			c += 1
		if i == 0:
			pre1.append(a[i])
		else:
			pre1.append(pre1[-1] + a1[i])
		if pre1[i] < 0 and c2 == 0:
			c2 = i + 1
	if c1 < c2:
		print(*a)
	else:
		print(*a1)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	lis = list(map(int, input().split()))
	num = []
	for i in range(n):
		if lis[i] == 0:
			num.append(arr[i])
	num.sort(reverse=True)
	ans = []
	j = 0
	for i in range(n):
		if lis[i] == 1:
			ans.append(arr[i])
		else:
			ans.append(num[j])
			j += 1
	print(' '.join((str(x) for x in ans)))


for _ in range(int(input())):
	n = int(input())
	arr = [int(num) for num in input().split(' ')]
	lock = [int(num) for num in input().split(' ')]
	temp = []
	idx = []
	for i in range(n):
		if lock[i] == 0:
			temp.append(arr[i])
			idx.append(i)
	temp.sort()
	for i in range(n):
		if i in idx:
			arr[i] = temp.pop()
	for item in arr:
		print(item, end=' ')
	print('')


t = int(input())
q = []
for j in range(t):
	n = int(input())
	l = list(map(int, list(input().split(' '))))
	m = list(map(int, list(input().split(' '))))
	s = [l[i] for i in range(n) if m[i] == 0]
	s.sort(reverse=True)
	j = 0
	for i in range(n):
		if m[i] == 0:
			l[i] = s[j]
			j += 1
	q.append(l)
for j in q:
	for i in j:
		print(i, end=' ')
	print()


test = int(input())
for _ in range(test):
	n = int(input())
	arr = list(map(int, input().split()))
	s = list(map(int, input().split()))
	if sum(s) == n or sum(s) == n - 1:
		print(*arr)
	else:
		g = sum(arr)
		rem = []
		for i in range(n):
			if s[i] == 0:
				rem.append(arr[i])
		rem.sort()
		if g >= 0:
			for i in range(n):
				if s[i] == 0:
					arr[i] = rem.pop()
		else:
			rem.reverse()
			for i in range(n):
				if s[i] == 0:
					arr[i] = rem.pop()
		print(*arr)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]
	c = []
	for i in range(n):
		if b[i] == 0:
			c.append(a[i])
	c.sort(reverse=True)
	d = 0
	for i in range(n):
		if b[i] == 0:
			a[i] = c[d]
			d += 1
	for i in range(n):
		print(a[i], end=' ')
	print()


import os
import sys
from io import BytesIO, IOBase
import math

def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		l = list(map(int, input().split()))
		ls = []
		for i in range(n):
			if l[i] == 0:
				ls.append(a[i])
		ls.sort(reverse=True)
		ans = []
		j = 0
		ans = []
		for i in range(n):
			if l[i]:
				ans.append(a[i])
			else:
				ans.append(ls[j])
				j += 1
		print(*ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	locked = list(map(int, input().split()))
	b = []
	for i in range(n):
		if locked[i] == 0:
			b.append(a[i])
	index = 0
	b.sort(reverse=True)
	for i in range(n):
		if locked[i] == 0:
			a[i] = b[index]
			index += 1
	print(*a)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	p = list(map(int, input().split()))
	l = []
	d = []
	j = 0
	for i in range(n):
		if p[i] == 0:
			d.append(j)
			l.append(a[i])
		j += 1
	l.sort(reverse=True)
	j = 0
	for i in d:
		a[i] = l[j]
		j += 1
	print(' '.join(map(str, a)))


import sys
import math, bisect
sys.setrecursionlimit(10 ** 5)
from collections import defaultdict
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, OrderedDict

def I():
	return int(sys.stdin.readline())

def neo():
	return map(int, sys.stdin.readline().split())

def Neo():
	return list(map(int, sys.stdin.readline().split()))
for _ in range(I()):
	N = I()
	A = Neo()
	l = Neo()
	Ans = []
	for (i, j) in zip(A, l):
		if j == 0:
			Ans.append(i)
	Ans.sort()
	Ans = Ans[::-1]
	for i in range(N):
		if l[i] == 0:
			A[i] = Ans[0]
			Ans = Ans[1:]
	print(*A)


import os, sys
from io import IOBase, BytesIO
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys

class ostream:

	def __lshift__(self, a):
		sys.stdout.write(str(a))
		return self
cout = ostream()
endl = '\n'

def solve():
	t = int(input())
	for _ in range(t):
		n = int(input())
		arr = list(map(int, input().split()))
		l = list(map(int, input().split()))
		new_r = []
		for i in range(len(l)):
			if l[i] == 0:
				new_r.append(arr[i])
		new_r.sort(reverse=True)
		ptr = 0
		k_mx = 0
		prev_sum = 0
		for i in range(len(l)):
			if l[i] == 0:
				arr[i] = new_r[ptr]
				ptr += 1
			prev_sum += arr[i]
			if prev_sum < 0:
				k_mx = i
		cout << ' '.join(map(str, arr)) << endl

def main():
	solve()
main()


import heapq
from collections import *
import math

def solve():
	n = int(input())
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]
	pos = []
	neg = []
	for i in range(n):
		if b[i] == 0 and a[i] < 0:
			neg.append(a[i])
		elif b[i] == 0 and a[i] >= 0:
			pos.append(a[i])
	pos.sort(reverse=True)
	neg.sort()
	ans = [0 for i in range(n)]
	for i in range(n):
		if b[i] == 1:
			ans[i] = a[i]
		elif len(pos) != 0:
			ans[i] = pos.pop(0)
		else:
			ans[i] = neg.pop()
	print(*ans)
t = int(input())
for _ in range(t):
	solve()


import sys

def solution():
	T = int(input())
	arr = []
	locked = []
	result = []
	for _ in range(T):
		n = int(input())
		arr = list(map(int, input().split()))
		locked = list(map(int, input().split()))
		result = helper(arr, locked)
		print(*result)

def helper(arr, locked):
	n = len(arr)
	unlocked = []
	for i in range(n):
		if locked[i] == 0:
			unlocked.append(arr[i])
	unlocked.sort(reverse=True)
	index = 0
	for i in range(n):
		if locked[i] == 0:
			arr[i] = unlocked[index]
			index += 1
	return arr
solution()


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	l = list(map(int, input().split()))
	new = []
	ref = []
	for j in range(n):
		if l[j] != 1:
			new.append(arr[j])
		else:
			ref.append((arr[j], j))
	new = sorted(new)[::-1]
	for x in ref:
		new.insert(x[1], x[0])
	print(*new)


for i in range(int(input())):
	N = int(input())
	output = []
	arr = list(map(int, input().split()))
	status = list(map(int, input().split()))
	sorting_array = []
	for i in range(N):
		if status[i] == 0:
			sorting_array.append(arr[i])
	sorted_array = sorted(sorting_array)
	sorted_array.reverse()
	d = 0
	for i in range(N):
		if status[i] == 0:
			output.append(sorted_array[d])
			d += 1
		else:
			output.append(arr[i])
	print(*output)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	allowed = []
	for i in range(n):
		if l[i] == 0:
			allowed.append(a[i])
	allowed.sort(reverse=True)
	p = sum(a)
	if p >= 0:
		for i in range(n - 1, -1, -1):
			if l[i] == 0:
				a[i] = allowed.pop()
			p -= a[i]
	print(' '.join((str(i) for i in a)))


from sys import stdin
input = lambda : stdin.readline().strip()
lin = lambda : list(map(int, input().split()))
iin = lambda : int(input())
main = lambda : map(int, input().split())
from math import ceil, sqrt, factorial, log
from collections import deque
from bisect import bisect_left

def gcd(a, b):
	(a, b) = (max(a, b), min(a, b))
	while a % b != 0:
		(a, b) = (b, a % b)
	return b

def solve():
	n = iin()
	a = lin()
	l = lin()
	su = sum(a)
	(q, w) = ([], [])
	for i in range(n):
		if l[i] == 0:
			if a[i] >= 0:
				q.append(a[i])
			else:
				w.append(a[i])
	q.sort(reverse=True)
	w.sort(reverse=True)
	for i in range(n - 1, -1, -1):
		if l[i] == 1:
			su -= a[i]
		else:
			if w:
				a[i] = w.pop()
			else:
				a[i] = q.pop()
			su -= a[i]
	print(*a)
qwe = 1
qwe = int(input())
for _ in range(qwe):
	solve()


from collections import *
for T in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	m = sorted([x for (i, x) in enumerate(a) if b[i] == 0])[::-1]
	j = 0
	for (i, x) in enumerate(a):
		if b[i] == 0:
			a[i] = m[j]
			j += 1
	print(' '.join(map(str, a)))


t = int(input())
for i in range(0, t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	b = list(map(int, input().split(' ')))
	l = []
	for i in range(0, len(a)):
		if b[i] == 0:
			l.append(a[i])
	if not l:
		print(*a)
	else:
		j = 0
		l.sort(reverse=True)
		for i in range(0, len(a)):
			if b[i] == 0:
				a[i] = l[j]
				j += 1
		print(*a)


from collections import deque
h = int(input())
l = []
for i in range(h):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	p = []
	queue = deque
	s = []
	n = 0
	d = 0
	for j in a:
		n += j
		p.append(n)
	for j in range(len(a)):
		if b[j] == 0:
			s.append(a[j])
	s.sort()
	for i in range(len(a)):
		if b[i] == 0:
			a[i] = s.pop()
	for j in a:
		print(j, end=' ')
	print()


t = int(input())
for q in range(t):
	n = int(input())
	a = [int(i) for i in input().strip().split(' ')]
	x = [int(i) for i in input().strip().split(' ')]
	b = []
	for i in range(n):
		if x[i] == 0:
			b.append(a[i])
	b.sort()
	b.reverse()
	j = 0
	for i in range(n):
		if x[i] == 0:
			print(b[j], end=' ')
			j += 1
		else:
			print(a[i], end=' ')
	print()


def solve(arr, can_modify):
	can_modify_arr = sorted([arr_item for (arr_item, can_modify_item) in zip(arr, can_modify) if can_modify_item == 0])[::-1]
	cannot_modify_arr = [arr_item for (arr_item, can_modify_item) in zip(arr, can_modify) if can_modify_item == 1]
	can_modify_ptr = cannot_modify_ptr = 0
	result = []
	for can_modify_item in can_modify:
		if can_modify_item == 1:
			result.append(cannot_modify_arr[cannot_modify_ptr])
			cannot_modify_ptr += 1
		else:
			result.append(can_modify_arr[can_modify_ptr])
			can_modify_ptr += 1
	return result
for _ in range(int(input())):
	_ = int(input())
	arr = [int(t) for t in input().split()]
	can_modify = [int(t) for t in input().split()]
	print(*solve(arr, can_modify))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	ke = []
	x = zip(b, a)
	for (i, j) in x:
		if i == 0:
			ke.append(j)
	ke = sorted(ke, reverse=True)
	indx = 0
	for (k, i) in enumerate(b):
		if i == 0:
			a[k] = ke[indx]
			indx += 1
	for i in a:
		print(i, end=' ')
	print()


from sys import stdin, stdout
for _ in ' ' * int(stdin.readline()):
	a = int(stdin.readline())
	b = list(map(int, stdin.readline().split()))
	c = list(map(int, stdin.readline().split()))
	ans = [0] * a
	k = []
	for i in range(a):
		if c[i]:
			ans[i] = b[i]
		else:
			k.append(b[i])
	k.sort()
	for i in range(a):
		if c[i] == 0:
			ans[i] = k.pop()
	stdout.write(' '.join(map(str, ans)) + '\n')


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	lock = list(map(int, input().split()))
	new = []
	for i in range(n):
		if lock[i] == 0:
			new.append(a[i])
	new.sort()
	for i in range(n):
		if lock[i] == 0:
			a[i] = new.pop()
	print(*a)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def YES(c):
	return IF(c, 'YES', 'NO')

def Yes(c):
	return IF(c, 'Yes', 'No')

def main():
	t = I()
	rr = []
	for _ in range(t):
		n = I()
		a = LI()
		b = LI()
		t = []
		c = []
		for i in range(n):
			if b[i] == 0:
				t.append(i)
				c.append(a[i])
		c.sort(reverse=True)
		for (i, u) in zip(t, c):
			a[i] = u
		rr.append(JA(a, ' '))
	return JA(rr, '\n')
print(main())


import sys
import math
input = sys.stdin.readline
from collections import deque
from queue import LifoQueue

def binary_search(a, n):
	L = 0
	R = len(a) - 1
	while L <= R:
		mid = L + (R - L) // 2
		if a[mid] == n:
			return mid
		elif a[mid] < n:
			L = mid + 1
		else:
			R = mid - 1
	return -1
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	i = 0
	p = []
	while i < n:
		if not l[i]:
			p.append(a[i])
		i += 1
	p.sort(reverse=True)
	c = 0
	for i in range(n):
		if not l[i]:
			a[i] = p[c]
			c += 1
	print(*a)


TC = int(input())
for tc in range(TC):
	N = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	S = sum(A)
	result = A
	if S >= 0:
		nb = []
		for (a, b) in zip(A, B):
			if b == 0:
				nb.append(a)
		nb.sort()
		j = 0
		for i in range(N - 1, -1, -1):
			if B[i] == 0:
				result[i] = nb[j]
				j += 1
	for r in result:
		print(r, end=' ')
	print()


for _ in range(int(input())):
	N = int(input())
	X = list(map(int, input().split()))
	L = list(map(int, input().split()))
	Unlocked = [X[i] for i in range(N) if L[i] == 0]
	Unlocked = sorted(Unlocked)
	for i in range(N):
		if L[i] == 0:
			X[i] = Unlocked.pop()
	print(*X)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	if sum(a) < 0:
		print(' '.join([str(i) for i in a]))
	else:
		unlocked = [a[i] for i in range(n) if not l[i]]
		sorted_vals = sorted(unlocked, key=int, reverse=True)
		output = []
		i = 0
		j = 0
		while i < n:
			if l[i]:
				output.append(a[i])
			else:
				output.append(sorted_vals[j])
				j += 1
			i += 1
		print(' '.join([str(i) for i in output]))


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	C = []
	for i in range(n):
		if B[i] == 0:
			C.append(A[i])
	C.sort()
	for i in range(n):
		if B[i]:
			print(A[i], end=' ')
		else:
			print(C[-1], end=' ')
			C.pop()
	print()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	ans = [0] * n
	vspomog = []
	for i in range(n):
		if b[i] != 1:
			vspomog.append(a[i])
		else:
			ans[i] = a[i]
	vspomog.sort()
	ukazatel_m = 0
	ukazatel = len(vspomog) - 1
	for i in range(n):
		if b[i] == 1:
			ukazatel_m += 1
		if b[i] == 0:
			ans[ukazatel_m] = vspomog[ukazatel]
			ukazatel -= 1
			ukazatel_m += 1
	print(*ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = []
	for i in range(n):
		if b[i] == 0:
			c.append(a[i])
	c = sorted(c, reverse=True)
	l = 0
	v = ''
	for i in range(n):
		if b[i] == 0:
			v += str(c[l]) + ' '
			l += 1
		else:
			v += str(a[i]) + ' '
	print(v.strip())


import sys

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c for j in range(b)] for i in range(a)]

def list3d(a, b, c, d):
	return [[[d for k in range(c)] for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e for l in range(d)] for k in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 19
MOD = 10 ** 9 + 7
EPS = 10 ** (-10)
for _ in range(INT()):
	N = INT()
	A = LIST()
	B = LIST()
	ans = [0] * N
	A2 = []
	for i in range(N):
		if B[i]:
			ans[i] = A[i]
		else:
			A2.append(A[i])
	A2.sort(reverse=1)
	j = 0
	for i in range(N):
		if not B[i]:
			ans[i] = A2[j]
			j += 1
	print(*ans)


for _ in range(int(input())):
	n = int(input())
	(*arr,) = map(int, input().split())
	(*locks,) = map(int, input().split())
	locked = []
	unlocked = []
	for (v, is_locked) in zip(arr, locks):
		if not is_locked:
			unlocked.append(v)
	unlocked.sort(reverse=True)
	arr_idx = 0
	unlocked_idx = 0
	while unlocked_idx < len(unlocked) and arr_idx < len(arr):
		if not locks[arr_idx]:
			arr[arr_idx] = unlocked[unlocked_idx]
			unlocked_idx += 1
		arr_idx += 1
	print(*arr)


import math
test_cases = int(input())
for test_case in range(test_cases):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	unlocked = []
	for i in range(n):
		if l[i] == 0:
			unlocked.append(a[i])
	unlocked.sort(reverse=True)
	ans = []
	if unlocked == []:
		ans = a
	j = 0
	c = 0
	for i in unlocked:
		c += 1
		while j <= n - 1:
			if l[j] == 0:
				ans.append(i)
				j += 1
				if c < len(unlocked):
					break
			else:
				ans.append(a[j])
				j += 1
	print(*ans)


import sys
input = sys.stdin.readline
INF = 10 ** 9
t = int(input())
for i in range(t):
	n = int(input())
	a = [int(item) for item in input().split()]
	l = [int(item) for item in input().split()]
	ans = [None] * n
	floating = []
	for (i, (ai, li)) in enumerate(zip(a, l)):
		if li:
			ans[i] = ai
		else:
			floating.append(ai)
	floating.sort()
	for i in range(n):
		if ans[i] == None:
			ans[i] = floating.pop()
	print(*ans)


import math
debug = True

def pprint(*args, delimeter='-'):
	if debug:
		print('{0} DEBUG {0}'.format(delimeter * 20))
		print(*args)
		print('{0} DEBUG {0}'.format(delimeter * 20))

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	if sum(a) < 0:
		return a
	unlocked = []
	for i in range(n):
		if l[i] == 0:
			unlocked.append(a[i])
	unlocked.sort(reverse=True)
	index = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = unlocked[index]
			index += 1
	return a
t = int(input())
while t > 0:
	t -= 1
	print(*solve())


from collections import Counter, defaultdict as dft

def mp():
	return map(int, input().split())

def ml():
	return list(map(int, input().split()))

def solve():
	n = int(input())
	arr = ml()
	lock = ml()
	res = [0] * n
	unlok = []
	sml = 0
	smu = 0
	for i in range(n):
		if lock[i]:
			sml += arr[i]
		else:
			unlok.append(arr[i])
			smu += arr[i]
	unlok.sort(reverse=True)
	s = 0
	for i in range(n):
		if lock[i]:
			res[i] = arr[i]
		else:
			res[i] = unlok[s]
			s += 1
	print(*res)
t = int(input())
for _ in range(t):
	solve()


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	lock = list(map(int, input().split()))
	unlock_integers = []
	for i in range(n):
		if lock[i] == 0:
			unlock_integers.append(a[i])
	unlock_integers = sorted(unlock_integers)
	q = 0
	total = sum(a)
	result = [None] * n
	for i in range(n - 1, -1, -1):
		if lock[i] == 0:
			if total >= 0:
				result[i] = unlock_integers[q]
				q += 1
			else:
				result[i] = unlock_integers.pop()
		else:
			result[i] = a[i]
		total -= result[i]
	print(*result)


from itertools import permutations
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	locks = list(map(int, input().split()))
	neg = []
	pos = []
	for i in range(n):
		if locks[i] == 0:
			pos.append(arr[i])
	pos = sorted(pos, reverse=True)
	x = 0
	for i in range(n):
		if locks[i] == 0:
			arr[i] = pos[x]
			x += 1
	print(*arr)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	b = []
	for i in range(n):
		if l[i] == 0:
			b.append(a[i])
	b.sort()
	j = 0
	p = 0
	ans = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = b.pop()
		print(a[i], end=' ')
	print('')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ls = list(map(int, input().split()))
	a = []
	for i in range(n):
		if ls[i] == 0:
			a.append(arr[i])
	a.sort()
	var = 0
	for i in range(n - 1, -1, -1):
		if ls[i] == 0:
			arr[i] = a[var]
			var += 1
	print(*arr)


T = int(input())
for _ in range(T):
	n = int(input())
	aa = list(map(int, input().split()))
	ll = list(map(int, input().split()))
	xx = sorted((a for (a, l) in zip(aa, ll) if l == 0), reverse=True)
	c = 0
	for (i, l) in enumerate(ll):
		if l == 0:
			aa[i] = xx[c]
			c += 1
	print(' '.join(map(str, aa)))


class Solution:

	def solve(self, n, arr, locked):
		ans = []
		unlocked_it = [arr[i] for i in range(n) if not locked[i]]
		unlocked_it.sort()
		for i in range(n):
			if locked[i]:
				ans.append(arr[i])
			else:
				ans.append(unlocked_it.pop())
		return ans

def main():
	obj = Solution()
	t = int(input())
	while t:
		n = int(input())
		arr = list(map(int, input().split()))
		locked = list(map(int, input().split()))
		print(*obj.solve(n, arr, locked))
		t -= 1
main()


INF = float('inf')

def prod(arr):
	ret = 1
	for x in arr:
		ret *= x
	return ret

def tc():
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	uis = []
	for i in range(n):
		if not l[i]:
			uis.append(i)
	uvals = [a[i] for i in uis]
	uvals.sort()
	for i in uis:
		a[i] = uvals.pop()
	print(' '.join(map(str, a)))
T = int(input())
for _ in range(T):
	tc()


def solve(n, a, b):
	bi = [i for i in range(n) if not b[i]]
	ax = [a[i] for i in range(n) if not b[i]]
	ax.sort(reverse=True)
	for i in range(len(bi)):
		a[bi[i]] = ax[i]
	print(*a)
t = int(input())
for _ in range(t):
	n = int(input())
	(*a,) = map(int, input().split())
	(*b,) = map(int, input().split())
	solve(n, a, b)


t = int(input())
for i in range(t):
	n = int(input())
	nums = list(map(int, input().split()))
	st = list(map(int, input().split()))
	sv = []
	for j in range(n):
		if st[j] == 0:
			sv.append(nums[j])
	sv.sort(reverse=True)
	new = []
	k = 0
	for j in range(n):
		if st[j] == 1:
			new.append(nums[j])
		else:
			new.append(sv[k])
			k += 1
	print(*new)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	a2 = [a[i] for i in range(n) if l[i] == 0]
	a2.sort(key=lambda x: -x)
	i = 0
	for j in range(n):
		if l[j] == 0:
			a[j] = a2[i]
			i += 1
	print(*a)


from random import shuffle

def solve():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		l = list(map(int, input().split()))
		not_fixed = []
		fixed = []
		for i in range(n):
			if not l[i]:
				not_fixed.append(a[i])
			else:
				fixed.append(a[i])
		shuffle(not_fixed)
		not_fixed = sorted(not_fixed, reverse=True)
		(i1, i2) = (0, 0)
		for e in l:
			if not e:
				print(not_fixed[i1], end=' ')
				i1 += 1
			else:
				print(fixed[i2], end=' ')
				i2 += 1
		print()
solve()


import sys
input = sys.stdin.readline
for nt in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = []
	for i in range(n):
		if b[i] != 1:
			c.append(a[i])
	ans = []
	j = 0
	c.sort(reverse=True)
	for i in range(n):
		if b[i] == 0:
			ans.append(c[j])
			j += 1
		else:
			ans.append(a[i])
	print(*ans)


def rearr(n, a, l):
	unlocked = []
	for i in range(len(a)):
		if l[i] == 0:
			unlocked.append(a[i])
	unlocked.sort(reverse=True)
	j = 0
	for i in range(len(a)):
		if l[i] == 0:
			a[i] = unlocked[j]
			j += 1
		if j == len(unlocked):
			break
	return a
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	print(*rearr(n, a, l))


number_tests = int(input())
for _ in range(number_tests):
	n = int(input())
	li = list(map(int, input().split()))
	lock = list(map(int, input().split()))
	ls = sorted([v for (i, v) in enumerate(li) if lock[i] == 0])[::-1]
	lr = []
	j = 0
	for i in range(n):
		if lock[i]:
			lr.append(li[i])
		else:
			lr.append(ls[j])
			j += 1
	print(*lr)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	p = []
	for i in range(n):
		if l[i] == 0:
			p.append(a[i])
	p.sort()
	for i in range(n):
		if l[i] == 0:
			a[i] = p.pop()
	print(*a)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
import threading
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = int(input())
while t != 0:
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	x = []
	y = []
	for i in range(n):
		if l[i] == 0:
			x.append(a[i])
	x.sort(reverse=True)
	j = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = x[j]
			j += 1
	tot = 0
	for (i, item) in enumerate(a):
		tot += item
		if tot < 0 and l[i] == 0:
			y.append(i)
	for i in range(n):
		if i not in y and l[i] == 0:
			y.append(i)
	for i in range(len(y)):
		y[i] = a[y[i]]
	j = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = y[j]
			j += 1
	for (i, item) in enumerate(a):
		if i == n - 1:
			print(item)
		else:
			print(item, end=' ')
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().rstrip().split()))
	l = list(map(int, input().rstrip().split()))
	ans = []
	s = 0
	ctneg = 0
	for i in a:
		if i < 0:
			ctneg += 1
		s += i
	if s < 0 or ctneg == 0:
		print(*a)
		continue
	ind = {}
	val = []
	for i in range(n):
		if l[i] == 0:
			val.append(a[i])
			ind[i] = True
	val.sort(reverse=True)
	j = 0
	for i in range(n):
		if i not in ind.keys():
			ans.append(a[i])
		else:
			ans.append(val[j])
			j += 1
	print(*ans)


import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	C = []
	for j in range(n):
		if B[j] == 0:
			C.append(A[j])
	C.sort(reverse=True)
	indc = 0
	for j in range(n):
		if B[j] == 0:
			A[j] = C[indc]
			indc += 1
	print(*A)


t = int(input())
for _ in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	l = list(map(int, input().split()))
	an = []
	for i in range(n):
		if l[i] == 0:
			an.append(x[i])
	an.sort(reverse=True)
	k = 0
	ans = []
	for i in range(n):
		if l[i] == 0:
			x[i] = an[k]
			k += 1
	print(*x)


from sys import stdin, stdout
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf
mod = pow(10, 9) + 7
mod2 = 998244353

def inp():
	return stdin.readline().strip()

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def smp():
	return map(str, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]

def remadd(x, y):
	return 1 if x % y else 0
for _ in range(int(inp())):
	n = int(inp())
	arr1 = lmp()
	arr2 = lmp()
	ml = []
	for i in range(n):
		if arr2[i] == 0:
			ml.append(arr1[i])
	ml.sort()
	for i in range(n):
		if arr2[i] == 0:
			arr1[i] = ml.pop()
	outa(*arr1)


for i in range(int(input())):
	n = int(input())
	ls1 = [int(a) for a in input().split()]
	ls2 = [int(a) for a in input().split()]
	ls3 = []
	for j in range(n):
		if ls2[j] == 0:
			ls3.append(ls1[j])
	ls3.sort()
	for j in range(n):
		if ls2[j]:
			ls2[j] = ls1[j]
		else:
			ls2[j] = ls3[-1]
			ls3.pop()
	an = ''.join((str(a) + ' ' for a in ls2))
	print(an)
	ls4 = []
	for j in range(1, n + 1):
		ls4.append(sum(ls2[:j]))


import math
t = int(input())
for i in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	z = []
	for i in range(n):
		if y[i] == 0:
			z.append(x[i])
	z.sort()
	z.reverse()
	k = 0
	j = 0
	while j < len(z):
		if y[k] != 1:
			x[k] = z[j]
			j += 1
		k += 1
	print(*x)


c = int(input())
for i in range(c):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	item = []
	for i in range(n):
		if l[i] == 0:
			item.append(a[i])
		else:
			continue
	item.sort(reverse=True)
	j = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = item[j]
			j = j + 1
	for i in a:
		print(i, end=' ')
	print()


import math
from collections import defaultdict
import functools as f
from itertools import permutations as p
t = 1
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	pos = list(map(int, input().split()))
	l = []
	for i in range(n):
		if pos[i] == 0:
			l.append(a[i])
	l.sort(reverse=True)
	j = 0
	for i in range(n):
		if pos[i] == 0:
			a[i] = l[j]
			j += 1
	print(*a)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	pos = []
	neg = []
	for i in range(0, n):
		if l[i] == 1:
			continue
		if a[i] < 0:
			neg.append(a[i])
		else:
			pos.append(a[i])
	neg.sort(reverse=True)
	pos.sort(reverse=True)
	p1 = len(neg) - 1
	p2 = len(pos) - 1
	ans = [0] * n
	for i in range(n - 1, -1, -1):
		if l[i] == 1:
			ans[i] = a[i]
		elif p1 >= 0:
			ans[i] = neg[p1]
			p1 -= 1
		else:
			ans[i] = pos[p2]
			p2 -= 1
	print(*ans)


import math
from collections import Counter, defaultdict, deque

def int1():
	return int(input())

def map1():
	return map(int, input().split())

def list1():
	return list(map(int, input().split()))
mod = pow(10, 9) + 7

def solve():
	n = int1()
	l1 = list1()
	l2 = list1()
	l3 = []
	for i in range(n):
		if l2[i] == 0:
			l3.append(l1[i])
	l3.sort()
	for i in range(n):
		if l2[i] == 0:
			l1[i] = l3.pop()
	print(*l1)
for _ in range(int1()):
	solve()


for _ in range(int(input())):
	n = int(input())
	data = list(map(int, input().split()))
	flag = list(map(int, input().split()))
	for_sort = []
	for i in range(n):
		if flag[i] == 0:
			for_sort.append(data[i])
	for_sort.sort(reverse=True)
	index = 0
	final_list = []
	for i in range(n):
		if flag[i] == 0:
			final_list.append(for_sort[index])
			index += 1
		else:
			final_list.append(data[i])
	for x in final_list:
		print(x, end=' ')
	print()


from sys import stdin

def input():
	return stdin.readline()

def lmi():
	return list(map(int, input().split()))
T = int(input())
for _ in range(T):
	input()
	temp = list()
	(s, l) = (lmi(), lmi())
	for (i, c) in enumerate(s):
		if l[i] == 0:
			temp.append(c)
	temp = sorted(temp)[::-1]
	cc = 0
	for (i, x) in enumerate(s):
		if l[i] == 1:
			print(x, end=' ')
		else:
			print(temp[cc], end=' ')
			cc += 1
	print()


for _ in range(int(input())):
	n = int(input())
	v = list(map(int, input().split()))
	l = list(map(int, input().split()))
	pl = []
	j = 0
	for i in range(n):
		if l[i] == 0:
			pl.append(v[i])
	pl.sort(reverse=True)
	x = []
	for i in range(n):
		if l[i] == 0:
			x.append(pl[j])
			j += 1
		else:
			x.append(v[i])
	for i in x:
		print(i, end=' ')
	print()


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	locked = list(map(int, stdin.readline().split()))
	(b, ans) = ([], [])
	for (l, v) in zip(locked, a):
		if not l:
			b += [v]
	b.sort()
	for i in range(n):
		if locked[i]:
			ans += [a[i]]
		else:
			ans += [b[-1]]
			b.pop()
	print(*ans)


from sys import *
from math import *
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	b = list(map(int, stdin.readline().split()))
	c = []
	for i in range(n):
		if b[i] == 0:
			c.append(a[i])
			a[i] = 10 ** 5 + 1
	c.sort()
	c = c[::-1]
	j = 0
	for i in range(n):
		if a[i] == 10 ** 5 + 1:
			a[i] = c[j]
			j += 1
	print(*a)


import collections

def run():
	n = int(input())
	nums = list(map(int, input().split()))
	locks = list(map(int, input().split()))
	res = []
	total = sum(nums)
	if total < 0:
		print(' '.join((str(i) for i in nums)))
		return
	for (i, j) in zip(nums, locks):
		if j == 0:
			res.append(i)
	res = sorted(res)
	res = res[::-1]
	ret = []
	for i in range(n - 1, -1, -1):
		if locks[i] == 1:
			ret.append(nums[i])
		else:
			ret.append(res.pop())
	print(' '.join((str(i) for i in ret[::-1])))
T = 1
if T:
	for i in range(int(input())):
		run()
else:
	run()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	b = []
	for i in range(n):
		if l[i] == 0:
			b.append(a[i])
	c = b[:]
	b.sort()
	for i in range(n - 1, -1, -1):
		if l[i] == 0:
			a[i] = b.pop()
	k = -1
	sm = 0
	for i in range(n):
		sm += a[i]
		if sm < 0:
			k = i
	d = a[:]
	c.sort(reverse=True)
	for i in range(n - 1, -1, -1):
		if l[i] == 0:
			d[i] = c.pop()
	f = -1
	sm = 0
	for i in range(n):
		sm += d[i]
		if sm < 0:
			f = i
	if k <= f:
		print(*a)
	else:
		print(*d)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	lock = list(map(int, input().split()))
	b = []
	for i in range(n):
		if lock[i] == 0:
			b.append(a[i])
	b.sort()
	b = b[::-1]
	ind = 0
	for i in range(n):
		if lock[i] == 0:
			a[i] = b[ind]
			ind += 1
	a = [str(i) for i in a]
	print(' '.join(a))


gans = []
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	r = list(map(int, input().split()))
	u = []
	bl = []
	for i in range(n):
		if r[i] == 0:
			u.append(a[i])
		else:
			bl.append(a[i])
	u.sort(reverse=1)
	ans = []
	ind1 = ind2 = 0
	for i in range(n):
		if r[i] == 1:
			ans.append(bl[ind2])
			ind2 += 1
		else:
			ans.append(u[ind1])
			ind1 += 1
	gans.append(' '.join(map(str, ans)))
print('\n'.join(gans))


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	lock = list(input().split())
	asd_list = []
	for (i, e) in enumerate(a):
		if lock[i] == '0':
			asd_list.append(e)
	asd_list.sort(reverse=True)
	for i in range(n):
		if lock[i] == '0':
			a[i] = asd_list.pop(0)
	print(*a)


import bisect
import heapq
T = int(input())
for _ in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	L = list(map(int, input().split()))
	U = [A[i] for i in range(N) if L[i] == 0]
	U = sorted(U, reverse=True)
	index = 0
	for i in range(N):
		if L[i]:
			print(A[i], end=' ')
		else:
			print(U[index], end=' ')
			index += 1
	print()


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	l = list(map(int, input().split()))
	b = []
	for i in range(n):
		if l[i] == 0:
			b.append(arr[i])
	if len(b):
		b.sort()
	ans = []
	for i in range(n):
		if l[i] == 1:
			ans.append(arr[i])
		else:
			ans.append(b.pop())
	print(*ans)


t = int(input())
for _ in range(t):
	n = int(input())
	changing_list = list(map(int, input().split()))
	locking_list = list(map(int, input().split()))
	sort_list = []
	for i in range(n):
		if locking_list[i] == 0:
			sort_list.append(changing_list[i])
			changing_list[i] = '_'
	sort_list.sort()
	for j in range(n):
		if changing_list[j] == '_':
			changing_list[j] = sort_list[-1]
			sort_list.pop()
	print(*changing_list)


t = int(input())
p = []
n = []
e = []
for i in range(t):
	d = int(input())
	r = list(map(int, input().split()))
	y = list(map(int, input().split()))
	p.append(y)
	n.append(d)
	e.append(r)

def tully(n, p, e):
	mt = []
	for i in range(n):
		if p[i] == 0:
			mt.append(e[i])
	mt.sort()
	j = len(mt) - 1
	for i in range(n):
		if p[i] == 0:
			e[i] = mt[j]
			j -= 1
	return e
for i in range(t):
	g = tully(n[i], p[i], e[i])
	for x in range(n[i]):
		if x != n[i] - 1:
			print(e[i][x], end=' ')
		else:
			print(e[i][x])


import sys

def rl(proc=None):
	if proc is not None:
		return proc(sys.stdin.readline())
	else:
		return sys.stdin.readline().rstrip()

def srl(proc=None):
	if proc is not None:
		return list(map(proc, rl().split()))
	else:
		return rl().split()

def solve(A, B):
	U = [a for (a, b) in zip(A, B) if not b]
	U.sort(reverse=True)
	r = []
	u = iter(U)
	for (a, b) in zip(A, B):
		if b:
			r.append(a)
		else:
			r.append(next(u))
	return r

def main():
	T = rl(int)
	for t in range(1, T + 1):
		rl()
		A = srl(int)
		B = srl(int)
		print(' '.join(map(str, solve(A, B))))
main()


from itertools import compress

def solveB(xs, fixed):
	ordered = sorted(list(compress(xs, (not x for x in fixed))))
	fixed_elems = list(reversed(list(compress(xs, fixed))))
	result = []
	for x in fixed:
		if x:
			result.append(fixed_elems.pop())
		else:
			result.append(ordered.pop())
	return result
t = int(input())
for _ in range(t):
	input()
	xs = list(map(int, input().split()))
	fs = list(map(int, input().split()))
	print(*solveB(xs, fs))


from sys import stdin

class Input:

	def __init__(self):
		self.lines = stdin.read().splitlines()
		self.idx = 0

	def line(self):
		try:
			return self.lines[self.idx]
		finally:
			self.idx += 1

	def array(self, sep=' ', cast=int):
		return list(map(cast, self.line().split(sep=sep)))

	def known_tests(self):
		(num_of_cases,) = self.array()
		for case in range(num_of_cases):
			yield self

	def unknown_tests(self):
		while self.idx < len(self.lines):
			yield self

class Problem:

	def solve(self, n, a, l):
		b = []
		for i in range(n):
			if l[i]:
				pass
			else:
				b.append(a[i])
		b.sort(reverse=True)
		i = 0
		for x in b:
			while l[i]:
				i += 1
			a[i] = x
			i += 1
		print(*a)
p = Problem()
for tc in Input().known_tests():
	p.solve(*tc.array(), tc.array(), tc.array())


from sys import stdin, stdout
for _ in ' ' * int(stdin.readline()):
	a = int(stdin.readline())
	b = list(map(int, stdin.readline().split()))
	c = list(map(int, stdin.readline().split()))
	n = []
	p = []
	ans = [0] * a
	for i in range(a):
		if c[i]:
			ans[i] = b[i]
		elif b[i] >= 0:
			p.append(b[i])
		else:
			n.append(b[i])
	n.sort()
	p.sort()
	for i in range(a - 1, -1, -1):
		if c[i] == 0:
			z = sum(ans[:i]) + sum(n) + sum(p)
			if z >= 0:
				if n:
					ans[i] = n.pop(0)
				else:
					ans[i] = p.pop(0)
			elif n:
				ans[i] = n.pop()
			else:
				ans[i] = p.pop()
	stdout.write(' '.join(map(str, ans)) + '\n')


from heapq import heappop, heappush
t = int(input())
for _ in range(t):
	N = int(input())
	a = list(map(int, input().split()))
	SUMA = sum(a)
	l = list(map(int, input().split()))
	unlock = []
	for i in range(N):
		if l[i] == 0:
			heappush(unlock, a[i])
			a[i] = None
	for i in range(N):
		if a[N - 1 - i] is None:
			a[N - 1 - i] = heappop(unlock)
	print(*a)


mod = 10 ** 9 + 7

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	v = []
	for i in range(n):
		if l[i] == 0:
			v.append(a[i])
	v.sort(reverse=True)
	id = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = v[id]
			id += 1
	id = -1
	s = 0
	print(*a)
t = int(input())
while t > 0:
	solve()
	t -= 1


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	sign = list(map(int, input().split()))
	m = []
	for i in range(n):
		if sign[i] == 0:
			m.append(arr[i])
	m.sort(reverse=True)
	ans = 10 ** 10
	c = 0
	prev = 0
	ll = 10 ** 10
	for i in range(n):
		if sign[i] == 0:
			arr[i] = m[c]
			c += 1
	print(*arr)


from sys import stdin
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	lock = list(map(int, stdin.readline().split()))
	z = []
	for i in range(n):
		if lock[i] == 0:
			z.append(a[i])
	z.sort(reverse=True)
	j = 0
	for i in range(n):
		if lock[i] == 0:
			a[i] = z[j]
			j += 1
	print(*a)


from math import inf, ceil
from heapq import *
from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	brr = list(map(int, input().split()))
	crr = [arr[i] for i in range(n) if not brr[i]]
	crr.sort(reverse=True)
	ind = 0
	for i in range(n):
		if not brr[i]:
			arr[i] = crr[ind]
			ind += 1
	print(*arr)


from collections import Counter
from collections import deque
from sys import stdin
from bisect import *
from heapq import *
import math
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
mod = int(1000000000.0) + 7
inf = float('inf')
(t,) = gil()

def getK(a):
	v = a[:]
	for i in range(1, n):
		v[i] += v[i - 1]
	for i in reversed(range(n)):
		if v[i] < 0:
			return i + 1
	return 0
for _ in range(t):
	(n,) = gil()
	a = gil()
	l = gil()
	v = []
	for i in range(n):
		if l[i] == 0:
			v.append(a[i])
	v.sort()
	j = 0
	a1 = []
	a2 = []
	for i in range(n):
		a1.append(a[i] if l[i] else v[j])
		if l[i] == 0:
			j += 1
	j = len(v) - 1
	for i in range(n):
		a2.append(a[i] if l[i] else v[j])
		if l[i] == 0:
			j -= 1
	if getK(a1) <= getK(a2):
		print(*a1)
	else:
		print(*a2)


for i in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	L = list(map(int, input().split()))
	B = [-1000000000 if L[i] == 1 else A[i] for i in range(n)]
	B.sort()
	B = B[::-1]
	k = 0
	for i in range(n):
		if L[i] == 0:
			A[i] = B[k]
			k += 1
	for i in A:
		print(i, end=' ')
	print()


T = int(input())
while T > 0:
	n = int(input())
	arr = list(map(int, input().split()))
	lock = list(map(int, input().split()))
	total = sum(arr)
	if total < 0:
		print(*arr)
	else:
		unlock = []
		for i in range(n):
			if lock[i] == 0:
				unlock.append(arr[i])
		unlock.sort()
		for i in range(n - 1, -1, -1):
			if lock[i] == 1:
				total -= arr[i]
			else:
				arr[i] = unlock.pop(0)
				total -= arr[i]
		print(*arr)
	T -= 1


for _ in range(int(input())):
	N = int(input())
	List = [int(x) for x in input().split()]
	Lock = [int(x) for x in input().split()]
	X = []
	for i in range(N):
		if not Lock[i]:
			X.append(List[i])
	X.sort(reverse=True)
	j = 0
	for i in range(N):
		if not Lock[i]:
			List[i] = X[j]
			j += 1
	print(*List)


def testcase():
	n = int(input())
	arr = list(map(int, input().split()))
	loc = list(map(int, input().split()))
	unlocked = []
	for i in range(n):
		if loc[i] == 0:
			unlocked.append(arr[i])
	ui = 0
	unlocked.sort()
	new_arr = []
	for i in range(n):
		if loc[i] == 1:
			new_arr.append(arr[i])
		else:
			new_arr.append(unlocked[ui])
			ui += 1
	prefix = [new_arr[0]]
	for i in range(1, n):
		prefix.append(prefix[i - 1] + new_arr[i])
	ans1 = 0
	ans1_arr = list(new_arr)
	for i in range(n):
		if prefix[i] < 0:
			ans1 = i + 1
	ui = len(unlocked) - 1
	new_arr = []
	for i in range(n):
		if loc[i] == 1:
			new_arr.append(arr[i])
		else:
			new_arr.append(unlocked[ui])
			ui -= 1
	prefix = [new_arr[0]]
	for i in range(1, n):
		prefix.append(prefix[i - 1] + new_arr[i])
	ans2 = 0
	for i in range(n):
		if prefix[i] < 0:
			ans2 = i + 1
	if ans1 <= ans2:
		for i in ans1_arr:
			print(i, end=' ')
		print()
	else:
		for i in new_arr:
			print(i, end=' ')
		print()
t = int(input())
for _ in range(t):
	testcase()


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	arr_l = list(map(int, input().split()))
	arr_free = [arr[j] for j in range(n) if arr_l[j] == 0]
	arr_free_s = sorted(arr_free, reverse=True)
	ans = []
	cnt = 0
	for j in range(n):
		if arr_l[j] == 1:
			ans.append(arr[j])
		else:
			ans.append(arr_free_s[cnt])
			cnt += 1
	for j in range(n):
		print(ans[j], end=' ')
	print()


import sys
input = sys.stdin.readline

def solve():
	n = int(input())
	al = list(map(int, input().split()))
	l = list(map(int, input().split()))
	get = []
	for i in range(n):
		if l[i] == 1:
			continue
		get.append(al[i])
	get.sort()
	for i in range(n):
		if l[i] != 1:
			al[i] = get.pop()
	print(' '.join((str(x) for x in al)))

def main():
	t = int(input())
	for _ in range(t):
		solve()
main()


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	l = [int(x) for x in input().split()]
	ans = []
	c = 0
	for z in range(n):
		if not l[z]:
			ans.append(a[z])
			c += 1
	ans.sort()
	fa = []
	for z in range(n):
		if l[z]:
			fa.append(a[z])
		else:
			fa.append(ans[c - 1])
			c -= 1
	print(*fa)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	c = []
	for (x, y) in zip(a, b):
		if y == 1:
			continue
		c.append(x)
	c.sort(reverse=True)
	i = 0
	for x in range(n):
		if b[x] == 0:
			a[x] = c[i]
			i += 1
	for x in a:
		print(x, end=' ')
	print()


import sys as _sys

def main():
	t = int(input())
	for i_t in range(t):
		(n,) = _read_ints()
		a = tuple(_read_ints())
		l = tuple(_read_ints())
		result = find_min_k(sequence=a, is_index_locked=l)
		print(*result)

def _read_line():
	result = _sys.stdin.readline()
	assert result[-1] == '\n'
	return result[:-1]

def _read_ints():
	return map(int, _read_line().split(' '))

def find_min_k(sequence, is_index_locked):
	assert len(sequence) == len(is_index_locked)
	unlocked_subsequence = [x for (i, x) in enumerate(sequence) if not is_index_locked[i]]
	unlocked_subsequence.sort()
	sequence = list(sequence)
	for i in range(len(sequence)):
		if not is_index_locked[i]:
			sequence[i] = unlocked_subsequence.pop()
	return sequence
main()


def ans_if_list(ans):
	if isinstance(ans, str):
		return ans
	elif isinstance(ans, int):
		return ans
	else:
		return ' '.join((str(ans[i]) for i in range(len(ans))))

def input_list(typ='int'):
	if typ == 'int':
		return list(map(int, input().split()))
	elif typ == 'str':
		return list(input())

def main():
	t = int(input())
	for q in range(t):
		l = int(input())
		arr = input_list()
		locked = input_list()
		total_sum = sum(arr)
		if sum(locked) == l:
			ans = arr
		else:
			ans = list()
			unlocked_pos = list()
			unlocked_neg = list()
			for i in range(l):
				if locked[i] == 1:
					pass
				elif arr[i] > 0:
					unlocked_pos.append(arr[i])
				else:
					unlocked_neg.append(arr[i])
			unlocked_pos = sorted(unlocked_pos)
			unlocked_neg = sorted(unlocked_neg)
			unlocked = unlocked_neg + unlocked_pos
			ind = -1
			if total_sum < 0:
				ans = arr
			elif total_sum == 0:
				for i in range(l):
					if locked[i] == 1:
						ans.append(arr[i])
					else:
						ans.append(unlocked[ind])
						ind += -1
			else:
				for i in range(l):
					if locked[i] == 1:
						ans.append(arr[i])
					else:
						ans.append(unlocked[ind])
						ind += -1
		print(ans_if_list(ans))
main()


import sys
pin = sys.stdin.readline
for T in range(int(pin())):
	N = int(pin())
	A = [*map(int, pin().split())]
	L = [*map(int, pin().split())]
	R = [0 for n in range(N)]
	S = []
	for n in range(N):
		(a, l) = (A[n], L[n])
		if l:
			R[n] = a
		else:
			S.append(a)
	S.sort(reverse=True)
	i = 0
	for n in range(N):
		if L[n]:
			continue
		else:
			R[n] = S[i]
			i += 1
	for r in R:
		print(r, end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	l = [int(i) for i in input().split()]
	b = []
	for i in range(n):
		if l[i] == 0:
			b.append(a[i])
	b.sort(reverse=True)
	ind = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = b[ind]
			ind += 1
	print(*a)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	brr = list(map(int, input().split()))
	a = [arr[i] for i in range(n) if brr[i] == 0]
	a.sort(reverse=True)
	ind = 0
	for i in range(n):
		if brr[i] == 0:
			arr[i] = a[ind]
			ind += 1
	print(*arr)


import os
import sys
from io import BytesIO, IOBase

def solution(arr, status, n):
	unlock = iter(sorted([arr[i] for i in range(n) if status[i] == 0], reverse=True))
	res = [next(unlock) if status[i] == 0 else arr[i] for i in range(n)]
	write(*res)

def main():
	for _ in range(r_int()):
		n = r_int()
		arr = r_array()
		status = r_array()
		solution(arr, status, n)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')

def write(*args, end='\n'):
	for x in args[:-1]:
		sys.stdout.write(str(x) + ' ')
	sys.stdout.write(str(args[-1]))
	sys.stdout.write(end)

def r_array():
	return [int(x) for x in input().split()]

def r_int():
	return int(input())
main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	dic = {}
	arr = []
	for i in range(len(l)):
		if l[i] == 1:
			dic[i] = 1
			continue
		arr.append(a[i])
	arr.sort(reverse=True)
	ans = []
	index = 0
	for i in range(len(a)):
		if i in dic:
			ans.append(a[i])
		else:
			ans.append(arr[index])
			index += 1
	print(*ans)


def f(c, d):
	l = 0
	e = []
	while l < len(c) and len(e) == c[l][1]:
		e.append(c[l][0])
		l += 1
	for i in range(len(d)):
		while l < len(c) and len(e) == c[l][1]:
			e.append(c[l][0])
			l += 1
		e.append(d[i])
	while l < len(c) and len(e) == c[l][1]:
		e.append(c[l][0])
		l += 1
	p = ps(e)
	ans = 0
	for i in range(len(e)):
		if p[i] < 0:
			ans = i
	return ans

def ps(a):
	p = []
	s = 0
	for i in range(len(a)):
		s += a[i]
		p.append(s)
	return p
t = int(input())
for t1 in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]
	c = [[a[i], i] for i in range(n) if b[i] == 1]
	d = [a[i] for i in range(n) if b[i] == 0]
	d.sort()
	a1 = f(c, d)
	d.sort(reverse=True)
	a2 = f(c, d)
	if a1 < a2:
		d.sort()
		l = 0
		e = []
		while l < len(c) and len(e) == c[l][1]:
			e.append(c[l][0])
			l += 1
		for i in range(len(d)):
			while l < len(c) and len(e) == c[l][1]:
				e.append(c[l][0])
				l += 1
			e.append(d[i])
		while l < len(c) and len(e) == c[l][1]:
			e.append(c[l][0])
			l += 1
		print(' '.join([str(i) for i in e]))
	else:
		l = 0
		e = []
		while l < len(c) and len(e) == c[l][1]:
			e.append(c[l][0])
			l += 1
		for i in range(len(d)):
			while l < len(c) and len(e) == c[l][1]:
				e.append(c[l][0])
				l += 1
			e.append(d[i])
		while l < len(c) and len(e) == c[l][1]:
			e.append(c[l][0])
			l += 1
		print(' '.join([str(i) for i in e]))


for t in range(int(input())):
	n = int(input())
	A = [int(i) for i in input().split()]
	B = [int(i) for i in input().split()]
	l = []
	for i in range(n):
		if B[i] == 0:
			l.append(A[i])
	l.sort(reverse=True)
	j = 0
	for i in range(n):
		if B[i] == 0:
			A[i] = l[j]
			j += 1
	print(*A)


import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(t,) = I()
for i in range(t):
	(n,) = I()
	a = I()
	l = I()
	ar = [a[i] for i in range(n) if l[i] == 0]
	ar.sort(reverse=True)
	x = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = ar[x]
			x += 1
	print(*a)


t = int(input())
for test in range(t):
	n = int(input())
	l = [int(i) for i in input().split(' ')]
	lock = [int(i) for i in input().split(' ')]
	l1 = []
	for j in range(n):
		if lock[j] == 0:
			l1.append(l[j])
	l2 = sorted(l1, reverse=True)
	k = 0
	res = ''
	for j in range(n):
		if lock[j] == 0:
			res = res + str(l2[k]) + ' '
			k += 1
		else:
			res = res + str(l[j]) + ' '
	print(res)


T = int(input())
for _ in range(T):
	sz = int(input())
	a = list(map(int, input().split()))
	bl = list(map(int, input().split()))
	unblocked = []
	for k in range(sz):
		if bl[k] == 0:
			unblocked.append(a[k])
	unblocked.sort(reverse=True)
	idx = 0
	for k in range(sz):
		if bl[k] == 0:
			a[k] = unblocked[idx]
			idx += 1
	print(*a)


for test in range(int(input())):
	pocet = int(input())
	a = list(map(int, input().split(' ')))
	L = list(map(int, input().split(' ')))
	unlocked = []
	for i in range(len(a)):
		if L[i] == 0:
			unlocked.append(a[i])
	unlocked.sort(reverse=True)
	k = 0
	for i in range(len(a)):
		if L[i] == 0:
			a[i] = unlocked[k]
			k += 1
		print(a[i], end=' ')
	print()


def ceil_div(dividend, divisor):
	return (dividend + divisor - 1) // divisor
t = int(input())
for _ in range(t):
	input()
	a = list(map(int, input().split()))
	locks = list(map(int, input().split()))
	free_numbers = [x for (x, locked) in zip(a, locks) if not locked]
	free_numbers.sort(reverse=True)
	result = a
	free_indices = [i for (i, locked) in enumerate(locks) if not locked]
	for (i, x) in zip(free_indices, free_numbers):
		result[i] = x
	print(*result)


for _ in range(int(input())):
	n = int(input())
	pre = list(map(int, input().split()))
	locked = list(map(int, input().split()))
	l = []
	l1 = []
	for i in range(n):
		if locked[i] == 0:
			l.append(i)
	for i in l:
		l1.append(pre[i])
	l1.sort(reverse=True)
	for i in range(len(l)):
		pre[l[i]] = l1[i]
	for i in pre:
		print(i, end=' ')
	print()


import sys
sys.setrecursionlimit(10000)
from collections import defaultdict
from heapq import heappush
from heapq import heappop
from math import ceil

def solve():
	n = int(input())
	arr = list(map(int, input().split()))
	pos = list(map(int, input().split()))
	heap = []
	if sum(arr) < 0:
		for i in range(n):
			if pos[i] == 0:
				heappush(heap, arr[i])
		for i in range(n):
			if pos[i] == 0:
				arr[i] = heappop(heap)
	else:
		for i in range(n):
			if pos[i] == 0:
				heappush(heap, -arr[i])
		for i in range(n):
			if pos[i] == 0:
				arr[i] = -heappop(heap)
	ans = ''
	for i in arr:
		ans += str(i)
		ans += ' '
	print(ans)
for i in range(int(input())):
	solve()


import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]
for _ in range(II()):
	n = II()
	aa = LI()
	bb = LI()
	(pos, neg) = ([], [])
	for (a, b) in zip(aa, bb):
		if b == 0:
			if a >= 0:
				pos.append(a)
			else:
				neg.append(a)
	pos.sort(reverse=True)
	neg.sort(reverse=True)
	ans1 = [0] * n
	ff = pos + neg
	fi = 0
	for i in range(n):
		if bb[i] == 0:
			ans1[i] = ff[fi]
			fi += 1
		else:
			ans1[i] = aa[i]
	cs1 = [0] * n
	for i in range(n):
		cs1[i] = cs1[i - 1] + ans1[i]
	neg.reverse()
	ff = neg + pos
	ans2 = [0] * n
	fi = 0
	for i in range(n):
		if bb[i] == 0:
			ans2[i] = ff[fi]
			fi += 1
		else:
			ans2[i] = aa[i]
	cs2 = [0] * n
	for i in range(n):
		cs2[i] = cs2[i - 1] + ans2[i]
	i1 = i2 = -1
	for i in range(n - 1, -1, -1):
		if cs1[i] < 0:
			i1 = i
			break
	for i in range(n - 1, -1, -1):
		if cs2[i] < 0:
			i2 = i
			break
	if i1 < i2:
		print(*ans1)
	else:
		print(*ans2)


def answer(n, A, B):
	if sum(A) < 0:
		return A
	l = []
	for i in range(n):
		if B[i] == 0:
			l.append(A[i])
	l.sort()
	j = 0
	for i in range(n - 1, -1, -1):
		if B[i] == 0:
			A[i] = l[j]
			j += 1
	return A
t = int(input())
for i in range(t):
	n = int(input())
	arr1 = list(map(int, input().split()))
	arr2 = list(map(int, input().split()))
	print(*answer(n, arr1, arr2))


def ansf(f, target):
	arr1 = []
	arr2 = []
	for i in range(len(f)):
		a = target - f[i]
		arr1.append(a)
		arr2.append(f[i])
	b = arr1.index(min(arr1))
	return arr2[b]
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	lock = list(map(int, input().split()))
	f = []
	for i in range(len(arr)):
		if lock[i] == 0:
			f.append(arr[i])
	for i in range(len(arr)):
		if lock[i] == 0:
			p = ansf(f, arr[i])
			f.remove(p)
			arr[i] = p
	print(*arr, sep=' ')


a = int(input())
for x in range(a):
	d = int(input())
	b = list(map(int, input().split()))
	c = list(map(int, input().split()))
	j = []
	for y in range(d):
		if c[y] == 0:
			j.append(b[y])
	if j != []:
		j.sort(reverse=True)
	h = 0
	for y in range(d):
		if c[y] == 0:
			b[y] = j[h]
			h += 1
	print(*b)


for _ in range(int(input())):
	n = int(input())
	num = list(map(int, input().split()))
	lock_list = list(map(int, input().split()))
	current_sum = 0
	candidate = []
	for i in range(n):
		if lock_list[i] == 1:
			current_sum += num[i]
		else:
			candidate.append(num[i])
	if len(lock_list) == sum(lock_list):
		print(' '.join(map(str, num)))
	else:
		candidate.sort()
		for i in range(n):
			if lock_list[i] == 1:
				continue
			else:
				num[i] = candidate[-1]
				candidate.pop()
		print(' '.join(map(str, num)))


from math import ceil
for _ in range(int(input())):
	n = int(input())
	a = [*map(int, input().split())]
	b = [*map(int, input().split())]
	c = [a[i] for i in range(n) if b[i] != 1]
	c.sort(reverse=True)
	d = []
	ind = 0
	for i in range(n):
		if b[i] == 1:
			d.append(a[i])
		else:
			d.append(c[ind])
			ind += 1
	print(*d)


from sys import stdin, stdout
from math import gcd, sqrt, inf
from collections import deque
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 1 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
M = inf
for _ in range(I()):
	n = I()
	(*a,) = R()
	(*l,) = R()
	p = sorted([a[i] for i in range(n) if not l[i]])
	sm = sum(a)
	for i in range(n - 1, -1, -1):
		if sm < 0:
			break
		if not l[i]:
			a[i] = p.pop(0)
			l[i] = 1
		sm -= a[i]
	for i in range(n):
		if not l[i]:
			a[i] = p.pop(0)
	print(*a)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	state = list(map(int, input().split()))
	dummy_array = []
	for i in range(n):
		if state[i] == 0:
			dummy_array.append(arr[i])
	dummy_array.sort(reverse=True)
	for j in range(n):
		if state[j] == 0:
			a = dummy_array.pop(0)
			arr[j] = a
	for v in arr:
		print(v, end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	status = list(map(int, input().split()))
	res = [lis[i] for i in range(n) if status[i] == 0]
	res = sorted(res, reverse=True)
	k = 0
	for i in range(n):
		if status[i] == 0:
			lis[i] = res[k]
			k += 1
	print(*lis)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	newl = []
	for i in range(n):
		if l[i] == 0:
			newl.append(a[i])
	newl.sort(reverse=True)
	z = 0
	for i in range(n):
		if l[i] == 0:
			print(newl[z], end=' ')
			z += 1
		else:
			print(a[i], end=' ')
	print()


import os
import sys
from io import BytesIO, IOBase
import math
import itertools
import bisect
import heapq

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def binary(n):
	return bin(n).replace('0b', '')

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(i)
			n = n / i
	if n > 2:
		l.append(int(n))
	return l

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True

def maxPrimeFactors(n):
	maxPrime = -1
	while n % 2 == 0:
		maxPrime = 2
		n >>= 1
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			maxPrime = i
			n = n / i
	if n > 2:
		maxPrime = n
	return int(maxPrime)

def countcon(s, i):
	c = 0
	ch = s[i]
	for i in range(i, len(s)):
		if s[i] == ch:
			c += 1
		else:
			break
	return c

def lis(arr):
	n = len(arr)
	lis = [1] * n
	for i in range(1, n):
		for j in range(0, i):
			if arr[i] > arr[j] and lis[i] < lis[j] + 1:
				lis[i] = lis[j] + 1
	maximum = 0
	for i in range(n):
		maximum = max(maximum, lis[i])
	return maximum

def isSubSequence(str1, str2):
	m = len(str1)
	n = len(str2)
	j = 0
	i = 0
	while j < m and i < n:
		if str1[j] == str2[i]:
			j = j + 1
		i = i + 1
	return j == m

def maxfac(n):
	root = int(n ** 0.5)
	for i in range(2, root + 1):
		if n % i == 0:
			return n // i
	return n

def p2(n):
	c = 0
	while n % 2 == 0:
		n //= 2
		c += 1
	return c

def seive(n):
	primes = [True] * (n + 1)
	primes[1] = primes[0] = False
	for i in range(2, n + 1):
		if primes[i]:
			for j in range(i + i, n + 1, i):
				primes[j] = False
	p = []
	for i in range(0, n + 1):
		if primes[i]:
			p.append(i)
	return p

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def denofactinverse(n, m):
	fac = 1
	for i in range(1, n + 1):
		fac = fac * i % m
	return pow(fac, m - 2, m)

def numofact(n, m):
	fac = 1
	for i in range(1, n + 1):
		fac = fac * i % m
	return fac
for xyz in range(0, int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	lo = list(map(int, input().split()))
	repos = []
	for i in range(0, n):
		if lo[i] == 0:
			repos.append(l[i])
	repos.sort()
	for i in range(0, n):
		if lo[i] == 0:
			l[i] = repos.pop()
	print(*l)


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	lo = list(map(int, input().split()))
	st = []
	idx = []
	for i in range(n):
		if lo[i] == 0:
			st.append(arr[i])
			idx.append(i)
	st.sort(reverse=True)
	for i in range(len(st)):
		arr[idx[i]] = st[i]
	print(*arr)


import math
t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	s = sum(a)
	if s < 0:
		for x in a:
			print(x, end=' ')
		print()
	else:
		c = []
		for i in range(n):
			if b[i] == 0:
				c.append(a[i])
		(i, j) = (0, 0)
		c.sort(key=lambda x: -x)
		while i < n:
			if b[i] == 0:
				a[i] = c[j]
				j += 1
			i += 1
		for x in a:
			print(x, end=' ')
		print()
	t -= 1


def work():
	n = int(input())
	a = [int(i) for i in input().split()]
	l = [int(i) for i in input().split()]
	b = [a[i] for i in range(n) if l[i] == 0]
	b.sort(reverse=True)
	j = 0
	for i in range(n):
		if l[i] != 1:
			a[i] = b[j]
			j += 1
	print(' '.join([str(i) for i in a]))

def main():
	t = int(input())
	while t > 0:
		work()
		t -= 1
main()


for _ in range(int(input())):
	n = int(input())
	l1 = list(map(int, input().split()))
	l2 = list(map(int, input().split()))
	unlock = []
	for i in range(len(l1)):
		if l2[i] == 0:
			unlock.append(l1[i])
			l1[i] = 'a'
	unlock.sort()
	for i in range(n):
		if l1[i] == 'a':
			l1[i] = unlock[-1]
			unlock = unlock[:-1]
	ans = ''
	for i in range(n):
		ans += str(l1[i]) + ' '
	print(ans)


t = int(input())
w = []
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	st = list(map(int, input().split()))
	kk = []
	for i in range(n):
		if st[i] == 0:
			kk.append(arr[i])
	kk = sorted(kk)
	kk = kk[::-1]
	c = 0
	for i in range(n):
		if st[i] == 0:
			arr[i] = kk[c]
			c = c + 1
	w.append(arr)
for i in w:
	print(*i)


import sys
input = sys.stdin.buffer.readline

def solution():
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	arr = []
	arr2 = []
	for i in range(n):
		arr.append([a[i], b[i]])
		if b[i] == 0:
			arr2.append([a[i], b[i]])
	arr2.sort(reverse=True)
	i = 0
	j = 0
	while i < len(arr2):
		while j < len(arr) and i < len(arr2):
			if arr[j][1] == 0:
				arr[j][0] = arr2[i][0]
				i += 1
			j += 1
	for i in range(n):
		print(arr[i][0], end=' ')
	print()
t = int(input())
for _ in range(t):
	solution()


st = input()
t = int(st)
a = []
l = []
urut = []
for i in range(t):
	st = input()
	n = int(st)
	st = input()
	getting = ''
	for j in st:
		if j == ' ':
			a.append(int(getting))
			getting = ''
		else:
			getting += j
	a.append(int(getting))
	st = input()
	getting = ''
	for j in st:
		if j == ' ':
			l.append(int(getting))
			getting = ''
		else:
			getting += j
	l.append(int(getting))
	for j in range(n):
		if l[j] == 0:
			urut.append(a[j])
	urut.sort(reverse=True)
	nomor = 0
	for j in range(n):
		if l[j] == 0:
			print(f'{urut[nomor]} ', end='')
			nomor += 1
		else:
			print(f'{a[j]} ', end='')
	print('')
	a.clear()
	l.clear()
	urut.clear()


t = int(input())
for _ in range(t):
	n = int(input())
	l1 = [int(x) for x in input().split()]
	l2 = [int(x) for x in input().split()]
	l3 = sorted([l1[i] for i in range(n) if not l2[i]], reverse=True)
	j = 0
	l4 = []
	q = '\n'
	for i in range(n):
		if not l2[i]:
			l4.append(l3[j])
			j += 1
		else:
			l4.append(l1[i])
	print(*l4)


for x in range(int(input())):
	i_liczb = int(input())
	liczby = list(map(int, input().split()))
	otwarte = list(map(int, input().split()))
	for x in range(i_liczb):
		for y in range(x + 1, i_liczb):
			if liczby[y] > liczby[x] and (not otwarte[x]) and (not otwarte[y]):
				(liczby[x], liczby[y]) = (liczby[y], liczby[x])
	print(*liczby)


l = lambda : list(map(int, input().split()))
for _ in ' ' * int(input()):
	(n, a, b) = (int(input()), l(), l())
	x = []
	for i in range(n):
		if b[i] == 0:
			x += [a[i]]
	x.sort()
	i = 0
	for row in x[::-1]:
		for j in range(i, n):
			if b[j] == 0:
				a[j] = row
				i = j + 1
				break
		else:
			break
	print(*a)


def solve(n, seq, locked):
	index = n - 1
	movable = list(zip(seq, locked))
	refer = [i for (i, j) in movable if j == '0']
	refer.sort()
	referIndex = 0
	while index >= 0:
		if locked[index] == '0':
			seq[index] = refer[referIndex]
			referIndex += 1
		index -= 1
	for x in seq:
		print(x, end=' ')
	print('')
for y in range(int(input())):
	n = int(input())
	seq = list(map(int, input().split()))
	l = input().split()
	solve(n, seq, l)


import sys
input = sys.stdin.readline
(T,) = map(int, input().split())
for _ in range(T):
	(N,) = map(int, input().split())
	X = list(map(int, input().split()))
	Y = list(map(int, input().split()))
	Z = []
	vs = set()
	for i in range(N):
		if not Y[i]:
			Z.append((i, X[i]))
			vs.add(i)
	Z.sort(key=lambda x: x[1])
	R = [0] * N
	for i in range(N):
		if Y[i]:
			R[i] = X[i]
		else:
			R[i] = Z.pop()[1]
	print(*R)


import sys, bisect, string, math, time, functools, random, fractions
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
from itertools import permutations, combinations, groupby
rep = range
R = range

def Golf():
	(n, *t) = map(int, open(0).read().split())

def I():
	return int(input())

def S_():
	return input()

def IS():
	return input().split()

def LS():
	return [i for i in input().split()]

def MI():
	return map(int, input().split())

def LI():
	return [int(i) for i in input().split()]

def LI_():
	return [int(i) - 1 for i in input().split()]

def NI(n):
	return [int(input()) for i in range(n)]

def NI_(n):
	return [int(input()) - 1 for i in range(n)]

def NLI(n):
	return [[int(i) for i in input().split()] for i in range(n)]

def NLI_(n):
	return [[int(i) - 1 for i in input().split()] for i in range(n)]

def StoLI():
	return [ord(i) - 97 for i in input()]

def ItoS(n):
	return chr(n + 97)

def LtoS(ls):
	return ''.join([chr(i + 97) for i in ls])

def RA():
	return map(int, open(0).read().split())

def RLI(n=8, a=1, b=10):
	return [random.randint(a, b) for i in range(n)]

def RI(a=1, b=10):
	return random.randint(a, b)

def INP():
	N = 10
	A = 10
	n = random.randint(1, N)
	a = [random.randint(1, A) for i in range(n)]
	return (n, a)

def Rtest(T):
	(case, err) = (0, 0)
	for i in range(T):
		inp = INP()
		a1 = naive(*inp)
		a2 = solve(*inp)
		if a1 != a2:
			print((a1, a2), inp)
			err += 1
		case += 1
	print('Tested', case, 'case with', err, 'errors')

def GI(V, E, ls=None, Directed=False, index=1):
	org_inp = []
	g = [[] for i in range(V)]
	FromStdin = True if ls == None else False
	for i in range(E):
		if FromStdin:
			inp = LI()
			org_inp.append(inp)
		else:
			inp = ls[i]
		if len(inp) == 2:
			(a, b) = inp
			c = 1
		else:
			(a, b, c) = inp
		if index == 1:
			a -= 1
			b -= 1
		aa = (a, c)
		bb = (b, c)
		g[a].append(bb)
		if not Directed:
			g[b].append(aa)
	return (g, org_inp)

def GGI(h, w, search=None, replacement_of_found='.', mp_def={'#': 1, '.': 0}, boundary=1):
	mp = [boundary] * (w + 2)
	found = {}
	for i in R(h):
		s = input()
		for char in search:
			if char in s:
				found[char] = (i + 1) * (w + 2) + s.index(char) + 1
				mp_def[char] = mp_def[replacement_of_found]
		mp += [boundary] + [mp_def[j] for j in s] + [boundary]
	mp += [boundary] * (w + 2)
	return (h + 2, w + 2, mp, found)

def TI(n):
	return GI(n, n - 1)

def accum(ls):
	rt = [0]
	for i in ls:
		rt += [rt[-1] + i]
	return rt

def bit_combination(n, base=2):
	rt = []
	for tb in R(base ** n):
		s = [tb // base ** bt % base for bt in R(n)]
		rt += [s]
	return rt

def gcd(x, y):
	if y == 0:
		return x
	if x % y == 0:
		return y
	while x % y != 0:
		(x, y) = (y, x % y)
	return y

def YN(x):
	print(['NO', 'YES'][x])

def Yn(x):
	print(['No', 'Yes'][x])

def show(*inp, end='\n'):
	if show_flg:
		print(*inp, end=end)
mo = 10 ** 9 + 7
inf = float('inf')
FourNb = [(-1, 0), (1, 0), (0, 1), (0, -1)]
EightNb = [(-1, 0), (1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)]
compas = dict(zip('WENS', FourNb))
cursol = dict(zip('LRUD', FourNb))
l_alp = string.ascii_lowercase
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
input = lambda : sys.stdin.readline().rstrip()

class Comb:

	def __init__(self, n, mo=10 ** 9 + 7):
		self.fac = [0] * (n + 1)
		self.inv = [1] * (n + 1)
		self.fac[0] = 1
		self.fact(n)
		for i in range(1, n + 1):
			self.fac[i] = i * self.fac[i - 1] % mo
			self.inv[n] *= i
			self.inv[n] %= mo
		self.inv[n] = pow(self.inv[n], mo - 2, mo)
		for i in range(1, n):
			self.inv[n - i] = self.inv[n - i + 1] * (n - i + 1) % mo
		return

	def fact(self, n):
		return self.fac[n]

	def invf(self, n):
		return self.inv[n]

	def comb(self, x, y):
		if y < 0 or y > x:
			return 0
		return self.fac[x] * self.inv[x - y] * self.inv[y] % mo
show_flg = False
show_flg = True
ans = 0

def solve_A(x, y, k):
	a = (k * y + k - 1 + x - 2) // (x - 1)
	return a + k
for _ in range(I()):
	ans = 0
	n = I()
	a = LI()
	l = LI()
	v = [0] * n
	b = []
	for i in range(n):
		if l[i] == 1:
			v[i] = a[i]
		else:
			b += (a[i],)
	x = sorted(b)
	for i in range(n):
		if l[i] == 0:
			v[i] = x.pop()
	print(*v)


from math import floor, ceil
for j in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	lis = list(map(int, input().split()))
	bal = [arr[i] for i in range(n) if lis[i] != 1]
	su = sum(arr)
	bal.sort()
	for i in range(n):
		if lis[i] == 1:
			pass
		else:
			arr[i] = bal[-1]
			bal.pop()
	print(*arr)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = list(map(int, input().split()))
	if sum(a) >= 0:
		u = []
		for i in range(len(a)):
			if s[i] == 0:
				u.append(a[i])
		u.sort()
		for i in range(len(a)):
			if s[i] == 0:
				a[i] = u.pop()
	print(*a)


from sys import stdin
ip = stdin.readline
for _ in range(int(ip())):
	n = int(ip())
	a = list(map(int, ip().split()))
	l = list(map(int, ip().split()))
	s = []
	for i in range(n):
		if not l[i]:
			s.append(a[i])
	s.sort()
	for i in range(n):
		if not l[i]:
			a[i] = s.pop()
	print(*a)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	(val, pos) = ([], [])
	for i in range(n):
		if l[i] == 0:
			val.append(a[i])
			pos.append(i)
	val.sort(reverse=True)
	for i in range(len(pos)):
		a[pos[i]] = val[i]
	print(*a, sep=' ')


from sys import stdin, stdout

def gmi():
	return map(int, stdin.readline().strip().split())

def gms():
	return map(str, stdin.readline().strip().split())

def gari():
	return list(map(int, stdin.readline().strip().split()))

def gars():
	return list(map(int, stdin.readline().strip().split()))

def gs():
	return stdin.readline().strip()

def gls():
	return list(stdin.readline())

def gi():
	return int(stdin.readline())
from math import ceil, floor
t = gi()
while t:
	t -= 1
	n = gi()
	ar = gari()
	l = gari()
	br = [ar[i] for i in range(n) if l[i] == 0]
	br.sort()
	ans = []
	for i in range(n):
		if l[i]:
			ans.append(ar[i])
		else:
			p = br.pop()
			ans.append(p)
	print(*ans)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	open = []
	for i in range(n):
		if l[i] == 0:
			open.append(a[i])
	open.sort()
	open.reverse()
	ind = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = open[ind]
			ind += 1
	print(*a)


from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf

def sp():
	return map(int, input().split())
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(sp())
	b = list(sp())
	ls = []
	for i in range(n):
		if b[i] == 0:
			ls.append(l[i])
	ls.sort(reverse=True)
	j = 0
	for i in range(n):
		if b[i] == 1:
			print(l[i], end=' ')
		else:
			print(ls[j], end=' ')
			j += 1
	print()


import sys
input = sys.stdin.readline

def solution():
	n = int(input())
	A = list(map(int, input().split()))
	L = list(map(int, input().split()))
	stack = []
	for i in range(n):
		if L[i] == 0:
			stack.append(A[i])
	stack.sort()
	for i in range(n):
		if L[i] == 0:
			A[i] = stack.pop()
	print(*A)
T = int(input())
for _ in range(T):
	solution()


for sfs in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	s = []
	p = 0
	for i in range(n):
		if l[i] == 0:
			p += 1
			s.append(a[i])
	s.sort()
	s.reverse()
	k = 0
	for i in range(n):
		if k < p and l[i] == 0:
			a[i] = s[k]
			k += 1
	print(str(a)[1:-1].replace(',', ''))


t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = [int(x) for x in input().split()]
	l = [int(x) for x in input().split()]
	temp = []
	for (i, j) in zip(arr, l):
		if j == 0:
			temp.append(i)
	temp.sort(reverse=True)
	ans = []
	k = 0
	for (i, j) in zip(arr, l):
		if j == 0:
			ans.append(temp[k])
			k += 1
		else:
			ans.append(i)
	print(*ans)


def ans(a, n, b):
	t = []
	for i in range(n):
		if b[i] == 0:
			t.append(a[i])
	t.sort(reverse=True)
	answer = [0] * n
	j = 0
	for i in range(n):
		if b[i] == 0:
			answer[i] = t[j]
			j += 1
		else:
			answer[i] = a[i]
	s = ''
	for i in answer:
		s += str(i) + ' '
	print(s)
	return
m = int(input())
for i in range(m):
	n = int(input())
	arr1 = input().split()
	arr2 = input().split()
	a = []
	b = []
	for i in arr1:
		a.append(int(i))
	for i in arr2:
		b.append(int(i))
	ans(a, n, b)


l = lambda : list(map(int, input().split()))
for _ in ' ' * int(input()):
	(n, a, b, x) = (int(input()), l(), l(), [])
	for i in range(n):
		if b[i] == 0:
			x += [a[i]]
	x.sort()
	for i in range(n):
		if b[i] == 0:
			a[i] = x.pop()
	print(*a)


import sys, collections, math

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def input():
	return sys.stdin.readline().strip()
mod = 1000000007
for _ in range(int(input())):
	n = int(input())
	arr = get_array()
	x = get_array()
	b = []
	for i in range(n):
		if x[i] == 0:
			b.append(arr[i])
			arr[i] = mod
	b.sort(reverse=True)
	j = 0
	for i in range(n):
		if arr[i] == mod:
			arr[i] = b[j]
			j += 1
	print(*arr)


def negativePrefixes(n, array, lock):
	free = []
	for x in range(n):
		if lock[x] == 0:
			free.append(array[x])
	free.sort(reverse=True)
	i = 0
	for x in range(n):
		if lock[x] == 0:
			array[x] = free[i]
			i += 1
	return array
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	ans = negativePrefixes(n, a, b)
	for x in ans:
		print(x, end=' ')
	print()


y = lambda : [*map(int, input().split())]
for _ in range(int(input())):
	n = int(input())
	a = y()
	l = y()
	b = []
	for i in range(n):
		if l[i] < 1:
			b.append(a[i])
	b.sort()
	for i in range(n):
		if l[i] < 1:
			a[i] = b.pop()
	print(' '.join(map(str, a)))


import sys
input = sys.stdin.readline
cases = int(input())
for case in range(cases):
	n = int(input())
	a = [int(x) for x in input().split()]
	l = [int(x) for x in input().split()]
	pushp = []
	pushm = []
	for i in range(n):
		if a[i] > 0 and l[i] == 0:
			pushp.append(a[i])
		elif a[i] <= 0 and l[i] == 0:
			pushm.append(a[i])
	pushp.sort(reverse=True)
	pushm.sort(reverse=True)
	for i in range(n):
		if l[i] == 0:
			if len(pushp) > 0:
				a[i] = pushp[0]
				pushp.pop(0)
			else:
				a[i] = pushm[0]
				pushm.pop(0)
	print(*a)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	stat = list(map(int, input().split()))
	unsorted = []
	for i in range(n):
		if stat[i] == 1:
			continue
		else:
			unsorted.append(l[i])
	unsorted = sorted(unsorted, reverse=True)
	j = 0
	for i in range(n):
		if stat[i] == 1:
			print(l[i], end=' ')
		else:
			print(unsorted[j], end=' ')
			j += 1
	print(' ')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = []
	for i in range(n):
		if b[i] == 0:
			c.append(a[i])
	c.sort()
	for i in range(n):
		if b[i] == 0:
			a[i] = c.pop()
	print(*a)


for nt in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	b = []
	for i in range(n):
		if not l[i]:
			b.append(a[i])
	b.sort(reverse=True)
	i = 0
	j = 0
	while j < n and i < n:
		while i < n and l[i]:
			i += 1
		if i >= n:
			break
		a[i] = b[j]
		i += 1
		j += 1
	print(*a)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	ans = [None] * n
	for i in range(n):
		if l[i] == 1:
			ans[i] = a[i]
	t = []
	for i in range(n):
		if l[i] == 0:
			t.append(a[i])
	t.sort()
	j = len(t) - 1
	for i in range(n):
		if ans[i] == None:
			ans[i] = t[j]
			j = j - 1 if j > 0 else j
	print(*ans)


for o in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	a = list(map(int, input().split()))
	p = []
	q = []
	for i in range(n):
		if a[i] == 0:
			p.append(s[i])
		else:
			q.append(s[i])
	p.sort(reverse=True)
	i = 0
	j = 0
	w = []
	while i + j != n:
		if a[i + j] == 1:
			w.append(str(q[i]))
			i = i + 1
		else:
			w.append(str(p[j]))
			j = j + 1
	w = ' '.join(w)
	print(w)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	b = []
	for i in range(n):
		if l[i] == 0:
			b.append(a[i])
	b.sort(reverse=True)
	j = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = b[j]
			j += 1
	print(*a)


for _ in range(int(input())):
	n = int(input())
	a = [*map(int, input().split())]
	l = [*map(int, input().split())]
	b = [x if y else None for (x, y) in zip(a, l)]
	s = [x for (x, y) in zip(a, l) if not y]
	s.sort(reverse=True)
	x = sum(a)
	ans = None
	for (i, j) in zip(range(n - 1, -1, -1), b[::-1]):
		if b[i] != None:
			x -= b[i]
			continue
		if x - s[-1] < 0:
			b[i] = s[-1]
			s.pop()
			break
		x -= s[-1]
		b[i] = s[-1]
		s.pop()
	if len(s) > 0:
		b = [x if x != None else s.pop() for x in b]
	print(*b)


import sys

def main():
	tn = int(input())
	for ti in range(tn):
		n = int(input())
		A = [int(a) for a in input().split()]
		L = [int(l) for l in input().split()]
		B = []
		for (a, l) in zip(A, L):
			if l == 1:
				continue
			B.append(a)
		B.sort(reverse=True)
		ans = []
		bi = 0
		for (a, l) in zip(A, L):
			if l == 1:
				ans.append(a)
			else:
				ans.append(B[bi])
				bi += 1
		print(' '.join(map(str, ans)))
main()


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def lowbit(x: int):
	return x & -x

def treearray_update(pos: int, x: int, array: list):
	while pos < len(array):
		array[pos] += x
		pos += lowbit(pos)

def treearray_getsum(pos: int, array: list) -> int:
	ans = 0
	while pos > 0:
		ans += array[pos]
		pos -= lowbit(pos)
	return ans
t = int(input())
for _ in range(t):
	n = int(input())
	li = [int(i) for i in input().split()]
	lo = [int(i) for i in input().split()]
	p = []
	idx = []
	su = 0
	for i in range(n):
		if not lo[i]:
			p.append(li[i])
			idx.append(i)
		su += li[i]
	if su < 0:
		print(*li)
		continue
	pref = []
	su = 0
	first = -1
	p.sort()
	for i in range(n):
		if not lo[i]:
			li[i] = p.pop()
		su += li[i]
		pref.append(su)
	print(*li)


T = int(input())
for t in range(T):
	n = int(input())
	A = list(map(int, input().split()))
	L = list(map(int, input().split()))
	new_arr = []
	for i in range(n):
		if L[i] == 0:
			new_arr.append(A[i])
	new_arr.sort()
	new_arr.reverse()
	j = 0
	for i in range(n):
		if L[i] == 0:
			A[i] = new_arr[j]
			j += 1
	for a in A:
		print(a, end=' ')
	print('')


q = int(input())

def get_k(arr):
	p = [0]
	for i in arr:
		p.append(p[-1] + i)
	j = 0
	for i in range(len(arr)):
		if a[i] < 0:
			j = i
	return j
while q:
	q -= 1
	n = int(input())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	cs = []
	for i in range(len(a)):
		if not b[i]:
			cs.append(a[i])
	cs.sort()
	ncs = cs[:]
	cs.reverse()
	_a = a[:]
	j = 0
	for i in range(len(a)):
		if not b[i]:
			a[i] = cs[j]
			j += 1
	j = 0
	for i in range(len(_a)):
		if not b[i]:
			_a[i] = ncs[j]
			j += 1
	print(*(a if get_k(a) <= get_k(_a) else _a))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	s = sum(a)
	if s < 0:
		print(*a)
	else:
		ps = []
		ng = []
		for (i, x) in enumerate(a):
			if b[i] == 0:
				if x >= 0:
					ps += (x,)
				else:
					ng += (x,)
		c = []
		ps.sort()
		ng.sort()
		i = 0
		while i < n:
			if b[i]:
				c += (a[i],)
			elif len(ps):
				c += (ps.pop(),)
			else:
				c += (ng.pop(),)
			i += 1
		print(*c)


import sys
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2 ** 20)
T = 1
T = int(stdin.readline())
for _ in range(T):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().rstrip().split()))
	l = list(map(int, stdin.readline().rstrip().split()))
	if [1] * n == l:
		print(*a)
		continue
	ai = []
	p = []
	for i in range(n):
		if l[i] == 0:
			ai.append(a[i])
	ai.sort(reverse=True)
	for i in range(n):
		if l[i] == 0:
			a[i] = ai[0]
			ai.pop(0)
	print(*a)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	k = []
	o = []
	for i in range(n):
		if l[i] == 1:
			k.append([i, a[i]])
		else:
			o.append(a[i])
	(a, l) = (0, 0)
	o.sort(reverse=True)
	for i in range(len(k)):
		o.insert(k[i][0], k[i][1])
	print(*o)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	lc = list(map(int, input().split()))
	temp = []
	for i in range(n):
		if lc[i] == 0:
			temp.append(arr[i])
	temp.sort(reverse=True)
	p = 0
	for i in range(n):
		if lc[i] == 0:
			arr[i] = temp[p]
			p += 1
	print(*arr)


from collections import defaultdict as dd
import math
import sys
input = sys.stdin.readline

def nn():
	return int(input())

def li():
	return list(input())

def mi():
	return map(int, input().split())

def lm():
	return list(map(int, input().split()))

def solve():
	n = nn()
	nums = lm()
	lock = lm()
	unlocked = []
	locked = []
	for i in range(n):
		if lock[i] == 0:
			unlocked.append(nums[i])
		else:
			locked.append(nums[i])
	unlocked.sort(reverse=True)
	ans = []
	l = 0
	u = 0
	for i in range(n):
		if lock[i] == 0:
			ans.append(unlocked[u])
			u += 1
		else:
			ans.append(locked[l])
			l += 1
	print(*ans)
	return
q = nn()
for _ in range(q):
	solve()


test = int(input())
for t in range(0, test):
	n = int(input())
	a = [int(i) for i in input().split(' ')]
	l = [int(i) for i in input().split(' ')]
	r = []
	for i in range(0, n):
		if l[i] == 0:
			r.append(a[i])
	r.sort()
	r.reverse()
	p = 0
	ans = []
	for i in range(0, n):
		if l[i] == 0:
			ans.append(r[p])
			p += 1
		else:
			ans.append(a[i])
	for e in ans:
		print(e, end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	l1 = list(map(int, input().split()))
	l2 = list(map(int, input().split()))
	num = []
	for i in range(n):
		if not l2[i]:
			num.append(l1[i])
	num.sort(reverse=True)
	x = 0
	for i in range(n):
		if l2[i]:
			print(l1[i], end=' ')
		else:
			print(num[x], end=' ')
			x += 1
	print()


import math
t = int(input())
for i in range(t):
	n = int(input())
	b = list(map(int, input().split()))
	c = list(map(int, input().split()))
	res = []
	ans = [float('inf')] * n
	for j in range(n):
		if c[j] == 1:
			ans[j] = b[j]
	s1 = 0
	s2 = 0
	for j in range(n):
		if c[j] == 0:
			if b[j] >= 0:
				s1 += 0
			else:
				s2 += 0
			res.append(b[j])
	if s1 >= s2:
		res.sort(reverse=True)
		j = 0
		p = 0
		while j < n:
			if ans[j] == float('inf'):
				ans[j] = res[p]
				p += 1
			j += 1
	else:
		res.sort()
		j = 0
		p = 0
		while j < n:
			if ans[j] == float('inf'):
				ans[j] = res[p]
				p += 1
			j += 1
	print(*ans)


import math
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	l1 = []
	for i in range(n):
		if l[i] == 0:
			l1.append(a[i])
	if len(l1) == 0:
		print(*a)
	else:
		l1.sort(reverse=True)
		n1 = len(l1)
		j = 0
		for i in range(n):
			if l[i] == 0:
				a[i] = l1[j]
				j += 1
		print(*a)


for _ in range(int(input())):
	n = int(input())
	o_arr = list(map(int, input().split()))
	l = list(map(int, input().split()))
	ans = []
	for i in range(n):
		if l[i] == 0:
			ans.append(o_arr[i])
	ans.sort(reverse=True)
	i = 0
	for j in range(n):
		if l[j] == 0:
			o_arr[j] = ans[i]
			i += 1
	print(*o_arr)


I = input
for _ in [0] * int(I()):
	I()
	a = I().split()
	b = [i for (i, x) in enumerate(I()[::2]) if '1' > x]
	for (i, x) in zip(b[::-1], sorted((int(a[i]) for i in b))):
		a[i] = x
	print(*a)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	pos = list(map(int, input().split()))
	l1 = []
	for i in range(n):
		if pos[i] == 0:
			l1.append(l[i])
	l1.sort(reverse=True)
	j = 0
	for i in range(n):
		if pos[i] == 0:
			l[i] = l1[j]
			j += 1
	(ln, suma) = (0, 0)
	for i in range(n):
		suma += l[i]
		if suma < 0:
			ln = i + 1
	print(*l)


import sys

def I():
	return int(sys.stdin.readline().rstrip())

def LI():
	return list(map(int, sys.stdin.readline().rstrip().split()))
t = I()
for _ in range(t):
	n = I()
	A = LI()
	L = LI()
	B = [A[i] for i in range(n) if L[i] == 0]
	B.sort(reverse=True)
	ANS = []
	r = 0
	for i in range(n):
		if L[i] == 1:
			ANS.append(A[i])
		else:
			ANS.append(B[r])
			r += 1
	print(*ANS)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	locked = list(map(int, input().split()))
	tmp = []
	for (i, num) in enumerate(arr):
		if not locked[i]:
			tmp.append(num)
	tmp.sort(reverse=True)
	j = 0
	for (i, num) in enumerate(arr):
		if not locked[i]:
			arr[i] = tmp[j]
			j += 1
	print(' '.join(map(str, arr)))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	s = []
	for j in range(n):
		if l[j] == 0:
			s.append(a[j])
	s = sorted(s, reverse=True)
	k = 0
	for j in range(n):
		if l[j] == 0:
			a[j] = s[k]
			k += 1
	print(*a)


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	X = []
	Y = []
	for i in range(N - 1, -1, -1):
		if B[i]:
			X += [A[i]]
		else:
			Y += [A[i]]
	Y.sort()
	print(*(X.pop() if b else Y.pop() for b in B))


import sys
ii = lambda : sys.stdin.readline().strip()
idata = lambda : [int(x) for x in ii().split()]

def solve():
	n = int(ii())
	data = idata()
	l = idata()
	ans = [0] * n
	d = []
	for i in range(n):
		if l[i]:
			ans[i] = data[i]
		else:
			d += [data[i]]
	data = sorted(d)[::-1]
	for i in range(n):
		if not l[i]:
			ans[i] = data.pop(0)
	print(*ans)
	return
for t in range(int(ii())):
	solve()


import sys
from collections import defaultdict
input = sys.stdin.readline
from collections import deque

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def change(tab, i, j):
	temp = tab[i]
	tab[i] = tab[j]
	tab[j] = temp
nb = inp()
for i in range(nb):
	n = inp()
	tab = inlt()
	lock = inlt()
	unlocked = []
	for i in range(n):
		if lock[i] == 0:
			unlocked.append(tab[i])
	unlocked.sort()
	t = 0
	for i in range(len(unlocked)):
		while lock[t] == 1:
			t += 1
		tab[t] = unlocked[len(unlocked) - 1 - i]
		t += 1
	rep = ''
	for i in range(n):
		rep = rep + ' ' + str(tab[i])
	print(rep)


r = []
for _ in range(int(input())):
	n = int(input())
	a = list(map(lambda x: int(x), input().split()))
	l = list(map(lambda x: int(x), input().split()))
	al0 = []
	for i in range(n):
		if not l[i]:
			al0.append(a[i])
	al0.sort(reverse=True)
	for i in range(n):
		if not l[i]:
			a[i] = al0[0]
			del al0[0]
	r.append(a)
for a in r:
	print(' '.join(list(map(lambda x: str(x), a))))


for i in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]
	c = []
	for i in range(len(b)):
		if b[i] == 0:
			c.append(a[i])
	c.sort(reverse=True)
	i = 0
	while i < len(b) and b[i] != 0:
		i = i + 1
	j = 0
	while i < len(a):
		if b[i] == 0:
			a[i] = c[j]
			i = i + 1
			j = j + 1
		else:
			i = i + 1
	for i in a:
		print(i, ' ', end='')
	print()


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = [int(ai) for ai in input().split()]
	l = [int(ai) for ai in input().split()]
	fNos = [a[i] for i in range(0, n) if not l[i]]
	fNos.sort(reverse=True)
	for i in range(0, n):
		if l[i]:
			continue
		a[i] = fNos[0]
		fNos.pop(0)
	for ai in a:
		print(ai, end=' ')
	print()


for _ in range(int(input())):
	x = int(input())
	(*li1,) = map(int, input().split())
	(*li2,) = map(int, input().split())
	li3 = []
	for i in range(x):
		if li2[i] == 0:
			li3.append(li1[i])
	li3.sort()
	for i in range(x):
		if li2[i] == 0:
			li1[i] = li3.pop()
	print(*li1)


T = int(input())
ret = []
for t in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	ans = [0 if l[i] == 0 else a[i] for i in range(n)]
	ireru = []
	for i in range(n):
		if l[i] == 0:
			ireru.append(a[i])
	ireru.sort()
	for i in range(n):
		if l[i] == 0:
			ans[i] = ireru.pop()
	ret.append(ans)
for t in range(T):
	print(' '.join(list(map(str, ret[t]))))


def solution(n, a, l):
	unlocked = []
	for i in range(n):
		if not l[i]:
			unlocked.append(a[i])
	unlocked.sort()
	for i in range(n):
		if not l[i]:
			a[i] = unlocked.pop()
	return a
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	l = list(map(int, input().split(' ')))
	print(*solution(n, a, l))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	res = list(map(int, input().split()))
	temp = []
	for i in range(n):
		if res[i] == 0:
			temp.append(l[i])
			l[i] = ''
	temp.sort()
	k = len(temp) - 1
	for i in range(n):
		if l[i] == '':
			l[i] = temp[k]
			k -= 1
	print(*l)


import sys, math, cmath, time
start_time = time.time()

def ceil(a, b):
	return -(-a // b)

def solve():
	n = inp()
	a = inlt()
	b = inlt()
	q = []
	for i in range(len(a)):
		if b[i] == 0:
			q.append(a[i])
	q.sort()
	for i in range(len(a)):
		if b[i] == 0:
			a[i] = q.pop()
	print(*a)

def main():
	global tt
	if not ONLINE_JUDGE:
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	t = 1
	t = inp()
	for tt in range(1, t + 1):
		solve()
	if not ONLINE_JUDGE:
		print('Time Elapsed :', time.time() - start_time, 'seconds')
		sys.stdout.close()

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	return input().strip()

def invr():
	return map(int, input().split())

def counter(a):
	q = [0] * (max(a) + 2)
	for i in range(len(a)):
		q[a[i]] = q[a[i]] + 1
	return q

def counter_elements(a):
	q = dict()
	for i in range(len(a)):
		if a[i] not in q:
			q[a[i]] = 0
		q[a[i]] = q[a[i]] + 1
	return q

def string_counter(a):
	q = [0] * 26
	for i in range(len(a)):
		q[ord(a[i]) - 97] = q[ord(a[i]) - 97] + 1
	return q

def factors(n):
	q = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			q.append(i)
			q.append(n // i)
	return list(sorted(list(set(q))))

def prime_factors(n):
	q = []
	while n % 2 == 0:
		q.append(2)
		n = n // 2
	for i in range(3, int(n ** 0.5) + 1, 2):
		while n % i == 0:
			q.append(i)
			n = n // i
	if n > 2:
		q.append(n)
	return list(sorted(q))

def transpose(a):
	(n, m) = (len(a), len(a[0]))
	b = [[0] * n for i in range(m)]
	for i in range(m):
		for j in range(n):
			b[i][j] = a[j][i]
	return b

def factorial(n, m=1000000007):
	q = 1
	for i in range(n):
		q = q * (i + 1) % m
	return q
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
	input = sys.stdin.readline
main()


from collections import defaultdict
from queue import deque

def arrinp():
	return [*map(int, input().split(' '))]

def mulinp():
	return map(int, input().split(' '))

def intinp():
	return int(input())

def solution():
	n = intinp()
	a = arrinp()
	l = arrinp()
	arr = []
	for i in range(n):
		if l[i] == 0:
			arr.append(a[i])
	arr.sort(reverse=True)
	j = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = arr[j]
			j += 1
	print(*a)
testcases = 1
testcases = int(input())
for _ in range(testcases):
	solution()


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l2 = list(map(int, input().split()))
	arr = []
	for i in range(n):
		if l2[i] == 0:
			arr.append(l[i])
	arr.sort(reverse=True)
	j = 0
	for i in range(n):
		if l2[i] == 0:
			l[i] = arr[j]
			j += 1
	print(*l)


import sys
from collections import defaultdict as dd
from collections import Counter as cc
from queue import Queue
import math
import itertools
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
input = lambda : sys.stdin.buffer.readline().rstrip()
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	q = []
	for i in range(n):
		if l[i] == 0:
			q.append(a[i])
	q = sorted(q)
	t = sum(a)
	j = 0
	for i in range(n - 1, -1, -1):
		if l[i] == 0:
			a[i] = q[j]
			t -= q[j]
			j += 1
		else:
			t -= a[i]
	print(*a)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	l = list(map(int, input().split()))
	pos = []
	val = []
	for i in range(n):
		if l[i] == 0:
			pos.append(i)
			val.append(arr[i])
	val.sort(reverse=True)
	for i in range(len(pos)):
		arr[pos[i]] = val[i]
	for i in range(n):
		print(arr[i], end=' ')
	print()


from heapq import heapify, heappop
from collections import deque
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	a1 = deque([x for (x, y) in zip(a, l) if y == 1])
	a0 = [-x for (x, y) in zip(a, l) if y == 0]
	heapify(a0)
	ans = []
	for i in l:
		if i == 1:
			x = a1.popleft()
			ans.append(x)
		else:
			x = heappop(a0)
			ans.append(-x)
	print(*ans)


import sys
for i in range(0, int(sys.stdin.readline())):
	l = sys.stdin.readline()
	vals = list(map(int, sys.stdin.readline().split()))
	locked = list(map(int, sys.stdin.readline().split()))
	ulvals = [vals[i] for i in range(0, len(vals)) if locked[i] == 0]
	ulvals.sort(reverse=True)
	c = 0
	ssum = ''
	for j in range(0, len(vals)):
		if locked[j] == 1:
			ssum += str(vals[j])
		else:
			ssum += str(ulvals[c])
			c += 1
		ssum += ' '
	sys.stdout.write(ssum)
	sys.stdout.write('\n')


import io
import os
import sys
import math
import random
from copy import copy
from decimal import Decimal
fileno = sys.stdin.fileno()
input = io.BytesIO(os.read(fileno, os.fstat(fileno).st_size)).readline

def find_k(A):
	found = False
	p = 0
	k = -1
	for i in range(len(A)):
		p += A[i]
		if p < 0:
			found = True
			k = i
	return k + 1 if found else 0

def partial_sort(A, B, reverse, rnd=False):
	free_elements = [a for (a, b) in zip(A, B) if b == 0]
	if rnd:
		random.shuffle(free_elements)
	else:
		free_elements.sort(reverse=reverse)
	A = copy(A)
	i = 0
	j = 0
	while j < len(free_elements):
		while B[i] == 1:
			i += 1
		A[i] = free_elements[j]
		i += 1
		j += 1
	return A
T = int(input())
for t in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	best_k = find_k(A)
	ans = A
	for option in [partial_sort(A, B, False), partial_sort(A, B, True)]:
		k = find_k(option)
		if k < best_k:
			best_k = k
			ans = option
	print(' '.join(map(str, ans)))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().rstrip().split(' ')))
	l = list(map(int, input().rstrip().split(' ')))
	lockedList = []
	unlockedList = []
	for i in range(n):
		if l[i] == 0:
			unlockedList.append(a[i])
		else:
			lockedList.append(a[i])
	unlockedList = sorted(unlockedList, reverse=True)
	currentSum = 0
	pl = 0
	pu = 0
	out = []
	for i in range(n):
		if l[i] == 0:
			currentSum += unlockedList[pu]
			out.append(unlockedList[pu])
			pu += 1
		else:
			currentSum += lockedList[pl]
			out.append(lockedList[pl])
			pl += 1
	print(*out)


t = int(input())
while t > 0:
	n = int(input())
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]
	s = []
	for i in range(n):
		if b[i] == 0:
			s.append(a[i])
	s = sorted(s, reverse=True)
	out = []
	i = 0
	j = 0
	while i < n:
		if b[i] == 1:
			i += 1
			continue
		else:
			a[i] = s[j]
			j += 1
		i += 1
	print(*a)
	t -= 1


from collections import defaultdict as df
for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = list(map(int, input().split()))
	s = []
	for i in range(n):
		if not l1[i]:
			s.append(l[i])
	s.sort(reverse=True)
	k = 0
	for i in range(n):
		if l1[i]:
			print(l[i], end=' ')
		else:
			print(s[k], end=' ')
			k += 1
	print()


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	l = list(map(int, input().split()))
	new = []
	for i in range(n):
		if not l[i]:
			new.append(arr[i])
	new.sort()
	for i in range(n):
		if not l[i]:
			arr[i] = new.pop()
	print(*arr)


from collections import defaultdict, Counter
from math import log, ceil, floor
from itertools import accumulate

def cumulativeSum(lst):
	print(list(accumulate(lst)))

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	temp = []
	for i in range(n):
		if l[i] == 0:
			temp.append(a[i])
	temp.sort(reverse=True)
	s = sum(a)
	if s >= 0:
		ans = []
		i = 0
		for j in range(n):
			if l[j] == 0:
				ans.append(temp[i])
				i += 1
			else:
				ans.append(a[j])
	else:
		ans = a
	print(*ans)
t = 1
t = int(input())
for _ in range(t):
	solve()


tc = int(input())
for _ in range(tc):
	n = int(input())
	arr = list(map(int, input().split()))
	larr = list(map(int, input().split()))
	narr = []
	for i in range(n):
		if larr[i] == 0:
			narr.append(arr[i])
	narr.sort(reverse=True)
	for i in range(n):
		if larr[i] == 0:
			arr[i] = narr[0]
			del narr[0]
	print(*arr)


for y in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	lst2 = list(map(int, input().split()))
	lstm = []
	for i in range(n):
		if lst2[i] == 0:
			lstm.append(lst[i])
	lstm.sort(reverse=True)
	temp = 0
	for i in range(n):
		if lst2[i] == 1:
			print(lst[i], end=' ')
		else:
			print(lstm[temp], end=' ')
			temp += 1
	print()


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	locked = list(map(int, input().split()))
	sarr = []
	for i in range(n):
		if locked[i] == 0:
			sarr.append(arr[i])
	sarr.sort()
	j = 0
	for i in range(n):
		if locked[i] == 0:
			arr[i] = sarr[-1 - j]
			j += 1
	pre = 0
	last = 0
	for i in range(n):
		pre += arr[i]
		if pre < 0:
			last = i + 1
	print(*arr)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	l = [int(i) for i in input().split()]
	b = []
	for i in range(n):
		if l[i] == 0:
			b.append(a[i])
	b.sort()
	for i in range(n):
		if l[i] == 0:
			x = b.pop()
			a[i] = x
	print(*a)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	chk = list(map(int, input().split()))
	val = []
	for i in range(n):
		if not chk[i]:
			val.append(l[i])
	val.sort(reverse=True)
	j = 0
	for i in range(n):
		if not chk[i]:
			l[i] = val[j]
			j += 1
	print(*l)


R = lambda : list(map(int, input().split()))
t = R()
for i in range(0, t[0]):
	n = R()
	lst = R()
	ar = R()
	b = []
	for j in range(0, n[0]):
		if ar[j] < 1:
			b += [lst[j]]
	b.sort()
	for j in range(0, n[0]):
		if ar[j] < 1:
			lst[j] = b.pop()
	print(*lst)


from math import gcd
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	l = list(map(int, input().split()))
	temp = []
	for i in range(n):
		if l[i] == 0:
			temp.append(arr[i])
	temp.sort()
	s = 0
	j = 0
	maxx = -1
	for i in range(n):
		if l[i] == 0:
			s += temp[j]
			j += 1
			if s < 0:
				maxx = i
		else:
			s += arr[i]
			if s < 0:
				maxx = i
	temp = temp[::-1]
	s = 0
	j = 0
	m1 = -1
	for i in range(n):
		if l[i] == 0:
			s += temp[j]
			j += 1
			if s < 0:
				m1 = i
		else:
			s += arr[i]
			if s < 0:
				m1 = i
	if maxx < m1:
		temp = temp[::-1]
	j = 0
	for i in range(n):
		if l[i] == 0:
			arr[i] = temp[j]
			j += 1
	print(*arr)


def schet():
	n = int(input())
	mas = list(map(int, input().split()))
	mas_ans = list(map(int, input().split()))
	temp_list = []
	for i in range(len(mas)):
		if mas_ans[i] == 0:
			temp_list.append(mas[i])
	temp_list = sorted(temp_list, reverse=True)
	for i in range(len(mas)):
		if mas_ans[i] == 0:
			mas[i] = temp_list.pop(0)
	print(*mas)
t = int(input())
for i in range(t):
	schet()


tests = int(input())
for t in range(tests):
	n = int(input())
	ls = list(map(int, input().split()))
	ls_locked = list(map(int, input().split()))
	unlocked = [ls[idx] for (idx, item) in enumerate(ls_locked) if item == 0]
	unlocked = sorted(unlocked)[::-1]
	count = 0
	res_ls = []
	for (idx, item) in enumerate(ls_locked):
		if item == 0:
			res_ls.append(str(unlocked[count]))
			count += 1
		else:
			res_ls.append(str(ls[idx]))
	print(' '.join(res_ls))


import heapq

def ans(n, arr, lock):
	heap = []
	lst = ['x'] * n
	for i in range(n):
		if lock[i] == 1:
			lst[i] = str(arr[i])
		else:
			heapq.heappush(heap, -1 * arr[i])
	for i in range(n):
		if lst[i] == 'x':
			lst[i] = str(-heapq.heappop(heap))
	return ' '.join(lst)
T = int(input())
for i in range(T):
	n = int(input())
	arr = list(map(int, input().strip().split(' ')))
	lock = list(map(int, input().strip().split(' ')))
	print(ans(n, arr, lock))


import math

def get_int():
	return int(input())

def get_ints():
	return [int(x) for x in input().split()]
t = get_int()
for test in range(0, t):
	n = get_int()
	a = get_ints()
	locked = get_ints()
	s = [a[i] for i in range(0, len(a)) if locked[i] == 0]
	s.sort(reverse=True)
	idx = 0
	for i in range(0, n):
		if locked[i] == 0:
			a[i] = s[idx]
			idx += 1
	print(*a, sep=' ')


import sys
input = sys.stdin.buffer.readline
T = int(input())

def f(x):
	b = [0] * len(x)
	for i in range(len(x)):
		b[i] = b[i - 1] + x[i]
	return b
for testcase in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	L = list(map(int, input().split()))
	b = []
	plus = 0
	for i in range(n):
		if L[i] == 0:
			b.append(a[i])
	b.sort(reverse=True)
	k = 0
	for i in range(n):
		if L[i] == 0:
			a[i] = b[k]
			k += 1
	print(*a)


import math
import sys
import collections
import bisect
input = sys.stdin.readline
flush = sys.stdout.flush
readint = lambda : int(input())
readintm = lambda : map(int, input().strip().split())
readintl = lambda : list(map(int, input().strip().split()))
readfloatm = lambda : map(float, input().strip().split())
readfloatl = lambda : list(map(float, input().strip().split()))
readstr = lambda : input().strip()
readstrl = lambda : list(input().strip())
join = lambda x, l: x.join(map(str, l))
print = lambda *inputs: sys.stdout.write(' '.join(map(str, inputs)) + '\n')
printl = lambda l: sys.stdout.write(' '.join(map(str, l)) + '\n')
ceil = lambda x, y: (x + y - 1) // y
mod = 1000000007

def pre(x):
	current = 0
	ans = []
	for num in x:
		current += num
		ans.append(current)
	return ans
for _ in range(readint()):
	n = readint()
	nums = readintl()
	locks = readintl()
	if sum(nums) < 0 or all(locks):
		print(join(' ', nums))
	else:
		reorder = sorted([nums[i] for i in range(n) if not locks[i]], reverse=True)
		pos = 0
		for i in range(n):
			if not locks[i]:
				nums[i] = reorder[pos]
				pos += 1
		printl(nums)


from copy import *
from sys import *
from collections import *
from itertools import *
from functools import *
from math import *

def dbg(*args):
	print(*args, file=stderr)

def dbv(var_name):
	print(f'{var_name} = {eval(var_name)}', file=stderr)

def mis(fr=None):
	if fr is None:
		fr = input().split()
	return list(map(int, fr))

def mbs(fr=None):
	if fr is None:
		fr = input().split()
	return list(map(bool, fr))
for _ in range(int(input())):
	n = int(input())
	arr = mis()
	lck = mbs(mis())
	uarr = [arr[i] for i in filterfalse(lambda x: lck[x], [i for i in range(n)])]
	suarr = sorted(uarr, reverse=True)
	ans = copy(arr)
	ptr = 0
	for i in range(n):
		if lck[i]:
			continue
		ans[i] = suarr[ptr]
		ptr += 1
		if ptr == len(suarr):
			break
	print(*ans)


t = int(input())
for j in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	z = list(map(int, input().split()))
	b = list()
	for i in range(n):
		if z[i] == 0:
			b.append(a[i])
	b = sorted(b, reverse=True)
	pos = 0
	for i in range(n):
		if z[i] == 0:
			a[i] = b[pos]
			pos += 1
		print(a[i], end=' ')


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
from decimal import Decimal
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
for _ in range(int(data())):
	n = int(data())
	arr = l()
	lock = l()
	val = []
	for i in range(n):
		if lock[i]:
			continue
		val.append(arr[i])
	val.sort()
	for i in range(n):
		if lock[i]:
			continue
		arr[i] = val.pop()
	outa(*arr)


t = int(input())
for you in range(t):
	n = int(input())
	l = input().split()
	li = [int(i) for i in l]
	arr = []
	b = input().split()
	bi = [int(i) for i in b]
	for i in range(n):
		if bi[i] == 0:
			arr.append(li[i])
	arr.sort()
	arr.reverse()
	l = [0 for i in range(n)]
	curr = 0
	for i in range(n):
		if bi[i]:
			l[i] = li[i]
		else:
			l[i] = arr[curr]
			curr += 1
	for i in l:
		print(i, end=' ')
	print()


def check(pos):
	cur = 0
	Nn = list(N)
	Pp = list(P)
	for j in range(pos):
		if l[j] == 0:
			if Nn != []:
				ans.append(Nn.pop())
			else:
				ans.append(Pp.pop())
		else:
			ans.append(a[j])
	for j in range(pos, n):
		if l[j] == 0:
			if Pp != []:
				ans.append(Pp.pop())
			else:
				ans.append(Nn.pop(0))
		else:
			ans.append(a[j])
	summary = 0
	res = 0
	for j in range(n):
		summary += ans[j]
		if summary < 0:
			res = j
	return res
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	P = []
	N = []
	for i in range(n):
		if l[i] == 0:
			if a[i] > 0:
				P.append(a[i])
			else:
				N.append(a[i])
	P.sort()
	N.sort(reverse=True)
	cur_ans = n + 1
	for i in range(n):
		ans = []
		curr = check(i)
		if curr < cur_ans:
			cur_ans = curr
			final = list(ans)
	print(' '.join((str(x) for x in final)))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = []
	for i in range(n):
		if b[i] == 0:
			c.append(a[i])
	c.sort(reverse=True)
	lo = 0
	for i in range(n):
		if b[i] == 0:
			a[i] = c[lo]
			lo += 1
	print(*a)


from math import ceil
t = int(input())
for _ in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	command = list(map(int, input().split()))
	temp = []
	for i in range(n):
		if not command[i]:
			temp.append(lst[i])
	temp.sort(reverse=True)
	i = 0
	j = 0
	while i < n and j < len(temp):
		if not command[i]:
			print(temp[j], end=' ')
			j += 1
		else:
			print(lst[i], end=' ')
		i += 1
	while i < n:
		print(lst[i], end=' ')
		i += 1
	print()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	s = sum(a)
	ans = []
	if s < 0:
		print(*a)
	else:
		b = []
		for i in range(n):
			if not l[i]:
				b.append(a[i])
		b.sort(reverse=True)
		j = 0
		for i in range(n):
			if not l[i]:
				ans.append(b[j])
				j += 1
			else:
				ans.append(a[i])
	print(*ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(s) for s in input().split(' ')]
	l = [int(s) for s in input().split(' ')]
	positive_sum = 0
	negative_sum = 0
	unlocked = []
	for i in range(n):
		if l[i] == 0:
			unlocked.append(a[i])
	for i in range(n):
		if l[i] == 0:
			if a[i] < 0:
				negative_sum += abs(-a[i])
			else:
				positive_sum += abs(a[i])
	if positive_sum >= negative_sum:
		unlocked.sort()
	else:
		unlocked.sort(reverse=True)
	unlocked.sort()
	for i in range(n):
		if l[i] == 0:
			a[i] = unlocked[-1]
			unlocked.pop()
	out = ''
	for el in a:
		out += str(el) + ' '
	print(out[:-1])


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	lock = [int(x) for x in input().split()]
	sortthis = [a for (a, b) in zip(arr, lock) if not b]
	sortthis.sort(reverse=1)
	indexs = [i for (i, x) in enumerate(lock) if x == 0]
	for (i, x) in zip(indexs, sortthis):
		arr[i] = x
	print(*arr)


import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
ORDA = 97

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return [int(i) for i in input().split()]

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=2):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	new_number = 0
	while number > 0:
		new_number += number % base
		number //= base
	return new_number

def cdiv(n, k):
	return n // k + (n % k != 0)

def ispal(s):
	for i in range(len(s) // 2 + 1):
		if s[i] != s[-i - 1]:
			return False
	return True

def main():
	for _ in range(ii()):
		n = ii()
		arr = li()
		l = li()
		unl = []
		lo = []
		for i in range(n):
			if l[i] == 0:
				unl.append(arr[i])
			else:
				lo.append(i)
		unl = sorted(unl, reverse=True)
		for idx in lo:
			unl.insert(idx, arr[idx])
		print(*unl)
main()


import sys, math
import io, os
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter

def data():
	return sys.stdin.readline().strip()

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write(' '.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
from decimal import Decimal
mod = int(1000000000.0) + 7
INF = float('inf')
for t in range(int(data())):
	n = int(data())
	a = mdata()
	l = mdata()
	un = []
	for i in range(n):
		if l[i] == 0:
			un.append(a[i])
	k = 0
	s = 0
	if un:
		un.sort(reverse=True)
	cnt = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = un[cnt]
			cnt += 1
	outl(a)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = []
	for i in range(n):
		if b[i] == 0:
			c.append(a[i])
	c.sort()
	d = -1
	for i in range(n):
		if b[i] == 0:
			print(c[d], end=' ')
			d = d - 1
		else:
			print(a[i], end=' ')
	print('\n')


rw = int(input())
for qwe in range(rw):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	k = []
	for i in range(n):
		if l[i] == 0:
			k.append(a[i])
			a[i] = 0
	k.sort()
	k.reverse()
	for i in range(n):
		if l[i] == 0:
			a[i] = k.pop(0)
	for i in a:
		print(i, end=' ')
	print('')


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	u = []
	for __ in range(n):
		if l[__] == 0:
			u.append(a[__])
	u.sort(reverse=True)
	fin = []
	i = 0
	for __ in range(n):
		if l[__] == 0:
			fin.append(u[i])
			i += 1
		else:
			fin.append(a[__])
	print(*fin)
for _ in range(int(input())):
	solve()


def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = 0
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = 1
			break
	return ans

def kadane(x):
	sum_so_far = 0
	current_sum = 0
	for i in x:
		current_sum += i
		if current_sum < 0:
			current_sum = 0
		else:
			sum_so_far = mpos(sum_so_far, current_sum)
	return sum_so_far

def pref(li):
	pref_sum = [0]
	for i in li:
		pref_sum.append(pref_sum[-1] + i)
	return pref_sum

def graph(n, m):
	adj = dict()
	for i in range(1, n + 1):
		adj.setdefault(i, 0)
	for i in range(m):
		(a, b) = map(int, input().split())
		adj[a] += 1
		adj[b] += 1
	return adj
import sys
import threading
from math import *
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	li = []
	for i in range(n):
		if b[i] == 0:
			li.append(a[i])
	li.sort(reverse=True)
	j = 0
	for i in range(n):
		if b[i] == 0:
			a[i] = li[j]
			j += 1
	print(*a)


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	for _ in range(ri()):
		n = ri()
		a = ria()
		b = ria()
		neg = deque([])
		pos = deque([])
		for i in range(n):
			if b[i] == 0:
				if a[i] < 0:
					neg.append(a[i])
				else:
					pos.append(a[i])
		neg = sorted(neg)
		pos = sorted(pos)
		for i in range(n):
			if b[i] == 0:
				if len(pos) > 0:
					a[i] = pos[-1]
					pos.pop()
				else:
					a[i] = neg[-1]
					neg.pop()
		print(*a)
main()


t = int(input())
for num in range(t):
	n = int(input())
	most = [int(i) for i in input().split()]
	blocks = [int(i) for i in input().split()]
	nise_list = []
	count_list = []
	for (count, item) in list(enumerate(blocks)):
		if item == 0:
			nise_list.append((most[count], count))
			count_list.append(count)
	nise_list.sort(reverse=True)
	i = 0
	for (item, count) in nise_list:
		most[count_list[i]] = item
		i += 1
	print(' '.join((str(b) for b in most)))


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
ALPHA = 'abcdefghijklmnopqrstuvwxyz'
M = 998244353
EPS = 1e-06

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
for _ in range(Int()):
	n = Int()
	a = array()
	status = array()
	have = [a[i] for i in range(n) if not status[i]]
	have.sort(reverse=True)
	ans = []
	ind = 0
	for i in range(n):
		if status[i]:
			ans.append(a[i])
		else:
			ans.append(have[ind])
			ind += 1
	print(*ans)


t = int(input())
for j in range(t):
	n = int(input())
	li = list(map(int, input().split()))
	l = list(map(int, input().split()))
	tl = [i for (i, j) in zip(li, l) if j == 0]
	tl.sort()
	(p, q) = (0, len(tl) - 1)
	while p < n:
		if l[p] == 0:
			li[p] = tl[q]
			q -= 1
		p += 1
	print(*li)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	p = list(map(int, input().split()))
	c = []
	m = 0
	for i in range(n):
		if p[i] == 0:
			m = i
			break
	for i in range(n):
		if p[i] == 0:
			c.append(a[i])
	c.sort(reverse=True)
	j = 0
	for i in range(n):
		if p[i] == 0:
			print(c[j], end=' ')
			j += 1
		else:
			print(a[i], end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	s = list(map(int, input().split()))
	index = []
	t = []
	for i in range(n):
		if s[i] == 0:
			index.append(i)
			t.append(lst[i])
	if len(t) == 0:
		print(*lst)
		continue
	t.sort(reverse=True)
	for i in range(len(index)):
		lst[index[i]] = t[i]
	print(*lst)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	u = []
	for i in range(n):
		if l[i] == 0:
			u.append(a[i])
	u.sort(reverse=True)
	j = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = u[j]
			j += 1
	print(*a)


N = int(input())
for _ in range(N):
	n = int(input())
	arr = list(map(int, input().split()))
	locks = list(map(int, input().split()))
	seeds = sorted([arr[i] for i in range(n) if not locks[i]])
	for i in range(n):
		if locks[i]:
			print(arr[i], end=' ')
		else:
			print(seeds.pop(), end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	pos = list(map(int, input().split()))
	l = []
	for i in range(n):
		if pos[i] == 0:
			l.append(a[i])
	l.sort()
	l.reverse()
	k = 0
	for j in range(n):
		if pos[j] == 0:
			a[j] = l[k]
			k += 1
	s = ''
	for jk in a:
		s += str(jk) + ' '
	print(s)


from math import *
t = int(input())
while t > 0:
	n = int(input())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	lol = []
	wow = []
	for i in range(n):
		if b[i] == 0:
			lol.append(a[i])
			wow.append(i)
	lol.sort(reverse=True)
	for i in range(len(lol)):
		a[wow[i]] = lol[i]
	for i in a:
		print(i, end=' ')
	print()
	t -= 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	unlocked_numbers = []
	for i in range(n):
		if l[i] == 0:
			unlocked_numbers.append(a[i])
	unlocked_numbers.sort(reverse=True)
	unlocked_iter = iter(unlocked_numbers)
	for i in range(n):
		if l[i] == 0:
			a[i] = next(unlocked_iter)
	print(*a)


def main():
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	new_lst = []
	for i in range(n):
		if not l[i]:
			new_lst.append(a[i])
	new_lst = sorted(new_lst, reverse=True)
	k = 0
	for i in range(n):
		if not l[i]:
			a[i] = new_lst[k]
			k += 1
	sm = 0
	for i in a:
		print(i, end=' ')
	print()
t = int(input())
for i in range(t):
	main()


from os import path
import sys
from heapq import heappush, heappop
from functools import cmp_to_key as ctk
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input().rstrip()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('a')

def ceil(a, b):
	return (a + b - 1) // b
file = 1

def f():
	sys.stdout.flush()

def solve():
	for _ in range(ii()):
		n = ii()
		a = li()
		b = li()
		p = []
		for i in range(n):
			if b[i] == 0:
				p.append(a[i])
		p.sort()
		for i in range(n):
			if b[i]:
				print(a[i], end=' ')
			else:
				print(p[-1], end=' ')
				p.pop()
		print()
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	input = sys.stdin.readline
solve()


from sys import stdin, stdout

def find(arr, N, K):
	a = [arr[i] for i in range(N) if K[i] == 0]
	a.sort()
	for i in range(N):
		if K[i] == 0:
			arr[i] = a.pop()
	return ' '.join(map(str, arr))

def main():
	for _ in range(int(stdin.readline())):
		N = int(stdin.readline())
		arr = list(map(int, stdin.readline().split()))
		K = list(map(int, stdin.readline().split()))
		print(find(arr, N, K))
main()


t = int(input())
ns = []
arrays = []
locked_info = []
for i in range(t):
	ns.append(int(input()))
	arrays.append([int(x) for x in input().split()])
	locked_info.append([int(j) for j in input().split()])
for j in range(t):
	n = ns[j]
	array = arrays[j]
	locked = locked_info[j]
	_sum = 0
	unlocked_values = []
	for i in range(n):
		if locked[i] == False:
			unlocked_values.append(array[i])
	unlocked_values = sorted(unlocked_values)
	for index in range(n):
		_sum += array[index]
	res = []
	for index in range(n - 1, -1, -1):
		if locked[index] == True:
			res.append(array[index])
		else:
			res.append(unlocked_values.pop(0))
	for elem in res[::-1]:
		print(elem, end=' ')
	print('')


import sys
input = lambda : sys.stdin.readline().rstrip()
T = int(input())
for _ in range(T):
	N = int(input())
	A = [int(a) for a in input().split()]
	L = [int(a) for a in input().split()]
	s = sorted([a for (a, l) in zip(A, L) if l == 0])
	B = []
	for i in range(N):
		if L[i]:
			B.append(A[i])
		else:
			B.append(s.pop())
	print(*B)


from sys import stdout, stdin
(_input, _print) = (stdin.readline, stdout.write)
(_range, _int, _enumerate, _sorted, _sum) = (range, int, enumerate, sorted, sum)

def solution():
	for _ in _range(_int(_input())):
		n = _int(_input())
		arr = [_int(i) for i in _input().split()]
		bit_arr = [_int(i) for i in _input().split()]
		z = _sorted([v for (i, v) in _enumerate(arr) if bit_arr[i] == 0], reverse=True)
		index = 0
		for i in _range(n):
			if bit_arr[i] == 0:
				arr[i] = z[index]
				index += 1
		_sum = 0
		k = 0
		for (i, v) in _enumerate(arr):
			_sum += v
			if _sum < 0:
				k = i
			else:
				break
		print(*arr)
solution()


from bisect import bisect_left
import os
import sys
from io import BytesIO, IOBase
from collections import Counter, defaultdict
from collections import deque
from functools import cmp_to_key
import math
import heapq

def sin():
	return input()

def ain():
	return list(map(int, sin().split()))

def sain():
	return input().split()

def iin():
	return int(sin())
MAX = float('inf')
MIN = float('-inf')
MOD = 1000000007

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	s = set()
	for p in range(2, n + 1):
		if prime[p]:
			s.add(p)
	return s

def readTree(n, m):
	adj = [deque([]) for _ in range(n + 1)]
	for _ in range(m):
		(u, v) = ain()
		adj[u].append(v)
		adj[v].append(u)
	return adj

def main():
	for _ in range(iin()):
		n = iin()
		a = ain()
		l = ain()
		d = deque([])
		for i in range(n):
			if l[i] == 0:
				d.append(a[i])
				a[i] = ''
		d = sorted(d)
		i = len(d) - 1
		j = 0
		while i >= 0:
			if a[j] == '':
				a[j] = d[i]
				i -= 1
				j += 1
			else:
				j += 1
		print(*a)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
if os.getcwd() == 'D:\\code':
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
	input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import heapq
import collections
from math import log2
import itertools
from functools import lru_cache
from sys import setrecursionlimit as srl
srl(2 * 10 ** 6)
N = 200001
t = int(input())
for _ in range(1, t + 1):
	n = int(input())
	a = []
	l = []
	b = []
	in_ = map(int, input().split())
	in1_ = map(int, input().split())
	for x in in_:
		a.append(x)
	for y in in1_:
		l.append(y)
	for i in range(n):
		if l[i] == 0:
			b.append(a[i])
	j = 0
	b.sort(reverse=True)
	for i in range(n):
		if l[i] == 0:
			a[i] = b[j]
			j += 1
	print(*a)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	c = []
	for i in range(n):
		if b[i] == 0:
			c.append(a[i])
	c.sort()
	for i in range(n):
		if b[i] == 0:
			print(c.pop(), end=' ')
		else:
			print(a[i], end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	p = []
	for i in range(n):
		if l[i] == 0:
			p.append(a[i])
	p = sorted(p, reverse=True)
	q = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = p[q]
			q += 1
	print(*a)


for _ in range(int(input())):
	n = int(input())
	A = [int(_) for _ in input().split()]
	L = [int(_) for _ in input().split()]
	X = []
	for _ in range(n):
		if L[_] == 0:
			X.append(A[_])
	X.sort()
	X = list(reversed(X))
	i = 0
	for _ in range(n):
		if L[_] == 0:
			A[_] = X[i]
			i += 1
	print(*A)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	l = [int(x) for x in input().split()]
	vals = []
	for i in range(n):
		if l[i] == 0:
			vals.append(a[i])
	vals.sort(reverse=True)
	index = 0
	result = a.copy()
	for i in range(n):
		if l[i] == 0:
			result[i] = vals[index]
			index += 1
	print(*result)


import sys, math
input = lambda : sys.stdin.readline().rstrip()
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	b = []
	for i in range(n):
		if l[i] == 0:
			b.append(a[i])
	b = sorted(b)[::-1]
	k = 0
	for i in range(n):
		if l[i] == 1:
			print(a[i], end=' ')
		else:
			print(b[k], end=' ')
			k += 1
	print()


def solve():
	n = int(input().strip())
	a = list(map(int, input().strip().split()))
	l = list(map(int, input().strip().split()))
	new_a = []
	for i in range(n):
		if l[i] == 0:
			new_a.append(a[i])
	new_a = sorted(new_a, reverse=True)
	cursor = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = new_a[cursor]
			cursor += 1
			if cursor >= len(new_a):
				break
	print(*a)
t = int(input().strip())
for _ in range(t):
	solve()


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def print_list(l):
	print(' '.join(map(str, l)))
for _ in range(N()):
	n = N()
	a = RLL()
	l = RLL()
	data = [a[i] for i in range(n) if l[i] == 0]
	data.sort()
	for i in range(n):
		if l[i] == 0:
			a[i] = data.pop()
	print_list(a)


import sys
LI = lambda : list(map(int, sys.stdin.readline().split()))
MI = lambda : map(int, sys.stdin.readline().split())
SI = lambda : sys.stdin.readline().strip('\n')
II = lambda : int(sys.stdin.readline())
for _ in range(II()):
	n = II()
	a = LI()
	(l, u) = ([], [])
	tp = LI()
	for (i, v) in enumerate(tp):
		if v:
			l.append(a[i])
		else:
			u.append(a[i])
	rsu = sorted(u, reverse=True)
	(ans, i, j) = ([], 0, 0)
	for v in tp:
		if v:
			ans.append(l[i])
			i += 1
		else:
			ans.append(rsu[j])
			j += 1
	print(*ans)


def mi():
	return map(int, input().split())
for _ in range(int(input())):
	n = int(input())
	a = list(mi())
	l = list(mi())
	b = []
	for i in range(n):
		if not l[i]:
			b.append(a[i])
	b.sort(reverse=True)
	ind = 0
	for i in range(n):
		if not l[i]:
			a[i] = b[ind]
			ind += 1
	print(*a)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	x = [a[i] for i in range(n) if l[i] == 0]
	x.sort(reverse=True)
	for i in range(n):
		if l[i] == 0:
			a[i] = x.pop(0)
	print(*a)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
t = inp()
while t > 0:
	n = inp()
	arr = inlt()
	brr = inlt()
	l = []
	res = [0 for i in range(n)]
	for i in range(n):
		if brr[i] == 1:
			res[i] = arr[i]
		else:
			l.append(arr[i])
	l.sort(reverse=True)
	for i in range(n):
		if brr[i] == 0:
			res[i] = l[0]
			l.pop(0)
	print(*res)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = []
	for i in range(n):
		if b[i] == 0:
			c.append(a[i])
	c.sort(reverse=True)
	j = 0
	for i in range(n):
		if not b[i]:
			a[i] = c[j]
			j = j + 1
	print(' '.join(map(str, a)))


for _ in range(int(input())):
	am = int(input())
	arr = list(map(int, input().split()))
	blocked = list(map(int, input().split()))
	nBlockedInds = []
	nBlockedNums = []
	for i in range(am):
		if blocked[i] == 0:
			nBlockedInds.append(i)
			nBlockedNums.append(arr[i])
	nBlockedNums.sort(reverse=True)
	for i in range(len(nBlockedInds)):
		arr[nBlockedInds[i]] = nBlockedNums[i]
	print(*arr)


import os
import sys
from io import BytesIO, IOBase

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		A = list(map(int, input().split()))
		L = list(map(int, input().split()))
		B = list()
		for i in range(n):
			if L[i] == 0:
				B.append(A[i])
		B.sort()
		O = [0] * n
		for i in range(n):
			if L[i] == 1:
				O[i] = A[i]
			else:
				O[i] = B.pop()
		for o in O:
			print(o, end=' ')
		print()
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = lambda s: self.buffer.write(s.encode()) if self.writable else None

	def read(self):
		if self.buffer.tell():
			return self.buffer.read().decode('ascii')
		return os.read(self._fd, os.fstat(self._fd).st_size).decode('ascii')

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline().decode('ascii')

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

def print(*args, sep=' ', end='\n', file=sys.stdout, flush=False):
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(end)
	if flush:
		file.flush()
(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
sys.setrecursionlimit(10000)
main()


import sys
input = sys.stdin.readline
from bisect import bisect_left
from bisect import bisect_right
from math import ceil, factorial

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def nCr(n, k, modulus=1):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
		res %= modulus
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
for _ in range(int(input())):
	n = int(input())
	a = int_array()
	l = int_array()
	x = []
	for i in range(n):
		if not l[i]:
			x.append(a[i])
	x.sort()
	for i in range(n):
		if not l[i]:
			a[i] = x.pop()
	print(*a)


def solve():
	t = inp()
	for _ in range(t):
		n = inp()
		arr = seq()
		pos = seq()
		index = []
		for j in range(n):
			if pos[j] == 0:
				index.append(arr[j])
		index.sort()
		count = len(index) - 1
		for j in range(n):
			if pos[j] == 0:
				print(index[count], end=' ')
				count -= 1
			else:
				print(arr[j], end=' ')
		print()
import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
inp = lambda : int(input())
strng = lambda : input().strip()
jn = lambda x, l: x.join(map(str, l))
strl = lambda : list(input().strip())
mul = lambda : map(int, input().strip().split())
mulf = lambda : map(float, input().strip().split())
seq = lambda : list(map(int, input().strip().split()))
ceil = lambda x: int(x) if x == int(x) else int(x) + 1
ceildiv = lambda x, d: x // d if x % d == 0 else x // d + 1
flush = lambda : stdout.flush()
stdstr = lambda : stdin.readline()
stdint = lambda : int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
solve()


import math
import random
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
import threading
from collections import defaultdict
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n

class SegmentTree1:

	def __init__(self, data, default=0, func=lambda a, b: max(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] < key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def binary(x, length):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
for ik in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	u = list(map(int, input().split()))
	w = []
	for i in range(n):
		if u[i] == 0:
			w.append(l[i])
	a = [0] * n
	p = 0
	p1 = 0
	t = 0
	s = SegmentTree(a)
	for i in range(n):
		if u[i] == 0:
			a[i] = w[t]
			t += 1
		else:
			a[i] = l[i]
		if s.query(0, i) < 0:
			p = max(p, i)
		s.__setitem__(i, a[i])
	w.sort(reverse=True)
	t = 0
	a1 = [0] * n
	s = SegmentTree(a1)
	for i in range(n):
		if u[i] == 0:
			a1[i] = w[t]
			t += 1
		else:
			a1[i] = l[i]
		if s.query(0, i) < 0:
			p1 = max(p1, i)
		s.__setitem__(i, a1[i])
	if p1 < p:
		print(*a1)
	else:
		print(*a)


import math
for i in range(int(input())):
	n = int(input())
	arr = [int(item) for item in input().split()]
	brr = [int(item) for item in input().split()]
	crr = []
	drr = []
	for i in range(n):
		if brr[i] == 0:
			crr.append(arr[i])
	crr.sort()
	i = len(crr) - 1
	k = 0
	while k < n:
		if brr[k] == 0:
			arr[k] = crr[i]
			i = i - 1
		k = k + 1
	print(*arr)


for _ in range(int(input())):
	a = int(input())
	b = list(map(int, input().split()))
	c = list(map(int, input().split()))
	v = []
	for i in range(a):
		if c[i] == 0:
			v.append(b[i])
	v.sort()
	for i in range(a):
		if c[i] == 1:
			print(b[i], end=' ')
		else:
			print(v.pop(), end=' ')
	print()


t = int(input())
a = []
b = []
for i in range(t):
	x = int(input())
	x1 = list(map(int, input().split()))
	x2 = list(map(int, input().split()))
	a.append(x1)
	b.append(x2)
ans = [[] for i in range(t)]
for i in range(t):
	v = []
	w = len(a[i])
	for j in range(w):
		if b[i][j] == 0:
			v.append(a[i][j])
	v.sort(reverse=True)
	cnt = 0
	for j in range(w):
		if b[i][j] == 0:
			ans[i].append(v[cnt])
			cnt += 1
		else:
			ans[i].append(a[i][j])
for i in range(t):
	print(*ans[i])


t = int(input())
for l in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	ar = []
	for i in range(len(a)):
		if b[i] == 0:
			ar.append(a[i])
	ar.sort(reverse=True)
	d = 0
	for j in range(n):
		if b[j] == 0:
			a[j] = ar[d]
			d += 1
	a = list(map(str, a))
	print(' '.join(a))


import sys
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, sys.stdin.readline().split()))
	fixer = list(map(int, sys.stdin.readline().split()))
	ans = [0.5 for i in range(n)]
	usable = []
	for i in range(n):
		if fixer[i] == 0:
			usable.append(arr[i])
		else:
			ans[i] = arr[i]
	usable.sort()
	for i in range(n):
		if ans[i] == 0.5:
			m = usable.pop()
			ans[i] = m
	print(' '.join(map(str, ans)))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	lock = list(map(int, input().split()))
	temp = []
	for i in range(n):
		if lock[i] == 0:
			temp.append(l[i])
	temp.sort(reverse=True)
	j = 0
	for i in range(n):
		if lock[i] == 0:
			l[i] = temp[j]
			j += 1
	print(*l)


I = input
for _ in [0] * int(I()):
	I()
	(*a,) = map(int, I().split())
	b = I()[::2]
	c = sorted((x for (x, y) in zip(a, b) if '1' > y))
	i = k = l = s = 0
	j = -1
	for x in b:
		if '1' > x:
			a[i] = c[j]
			j -= 1
		i += 1
	for x in a:
		l += 1
		s += x
		k = (k, l)[l < 0]
	print(*a)


t = int(input())
for i in range(t):
	n = int(input())
	lstn = list(map(int, input().split()))
	lstl = list(map(int, input().split()))
	lst1 = []
	for h in range(n):
		if lstl[h] == 0:
			lst1.append(lstn[h])
	lst1.sort(reverse=True)
	lsta = []
	c = 0
	for f in range(n):
		if lstl[f] == 1:
			lsta.append(lstn[f])
		else:
			lsta.append(lst1[c])
			c += 1
	lst2 = [lsta[0]]
	k = -1
	for g in range(1, n):
		if lst2[-1] < 0:
			k = g - 1
			break
		else:
			lst2.append(lst2[-1] + lsta[g])
	if k == -1:
		k = 0
		print(*lsta)
	else:
		print(*lsta)


import sys
readline = sys.stdin.readline
INF = 1 << 60

def read_int():
	return int(readline())

def read_int_n():
	return list(map(int, readline().split()))

def read_float():
	return float(readline())

def read_float_n():
	return list(map(float, readline().split()))

def read_str():
	return readline().strip()

def read_str_n():
	return readline().strip().split()

def ep(*args):
	print(*args, file=sys.stderr)

def mt(f):
	import time

	def wrap(*args, **kwargs):
		s = time.perf_counter()
		ret = f(*args, **kwargs)
		e = time.perf_counter()
		ep(e - s, 'sec')
		return ret
	return wrap
from collections import deque

class Bisect:

	def __init__(self, func):
		self.__func = func

	def bisect_left(self, x, lo, hi):
		while lo < hi:
			mid = (lo + hi) // 2
			if self.__func(mid) < x:
				lo = mid + 1
			else:
				hi = mid
		return lo

	def bisect_right(self, x, lo, hi):
		while lo < hi:
			mid = (lo + hi) // 2
			if x < self.__func(mid):
				hi = mid
			else:
				lo = mid + 1
		return lo

def slv(N, A, L):
	ai = [(a, i) for (i, a) in enumerate(A)]
	locked = deque([aa for (l, aa) in zip(L, ai) if l == 1])
	unlocked = [aa for (l, aa) in zip(L, ai) if l == 0]
	unlocked.sort(reverse=True)
	unlocked = deque(unlocked)
	B = []
	while locked or unlocked:
		if locked and len(B) == locked[0][1]:
			(b, _) = locked.popleft()
			B.append(b)
		else:
			(b, _) = unlocked.popleft()
			B.append(b)
	return B

def main():
	for _ in range(read_int()):
		N = read_int()
		A = read_int_n()
		L = read_int_n()
		print(*slv(N, A, L))
main()


def f(L, b, n):
	temp = sorted([L[i] for i in range(n) if b[i] == 0], reverse=True)
	k = 0
	for i in range(n):
		if b[i] == 0:
			L[i] = temp[k]
			k += 1
	return ' '.join(map(str, L))
for _ in range(int(input().strip())):
	n = int(input().strip())
	L = list(map(int, input().strip().split()))
	b = list(map(int, input().strip().split()))
	print(f(L, b, n))


for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	status = list(map(int, input().split()))
	if sum(arr) < 0:
		print(*arr)
	else:
		ul = []
		for i in range(len(arr)):
			if status[i] == 0:
				ul.append(arr[i])
		ul.sort()
		ul.reverse()
		for i in range(len(arr)):
			if status[i] == 0:
				arr[i] = ul.pop(0)
		print(*arr)


from sys import stdin
import math
input = stdin.readline
T = int(input())
for t in range(1, T + 1):
	n = int(input())
	_input = list(map(int, input().split()))
	_idx = list(map(int, input().split()))
	ans = []
	for i in range(len(_input)):
		if _idx[i] == 0:
			ans.append(_input[i])
	if len(ans) == 0:
		for i in _input:
			print(i, end=' ')
		print()
		continue
	else:
		ans.sort(reverse=True)
		idx = 0
		for i in range(len(_input)):
			if _idx[i] == 0:
				_input[i] = ans[idx]
				idx += 1
			if idx == len(ans):
				break
		for i in _input:
			print(i, end=' ')
		print()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	ans = [0] * n
	store = []
	for i in range(0, len(a)):
		if b[i] == 1:
			ans[i] = a[i]
	for i in range(0, len(a)):
		if b[i] == 0:
			store.append(a[i])
	store.sort(reverse=True)
	j = 0
	for i in range(0, len(a)):
		if b[i] == 0:
			ans[i] = store[j]
			j += 1
	print(*ans)


t = int(input())
for w in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	x = []
	y = []
	for i in range(len(a)):
		if l[i] == 0:
			x.append(a[i])
		else:
			y.append(a[i])
	x.sort()
	x = x[::-1]
	a = []
	for e in l:
		if e == 0:
			a.append(x[0])
			x.pop(0)
		else:
			a.append(y[0])
			y.pop(0)
	print(' '.join((str(x) for x in a)))


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	A = list(map(int, input().strip().split(' ')))
	B = list(map(int, input().strip().split(' ')))
	C = []
	for i in range(n):
		if B[i] == 0:
			C.append(A[i])
	C.sort()
	for i in range(n):
		if B[i] == 0:
			A[i] = C[-1]
			C.remove(C[-1])
	print(*A)


from sys import stdin, stdout

def main():
	t = int(stdin.readline())
	outputs = []
	for __ in range(t):
		output = []
		arr = []
		n = int(stdin.readline())
		a = [int(num) for num in stdin.readline().split()]
		l = [int(num) for num in stdin.readline().split()]
		for i in range(n):
			if not l[i]:
				arr.append(a[i])
			else:
				continue
		arr.sort()
		for i in range(n):
			if not l[i]:
				output.append(f'{arr.pop()}')
			else:
				output.append(f'{a[i]}')
		outputs.append(' '.join(output) + '\n')
	for output in outputs:
		stdout.write(output)
main()


from functools import reduce
import os
import sys
from collections import *
from math import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def arr():
	return [int(i) for i in input().split()]

def sarr():
	return [int(i) for i in input()]

def starr():
	return [str(x) for x in input().split()]

def inn():
	return int(input())

def svalue():
	return tuple(map(str, input().split()))

def parr():
	return [value() for i in range(n)]
mo = 1000000007
for _ in range(inn()):
	n = inn()
	a = arr()
	l = arr()
	d = defaultdict(int)
	nsum = 0
	temp = []
	for i in range(n):
		if l[i] == 1:
			d[i] += 1
		else:
			temp += [a[i]]
		if a[i] < 0:
			nsum += a[i]
	if sum(a) - nsum < abs(nsum):
		temp.sort()
		ans = []
		j = 0
		for i in range(n):
			if i in d:
				ans += [a[i]]
			else:
				ans += [temp[j]]
				j += 1
	else:
		temp.sort(reverse=True)
		j = 0
		ans = []
		for i in range(n):
			if i in d:
				ans += [a[i]]
			else:
				ans += [temp[j]]
				j += 1
	print(*ans)


for t in range(int(input())):
	n = int(input())
	numbers = list(map(int, input().split()))
	locked = list(map(int, input().split()))
	unlocked = []
	for i in range(n):
		if locked[i] == 0:
			unlocked.append(numbers[i])
	unlocked.sort(reverse=True)
	for i in range(n):
		if locked[i] == 0:
			ele = unlocked.pop(0)
			numbers[i] = ele
	print(*numbers)


import math
t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	locked = list(map(int, input().split()))
	unlocked = [(a[i], i) for i in range(0, n) if locked[i] == 0]
	unlocked.sort(reverse=True, key=lambda val: val[0])
	poss = sorted(unlocked, key=lambda val: val[1])
	for i in range(0, len(unlocked)):
		val = unlocked[i][0]
		pos = poss[i][1]
		a[pos] = val
	print(' '.join((str(val) for val in a)))
	t -= 1


import math
import collections
from sys import stdin, stdout, setrecursionlimit
from bisect import bisect_left as bsl
from bisect import bisect_right as bsr
import heapq as hq
setrecursionlimit(2 ** 20)
t = 1
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().rstrip().split()))
	l = list(map(int, stdin.readline().rstrip().split()))
	d = {}
	ng = []
	pos = []
	for i in range(n):
		if l[i] == 1:
			d[i] = True
		elif a[i] <= 0:
			ng.append(a[i])
		else:
			pos.append(a[i])
	ng.sort(reverse=True)
	pos.sort(reverse=True)
	nl = [i for i in pos]
	for j in ng:
		nl.append(j)
	cnt = 0
	for i in range(n):
		if i not in d:
			a[i] = nl[cnt]
			cnt += 1
	print(*a)


import math, sys
from sys import stdin, stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda : int(input())
li = lambda : list(map(int, input().split()))

def solve():
	n = I()
	a = li()
	b = li()
	c = []
	ans = [0] * n
	for i in range(n):
		if b[i]:
			ans[i] = a[i]
		else:
			c.append(a[i])
	c.sort(reverse=True)
	j = 0
	for i in range(n):
		if b[i]:
			continue
		ans[i] = c[j]
		j += 1
	print(*ans)
for _ in range(I()):
	solve()


from math import ceil
from decimal import *
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	l = []
	for i in range(n):
		if b[i] == 0:
			l.append(a[i])
	l.sort(reverse=True)
	k = 0
	for i in range(n):
		if b[i] == 0:
			a[i] = l[k]
			k += 1
	s = 0
	print(*a)


import sys, os, io
input = sys.stdin.readline
T = int(input())
ans = [0] * T
for t in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	L = list(map(int, input().split()))
	B = [A[i] for i in range(N) if L[i] == 0]
	B.sort()
	ans[t] = []
	for i in range(N):
		if L[i] == 0:
			ans[t].append(B.pop())
		else:
			ans[t].append(A[i])
for a in ans:
	print(*a)


for hajsdkhsajdh in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	ol = []
	for i in range(n):
		if l[i] == 0:
			ol.append(a[i])
	ol.sort(reverse=True)
	y = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = ol[y]
			y += 1
		print(a[i], end=' ')
	print()


def find_ans(n):
	s = list(map(int, input().split()))
	ls = input().split()
	do = [s[i] for i in range(n) if ls[i] == '0']
	do.sort()
	do.reverse()
	ans = []
	j = 0
	for i in range(n):
		if ls[i] == '1':
			ans.append(s[i])
		else:
			ans.append(do[j])
			j += 1
	print(*ans)
for _ in range(int(input())):
	n = int(input())
	find_ans(n)


from sys import stdin, stdout
import math, sys, heapq
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
from os import path
import random
import bisect as bi

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

	def I():
		return int(stdin.readline())

	def In():
		return map(int, stdin.readline().split())

def dict(a):
	d = {}
	for x in a:
		if d.get(x, -1) != -1:
			d[x] += 1
		else:
			d[x] = 1
	return d

def find_gt(a, x):
	i = bi.bisect_left(a, x)
	if i != len(a):
		return i
	else:
		return -1

def main():
	try:
		n = I()
		l = list(In())
		locked = list(In())
		temp = []
		for x in range(n):
			if locked[x] == 0:
				temp.append(l[x])
		if len(temp) > 0:
			temp.sort(reverse=True)
		j = 0
		for x in range(n):
			if locked[x] == 0:
				l[x] = temp[j]
				j += 1
		print(*l)
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(I()):
	main()


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	a = list(map(int, input().split()))
	u = []
	res = []
	for i in range(n):
		if a[i] == 0:
			u.append(l[i])
	u.sort()
	for i in range(n):
		if a[i] == 1:
			res.append(l[i])
		else:
			z = u.pop()
			res.append(z)
	print(*res)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	s = []
	out = [10 ** 6] * n
	for i in range(n):
		if l[i]:
			out[i] = a[i]
		else:
			s.append(a[i])
	s.sort()
	for i in range(n):
		if out[i] == 10 ** 6:
			out[i] = s.pop()
	print(' '.join(map(str, out)))


t = int(input())
ans = []
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	fu = list(map(int, input().split()))
	if sum(fu) == n:
		ans.append(l)
	else:
		q = []
		ps = True
		for i in range(n):
			if l[i] < 0:
				ps = False
			if fu[i] == 0:
				q.append(l[i])
		if ps == True:
			ans.append(l)
		else:
			q.sort(reverse=True)
			j = 0
			for i in range(n):
				if fu[i] == 0:
					l[i] = q[j]
					j += 1
			ans.append(l)
for i in ans:
	for j in i:
		print(j, end=' ')
	print()


def inp():
	return int(input().strip())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def iss(s):
	t = s[::-1]
	return t == s
for _ in range(int(input())):
	n = inp()
	arr = inlt()
	b = inlt()
	unlocked = []
	for i in range(n):
		if b[i] == 0:
			unlocked.append(arr[i])
	unlocked.sort()
	unlocked.reverse()
	k = 0
	for i in range(n):
		if b[i] == 0:
			arr[i] = unlocked[k]
			k += 1
	print(*arr)


t = int(input())
while t > 0:
	n = int(input())
	x = [int(i) for i in input().split()]
	y = [int(i) for i in input().split()]
	dum = []
	for i in range(len(y)):
		if y[i] == 0:
			dum.append(x[i])
	dum.sort(reverse=True)
	c = 0
	for i in range(len(y)):
		if y[i] == 0:
			x[i] = dum[c]
			c += 1
	print(*x)
	t -= 1


from collections import deque
t = int(input())
while t:
	d = deque()
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	for x in range(n):
		if not l[x]:
			d.append(a[x])
	d = sorted(d, reverse=True)
	y = 0
	for x in range(n):
		if l[x]:
			print(a[x], end=' ')
		else:
			print(d[y], end=' ')
			y += 1
	print()
	t -= 1


import math, sys
from collections import defaultdict, Counter, deque
from functools import reduce
INF = float('inf')
MOD = int(1000000000.0) + 7

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def isPrime(n):
	if n <= 1:
		return False
	i = 2
	while i ** 2 <= n:
		if n % i == 0:
			return False
		i += 1
	return True

def primeFactors(n):
	factors = defaultdict(int)
	i = 2
	while i ** 2 <= n:
		while n % i == 0:
			factors[i] += 1
			n //= i
		i += 1
	if n > 1:
		factors[n] += 1
	return factors

def main():
	n = int(input())
	arr = array()
	lock = array()
	neg = []
	pos = []
	for i in range(n):
		if lock[i] == 0:
			if arr[i] < 0:
				neg.append(arr[i])
			else:
				pos.append(arr[i])
	num = sorted(pos, reverse=True) + sorted(neg, reverse=True)
	j = 0
	for i in range(n):
		if not lock[i]:
			arr[i] = num[j]
			j += 1
	print(*arr)

class DSU:

	def __init__(self, n):
		self.p = [i for i in range(n + 1)]
		self.size = [1 for i in range(n + 1)]
		self.minmax = [[i, i] for i in range(n + 1)]
		self.n = n

	def find(self, node):
		if self.p[node] != node:
			self.p[node] = self.find(self.p[node])
		return self.p[node]

	def union(self, source, dest):
		source = self.find(source)
		dest = self.find(dest)
		if source != dest:
			if self.size[source] < self.size[dest]:
				(source, dest) = (dest, source)
			self.p[dest] = source
			self.size[source] += self.size[dest]
			mini = min(self.minmax[source][0], self.minmax[dest][0])
			maxi = max(self.minmax[source][1], self.minmax[dest][1])
			self.minmax[source] = [mini, maxi]

def input():
	return sys.stdin.readline().rstrip('\n')

def print(*args, sep=' ', end='\n'):
	first = True
	for arg in args:
		if not first:
			sys.stdout.write(sep)
		sys.stdout.write(str(arg))
		first = False
	sys.stdout.write(end)

def vars():
	return map(int, input().split())

def array():
	return list(map(int, input().split()))
t = 1
t = int(input())
for _ in range(t):
	main()


from functools import lru_cache
from sys import stdin, stdout
import sys
from math import *
input = stdin.readline
for __ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	l = list(map(int, input().split()))
	p = []
	for i in range(n):
		if l[i] == 0:
			p.append(ar[i])
	p.sort(reverse=True)
	x = 0
	for i in range(n):
		if l[i] == 0:
			ar[i] = p[x]
			x += 1
	print(*ar)


T = int(input())
for _ in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	locked = []
	unlocked = []
	for i in range(n):
		if b[i] == 0:
			unlocked.append(a[i])
		else:
			locked.append(a[i])
	unlocked.sort(reverse=True)
	i = 0
	j = 0
	ans = ''
	for _ in range(n):
		if b[_] == 0:
			ans += str(unlocked[i]) + ' '
			i += 1
		else:
			ans += str(locked[j]) + ' '
			j += 1
	print(ans)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
import copy
import time
starttime = time.time()
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def L():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
try:
	sys.stdin = open('input.txt', 'r')
except:
	pass
for _ in range(L()[0]):
	n = L()[0]
	A = L()
	B = L()
	x = []
	for i in range(n):
		if B[i] == 0:
			x.append(A[i])
	x.sort(reverse=True)
	j = 0
	for i in range(n):
		if B[i] == 0:
			A[i] = x[j]
			j += 1
	print(*A)
endtime = time.time()


import sys
from math import sqrt, gcd, ceil, log, floor
from bisect import bisect, bisect_left
from collections import defaultdict, Counter, deque
from heapq import heapify, heappush, heappop
input = sys.stdin.readline
read = lambda : list(map(int, input().strip().split()))

def main():
	ans_ = []
	for _ in range(int(input())):
		n = int(input())
		arr = read()
		lok = read()
		tem = [arr[i] for (i, elem) in enumerate(lok) if elem == 0]
		tem.sort(reverse=True)
		ind = 0
		for i in range(n):
			if lok[i] == 0:
				arr[i] = tem[ind]
				ind += 1
		ans_.append(' '.join(map(str, arr)))
	print('\n'.join(ans_))
main()


from sys import stdin, stdout
import math
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	arr = list(map(int, stdin.readline().split()))
	pos = list(map(int, stdin.readline().split()))
	a1 = []
	new = []
	for i in range(n):
		if pos[i] == 0:
			a1.append(arr[i])
	if len(a1) != 0:
		a1.sort(reverse=True)
	j = 0
	maxi = 0
	for i in range(n):
		if pos[i] == 0:
			new.append(a1[j])
			j += 1
		else:
			new.append(arr[i])
	print(' '.join((str(i) for i in new)))


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	inds = list(map(int, input().split()))
	p = sorted([x for (x, v) in zip(l, inds) if v == 0])
	print(*[l[i] if inds[i] else p.pop() for i in range(n)])


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	c = sorted([x for (i, x) in enumerate(a) if b[i] == 0], reverse=True)
	print(*[x if y else c.pop(0) for (x, y) in zip(a, b)])


t = int(input())

def rearrange(n, arr, locked):
	temp = []
	for i in range(n):
		if locked[i] == 0:
			temp.append(arr[i])
	temp.sort()
	for i in range(n):
		if locked[i] == 0:
			arr[i] = temp.pop()
	return arr
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	locked = list(map(int, input().split()))
	Ans = rearrange(n, arr, locked)
	print(*Ans)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	lock = list(map(int, input().split()))
	temp = []
	for i in range(n):
		if not lock[i]:
			temp.append(arr[i])
	temp.sort()
	for i in range(n):
		if not lock[i]:
			arr[i] = temp.pop()
	print(*arr)


from math import ceil

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		values = list(map(int, input().split()))
		possible = list(map(int, input().split()))
		my_values = []
		for i in range(n):
			if not possible[i]:
				my_values.append(values[i])
		my_values.sort()
		j = len(my_values) - 1
		for i in range(n):
			if not possible[i]:
				values[i] = my_values[j]
				j -= 1
		print(*values)
main()


import math
import os
import random
import re
import sys
from collections import defaultdict, Counter, deque
import heapq
from functools import lru_cache
from itertools import accumulate, chain, combinations, permutations, groupby, zip_longest
from math import floor, ceil, sqrt

def solve(arr, locked):
	indices = []
	for (i, v) in enumerate(locked):
		if not v:
			indices += (i,)
	unlocked = [arr[i] for i in indices]
	unlocked.sort(reverse=1)
	j = 0
	for i in indices:
		arr[i] = unlocked[j]
		j += 1
	return arr
t = int(input())
res = []
for _ in range(t):
	t = int(input())
	arr = list(map(int, input().split()))
	locked = list(map(int, input().split()))
	res += (solve(arr, locked),)
for arr in res:
	print(' '.join(map(str, arr)))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	lock = list(map(int, input().split()))
	ans = []
	hold = []
	for i in range(n):
		if lock[i] == 0:
			hold.append(arr[i])
	hold.sort(reverse=True)
	k = 0
	for i in range(len(arr)):
		if lock[i] == 1:
			ans.append(arr[i])
		else:
			ans.append(hold[k])
			k += 1
	print(*ans)


import itertools
import math
import sys
from collections import defaultdict

def stdinWrapper():
	data = '5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1\n\n\n\n\n'
	for line in data.split('\n'):
		yield line
if '--debug' not in sys.argv:

	def stdinWrapper():
		while True:
			yield input()
inputs = stdinWrapper()

def inputWrapper():
	return next(inputs)

def getType(_type):
	return _type(inputWrapper())

def getArray(_type):
	return [_type(x) for x in inputWrapper().split()]

def solve(a, locked):

	def pref_sum(data):
		res = [data[0]]
		for i in range(1, len(data)):
			res.append(res[i - 1] + data[i])
		return res

	def k(data):
		data = pref_sum(data)
		for i in reversed(range(len(data))):
			if data[i] < 0:
				return i + 1
		return 0
	unlocked = []
	for i in range(len(a)):
		if not locked[i]:
			unlocked.append(a[i])
	unlocked_sorted = list(reversed(sorted(unlocked)))
	result = []
	for i in range(len(a)):
		if locked[i]:
			result.append(a[i])
		else:
			result.append(unlocked_sorted.pop(0))
	return ' '.join([str(x) for x in result])
t = getType(int)
for _ in range(t):
	n = getType(int)
	a = getArray(int)
	locked = [bool(x) for x in getArray(int)]
	print(solve(a, locked))


for _ in range(int(input())):
	(n, a, l, s) = (int(input()), list(map(int, input().split())), list(map(int, input().split())), [])
	for i in range(n):
		if not l[i]:
			s.append(a[i])
	s.sort()
	for i in range(n):
		if not l[i]:
			a[i] = s.pop()
	print(*a)


import os, io, sys, time, collections, math, pprint, itertools as it, operator as op, bisect as bs, functools as fn
(maxx, localsys, mod) = (float('inf'), 0, int(1000000000.0 + 7))
nCr = lambda n, r: reduce(mul, range(n - r + 1, n + 1), 1) // factorial(r)
ceil = lambda n, x: (n + x - 1) // x
(osi, oso) = ('/home/priyanshu/Documents/cp/input.txt', '/home/priyanshu/Documents/cp/output.txt')
if os.path.exists(osi):
	sys.stdin = open(osi, 'r')
	sys.stdout = open(oso, 'w')
input = sys.stdin.readline

def maps():
	return map(int, input().split())
for _ in range(int(input())):
	n = int(input())
	a = list(maps())
	l = list(maps())
	idx = 0
	l2 = []
	ans = [0] * n
	for i in range(n):
		if l[i]:
			if a[i] < 0:
				idx = i
			ans[i] = a[i]
		else:
			l2.append(a[i])
	l2.sort()
	for j in range(idx):
		if not l[j]:
			l[j] = 1
			ans[j] = l2.pop()
	for i in range(n):
		if not l[i]:
			ans[i] = l2.pop()
	print(*ans)


import sys
import string
from heapq import *
from bisect import *
from collections import Counter, defaultdict
from math import fsum, sqrt, gcd, ceil, factorial
from operator import *
from itertools import accumulate
inf = float('inf')
flush = lambda : sys.stdout.flush
comb = lambda x, y: factorial(x) // factorial(y) // factorial(x - y)
en = lambda x: list(enumerate(x))
ip = lambda : input()
ii = lambda : int(input())
r = lambda : map(int, input().split())
rr = lambda : list(r())

def solve():
	n = ii()
	arr = rr()
	brr = rr()
	crr = [i for (i, j) in zip(arr, brr) if not j]
	crr.sort(reverse=True)
	k = 0
	for i in range(n):
		if not brr[i]:
			arr[i] = crr[k]
			k += 1
	print(*arr)
for _ in range(ii()):
	solve()


def solve(arr, locks, n):
	unlocks = [arr[i] for i in range(n) if locks[i] == 0]
	unlocks.sort()
	(i, j) = (n - 1, 0)
	while i >= 0:
		if locks[i] == 0:
			arr[i] = unlocks[j]
			j += 1
		i -= 1
for case in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	locks = list(map(int, input().split()))
	solve(arr, locks, n)
	print(*arr)


import sys
import math
import collections
import operator as op
from collections import deque
from math import gcd, inf, sqrt, pi, cos, sin, ceil, log2
from bisect import bisect_right, bisect_left
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2 ** 20)

def factorial(n):
	if n == 0:
		return 1
	return n * factorial(n - 1)

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def prime_factors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return list(factors)

def sumDigits(no):
	return 0 if no == 0 else int(no % 10) + sumDigits(int(no / 10))
MOD = 1000000007
PMOD = 998244353
N = 10 ** 9
T = 1
T = int(stdin.readline())
for _ in range(T):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().rstrip().split()))
	l = list(map(int, stdin.readline().rstrip().split()))
	if [1] * n == l:
		print(*a)
		continue
	arr_i = []
	p = []
	for i in range(n):
		if l[i] == 0:
			arr_i.append(a[i])
	arr_i.sort(reverse=True)
	for i in range(n):
		if l[i] == 0:
			a[i] = arr_i[0]
			arr_i.pop(0)
	print(*a)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	x = []
	y = []
	for i in range(len(b)):
		if b[i] == 0:
			y.append(i)
			x.append(a[i])
	if sum(a) < 0:
		print(*a)
	else:
		x.sort(reverse=True)
		for i in range(len(y)):
			a[y[i]] = x[i]
		print(*a)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	b = []
	for i in range(n):
		if l[i] == 0:
			b.append(a[i])
	b.sort()
	j = len(b) - 1
	for i in range(n):
		if l[i] == 0:
			a[i] = b[j]
			j -= 1
	for i in a:
		print(i, end=' ')
	print()


import sys
from copy import *
from bisect import *

def eprint(*args):
	print(*args, file=sys.stderr)
zz = 1
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')
di = [[-1, 0], [1, 0], [0, 1], [0, -1]]

def inc(d, c, x=1):
	d[c] = d[c] + x if c in d else x

def bo(i):
	return ord(i) - ord('A')

def li():
	return [int(xx) for xx in input().split()]

def fli():
	return [float(x) for x in input().split()]

def comp(a, b):
	if a > b:
		return 2
	return 2 if a == b else 0

def gi():
	return [xx for xx in input().split()]

def fi():
	return int(input())

def pro(a):
	return reduce(lambda a, b: a * b, a)

def swap(a, i, j):
	(a[i], a[j]) = (a[j], a[i])

def si():
	return list(input().rstrip())

def mi():
	return map(int, input().split())

def gh():
	sys.stdout.flush()

def isvalid(i, j):
	return 0 <= i < n and 0 <= j < m and (a[i][j] != '.')

def bo(i):
	return ord(i) - ord('a')

def graph(n, m):
	for i in range(m):
		(x, y) = mi()
		a[x].append(y)
		a[y].append(x)
t = fi()
while t > 0:
	t -= 1
	n = fi()
	a = li()
	l = li()
	d = {}
	v = []
	for i in range(n):
		if l[i] == 0:
			d[i] = 1
			v.append(a[i])
	v.sort(reverse=True)
	if len(v) == 0:
		print(*a)
		continue
	pre = [v[0]]
	for i in range(1, len(v)):
		pre.append(pre[-1] + v[i])
	pre.append(0)
	p = 0
	c = 0
	ans = 0
	for i in range(n):
		if l[i] == 0:
			if pre[p] + c < 0:
				ans = i + 1
			p += 1
		else:
			c += a[i]
	s = 0
	e = len(v) - 1
	for i in d:
		a[i] = v[s]
		s += 1
	print(*a)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	lock_un = list(map(int, input().split()))
	max_ = float('inf')
	a = []
	for i in range(len(l)):
		if lock_un[i] == 0:
			a.append(l[i])
			l[i] = max_
	a.sort(reverse=True)
	x = 0
	for i in range(len(l)):
		if l[i] == max_:
			l[i] = a[x]
			x += 1
	print(*l)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	locks = list(map(int, input().split()))
	lp = []
	for j in range(n):
		if not locks[j]:
			lp.append(l[j])
	lp.sort(reverse=True)
	cnt = 0
	for j in range(n):
		if locks[j] == 0:
			l[j] = lp[cnt]
			cnt += 1
		l[j] = str(l[j])
	print(' '.join(l))


import sys
from sys import stdin
import math
import fractions

def iinput():
	return int(input())

def minput():
	return map(int, input().split())

def linput():
	return list(map(int, input().split()))

def fiinput():
	return int(stdin.readline())

def fminput():
	return map(int, stdin.readline().strip().split())

def flinput():
	return list(map(int, stdin.readline().strip().split()))
for _ in range(iinput()):
	n = iinput()
	list1 = linput()
	list2 = linput()
	list3 = []
	for i in range(n):
		if list2[i] == 0:
			list3.append(list1[i])
	list3.sort()
	for i in range(n):
		if list2[i] == 0:
			list1[i] = list3.pop()
	print(*list1)


t = int(input())
for p in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	unlocked = []
	for i in range(n):
		if l[i] == 0:
			unlocked.append(a[i])
	m = len(unlocked)
	unlocked.sort(reverse=True)
	j = 0
	for i in range(m):
		while l[j] == 1 and j < n:
			j += 1
		if l[j] == 0:
			a[j] = unlocked[i]
			j += 1
	print(*a)


import sys
input = sys.stdin.readline

def print(val):
	sys.stdout.write(str(val) + '\n')

def prog():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		b = list(map(int, input().split()))
		unlocked_el = []
		unlocked = []
		locked = []
		output = [0] * n
		for i in range(n):
			if b[i]:
				locked.append(i)
			else:
				unlocked.append(i)
				unlocked_el.append(a[i])
		unlocked_el.sort(reverse=True)
		for i in locked:
			output[i] = str(a[i])
		for i in range(len(unlocked)):
			output[unlocked[i]] = str(unlocked_el[i])
		print(' '.join(output))
prog()


t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	L = list(map(int, input().split()))
	P = []
	N = []
	for i in range(n):
		if L[i] == 0:
			if A[i] >= 0:
				P.append(A[i])
			else:
				N.append(A[i])
	P.sort(reverse=True)
	s = sum(A)
	N.sort(reverse=True)
	T = P + N
	T.reverse()
	ans = []
	for i in range(n):
		if L[i] == 1:
			ans.append(A[i])
		else:
			ans.append(T.pop())
	print(*ans)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	l = [int(i) for i in input().split()]
	num_un = 0
	for i in l:
		num_un += i
	num_un = n - num_un
	res_un = [0 for i in range(num_un)]
	act = 0
	for i in range(n):
		if l[i] == 0:
			res_un[act] = a[i]
			act += 1
	res_un = sorted(res_un, reverse=True)
	act = 0
	res = [0 for i in range(n)]
	for i in range(n):
		if l[i] == 1:
			res[i] = a[i]
		else:
			res[i] = res_un[act]
			act += 1
	print(*res)


def vetorCriminoso(numVet, posVet):
	podeMudar = []
	for i in range(len(numVet)):
		if not posVet[i]:
			podeMudar.append(numVet[i])
			numVet[i] = None
	podeMudar.sort()
	for i in range(len(numVet)):
		if numVet[i] == None:
			numVet[i] = podeMudar.pop()
	return numVet
j = int(input())
for i in range(j):
	tam = int(input())
	numVet = list(map(int, input().split()))
	posVet = list(map(int, input().split()))
	numVet = vetorCriminoso(numVet, posVet)
	for x in numVet:
		print(x, end=' ')
	print()


import math
from decimal import *
import random
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	state = list(map(int, input().split()))
	mrk = [0 for i in range(n)]
	ans = []
	for i in range(n):
		if state[i] == 0:
			mx = -int(1000000.0)
			pos = -1
			for j in range(n):
				if mrk[j] == 0 and state[j] == 0:
					if arr[j] > mx:
						pos = int(j)
						mx = int(arr[j])
			mrk[pos] = 1
			ans.append(str(arr[pos]))
		else:
			ans.append(str(arr[i]))
	cnt = 0
	print(' '.join(ans))


import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = []
	d = []
	for i in range(n):
		if b[i] == 0:
			c.append(a[i])
	c.sort(reverse=True)
	j = 0
	for i in range(n):
		if b[i] == 0:
			print(c[j], end=' ')
			j += 1
		else:
			print(a[i], end=' ')
	print()


from sys import stdin, stdout

def input():
	a = stdin.readline()
	if a[-1] == '\n':
		a = a[:-1]
	return a

def print(*argv, end='\n', sep=' '):
	n = len(argv)
	for i in range(n):
		if i == n - 1:
			stdout.write(str(argv[i]))
		else:
			stdout.write(str(argv[i]) + sep)
	stdout.write(end)
mod = 10 ** 9 + 7

def lcm(x, y):
	return x * y / gcd(x, y)

def fact(x, mod=mod):
	ans = 1
	for i in range(1, x + 1):
		ans = ans * i % mod
	return ans

def arr2D(n, m, default=0):
	lst = []
	for i in range(n):
		temp = [default] * m
		lst.append(temp)
	return lst

def sortDictV(x):
	return {k: v for (k, v) in sorted(x.items(), key=lambda item: item[1])}

def kc(ans):
	summ = 0
	ind = 0
	for i in range(len(ans)):
		summ += ans[i]
		if summ < 0:
			ind = i
	return ind

def solve(n, lst, lock):
	lst1 = []
	for i in range(n):
		if lock[i] == 0:
			lst1.append(lst[i])
	lstS = sorted(lst1, reverse=True)
	ans = []
	s = 0
	for i in range(n):
		if lock[i] == 0:
			ans.append(lstS[s])
			s += 1
		else:
			ans.append(lst[i])
	return ' '.join(list(map(str, ans)))
for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	lock = list(map(int, input().split()))
	print(solve(n, lst, lock))


def readis():
	return map(int, input().strip().split())
T = int(input())
while T:
	T -= 1
	n = int(input())
	xs = list(readis())
	ls = list(readis())
	ys = sorted((x for (x, l) in zip(xs, ls) if l == 0), reverse=True)
	i = 0
	for (j, l) in enumerate(ls):
		if l == 0:
			xs[j] = ys[i]
			i += 1
	print(' '.join(map(str, xs)))


testcases = int(input())
for testcase in range(testcases):
	n = int(input())
	temparr = input()
	temparr = temparr.split()
	arr = []
	for i in temparr:
		arr.append(int(i))
	lockarr = []
	temparr = input()
	temparr = temparr.split()
	for i in temparr:
		lockarr.append(int(i))
	closearr = []
	for i in range(n):
		if lockarr[i] == 0:
			closearr.append(arr[i])
	closearr = sorted(closearr)
	closearr = closearr[::-1]
	ans = []
	newans = []
	index = 0
	for i in range(n):
		if lockarr[i] == 0:
			ans.append(str(closearr[index]))
			index += 1
		else:
			ans.append(str(arr[i]))
	for i in range(n):
		newans.append(int(arr[i]))
	nnans = []
	cur = 0
	for i in range(n):
		cur += newans[i]
		nnans.append(cur)
	print(' '.join(ans))


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192
from sys import stderr

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def pow(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res
from functools import reduce

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def gcd(a, b):
	if a == b:
		return a
	while b > 0:
		(a, b) = (b, a % b)
	return a

def solve():
	n = int(inp())
	ar = lis()
	unlocked = lis()
	unlock = []
	for i in range(n):
		if unlocked[i] == 0:
			unlock.append(ar[i])
	unlock.sort()
	p = 0
	ans = []
	for i in range(n):
		if unlocked[i] == 1:
			ans.append(ar[i])
		else:
			ans.append(unlock.pop())
	print(*ans)
testcase(int(inp()))


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	for _ in range(ri()):
		n = ri()
		a = ria()
		b = ria()
		neg = deque([])
		pos = deque([])
		for i in range(n):
			if b[i] == 0:
				if a[i] < 0:
					neg.append(a[i])
				else:
					pos.append(a[i])
		neg = sorted(neg)
		pos = sorted(pos)
		for i in range(n):
			if b[i] == 0:
				if len(pos) > 0:
					a[i] = pos[-1]
					pos.pop()
				else:
					a[i] = neg[-1]
					neg.pop()
		print(*a)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


t = int(input())
for i in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	locks = [int(i) for i in input().split()]
	new_arr = [arr[i] if locks[i] == 1 else float('-inf') for i in range(n)]
	curr = []
	for i in range(n):
		if locks[i] == 0:
			curr.append(arr[i])
	curr = sorted(curr)[::-1]
	j = 0
	for i in range(n):
		if new_arr[i] == float('-inf'):
			new_arr[i] = curr[j]
			j += 1
		else:
			pass
	for j in new_arr:
		print(j, end=' ')
	print()


import math
t = int(input())
for tt in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	l = list(map(int, input().split()))
	temp = []
	for i in range(len(arr)):
		if l[i] == 0:
			temp.append(arr[i])
	temp.sort(reverse=True)
	x = 0
	for i in range(len(arr)):
		if l[i] == 0:
			arr[i] = temp[x]
			x += 1
	for i in range(len(arr)):
		print(arr[i], end=' ')
	print()


def ans(arr, l):
	temp = []
	for i in range(n):
		if l[i] == 0:
			temp.append(arr[i])
			arr[i] = 'X'
	j = 0
	temp.sort(reverse=True)
	for i in range(n):
		if arr[i] == 'X':
			arr[i] = temp[j]
			j += 1
	return arr
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().strip().split()))
	l = list(map(int, input().strip().split()))
	result = ans(a, l)
	print(*result)


t = int(input())
while t:
	t -= 1
	n = int(input())
	l = list(map(int, input().split()))
	ind = list(map(int, input().split()))
	l1 = []
	for i in range(n):
		if ind[i] == 0:
			l1.append(l[i])
	l1.sort(reverse=True)
	j = 0
	for i in range(n):
		if ind[i] == 0:
			l[i] = l1[j]
			j += 1
	for i in l:
		print(i, end=' ')
	print()


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split()))[:n]
	locking_arr = list(map(int, input().strip().split()))[:n]
	unlocked_arr = [arr[i] for i in range(n) if locking_arr[i] == 0]
	unlocked_arr = sorted(unlocked_arr, reverse=True)
	ans = []
	count = 0
	for i in range(n):
		if locking_arr[i] == 0:
			ans.append(unlocked_arr[count])
			count += 1
		else:
			ans.append(arr[i])
	for i in ans:
		print(i, end=' ')
	print()


from sys import stdout
from sys import stdin

def get():
	return stdin.readline().strip()

def getf():
	return [int(i) for i in get().split()]

def put(a, end='\n'):
	stdout.write(str(a) + end)

def putf(a, sep=' ', end='\n'):
	stdout.write(sep.join([str(i) for i in a]) + end)

def solve(a, n, l):
	p = []
	for i in range(n):
		if l[i] == 0:
			p += [a[i]]
	p = sorted(p, reverse=False)
	for i in range(n):
		if l[i] == 0:
			a[i] = p.pop()
	return a

def main():
	t = int(get())
	ans = []
	for i in range(t):
		n = int(get())
		a = getf()
		l = getf()
		putf(solve(a, n, l))
main()


def solve(arr, pos, n):
	if sum(pos) >= n - 1:
		return arr
	val = []
	for i in range(n):
		if pos[i] == 0:
			val.append(arr[i])
	val.sort()
	for i in range(n):
		if pos[i] == 0:
			arr[i] = val.pop()
	return arr
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	pos = list(map(int, input().split()))
	r = solve(arr, pos, n)
	print(*r, sep=' ')


import math
import os
import random
import re
import string
from sys import *
from collections import *

def bs(arr, x, l, r):
	if l <= r:
		mid = (r + l) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] > x:
			return bs(arr, x, l, mid - 1)
		else:
			return bs(arr, x, mid + 1, r)
	else:
		return -1

def solve():
	for _ in range(int(input())):
		n = int(input())
		l = list(map(int, stdin.readline().strip().split()))
		pos = list(map(int, stdin.readline().strip().split()))
		if pos.count(1) == n:
			print(*l)
			continue
		r = []
		for i in range(n):
			if pos[i] == 0:
				r.append(l[i])
		r.sort(reverse=True)
		for i in range(n):
			if pos[i] == 0:
				l[i] = r[0]
				r.pop(0)
			if len(r) == 0:
				break
		print(*l)
solve()


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	lock_arr = list(map(int, input().split()))
	switch_arr = []
	for (idx, j) in enumerate(lock_arr):
		if j == 0:
			switch_arr.append(arr[idx])
	switch_arr = sorted(switch_arr, reverse=True)
	l = 0
	for (idx, g) in enumerate(lock_arr):
		if g == 0:
			arr[idx] = switch_arr[l]
			l += 1
	print(' '.join(map(str, arr)))


import math
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	f = list(map(int, input().split()))
	d = []
	for i in range(n):
		if f[i] == 0:
			d.append(a[i])
	d.sort()
	d.reverse()
	k = 0
	for i in range(n):
		if f[i] == 0:
			a[i] = d[k]
			k += 1
	print(*a)


t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	l1 = input().split()
	l2 = input().split()
	a = []
	l = []
	for i in range(len(l1)):
		l.append(int(l2[i]))
		if l[i] == 0:
			a.append(int(l1[i]))
	a.sort(reverse=True)
	j = 0
	for i in range(n):
		if l[i] == 0:
			print(a[j], end=' ')
			j = j + 1
		else:
			print(int(l1[i]), end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	fixed = list(map(int, input().split()))
	free_elems = []
	for i in range(n):
		if fixed[i] == 0:
			free_elems.append(arr[i])
	free_elems = sorted(free_elems, reverse=True)
	final_arr = []
	for i in range(n):
		if fixed[i] == 0:
			final_arr.append(free_elems[0])
			free_elems.pop(0)
		else:
			final_arr.append(arr[i])
	print(' '.join(list(map(str, final_arr))))


for t in range(int(input())):
	n = int(input())
	num = list(map(int, input().split()))
	lock = list(map(int, input().split()))
	temp = []
	index = []
	for i in range(n):
		if lock[i] == 0:
			temp.append(num[i])
			index.append(i)
	temp.sort()
	for i in index:
		num[i] = temp.pop()
	print(*num)


for _ in range(int(input())):
	n = int(input())
	k = list(map(int, input().split()))
	l = list(map(int, input().split()))
	x = [k[i] for i in range(n) if l[i] == 0]
	if len(x) > 0:
		x.sort(reverse=True)
	t = 0
	for i in range(n):
		if l[i] == 0:
			k[i] = x[t]
			t += 1
	print(*k)


t = int(input())
while t > 0:
	n = int(input())
	arr = [int(x) for x in input().split()]
	pos = [int(x) for x in input().split()]
	u = []
	for i in range(n):
		if pos[i] == 0:
			u.append(arr[i])
	u.sort(reverse=True)
	res = []
	j = 0
	for i in range(n):
		if pos[i] == 1:
			res.append(str(arr[i]))
		else:
			res.append(str(u[j]))
			j += 1
	print(' '.join(res))
	t -= 1


t = int(input())
for case in range(t):
	N = int(input())
	A = [int(n) for n in input().split()]
	pos = [int(n) for n in input().split()]
	unlocks = []
	for i in range(N):
		if pos[i] == 0:
			unlocks.append(A[i])
	unlocks.sort(reverse=True)
	k = 0
	for i in range(N):
		if pos[i] == 0:
			A[i] = unlocks[k]
			k += 1
	A = [str(n) for n in A]
	res = ' '.join(A)
	print(res)


input = __import__('sys').stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	blocked = []
	binds = []
	b = list(map(int, input().split()))
	for i in range(n):
		if not b[i]:
			blocked += (a[i],)
			binds += (i,)
	blocked.sort()
	for i in range(len(binds)):
		a[binds[i]] = blocked[i]
	k1 = -1
	cs = 0
	for i in range(n):
		cs += a[i]
		if cs < 0:
			k1 = i
	ans1 = a.copy()
	blocked.reverse()
	for i in range(len(binds)):
		a[binds[i]] = blocked[i]
	k2 = -1
	cs = 0
	for i in range(n):
		cs += a[i]
		if cs < 0:
			k2 = i
	ans2 = a.copy()
	ans = ans1 if k1 < k2 else ans2
	print(*ans)


t = int(input())
for i in range(t):
	n = int(input())
	nums = [int(temp) for temp in input().split()]
	lock = [int(temp) for temp in input().split()]
	newar = []
	sum = 0
	for i in range(n):
		sum += nums[i]
		if lock[i] == 0:
			newar.append(nums[i])
	if sum >= 0:
		m = len(newar)
		newar.sort(reverse=True)
		j = 0
		for i in range(n):
			if lock[i] == 0:
				nums[i] = newar[j]
				j += 1
	for i in range(n):
		print(nums[i], end=' ')
	print()


i = lambda : [*map(int, input().split())]
for _ in range(i()[0]):
	i()
	l = i()
	a = [j for (j, x) in enumerate(i()) if x < 1]
	for (j, x) in zip(a[::-1], sorted((l[f] for f in a))):
		l[j] = x
	print(*l)


from collections import deque
ansl = []
for _ in range(int(input())):
	n = int(input())
	al = list(map(int, input().split()))
	lockl = list(map(int, input().split()))
	free = []
	locked = []
	for (a, l) in zip(al, lockl):
		if l == 0:
			free.append(a)
		else:
			locked.append(a)
	free.sort(reverse=True)
	fq = deque(free)
	lq = deque(locked)
	ans = []
	for i in range(n):
		if lockl[i] == 0:
			ans.append(fq.popleft())
		else:
			ans.append(lq.popleft())
	ansl.append(ans)
for ans in ansl:
	print(*ans)


for T in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	b = []
	for i in range(len(a)):
		if l[i] != 1:
			b.append(a[i])
	b.sort(reverse=True)
	j = 0
	for i in range(len(a)):
		if l[i] != 1:
			a[i] = b[j]
			j += 1
	for i in a:
		print(i, end=' ')
	print()


for i in range(int(input())):
	len = int(input())
	array = [int(x) for x in input().split()]
	lock = [int(x) for x in input().split()]
	unlock = [array[x] for x in range(len) if not lock[x]]
	unlock.sort(reverse=True)
	j = 0
	for x in range(len):
		if lock[x]:
			print(array[x], end=' ')
		else:
			print(unlock[j], end=' ')
			j += 1
	print()


import math
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	p = list(map(int, input().split()))
	ar = []
	for i in range(n):
		if p[i] != 1:
			ar.append(a[i])
	ar.sort()
	for i in range(n):
		if p[i] == 0:
			a[i] = ar[-1]
			ar.pop()
	print(*a)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	fx = list(map(int, input().split()))
	change = []
	for i in range(n):
		if fx[i] == 0:
			change.append(arr[i])
	change.sort()
	for i in range(n):
		if fx[i] == 1:
			continue
		else:
			arr[i] = change.pop()
	print(*arr)


t = int(input())
for i in range(t):
	totalLen = int(input())
	totalInp = list(map(int, input().split(' ')))
	totalFreeInp = input().split(' ')
	copyArr = []
	for i in range(totalLen):
		if totalFreeInp[i] == '0':
			copyArr.append(totalInp[i])
	copyArr.sort(reverse=True)
	co = 0
	coEnd = len(copyArr) - 1
	co1 = 0
	for i in range(totalLen):
		if totalFreeInp[i] == '0':
			totalInp[i] = copyArr[co]
			co += 1
	for i in range(totalLen):
		print(totalInp[i], end=' ')
	print()


import math
t = int(input())
for q in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	lock = list(map(int, input().split()))
	lis = []
	for i in range(n):
		if lock[i] == 0:
			lis.append(arr[i])
	if len(lis) == 0:
		for i in arr:
			print(i, end=' ')
	else:
		lis.sort()
		for i in range(n):
			if lock[i] == 0:
				arr[i] = lis.pop()
		for i in arr:
			print(i, end=' ')
	print()


import sys
import heapq, functools, collections
import math, random
from collections import Counter, defaultdict

def solve(arr, brr):
	console('----- solving ------')
	movable = []
	for (a, b) in zip(arr, brr):
		if b == 0:
			movable.append(a)
	movable = sorted(movable)
	for i in range(len(arr)):
		if brr[i] == 0:
			arr[i] = movable.pop()
	return arr

def console(*args):
	print('\x1b[36m', *args, '\x1b[0m', file=sys.stderr)
	return
for case_num in range(int(input())):
	k = int(input())
	arr = list(map(int, input().split()))
	brr = list(map(int, input().split()))
	res = solve(arr, brr)
	print(' '.join([str(x) for x in res]))


def sol_args(n, arr, loc):
	sarr = [arr[i] for i in range(n) if loc[i] == 0]
	sarr.sort(reverse=True)
	sidx = 0
	for idx in range(n):
		if loc[idx] == 0:
			arr[idx] = sarr[sidx]
			sidx += 1
	print(' '.join(map(str, arr)))

def main():
	T = int(input())
	for t in range(T):
		n = int(input())
		arr = list(map(int, input().split()))
		loc = list(map(int, input().split()))
		sol_args(n, arr, loc)
main()


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	b = []
	for i in range(n):
		if not l[i]:
			b.append(a[i])
	b.sort()
	k = 0
	for i in range(n):
		if l[i] < 1:
			a[i] = b.pop()
	print(*a)


import sys
import collections
input = sys.stdin.readline
r = input
ir = int
for _ in range(int(input())):
	n = ir(r())
	a = list(map(ir, r().split()))
	l = list(map(ir, r().split()))
	unlocked_el = []
	unlocked = []
	locked = []
	ans = [0] * n
	for i in range(n):
		if l[i]:
			locked += [i]
		else:
			unlocked += [i]
			unlocked_el += [a[i]]
	unlocked_el.sort(reverse=True)
	for i in locked:
		ans[i] = a[i]
	for i in range(len(unlocked)):
		ans[unlocked[i]] = unlocked_el[i]
	print(*ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().strip().split(' ')))
	l = list(map(int, input().strip().split(' ')))
	a1 = []
	a2 = []
	a3 = []
	for j in range(n):
		if l[j] == 0:
			a1.append(a[j])
		else:
			a2.append(a[j])
			a3.append(j)
	a1.sort(reverse=True)
	for j in range(len(a3)):
		a1.insert(a3[j], a2[j])
	for j in range(n):
		print(a1[j])


for _ in range(int(input())):
	n = int(input())
	lock = []
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	for i in range(n):
		if b[i] == 0:
			lock.append(a[i])
	lock.sort(reverse=True)
	ind = 0
	for i in range(n):
		if b[i] == 0:
			a[i] = lock[ind]
			ind += 1
	print(*a)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	lock = list(map(int, input().split()))
	s = []
	for i in range(n):
		if not lock[i]:
			s.append(a[i])
	s.sort()
	s.reverse()
	j = 0
	for i in range(n):
		if not lock[i]:
			a[i] = s[j]
			j += 1
	print(*a)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	loc = list(map(int, input().split()))
	tmp = []
	for i in range(len(arr)):
		if loc[i] == 0:
			tmp.append(arr[i])
	tmp.sort(reverse=True)
	j = 0
	for i in range(len(arr)):
		if loc[i] == 0:
			arr[i] = tmp[j]
			j += 1
	print(*arr)


n = int(input())
for i in range(n):
	count = int(input())
	arr = list(map(int, input().split()))
	blo = list(map(int, input().split()))
	bak = []
	for j in range(count):
		if blo[j] == 0:
			bak.append(arr[j])
	bak.sort()
	for j in range(count):
		if blo[j] == 0:
			arr[j] = bak.pop()
	print(arr[0], end='')
	for j in range(1, count):
		print('', arr[j], end='')
	print()


from sys import stdin

def input():
	return stdin.readline().rstrip()
for _ in range(int(input())):
	L = int(input())
	lockSort = []
	s = list(map(int, input().split()))
	l = list(map(int, input().split()))
	for (i, c) in enumerate(s):
		if l[i] == 0:
			lockSort.append(c)
	lockSort = sorted(lockSort)[::-1]
	cnt = 0
	for (i, c) in enumerate(s):
		if l[i] == 1:
			print(c, end=' ')
		else:
			print(lockSort[cnt], end=' ')
			cnt += 1
	print()


t = int(input())
while t > 0:
	lenth = int(input())
	inp = list(map(int, input().split()))
	val = list(map(int, input().split()))
	temp = []
	pos = []
	for i in range(len(inp)):
		if val[i] == 0:
			temp.append(inp[i])
			pos.append(i)
	temp.sort(reverse=True)
	pre_sum = 0
	ind = 0
	ans = 0
	for i in range(len(inp)):
		if ind < len(pos) and pos[ind] == i:
			inp[i] = temp[ind]
			ind += 1
		print(inp[i], end=' ')
	print()
	t -= 1


for i in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	lock = [int(i) for i in input().split()]
	neg = []
	for i in range(n):
		if lock[i] == 0:
			neg.append(arr[i])
	neg.sort(reverse=True)
	j = 0
	for i in range(len(arr)):
		if lock[i] == 0:
			arr[i] = neg[j]
			j += 1
	print(*arr)


import sys
from collections import defaultdict
name = sys.stdin.read()
jobGrid = []
isJob = True
row = []
grid = name.split('\n')
grid = grid[:-1]
number_of_cases = int(grid[0])
for i in range(number_of_cases):
	l = list(map(int, grid[i * 3 + 2].split()))
	locked = list(map(int, grid[(i + 1) * 3].split()))
	extracted = []
	lockedarray = []
	for j in range(len(l)):
		if locked[j] == 0:
			extracted.append(l[j])
	extracted.sort()
	output = ''
	for k in range(len(l)):
		if locked[k] == 1:
			output += str(l[k])
		else:
			output += str(extracted.pop())
		output += ' '
	print(output.strip())


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	(uln, ulp) = ([], [])
	for i in range(len(a)):
		if a[i] < 0 and l[i] == 0:
			uln.append(a[i])
		if a[i] >= 0 and l[i] == 0:
			ulp.append(a[i])
	uln.sort(reverse=True)
	ulp.sort(reverse=True)
	ul = ulp + uln
	ui = 0
	m = 0
	s = 0
	for i in range(len(a)):
		if l[i] == 0:
			a[i] = ul[ui]
			ui += 1
		s += a[i]
		if s < 0:
			m = max(m, i + 1)
	print(*a)


def solve(n, ar, l):
	temp = []
	for i in range(n):
		if l[i] == 1:
			continue
		temp.append(ar[i])
	temp = sorted(temp, reverse=True)
	ptr = 0
	for i in range(n):
		if l[i] == 1:
			continue
		ar[i] = temp[ptr]
		ptr += 1
	pref = [0] * n
	pref[0] = ar[0]
	ans = 0
	for i in range(1, n):
		pref[i] = pref[i - 1] + ar[i]
	for i in range(n - 1, -1, -1):
		if pref[i] >= 0:
			continue
		else:
			break
	print(' '.join(map(str, ar)))
t = int(input())
for _ in range(t):
	n = int(input())
	ar = list(map(int, input().split()))
	l = list(map(int, input().split()))
	solve(n, ar, l)


t = int(input())
while t:
	n = int(input())
	num1 = list(map(int, input().strip().split()))
	num2 = list(map(int, input().strip().split()))
	num3 = [num1[i] for i in range(n) if num2[i] == 0]
	num4 = [i for i in range(n) if num2[i] == 0]
	num3.sort(reverse=True)
	for i in range(len(num3)):
		num1[num4[i]] = num3[i]
	tot = 0
	ans = 0
	for i in range(len(num1)):
		tot = tot + num1[i]
		if tot < 0:
			ans = i
	for i in range(len(num1) - 1):
		print(num1[i], end=' ')
	print(num1[n - 1])
	t = t - 1


from sys import stdin, stdout

def main():
	t = int(stdin.readline())
	for _ in range(t):
		n = int(stdin.readline())
		a = [int(i) for i in stdin.readline().split()]
		locked = [i == '1' for i in stdin.readline().split()]
		rearrangeable = []
		for i in range(n):
			if not locked[i]:
				rearrangeable.append(a[i])
		rearrangeable.sort()
		out = [0] * n
		for i in range(n):
			if locked[i]:
				out[i] = str(a[i])
			else:
				out[i] = str(rearrangeable[-1])
				del rearrangeable[-1]
		print(' '.join(out))
main()


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	a = list(map(int, input().split()))
	s = [l[i] for i in range(n) if a[i] == 0]
	s.sort(reverse=True)
	j = 0
	for i in range(n):
		if a[i] == 0:
			l[i] = s[j]
			j += 1
	print(' '.join(list(map(str, l))))


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	flags = list(map(int, input().split()))
	temp = []
	c = 0
	for (i, x) in enumerate(flags):
		if x == 1:
			temp.append(arr.pop(i - c))
			c += 1
	arr.sort(reverse=True)
	for (i, x) in enumerate(flags):
		if x == 1:
			arr.insert(i, temp.pop(0))
	for x in arr:
		print(x, end=' ')
	print()


def solve(arr, locks, n, ans):
	unlocked = []
	new = [-1] * n
	unlock_indices = []
	for i in range(n):
		if locks[i] == 0:
			unlocked.append(arr[i])
			unlock_indices.append(i)
		else:
			new[i] = str(arr[i])
	unlocked.sort()
	while unlocked:
		x = unlocked.pop()
		index = unlock_indices.pop(0)
		new[index] = str(x)
	ans.append(new)

def main():
	t = int(input())
	ans = []
	for i in range(t):
		n = int(input())
		arr = list(map(int, input().split()))
		locks = list(map(int, input().split()))
		solve(arr, locks, n, ans)
	for i in ans:
		print(' '.join(i))
main()


import sys
input = sys.stdin.buffer.readline
Q = int(input())
for _ in range(Q):
	N = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	P = []
	Inds = []
	for (i, (a, b)) in enumerate(zip(A, B)):
		if b == 0:
			P.append(a)
			Inds.append(i)
	P.sort(reverse=True)
	for (a, i) in zip(P, Inds):
		A[i] = a
	print(' '.join(map(str, A)))


for _ in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	z = []
	for i in range(n):
		if not y[i]:
			z.append(x[i])
	lenn = len(z)
	if lenn == 0:
		print(*x)
		continue
	z.sort(reverse=True)
	c = 0
	for i in range(n):
		if c < lenn and (not y[i]):
			x[i] = z[c]
			c += 1
	print(*x)


import math
T = int(input())
for _ in range(T):
	n = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	C = []
	for i in range(n):
		if B[i] == 0:
			C.append(A[i])
	C.sort(reverse=True)
	j = 0
	for i in range(n):
		if B[i] == 0:
			A[i] = C[j]
			j = j + 1
	print(*A)


for _ in range(int(input())):
	n = int(input())
	arr1 = list(map(int, input().split()))
	arr2 = list(map(int, input().split()))
	for i in range(n):
		if arr2[i] == 0:
			for j in range(i + 1, n):
				if arr2[j] != 1 and arr1[i] < arr1[j]:
					(arr1[i], arr1[j]) = (arr1[j], arr1[i])
	print(*arr1)


T = int(input())
for _ in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	tmp = []
	for i in range(len(a)):
		if l[i] == 0:
			tmp.append(a[i])
	tmp.sort()
	tmp = tmp[::-1]
	j = 0
	for i in range(len(a)):
		if l[i] == 0:
			a[i] = tmp[j]
			j += 1
	print(*a)


import io, os
R = lambda : map(int, input().split())
for _ in range(int(input())):
	n = int(input())
	a = list(R())
	d = list(R())
	(t, tt) = ([], [])
	for i in range(n):
		if d[i] == 0:
			if a[i] > 0:
				t.append(a[i])
			else:
				tt.append(a[i])
	t.sort()
	tt.sort()
	pre = 0
	for i in range(n):
		if d[i] == 0:
			if len(t) == 0:
				a[i] = tt[-1]
				tt.pop()
			else:
				a[i] = t[-1]
				t.pop()
		pre += a[i]
	s = [a[0]]
	for i in range(1, n):
		s.append(s[-1] + a[i])
	k = 0
	for i in range(1, n):
		if s[i] < 0:
			k = i + 1
	print(*a)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	l = []
	for i in range(n):
		if b[i] == 0:
			l.append(a[i])
	l.sort(reverse=True)
	j = 0
	for i in range(n):
		if b[i] == 0:
			a[i] = l[j]
			j += 1
	for i in a:
		print(i, end=' ')
	print('')


for i in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	li = [int(o) for o in input().split()]
	lst = []
	for i in range(n):
		if li[i] == 0:
			lst.append(l[i])
	lst.sort(reverse=True)
	ind = 0
	for i in range(n):
		if li[i] == 0:
			l[i] = lst[ind]
			ind += 1
		else:
			pass
	print(*l)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	move = []
	for i in range(n):
		if not l[i]:
			move.append(a[i])
	move.sort(reverse=True)
	coc = 0
	for i in range(n):
		if l[i]:
			print(a[i], end=' ')
			coc += 1
		else:
			print(move[i - coc], end=' ')
	print('')


import sys
import math

def II():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def MI():
	return map(int, sys.stdin.readline().split())

def SI():
	return sys.stdin.readline().strip()

def check(a, n):
	s = 0
	ans = 0
	for i in range(n):
		s += a[i]
		if s < 0:
			ans = i
	return ans
t = II()
for q in range(t):
	n = II()
	a = LI()
	l = LI()
	temp = []
	boo = False
	for i in range(n):
		if a[i] < 0 and l[i] == 1:
			boo = True
		if l[i] == 0:
			temp.append(a[i])
	le = 0
	t1 = []
	t2 = []
	for i in range(n):
		tt = sorted(temp[:])
		if l[i] == 1:
			t1.append(a[i])
		else:
			t1.append(tt[le])
			le += 1
	le = 0
	temp.sort(reverse=True)
	for i in range(n):
		if l[i] == 1:
			t2.append(a[i])
		else:
			t2.append(temp[le])
			le += 1
	if check(t1, n) < check(t2, n):
		print(*t1)
	else:
		print(*t2)


from math import *
from bisect import *
from collections import Counter, defaultdict
from sys import stdin, stdout
input = stdin.readline
I = lambda : int(input())
M = lambda : map(int, input().split())
LI = lambda : list(map(int, input().split()))
for _ in range(I()):
	n = I()
	a = LI()
	b = LI()
	c = []
	for i in range(n):
		if not b[i]:
			c += [a[i]]
	c.sort()
	for i in range(n):
		if b[i]:
			print(a[i], end=' ')
		else:
			print(c[-1], end=' ')
			c.pop()
	print()


import os
import heapq
import sys, threading
import math
import bisect
import operator
from collections import defaultdict
from io import BytesIO, IOBase

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def power(x, p, m):
	res = 1
	while p:
		if p & 1:
			res = res * x % m
		x = x * x % m
		p >>= 1
	return res

def inar():
	return [int(k) for k in input().split()]

def lcm(num1, num2):
	return num1 * num2 // gcd(num1, num2)

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		arr = inar()
		check = inar()
		pos = []
		take = []
		for i in range(n):
			if check[i] == 0:
				pos.append(i)
				take.append(arr[i])
		take.sort(reverse=True)
		ans = [-1] * n
		for i in range(len(pos)):
			ans[pos[i]] = take[i]
		for i in range(n):
			if check[i] == 1:
				ans[i] = arr[i]
		print(*ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	u = [a[i] for i in range(n) if l[i] == 0]
	u.sort()
	for i in range(n):
		if l[i] == 0:
			a[i] = u.pop()
		print('{0} '.format(a[i]), end='')
	print('')


for _ in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	lrr = [int(j) for j in input().split()]
	new_array = []
	for i in range(len(arr)):
		if lrr[i] == 0:
			new_array.append(arr[i])
	new_array.sort(reverse=True)
	j = 0
	for i in range(len(arr)):
		if lrr[i]:
			print(arr[i], end=' ')
		else:
			print(new_array[j], end=' ')
			j += 1
	print('\n')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	lock = [int(i) for i in input().split()]
	pos = []
	p_s = 0
	n_s = 0
	for i in range(n):
		if lock[i] == 0:
			if arr[i] >= 0:
				p_s += arr[i]
			else:
				n_s += arr[i]
			pos.append(arr[i])
	if n_s > p_s:
		print(*arr)
	else:
		ans = []
		pos.sort(reverse=True)
		for i in range(n):
			if lock[i] == 1:
				ans.append(arr[i])
			elif len(pos) > 0:
				ans.append(pos.pop(0))
		print(*ans)


def iinput():
	return [int(i) for i in input().split()]
inf = 10 ** 5 + 1
t = int(input())
for _ in range(t):
	n = int(input())
	a = iinput()
	l = iinput()
	ans = [0 for i in range(n)]
	for i in range(n):
		if l[i] == 1:
			ans[i] = a[i]
			a[i] = -inf
	for i in range(n):
		if not l[i]:
			ans[i] = max(a)
			a[a.index(max(a))] = -inf
	print(*ans)


from sys import maxsize, stdout, stdin, stderr
mod = int(1000000000.0 + 7)
import re

def tup():
	return map(int, stdin.readline().split())

def I():
	return int(stdin.readline())

def lint():
	return [int(x) for x in stdin.readline().split()]

def S():
	return input().strip()

def grid(r, c):
	return [lint() for i in range(r)]

def debug(*args, c=6):
	print('\x1b[3{}m'.format(c), *args, '\x1b[0m', file=stderr)
from math import log2, sqrt
for _ in range(I()):
	n = I()
	ls = lint()
	arr = [0] * n
	l = lint()
	p = []
	for i in range(n):
		if l[i] == 0:
			p.append(ls[i])
	p.sort(reverse=True)
	c = 0
	for i in range(n):
		if l[i] == 1:
			arr[i] = ls[i]
		else:
			arr[i] = p[c]
			c += 1
	print(*arr)


from collections import Counter
import string
import math
import sys

def array_int():
	return [int(i) for i in sys.stdin.readline().split()]

def vary(number_of_variables):
	if number_of_variables == 1:
		return int(sys.stdin.readline())
	if number_of_variables >= 2:
		return map(int, sys.stdin.readline().split())

def makedict(var):
	return dict(Counter(var))
from fractions import Fraction
mod = 100000007
for _ in range(vary(1)):
	n = vary(1)
	num = array_int()
	postion = array_int()
	values = [-1000000000] * n
	for i in range(n):
		if postion[i] == 0:
			values[i] = num[i]
	values.sort(reverse=True)
	j = 0
	for i in range(n):
		if postion[i] != 1:
			num[i] = values[j]
			j += 1
	print(*num)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ind = list(map(int, input().split()))
	tmp = [arr[i] for i in range(n) if ind[i] == 0]
	tmp.sort(reverse=True)
	i = 0
	while i < n:
		if ind[i] == 0:
			arr[i] = tmp.pop(0)
		i += 1
	print(*arr)


import collections

def solve(a, lock, n):
	unlocked = []
	for i in range(n):
		if lock[i] == 0:
			unlocked.append(a[i])
	unlocked.sort(reverse=True)
	j = 0
	for i in range(n):
		if lock[i] == 0:
			a[i] = unlocked[j]
			j += 1
	return a

def ga():
	return list(map(int, input().split()))
t = int(input())
while t:
	n = int(input())
	a = ga()
	lock = ga()
	res = solve(a, lock, n)
	for val in res:
		print(val, end=' ')
	print('\n', end='')
	t -= 1


from collections import defaultdict, Counter
import copy, math
from copy import deepcopy
s = 'abcdefghijklmnopqrstuvwxyz' * 7
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	a = list(map(int, input().split()))
	x = [l[i] for i in range(n) if a[i] == 0]
	x.sort()
	for i in range(n):
		if a[i] == 0:
			l[i] = x.pop()
	print(*l)


t = int(input())
for q in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	m = []
	for (i, j) in enumerate(b):
		if j == 0:
			m.append(a[i])
	m = sorted(m)[::-1]
	z = 0
	for (i, j) in enumerate(b):
		if j == 0:
			a[i] = m[z]
			z += 1
	for i in a:
		print(i, end=' ')
	print('')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = list(map(int, input().split()))
	l2 = []
	c = 0
	summ = []
	for i in range(n):
		c += l[i]
		summ.append(c)
	l3 = []
	for i in range(n):
		if l1[i] == 0:
			l2.append(l[i])
			l3.append(i)
	ans = []
	l2.sort(reverse=True)
	c = 0
	for i in range(n):
		if c < len(l3) and i == l3[c]:
			ans.append(l2[c])
			c += 1
		else:
			ans.append(l[i])
	print(*ans)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	lock = list(map(int, input().split()))
	u = []
	l = []
	i = 0
	for d in lock:
		if d == 1:
			l.append(arr[i])
		else:
			u.append(arr[i])
		i += 1
	u.sort(reverse=True)
	ans = []
	i = 0
	j = 0
	for d in lock:
		if d == 1:
			ans.append(l[i])
			i += 1
		else:
			ans.append(u[j])
			j += 1
	ans = map(str, ans)
	print(' '.join(ans))


t = int(input())
aa = []
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split(' ')]
	l = [int(i) for i in input().split(' ')]
	unlocked = []
	locked = []
	p = []
	for (i, j) in enumerate(a):
		if l[i] == 0:
			unlocked.append(j)
		else:
			locked.append(j)
	unlocked.sort(reverse=True)
	k = 0
	j = 0
	for i in range(len(a)):
		if l[i] == 1:
			a[i] = locked[k]
			k += 1
		else:
			a[i] = unlocked[j]
			j += 1
	aa.append(a)
for i in aa:
	print(*i)


for u in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = list(map(int, input().split()))
	k = []
	for i in range(n):
		if d[i] == 0:
			k.append(l[i])
	k.sort(reverse=True)
	x = 0
	for i in range(n):
		if d[i] == 0:
			l[i] = k[x]
			x += 1
	print(*l)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	p = []
	sa = []
	i = 0
	for j in map(int, input().split()):
		if j == 0:
			p.append(i)
			sa.append(a[i])
		i += 1
	sa.sort(reverse=True)
	j = 0
	for i in p:
		a[i] = sa[j]
		j += 1
	print(*a)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	mx = []
	inds = []
	ans = [a[i] if b[i] == 1 else 0 for i in range(n)]
	for i in range(n):
		if b[i] == 0:
			mx.append(a[i])
			inds.append(i)
	mx.sort(reverse=True)
	for i in range(len(mx)):
		ans[inds[i]] = mx[i]
	print(*ans)


def solve(a, p, L):
	tot = sum(a)
	x = ['*' for i in range(L)]
	if tot >= 0:
		cou = 0
		for i in range(L):
			if p[i] == 1:
				x[i] = a[i - cou]
				a.pop(i - cou)
				cou += 1
		a.sort(reverse=True)
		for i in range(L):
			if x[i] == '*':
				x[i] = a.pop(0)
		print(*x)
	else:
		print(*a)
for i in range(int(input())):
	L = int(input())
	a = list(map(int, input().split()))
	p = list(map(int, input().split()))
	solve(a, p, L)


import sys
for _ in range(int(input())):
	n = int(input())
	a = [int(o) for o in input().split()]
	l = [int(o) for o in input().split()]
	al = []
	for i in range(n):
		if l[i] == 0:
			al.append(a[i])
	al.sort()
	al = al[::-1]
	j = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = al[j]
			j += 1
	print(*a)


t = int(input())
while t:
	t -= 1
	n = int(input())
	l = list(map(int, input().split()))
	p = list(map(int, input().split()))
	availible = []
	for (i, v) in enumerate(p):
		if v == 0:
			availible.append(l[i])
	availible.sort()
	s = sum(l)
	ans = []
	if s >= 0:
		for (i, v) in enumerate(l):
			if p[i] == 1:
				ans.append(l[i])
			else:
				ans.append(availible.pop())
	else:
		j = 0
		for (i, v) in enumerate(l):
			if p[i] == 1:
				ans.append(l[i])
			else:
				ans.append(availible[j])
				j += 1
	print(*ans)


import sys
import math as mt
import collections as cc
import sys
import itertools as it
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
for tc in range(int(input())):
	(n,) = I()
	ar = I()
	l = I()
	s = []
	ll = l.copy()
	loc = cc.defaultdict(int)
	for i in range(n):
		if l[i] == 0:
			s.append(ar[i])
			loc[i] = -10 ** 6
		else:
			loc[i] = ar[i]
	s.sort(reverse=True)
	j = 0
	for i in range(n):
		if l[i] == 0:
			l[i] = s[j]
			j += 1
		else:
			l[i] = ar[i]
	print(*l)


def changeNumbers(numbers, positions):
	if 0 not in positions:
		print(*numbers)
		return
	notBlocked = []
	for i in range(len(positions)):
		if positions[i] == 0:
			notBlocked.append(numbers[i])
	notBlocked.sort(reverse=True)
	j = 0
	for i in range(len(numbers)):
		if positions[i] != 1:
			numbers[i] = notBlocked[j]
			j += 1
	print(*numbers)
	return
t = int(input())
for i in range(t):
	n = int(input())
	numbers = list(map(int, input().split()))
	positions = list(map(int, input().split()))
	changeNumbers(numbers, positions)


for _ in range(int(input())):
	N = int(input())
	array = [int(x) for x in input().split()]
	lock = [int(x) for x in input().split()]
	checklist = [[] for i in range(N)]
	movable = []
	for i in range(N):
		checklist[i].append(array[i])
		checklist[i].append(lock[i])
		if lock[i] == 0:
			movable.append(array[i])
	movable = sorted(movable, reverse=True)
	counter = 0
	for i in range(N):
		if checklist[i][1] == 0:
			array[i] = movable[counter]
			counter += 1
	print(*array)


rd = lambda : list(map(int, input().split()))
for t in range(int(input())):
	n = int(input())
	(v, b) = (rd(), rd())
	x = []
	for i in range(n):
		if not b[i]:
			x.append(v[i])
	x.sort()
	for i in range(n):
		if not b[i]:
			v[i] = x[-1]
			x.pop()
	print(*v)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	locked = list(map(int, input().split()))
	unlocked = []
	for i in range(n):
		if locked[i] == 0:
			unlocked.append(arr[i])
	unlocked.sort(reverse=True)
	j = 0
	for i in range(n):
		if locked[i] == 0:
			arr[i] = unlocked[j]
			j += 1
	print(*arr)


for _ in range(int(input())):
	n = int(input())
	Arr = list(map(int, input().split()))
	Bool = list(map(int, input().split()))
	Unlock = []
	for i in range(n):
		if not Bool[i]:
			Unlock.append(Arr[i])
	Unlock = sorted(Unlock, reverse=True)
	k = 0
	for i in range(n):
		if not Bool[i]:
			Arr[i] = Unlock[k]
			k += 1
	s = ''
	for num in Arr:
		s += str(num) + ' '
	print(s)


import os
import sys
from io import BytesIO, IOBase
_str = str
BUFSIZE = 8192

def str(x=b''):
	return x if type(x) is bytes else _str(x).encode()

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def inp():
	return sys.stdin.readline().rstrip('\n')

def mpint():
	return map(int, sys.stdin.readline().split(' '))

def itg():
	return int(sys.stdin.readline())
for __ in range(itg()):
	n = itg()
	arr = list(mpint())
	lock = tuple(mpint())
	ans = [None] * n
	unlock = []
	for (i, a) in enumerate(arr):
		if lock[i]:
			ans[i] = a
		else:
			unlock.append(a)
	unlock.sort()
	for (i, a) in enumerate(ans):
		if a is None:
			ans[i] = unlock.pop()
	print(*ans)


from sys import stdin, stdout, setrecursionlimit
from collections import defaultdict as dd
from math import ceil
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = []
	for i in range(n):
		if b[i] == 0:
			c.append(a[i])
	c.sort()
	d = []
	for i in range(n):
		if b[i] == 0:
			d.append(c.pop())
		else:
			d.append(a[i])
	print(*d)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for pp in range(t):
		solve(pp)

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def google(p):
	print('Case #' + str(p) + ': ', end='')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def solve(case):
	n = int(inp())
	a = lis()
	l = lis()
	neg = []
	pos = []
	for i in range(n):
		if l[i] == 1:
			continue
		if a[i] < 0:
			neg.append(a[i])
		else:
			pos.append(a[i])
	pos = sorted(pos)[::-1]
	pos = deque(pos)
	s = sum(pos)
	neg = sorted(neg)[::-1]
	neg = deque(neg)
	if len(neg) != 0:
		curr = neg[0]
		while curr <= s:
			pos.append(neg[0])
			neg.popleft()
			if len(neg) == 0:
				break
			curr += neg[0]
	for i in range(n):
		if l[i] == 1:
			continue
		if len(neg) > 0:
			a[i] = neg[0]
			neg.popleft()
		else:
			a[i] = pos[0]
			pos.popleft()
	print(' '.join((str(a[i]) for i in range(n))))
testcase(int(inp()))


for i in range(int(input())):
	n = int(input())
	values = list(map(int, input().split()))
	locks = list(map(int, input().split()))
	elements = []
	indexes = []
	for i in range(n):
		if locks[i] == 0:
			elements.append(values[i])
			indexes.append(i)
	elements.sort(reverse=True)
	for i in range(len(indexes)):
		values[indexes[i]] = elements[i]
	for value in values:
		print(value, end=' ')
	print()


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	unlocked = []
	for i in range(len(a)):
		if l[i] == 0:
			unlocked.append(i)
	elem = [a[i] for i in unlocked]
	elem.sort(reverse=True)
	for (i, j) in zip(unlocked, elem):
		a[i] = j
	st = ''
	for i in a:
		st += str(i)
		st += ' '
	print(st)
	t -= 1


def solve():
	N = int(input())
	A = [int(x) for x in input().split()]
	L = [int(x) for x in input().split()]
	values = sorted([a for (a, l) in zip(A, L) if l == 0])
	for i in range(N):
		if L[i] == 0:
			A[i] = values.pop()
	return ' '.join(map(str, A))
tc = int(input())
for _ in range(tc):
	print(solve())


from sys import stdin
t = int(stdin.readline())
for loop in range(t):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	b = list(map(int, stdin.readline().split()))
	c = []
	for i in range(n):
		if b[i] == 0:
			c.append(a[i])
	c.sort()
	c.reverse()
	cnt = 0
	for i in range(n):
		if b[i] == 0:
			a[i] = c[cnt]
			cnt += 1
	print(*a)


from sys import stdin, stdout

def INPUT():
	return list((int(i) for i in stdin.readline().split()))

def inp():
	return stdin.readline()

def out(x):
	return stdout.write(x)
import math
import random
import sys

def calc(A, L):
	for i in range(len(A)):
		for j in range(i + 1, len(A)):
			if L[i] + L[j] == 0:
				if A[i] < A[j]:
					(A[i], A[j]) = (A[j], A[i])
	print(*A)
for _ in range(int(input())):
	n = int(input())
	A = INPUT()
	L = INPUT()
	bool = True
	i = 0
	sum = 0
	for i in A:
		if i < 0:
			bool = False
		sum += i
	if bool:
		print(*A)
		continue
	if sum < 0:
		print(*A)
		continue
	calc(A, L)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ind = []
	at = []
	for (i, j) in enumerate(map(int, input().split())):
		if j == 0:
			ind.append(i)
			at.append(a[i])
	at.sort(reverse=True)
	for (i, j) in zip(ind, at):
		a[i] = j
	print(*a)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def gift():
	for _ in range(t):
		n = int(input())
		array = list(map(int, input().split()))
		pos = list(map(int, input().split()))
		unlocked = []
		unlockedval = []
		for i in range(n):
			if pos[i] == 0:
				unlocked.append(i)
				unlockedval.append(array[i])
		unlockedval.sort(reverse=True)
		for i in range(len(unlocked)):
			array[unlocked[i]] = unlockedval[i]
		yield ' '.join([str(x) for x in array])
t = int(input())
ans = gift()
print(*ans, sep='\n')


from os import environ
DEBUG = environ.get('DEBUG') == '1'

def dd(xx):
	if not DEBUG:
		return
	print('---')
	for x in xx.strip().split():
		print(f'# {x} = {globals()[x]}')
	print('---')
if DEBUG:
	print('# Debugging...')
t = int(input())
for _ in range(t):
	n = int(input().strip())
	a = list(map(int, input().strip().split()))
	l = list(map(int, input().strip().split()))
	b = []
	for (ai, li) in zip(a, l):
		if not li:
			b.append(ai)
	b = sorted(b, reverse=True)
	j = 0
	for (i, li) in enumerate(l):
		if not li:
			a[i] = b[j]
			j += 1
	print(*a)
	dd('n a l')


tt = int(input())
for i in range(tt):
	num = int(input())
	c = input()
	l = c.split(' ')
	d = input()
	p = d.split(' ')
	b = list()
	a = list()
	for j in range(num):
		if int(l[j]) >= 0 and int(p[j]) == 0:
			b.append(int(l[j]))
		elif int(p[j]) == 0:
			a.append(int(l[j]))
	a.sort(reverse=True)
	b.sort(reverse=True)
	b = b + a
	k = 0
	for j in range(num):
		if int(p[j]) == 0:
			print(int(b[k]), end=' ')
			k += 1
		else:
			print(int(l[j]), end=' ')
	print()


import sys
import math
from collections import defaultdict, Counter
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	c = []
	for j in range(n):
		if l[j] == 0:
			c.append(a[j])
	c.sort(reverse=True)
	ind = 0
	for j in range(n):
		if l[j] == 0:
			a[j] = c[ind]
			ind += 1
	print(*a)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	unlocked = [a[i] for i in range(n) if not l[i]]
	unlocked.sort(reverse=True)
	a1 = a.copy()
	a2 = a.copy()
	a3 = a.copy()
	ps = 0
	j = 0
	k1 = 0
	for i in range(n):
		if not l[i]:
			a1[i] = unlocked[j]
			j += 1
		ps += a1[i]
		if ps < 0:
			k1 = i
	unlocked.reverse()
	ps = 0
	j = 0
	k2 = 0
	for i in range(n):
		if not l[i]:
			a2[i] = unlocked[j]
			j += 1
		ps += a2[i]
		if ps < 0:
			k2 = i
	unlocked.reverse()
	ps = 0
	k3 = 0
	j = len(unlocked) - 1
	rever = True
	for i in range(n):
		if not l[i]:
			a3[i] = unlocked[j]
			unlocked.pop(j)
			j -= 1
		ps += a3[i]
		if ps < 0:
			k3 = i
			if rever:
				unlocked.reverse()
				rever = False
	k = min(k1, k2, k3)
	if k1 == k:
		print(*a1)
	elif k2 == k:
		print(*a2)
	else:
		print(*a3)


import sys
input = sys.stdin.readline
for test in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	l = [int(i) for i in input().split()]
	b = []
	for i in range(n):
		if l[i] == 0:
			b.append(a[i])
	b.sort(reverse=True)
	c = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = b[c]
			c += 1
	print(*a)


from sys import stdin
input = stdin.readline

def gt():
	return list(map(int, input().split()))

def work(ar: list, lst: list, pos: list):
	tmp1 = ar.copy()
	for i in range(len(pos)):
		tmp1[pos[i]] = lst[i]
	sum1 = 0
	k1 = 0
	for i in range(len(tmp1)):
		sum1 += tmp1[i]
		if sum1 < 0:
			k1 = i + 1
	return (k1, tmp1)
(t,) = gt()
while t:
	t -= 1
	(n,) = gt()
	ar = gt()
	l = gt()
	lst = []
	pos = []
	for i in range(len(ar)):
		if l[i]:
			continue
		lst.append(ar[i])
		pos.append(i)
		ar[i] = None
	lst.sort()
	(k1, ans1) = work(ar, lst, pos)
	lst.reverse()
	(k2, ans2) = work(ar, lst, pos)
	if k1 <= k2:
		print(*ans1)
	else:
		print(*ans2)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	lock_unlock = list(map(int, input().split()))
	unlocked = []
	index = []
	for i in range(len(arr)):
		if lock_unlock[i] == 0:
			unlocked.append(arr[i])
			index.append(i)
	unlocked.sort(reverse=True)
	for i in range(len(index)):
		arr[index[i]] = unlocked[i]
	print(*arr)


from math import ceil
from decimal import *

def get_ans(n, arr, blocked):
	unblocked = []
	for i in range(n):
		if blocked[i] == 0:
			unblocked.append(arr[i])
	unblocked.sort()
	unblocked.reverse()
	for i in range(n):
		if blocked[i] == 1:
			unblocked.insert(i, arr[i])
	return unblocked

def dfs(G, v, curr):
	v.append(curr)
	max_d = 1
	v_max_d = []
	tr = False
	for node in range(n):
		if G[curr][node] != -1:
			if node not in v:
				tr = True
				d_v = dfs(G, v, node)
				if d_v[0] + 1 > max_d:
					max_d = d_v[0] + 1
					v_max_d = d_v[1]
	if not tr:
		return (1, v)
	return (max_d, v_max_d)

def bfs(G, start):
	v = []
	querry = []
	v.append(start)
	querry.append(start)
	depth = 0
	while querry:
		depth += 1
		curr = querry.pop(0)
		for node in range(n):
			if G[curr][node] != -1 and node not in v:
				v.append(node)
				querry.append(node)
	return (depth, v)
t = int(input())
anss = []
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	blocked = list(map(int, input().split()))
	anss.append(get_ans(n, arr, blocked))
for i in anss:
	ans = ''
	for l in i:
		ans = ans + str(l) + ' '
	print(ans[:-1])


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	lo = list(map(int, input().split()))
	su = sum(lis)
	if su < 0:
		print(*lis)
	else:
		ln = []
		lp = []
		ar = []
		for i in range(n):
			if lo[i] == 0 and lis[i] < 0:
				ln.append(lis[i])
			elif lo[i] == 0 and lis[i] >= 0:
				lp.append(lis[i])
		ln.sort()
		ln.reverse()
		lp.sort()
		lp.reverse()
		k = -1
		for i in range(n - 1, -1, -1):
			if su < 0:
				k = i
				break
			elif lo[i] == 1:
				ar.append(lis[i])
				if lis[i] < 0:
					su += -lis[i]
				else:
					su -= lis[i]
			elif len(ln) > 0:
				su += -ln[-1]
				ar.append(ln[-1])
				ln.pop()
			else:
				ar.append(lp[-1])
				lp.pop()
		if k != -1:
			for i in range(k, -1, -1):
				if lo[i] == 1:
					ar.append(lis[i])
				elif len(ln) > 0:
					ar.append(ln[-1])
					ln.pop()
				else:
					ar.append(lp[-1])
					lp.pop()
		print(*ar[::-1])


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	x = list(map(int, input().split()))
	b = []
	for i in range(n):
		if x[i] == 0:
			b.append(a[i])
			a[i] = float('inf')
	b.sort(reverse=True)
	j = 0
	for i in range(n):
		if a[i] == float('inf'):
			a[i] = b[j]
			j += 1
	print(*a)
	t -= 1


from sys import stdin, stdout

class SOLVE:

	def solve(self):
		R = stdin.readline
		W = stdout.write
		ans = []
		for i in range(int(R())):
			n = int(R())
			a = [int(x) for x in R().split()]
			check = [int(x) for x in R().split()]
			unlock = []
			for j in range(n):
				if not check[j]:
					unlock.append(a[j])
			unlock.sort(reverse=True)
			k = 0
			for j in range(n):
				if not check[j]:
					a[j] = unlock[k]
					k += 1
			print(*a)

def main():
	s = SOLVE()
	s.solve()
main()


def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())
import math

def solve():
	N = getInt()
	A = getInts()
	L = getInts()
	targ = []
	curr_sum = 0
	free = []
	for i in range(N):
		targ.append(-curr_sum)
		if L[i]:
			curr_sum += A[i]
		else:
			free.append(A[i])
	sum_free = sum(free)
	free.sort(reverse=True)
	worst = N
	ans = []
	ok = True
	for i in range(N - 1, -1, -1):
		if L[i]:
			ans.append(A[i])
			continue
		ans.append(free.pop())
	ans.reverse()
	print(*ans)
	return
T = getInt()
for t in range(T):
	solve()


from math import *
from bisect import *
from collections import *
from random import *
from decimal import *
import sys
from itertools import product
input = sys.stdin.readline

def inp():
	return int(input())

def st():
	return input().rstrip('\n')

def lis():
	return list(map(int, input().split()))

def ma():
	return map(int, input().split())
t = inp()
while t:
	t -= 1
	n = inp()
	a = lis()
	b = lis()
	need = []
	for i in range(n):
		if b[i] == 0:
			need.append(a[i])
	need.sort(reverse=True)
	po = 0
	for i in range(n):
		if b[i] == 0:
			a[i] = need[po]
			po += 1
	print(*a)


for _ in range(int(input())):
	import math
	n = int(input())
	arr = list(map(int, input().split()))
	lock = list(map(int, input().split()))
	ans = [math.inf] * n
	a = []
	for i in range(n):
		if lock[i] == 1:
			ans[i] = arr[i]
		else:
			a.append(arr[i])
	a.sort(reverse=True)
	j = 0
	for i in range(n):
		if ans[i] == math.inf:
			ans[i] = a[j]
			j += 1
	print(*ans)


import os
import heapq
import sys, threading
import math as mt
import operator
from copy import copy
from collections import defaultdict, deque
from io import BytesIO, IOBase
sys.setrecursionlimit(2 * 10 ** 5)

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def inpt():
	return [int(k) for k in input().split()]

def main():
	for _ in range(int(input())):
		n = int(input())
		ar = inpt()
		lock = inpt()
		sm = sum(ar)
		temp = []
		for i in range(n):
			if lock[i] == 0:
				temp.append(ar[i])
		temp.sort()
		q = deque(temp)
		for i in range(n):
			if lock[i] == 0:
				ar[i] = q.pop()
		print(*ar)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from collections import deque
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	bckt = []
	for i in range(n):
		if not l[i]:
			bckt.append(a[i])
			a[i] = 10 ** 9
	bckt.sort(reverse=True)
	bckt = deque(bckt)
	for i in range(n):
		if a[i] == 10 ** 9:
			a[i] = bckt.popleft()
	print(*a)


for x in range(int(input())):
	n = int(input())
	c = []
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	if sum(a) < 0:
		print(*a)
	else:
		for i in range(n):
			if b[i] == 0:
				c.append(a[i])
		c.sort()
		for i in range(n):
			if b[i] == 0:
				a[i] = c.pop()
		print(*a)


t = int(input())
for j in range(t):
	n = int(input())
	h = list(map(int, input().split()))
	p = list(map(int, input().split()))
	l = []
	for i in range(n):
		if p[i] == 0:
			l.append(h[i])
	l.sort(reverse=True)
	b = 0
	for i in range(n):
		if p[i] == 0:
			h[i] = l[b]
			b = b + 1
	for i in range(n):
		print(h[i], end=' ')
	print()


import math
from collections import deque
from sys import stdin, stdout
from string import ascii_letters
import sys
letters = ascii_letters
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	can = list(map(int, input().split()))
	vals = sorted([i for i in range(n) if not can[i]], key=lambda x: -arr[x])
	res = [0] * n
	last = 0
	for i in range(n):
		if can[i]:
			res[i] = arr[i]
		else:
			res[i] = arr[vals[last]]
			last += 1
	print(*res)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	unlocked = []
	unlocked_indexes = []
	for (i, item) in enumerate(l):
		if item == 0:
			unlocked.append(a[i])
			unlocked_indexes.append(i)
	unlocked = sorted(unlocked)[::-1]
	new_a = list(a)
	for (i, index) in enumerate(unlocked_indexes):
		new_a[index] = unlocked[i]
	print(*new_a)


import time, math as mt, bisect as bs, sys
from sys import stdin, stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi = 3.141592653589793

def II():
	return int(stdin.readline())

def IP():
	return map(int, stdin.readline().split())

def L():
	return list(map(int, stdin.readline().split()))

def P(x):
	return stdout.write(str(x) + '\n')

def PI(x, y):
	return stdout.write(str(x) + ' ' + str(y) + '\n')

def lcm(a, b):
	return a * b // gcd(a, b)

def gcd(a, b):
	if a == 0:
		return b
	elif b == 0:
		return a
	if a > b:
		return gcd(a % b, b)
	else:
		return gcd(a, b % a)

def bfs(adj, v):
	visited = [False] * (v + 1)
	q = deque()
	while q:
		pass

def sieve():
	li = [True] * (2 * 10 ** 5 + 5)
	(li[0], li[1]) = (False, False)
	for i in range(2, len(li), 1):
		if li[i] == True:
			for j in range(i * i, len(li), i):
				li[j] = False
	prime = []
	for i in range(2 * 10 ** 5 + 5):
		if li[i] == True:
			prime.append(i)
	return prime

def setBit(n):
	count = 0
	while n != 0:
		n = n & n - 1
		count += 1
	return count
mx = 10 ** 7
spf = [mx] * (mx + 1)

def SPF():
	spf[1] = 1
	for i in range(2, mx + 1):
		if spf[i] == mx:
			spf[i] = i
			for j in range(i * i, mx + 1, i):
				if i < spf[j]:
					spf[j] = i
	return

def readTree(n, e):
	adj = [set() for i in range(n + 1)]
	for i in range(e):
		(u1, u2) = IP()
		adj[u1].add(u2)
	return adj
mod = 10 ** 9 + 7

def solve():
	n = II()
	a = L()
	l = L()
	li = []
	for i in range(n):
		if l[i] == 0:
			li.append(a[i])
	li.sort()
	for i in range(n):
		if l[i] == 0:
			a[i] = li.pop()
	pref = [0] * n
	pref[0] = a[0]
	for i in range(1, n):
		pref[i] = pref[i - 1] + a[i]
	print(*a)
	return
t = II()
for i in range(t):
	solve()


import sys

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())

def solve():
	n = mint()
	a = list(mints())
	b = list(mints())
	c = []
	for i in range(n):
		if b[i] == 0:
			c.append(a[i])
	c.sort(reverse=True)
	last = 0
	p = [0] * (n + 1)
	best = (10000, [])
	lastr = 0
	for i in range(len(c) + 1):
		d = a[:]
		k = 0
		r = lastr
		first = None
		for j in range(last, n):
			if b[j] == 0:
				d[j] = c[k]
				k += 1
				if first is None:
					first = j
					lastr = r
			p[j + 1] = p[j] + d[j]
			if p[j + 1] < 0:
				r = j + 1
		best = min(best, (r, d))
		if len(c) != 0:
			a[first] = c.pop()
			p[first + 1] = p[first] + a[first]
			if p[first + 1] < 0:
				lastr = first + 1
			last = first + 1
	print(' '.join(map(str, best[1])))
for i in range(mint()):
	solve()


from sys import stdin
input = stdin.buffer.readline
for _ in range(int(input())):
	n = int(input())
	(*a,) = map(int, input().split())
	(*b,) = map(int, input().split())
	c = [a[i] for i in range(n) if not b[i]]
	c.sort(reverse=True)
	j = 0
	for i in range(n):
		if not b[i]:
			a[i] = c[j]
			j += 1
	print(*a)


import os
import sys
from io import BytesIO, IOBase
import math
from decimal import *
getcontext().prec = 30
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def binary_search(data, val, le):
	test = le
	if val >= data[-1]:
		return [test - 1, test - 1]
	elif val <= data[0]:
		return [0, 0]
	high_index = test - 1
	low_index = 0
	while high_index > low_index:
		index = (high_index + low_index) // 2
		sub = data[index]
		if data[low_index] == val:
			return [low_index, low_index]
		elif sub == val:
			return [index, index]
		elif data[high_index] == val:
			return [high_index, high_index]
		elif sub > val:
			if high_index == index:
				return sorted([high_index, low_index])
			high_index = index
		else:
			if low_index == index:
				return sorted([high_index, low_index])
			low_index = index
	return sorted([high_index, low_index])
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	n = int(input())
	l1 = list(input().split(' '))
	l2 = list(map(int, input().split(' ')))
	l3 = []
	for i in range(n):
		if l2[i] == 0:
			l3.append(l1[i])
	l3 = [int(i) for i in l3]
	l3.sort(reverse=True)
	x = 0
	for i in range(n):
		if l2[i] == 0:
			l1[i] = str(l3[x])
			x += 1
	print(*l1)


def mp():
	return map(int, input().split())

def it():
	return int(input())
for _ in range(it()):
	n = it()
	l = list(mp())
	k = list(mp())
	v = []
	for i in range(n):
		if not k[i]:
			v.append(l[i])
	v.sort()
	ans = 0
	for i in range(n):
		if k[i] < 1:
			l[i] = v.pop()
	print(*l)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, array, locks) = (int(input()), [int(i) for i in input().split()], [int(i) for i in input().split()])
	unlocked = list(sorted([array[i] for i in range(n) if not locks[i]]))
	print(*[unlocked.pop() if not locks[i] else array[i] for i in range(n)])


import sys, os.path
from collections import *
from copy import *
import math
mod = 10 ** 9 + 7
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	arr = []
	for i in range(n):
		if l[i] == 0:
			arr.append(a[i])
	arr.sort(reverse=True)
	ind = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = arr[ind]
			ind += 1
	print(*a)


for i in range(int(input())):
	x = int(input())
	y = list()
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	for t in range(x):
		if b[t] == 0:
			y.append(a[t])
	y = sorted(y)
	l = len(y) - 1
	for z in range(x):
		if b[z] == 0:
			a[z] = y[l]
			l = l - 1
	print(*a)


import sys, math
try:
	(sys.stdin, sys.stdout) = (open('input.txt', 'r'), open('out.txt', 'w'))
except:
	pass
from sys import stdin, stdout
mod = int(1000000000.0 + 7)
from statistics import mode
from collections import *
from math import ceil, floor, inf, factorial, gcd, log2, sqrt, log
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()

def lcm(a, b):
	return a * b // gcd(a, b)

def setbits(n):
	return bin(n).count('1')

def resetbits(n):
	return bin(n).count('0')

def modinv(n, p):
	return pow(n, p - 2, p)

def ncr(n, r):
	(num, den) = (1, 1)
	r = min(n, n - r)
	for i in range(r):
		num *= n - i
		den *= i + 1
	return num // den

def ncr_p(n, r, p):
	(num, den) = (1, 1)
	r = min(r, n - r)
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * modinv(den, p) % p

def isPrime(num):
	if num <= 1:
		return False
	if num == 2 or n == 3:
		return True
	if num % 2 == 0 or num % 3 == 0:
		return False
	m = int(num ** 0.5) + 1
	for i in range(5, m, 6):
		if num % i == 0 or num % (i + 2) == 0:
			return False
	return True

def bin_search(s, low, high, val):
	while low <= high:
		mid = low + (high - low) // 2
		if s[mid] == val:
			return mid
		elif s[mid] < val:
			low = mid + 1
		else:
			high = mid - 1
	return -1

def sumofdigit(num):
	count = 0
	while num:
		count += num % 10
		num //= 10
	return count

def inputmatrix():
	(r, c) = iia()
	mat = [0] * r
	for i in range(r):
		mat[i] = iia()
	return (r, c, mat)

def prefix_sum(n, s):
	for i in range(1, n):
		s[i] += s[i - 1]
	return s

def binomial(n, k):
	if 0 <= k <= n:
		ntok = 1
		ktok = 1
		for t in range(1, min(k, n - k) + 1):
			ntok *= n
			ktok *= t
			n -= 1
		return ntok // ktok
	else:
		return 0

def divisors(n):
	res = []
	for i in range(1, ceil(sqrt(n)) + 1):
		if n % i == 0:
			if i == n // i:
				res.append(i)
			else:
				res.append(i)
				res.append(n // i)
	return res
for testcase in range(ii1()):
	n = ii1()
	arr = iia()
	check = iia()
	cansort = []
	for i in range(n):
		if check[i] == 0:
			cansort.append(arr[i])
	cansort.sort(reverse=True)
	k = 0
	j = 0
	for i in range(n):
		if check[i] == 0:
			arr[i] = cansort[j]
			j += 1
	print(*arr)


import math
for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	br = list(map(int, input().split()))
	rr = []
	tt = []
	for i in range(n):
		if br[i] == 0:
			rr.append(ar[i])
		else:
			tt.append(i)
	if len(rr) > 0:
		rr.sort()
		rr.reverse()
	for k in tt:
		rr.insert(k, ar[k])
	print(*rr)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	l = list(map(int, input().split()))
	tmp = []
	for i in range(n):
		if l[i] != 0:
			continue
		else:
			tmp.append(arr[i])
	tmp.sort(reverse=True)
	cnt = 0
	new_l = [0 for i in range(n)]
	for i in range(n):
		if l[i] != 0:
			new_l[i] = arr[i]
		else:
			new_l[i] = tmp[cnt]
			cnt += 1
		print(new_l[i], end=' ')
	print('')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import gcd, ceil

def pre(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi

def prod(a):
	ans = 1
	for each in a:
		ans = ans * each
	return ans

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
from collections import deque
for _ in range(int(input()) if True else 1):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	unlocked = []
	for i in range(n):
		if b[i] == 0:
			unlocked += [a[i]]
	unlocked = deque(sorted(unlocked, reverse=True))
	pre = 0
	for i in range(n):
		if b[i] == 0:
			a[i] = unlocked.popleft()
	print(*a)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	lst = list(map(int, input().split()))
	if 0 not in lst:
		print(*a, sep=' ')
	else:
		p = []
		for i in range(len(lst)):
			if lst[i] == 0:
				p.append(a[i])
		p.sort(reverse=True)
		j = 0
		for i in range(len(lst)):
			if lst[i] == 0:
				a[i] = p[j]
				j += 1
		print(*a, sep=' ')


for i in range(int(input())):
	n = int(input())
	l = [int(j) for j in input().split(' ')]
	c = [int(j) for j in input().split(' ')]
	t = [l[j] for j in range(n) if c[j] == 0]
	t.sort()
	for j in range(n):
		if c[j] == 0:
			l[j] = t.pop()
	for j in range(n - 1):
		print(l[j], end=' ')
	print(l[-1])


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	l = [int(x) for x in input().split()]
	unl = []
	for i in range(n):
		if l[i] == 0:
			unl.append(a[i])
	unl.sort(reverse=True)
	j = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = unl[j]
			j += 1
	print(' '.join([str(x) for x in a]))


import sys
from os import path
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
import math

def solve():
	n = int(input())
	a = [int(i) for i in input().split()]
	mask = [int(i) for i in input().split()]
	arr = []
	for (idx, i) in enumerate(mask):
		if i == 1:
			pass
		else:
			arr.append(a[idx])
	arr.sort(reverse=True)
	index = 0
	for (idx, i) in enumerate(mask):
		if i == 0:
			a[idx] = arr[index]
			index += 1
	print(' '.join((str(i) for i in a)))

def main():
	tc = int(input().strip())
	for case in range(tc):
		solve()
main()


from collections import deque
for _ in range(int(input())):
	n = int(input())
	data = list(map(int, input().split()))
	flag = list(map(int, input().split()))
	temp = []
	for i in range(n):
		if flag[i] == 0:
			temp.append(data[i])
	temp.sort(reverse=True)
	result = []
	temp = deque(temp)
	for i in range(n):
		if flag[i] == 0:
			result.append(temp.popleft())
		else:
			result.append(data[i])
	for x in result:
		print(x, end=' ')
	print()


t = int(input(''))
while t > 0:
	n = int(input(''))
	a = list(map(int, input().split()))
	indices = list(map(int, input().split()))
	i_list = []
	sorter = []
	final = []
	for i in range(n):
		if indices[i] == 1:
			i_list.append(i)
		else:
			sorter.append(a[i])
	copy = list(sorter)
	sorter.sort(reverse=True)
	for j in range(n):
		if j in i_list:
			final.append(a[j])
		else:
			final.append(sorter[0])
			sorter.pop(0)
	for ans in final:
		print(ans, end=' ')
	print()
	t -= 1


t = int(input())
for i in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	L = list(map(int, input().split()))
	B = [A[i] for i in range(n) if L[i] == 0]
	C = [i for i in range(n) if L[i] == 0]
	B.sort(reverse=True)
	for i in range(len(B)):
		A[C[i]] = B[i]
	print(*A)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	lock = list(map(int, input().split()))
	temp = []
	for i in range(n):
		if lock[i] == 0:
			temp.append(arr[i])
	temp.sort(reverse=True)
	count = 0
	for i in range(n):
		if lock[i] == 0:
			arr[i] = temp[count]
			count += 1
	for i in range(n):
		print(arr[i], end=' ')
	print()


def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		lu = list(map(int, input().split()))
		ans = ['0'] * n
		another = []
		for i in range(n):
			if not lu[i]:
				another.append(a[i])
			else:
				ans[i] = a[i]
		k = 0
		another.sort(reverse=True)
		for i in range(n):
			if ans[i] == '0':
				ans[i] = another[k]
				k += 1
		print(*ans)
main()


from bisect import *
from collections import *
from math import gcd, ceil, sqrt, floor, inf
from heapq import *
from itertools import *
from operator import add, mul, sub, xor, truediv, floordiv
from functools import *
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())
farr = [1]
ifa = []

def fact(x, mod=0):
	if mod:
		while x >= len(farr):
			farr.append(farr[-1] * len(farr) % mod)
	else:
		while x >= len(farr):
			farr.append(farr[-1] * len(farr))
	return farr[x]

def ifact(x, mod):
	global ifa
	ifa.append(pow(farr[-1], mod - 2, mod))
	for i in range(x, 0, -1):
		ifa.append(ifa[-1] * i % mod)
	ifa = ifa[::-1]

def per(i, j, mod=0):
	if i < j:
		return 0
	if not mod:
		return fact(i) // fact(i - j)
	return farr[i] * ifa[i - j] % mod

def com(i, j, mod=0):
	if i < j:
		return 0
	if not mod:
		return per(i, j) // fact(j)
	return per(i, j, mod) * ifa[j] % mod

def catalan(n):
	return com(2 * n, n) // (n + 1)

def linc(f, t, l, r):
	while l < r:
		mid = (l + r) // 2
		if t > f(mid):
			l = mid + 1
		else:
			r = mid
	return l

def rinc(f, t, l, r):
	while l < r:
		mid = (l + r + 1) // 2
		if t < f(mid):
			r = mid - 1
		else:
			l = mid
	return l

def ldec(f, t, l, r):
	while l < r:
		mid = (l + r) // 2
		if t < f(mid):
			l = mid + 1
		else:
			r = mid
	return l

def rdec(f, t, l, r):
	while l < r:
		mid = (l + r + 1) // 2
		if t > f(mid):
			r = mid - 1
		else:
			l = mid
	return l

def isprime(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return False
	return True

def binfun(x):
	c = 0
	for w in arr:
		c += ceil(w / x)
	return c

def lowbit(n):
	return n & -n

def inverse(a, m):
	a %= m
	if a <= 1:
		return a
	return (1 - inverse(m, a) * m) // a % m

class BIT:

	def __init__(self, arr):
		self.arr = arr
		self.n = len(arr) - 1

	def update(self, x, v):
		while x <= self.n:
			self.arr[x] += v
			x += x & -x

	def query(self, x):
		ans = 0
		while x:
			ans += self.arr[x]
			x &= x - 1
		return ans

class smt:

	def __init__(self, l, r, arr):
		self.l = l
		self.r = r
		self.value = (1 << 31) - 1 if l < r else arr[l]
		mid = (l + r) // 2
		if l < r:
			self.left = smt(l, mid, arr)
			self.right = smt(mid + 1, r, arr)
			self.value &= self.left.value & self.right.value

	def setvalue(self, x, val):
		if self.l == self.r:
			self.value = val
			return
		mid = (self.l + self.r) // 2
		if x <= mid:
			self.left.setvalue(x, val)
		else:
			self.right.setvalue(x, val)
		self.value = self.left.value & self.right.value

	def ask(self, l, r):
		if l <= self.l and r >= self.r:
			return self.value
		val = (1 << 31) - 1
		mid = (self.l + self.r) // 2
		if l <= mid:
			val &= self.left.ask(l, r)
		if r > mid:
			val &= self.right.ask(l, r)
		return val

class DSU:

	def __init__(self, n):
		self.c = [-1] * n

	def same(self, x, y):
		return self.find(x) == self.find(y)

	def find(self, x):
		if self.c[x] < 0:
			return x
		self.c[x] = self.find(self.c[x])
		return self.c[x]

	def union(self, u, v):
		(u, v) = (self.find(u), self.find(v))
		if u == v:
			return False
		if self.c[u] < self.c[v]:
			(u, v) = (v, u)
		self.c[u] += self.c[v]
		self.c[v] = u
		return True

	def size(self, x):
		return -self.c[self.find(x)]

class UFS:

	def __init__(self, n):
		self.parent = [i for i in range(n)]
		self.ranks = [0] * n

	def find(self, x):
		if x != self.parent[x]:
			self.parent[x] = self.find(self.parent[x])
		return self.parent[x]

	def union(self, u, v):
		(pu, pv) = (self.find(u), self.find(v))
		if pu == pv:
			return False
		if self.ranks[pu] >= self.ranks[pv]:
			self.parent[pv] = pu
			if self.ranks[pv] == self.ranks[pu]:
				self.ranks[pu] += 1
		else:
			self.parent[pu] = pv

def Prime(n):
	c = 0
	prime = []
	flag = [0] * (n + 1)
	for i in range(2, n + 1):
		if not flag[i]:
			prime.append(i)
			c += 1
		for j in range(c):
			if i * prime[j] > n:
				break
			flag[i * prime[j]] = prime[j]
			if i % prime[j] == 0:
				break
	return prime

def dij(s, graph):
	d = {}
	d[s] = 0
	heap = [(0, s)]
	seen = set()
	while heap:
		(dis, u) = heappop(heap)
		if u in seen:
			continue
		for v in graph[u]:
			if v not in d or d[v] > d[u] + graph[u][v]:
				d[v] = d[u] + graph[u][v]
				heappush(heap, (d[v], v))
	return d

def GP(it):
	res = []
	for (ch, g) in groupby(it):
		res.append((ch, len(list(g))))
	return res

class DLN:

	def __init__(self, val):
		self.val = val
		self.pre = None
		self.next = None
t = N()
for i in range(t):
	n = N()
	a = RLL()
	l = RLL()
	res = []
	for i in range(n):
		if l[i] == 0:
			res.append(a[i])
	res.sort(reverse=True)
	j = 0
	for i in range(n):
		if l[i] == 0:
			a[i] = res[j]
			j += 1
	print(*a)


import sys
input = sys.stdin.readline
t = int(input())
for tests in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	l = list(map(int, input().split()))
	B = []
	for i in range(n):
		if l[i] == 0:
			B.append(A[i])
	B.sort(reverse=True)
	ind = 0
	for i in range(n):
		if l[i] == 0:
			A[i] = B[ind]
			ind += 1
	print(*A)


t = int(input())
while t:
	d = {}
	count = 0
	n = int(input())
	a = [int(a) for a in input().split()]
	b = [int(b) for b in input().split()]
	for i in range(n):
		if b[i] == 1:
			d[i] = a[i]
	x = 0
	for k in d.keys():
		del a[k - x]
		x += 1
	a = sorted(a, reverse=True)
	for (k, v) in d.items():
		a.insert(k, v)
	for i in range(len(a)):
		if i == n - 1:
			print(a[i])
			break
		print(a[i], end=' ')
	t -= 1


for _ in range(int(input())):
	cs = input()
	l = [int(i) for i in input().split()]
	a = [int(i) for i in input().split()]
	c = []
	for i in range(len(l)):
		if a[i] == 0:
			c.append(l[i])
	z = sorted(c, reverse=True)
	j = 0
	for i in range(len(l)):
		if a[i] == 1:
			print(l[i], end=' ')
		else:
			print(z[j], end=' ')
			j += 1
	print()


for _ in range(int(input())):
	n = int(input())
	(a, b) = ([*map(int, input().split())], [*map(int, input().split())])
	c = []
	for i in range(n):
		if b[i] == 0:
			c.append(a[i])
	c.sort()
	j = len(c) - 1
	ans = 0
	for i in range(n):
		if b[i] == 0:
			a[i] = c[j]
			j -= 1
	print(*a)


t = int(input())
for i in range(t):
	N = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	C = []
	for j in range(len(A)):
		if B[j] == 0:
			C.append(A[j])
	C.sort(reverse=True)
	d = 0
	for k in range(len(A)):
		if B[k] == 0:
			A[k] = C[d]
			d += 1
	A = list(map(str, A))
	print(' '.join(A))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	locked = []
	unlocked = []
	for i in range(n):
		if b[i] == 0:
			unlocked.append(a[i])
		else:
			locked.append((i, a[i]))
	unlocked.sort()
	ans = [None for i in range(n)]
	for (i, val) in locked:
		ans[i] = val
	i = 0
	while i < n and len(unlocked):
		if ans[i] == None:
			ans[i] = unlocked.pop()
			i += 1
		else:
			i += 1
	print(*ans)


for _ in range(int(input())):
	n = int(input())
	arr = [int(c) for c in input().split()]
	key = [int(c) for c in input().split()]
	ans = []
	for i in range(len(arr)):
		if key[i] == 0:
			ans.append(arr[i])
	ans.sort(reverse=True)
	j = 0
	for i in range(len(arr)):
		if key[i] == 0:
			arr[i] = ans[j]
			j += 1
	print(*arr)


t = int(input())
z = 0

def QS(a):
	if len(a) < 2:
		return a
	else:
		pivot = a[0]
		L = [i for i in a[1:] if i > pivot]
		R = [i for i in a[1:] if i <= pivot]
		return QS(L) + [pivot] + QS(R)
while z < t:
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = []
	index = 0
	while index < n:
		if b[index] == 0:
			c.append(a[index])
			del a[index]
			a.insert(index, '*')
		index += 1
	c = QS(c)
	index = 0
	while index < n:
		if a[index] == '*':
			del a[index]
			a.insert(index, c[0])
			del c[0]
		index += 1
	A = ''
	for i in a:
		A += str(i) + ' '
	print(A)
	z += 1


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	l = list(map(int, input().split()))
	req = []
	for i in range(n):
		if l[i] == 0:
			req.append(arr[i])
	req.sort(reverse=True)
	j = 0
	for i in range(n):
		if l[i] == 0:
			arr[i] = req[j]
			j += 1
	p = 0
	k = 0
	for i in arr:
		p += i
		if p < 0:
			k = i + 1
	arr = map(str, arr)
	s = ' '.join(arr)
	print(s)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	temp = []
	for i in range(n):
		if l[i] == 0:
			temp.append(a[i])
	temp.sort(reverse=True)
	i = 0
	j = 0
	k = 0
	while k < n:
		if l[k] == 1:
			print(a[k], end=' ')
		else:
			print(temp[j], end=' ')
			j += 1
		k += 1
	print()


import math
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	l = list(map(int, input().split()))
	t = []
	for i in range(n):
		if l[i] == 0:
			t.append(arr[i])
	t.sort(reverse=True)
	c = 0
	for i in range(n):
		if l[i] == 0:
			arr[i] = t[c]
			c += 1
	print(*arr)


from math import *
from sys import *
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(map(int, input().split()))
	r = []
	b = []
	for i in range(n):
		if l[i] == 0:
			r.append(a[i])
			b.append(i)
	if len(b) == 0:
		print(*a)
		continue
	r.sort(reverse=True)
	j = 0
	for i in range(n):
		if l[i] == 0:
			print(r[j], end=' ')
			j = j + 1
		else:
			print(a[i], end=' ')
	print()


import sys
input = lambda : sys.stdin.readline().rstrip()
from math import ceil
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]
	s = []
	for i in range(n):
		if b[i] == 0:
			s.append(a[i])
	s.sort()
	for i in range(n):
		if b[i] == 0:
			a[i] = s.pop()
	print(*a)


from sys import stdin, stdout
import math
import heapq
import collections
input = stdin.readline

def inputnum():
	return int(input())

def inputnums():
	return map(int, input().split())

def inputlist():
	return list(map(int, input().split()))

def inputstring():
	return [x for x in input()]

def inputstrings():
	return [x for x in input().split()]

def inputstringnum():
	return [ord(x) - ord('a') for x in input()]

def inputmatrixchar(rows):
	arr2d = [[j for j in input().strip()] for i in range(rows)]
	return arr2d

def inputmatrixint(rows):
	arr2d = []
	for _ in range(rows):
		arr2d.append([int(i) for i in input().split()])
	return arr2d
t = int(input())
for q in range(t):
	n = inputnum()
	a = inputlist()
	l = inputlist()
	unlocked = []
	for i in range(n):
		if l[i] == 0:
			unlocked.append(a[i])
	unlocked.sort()
	for i in range(n):
		if l[i] == 0:
			a[i] = unlocked[len(unlocked) - 1]
			unlocked.pop()
	print(*a)


for _ in range(int(input())):
	n = int(input())
	ls = list(map(int, input().split()))
	lock = list(map(int, input().split()))
	ar = []
	for i in range(n):
		if not lock[i]:
			ar.append(ls[i])
	ar.sort(reverse=True)
	j = 0
	for i in range(n):
		if not lock[i]:
			ls[i] = ar[j]
			j += 1
	print(*ls)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	vis = list(map(int, input().split()))
	tmp = []
	j = 0
	for i in range(len(a)):
		if not vis[i]:
			tmp.append(a[i])
	tmp.sort(reverse=True)
	for i in range(len(a)):
		if not vis[i]:
			a[i] = tmp[j]
			j += 1
	for i in range(n):
		print(a[i], end=' ')


from collections import defaultdict
test = int(input())
for i in range(test):
	n = int(input())
	a = [int(x) for x in input().split(' ')]
	pos = [int(x) for x in input().split(' ')]
	fre = defaultdict(int)
	temp = []
	for i in range(n):
		if pos[i] == 1:
			fre[i] = 1
		else:
			temp.append(a[i])
	temp.sort(reverse=True)
	j = 0
	for i in range(n):
		if pos[i] != 1:
			a[i] = temp[j]
			j += 1
	print(*a)