Berland Music is a music streaming service built specifically to support Berland local artist. Its developers are currently working on a song recommendation module.

So imagine Monocarp got recommended $n$ songs, numbered from $1$ to $n$. The $i$-th song had its predicted rating equal to $p_i$, where $1 \le p_i \le n$ and every integer from $1$ to $n$ appears exactly once. In other words, $p$ is a permutation.

After listening to each of them, Monocarp pressed either a like or a dislike button. Let his vote sequence be represented with a string $s$, such that $s_i=0$ means that he disliked the $i$-th song, and $s_i=1$ means that he liked it.

Now the service has to re-evaluate the song ratings in such a way that:

the new ratings $q_1, q_2, \dots, q_n$ still form a permutation ($1 \le q_i \le n$; each integer from $1$ to $n$ appears exactly once);

every song that Monocarp liked should have a greater rating than every song that Monocarp disliked (formally, for all $i, j$ such that $s_i=1$ and $s_j=0$, $q_i>q_j$ should hold).

Among all valid permutations $q$ find the one that has the smallest value of $\sum\limits_{i=1}^n |p_i-q_i|$, where $|x|$ is an absolute value of $x$.

Print the permutation $q_1, q_2, \dots, q_n$. If there are multiple answers, you can print any of them.


-----Input-----

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

The first line of each testcase contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of songs.

The second line of each testcase contains $n$ integers $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$) — the permutation of the predicted ratings.

The third line contains a single string $s$, consisting of $n$ characters. Each character is either a $0$ or a $1$. $0$ means that Monocarp disliked the song, and $1$ means that he liked it.

The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$.


-----Output-----

For each testcase, print a permutation $q$ — the re-evaluated ratings of the songs. If there are multiple answers such that $\sum\limits_{i=1}^n |p_i-q_i|$ is minimum possible, you can print any of them.


-----Examples-----

Input
3
2
1 2
10
3
3 1 2
111
8
2 3 1 8 5 4 7 6
01110001
Output
2 1
3 1 2
1 6 5 8 3 2 4 7


-----Note-----

In the first testcase, there exists only one permutation $q$ such that each liked song is rating higher than each disliked song: song $1$ gets rating $2$ and song $2$ gets rating $1$. $\sum\limits_{i=1}^n |p_i-q_i|=|1-2|+|2-1|=2$.

In the second testcase, Monocarp liked all songs, so all permutations could work. The permutation with the minimum sum of absolute differences is the permutation equal to $p$. Its cost is $0$.

Source: codeforces | Difficulty: EASY
Tags: Sorting

Data structures

Mathematics

Greedy algorithms


Solutions:
for _ in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	s = input()
	liked = []
	disliked = []
	for i in range(n):
		if s[i] == '1':
			liked.append([p[i], i])
		else:
			disliked.append([p[i], i])
	liked.sort()
	disliked.sort()
	perm = 1
	final = [0] * n
	for _ in disliked:
		final[_[1]] = perm
		perm += 1
	for _ in liked:
		final[_[1]] = perm
		perm += 1
	print(*final)


from collections import Counter
from math import ceil, floor, log, gcd
import bisect as bs
import sys
input = sys.stdin.readline
inp_lis = lambda : list(map(int, input().split()))
inp_multi = lambda : map(int, input().split())
inp_int = lambda : int(input().strip())
for _ in range(int(input().strip())):
	n = inp_int()
	p = inp_lis()
	s = input().strip()
	o = {}
	z = {}
	for i in range(n):
		if s[i] == '0':
			z[p[i]] = i
		else:
			o[p[i]] = i
	t = len(z)
	for each in sorted(z.keys(), reverse=True):
		p[z[each]] = t
		t -= 1
	t = len(z) + 1
	for each in sorted(o.keys()):
		p[o[each]] = t
		t += 1
	print(*p)


num_cases = int(input())
for i in range(num_cases):
	num_songs = int(input())
	predicted_ratings = list(map(int, input().split()))
	like_dislike_string = input()
	initial_positions_dict = {}
	dislike_list = []
	like_list = []
	for j in range(num_songs):
		if like_dislike_string[j] == '0':
			dislike_list.append(predicted_ratings[j])
		else:
			like_list.append(predicted_ratings[j])
		initial_positions_dict[predicted_ratings[j]] = j
	dislike_list.sort()
	like_list.sort()
	predicted_ratings_sorted = dislike_list + like_list
	final_predict_list = [0] * num_songs
	new_predict = 1
	for value in predicted_ratings_sorted:
		final_predict_list[initial_positions_dict[value]] = new_predict
		new_predict += 1
	print(' '.join(list(map(str, final_predict_list))))


for t in range(int(input())):
	n = int(input())
	p = [int(s) for s in input().rstrip().split()]
	vote = input().rstrip()
	ls = []
	for i in range(n):
		ls.append([vote[i], p[i], i])
	ls.sort(key=lambda v: (v[0], v[1]))
	for i in range(n):
		ls[i].append(i + 1)
	ls.sort(key=lambda v: v[2])
	print(' '.join([str(v[-1]) for v in ls]))


t = int(input())
for _ in range(t):
	n = int(input())
	q = list(map(int, input().split()))
	s = list((int(i) for i in list(input())))
	arr1 = []
	arr2 = []
	for i in range(n):
		if s[i] == 0:
			arr1.append(q[i])
		else:
			arr2.append(q[i])
	arr1.sort()
	arr2.sort()
	h = {}
	c = 1
	for i in arr1:
		h[i] = c
		c += 1
	for i in arr2:
		h[i] = c
		c += 1
	for i in q:
		print(h[i], end=' ')
	print()


t = int(input())
results = []
for i in range(t):
	n = int(input())
	ratings = [int(x) for x in input().split()]
	s = input()
	k = s.count('0')
	rateStore = {'0': [], '1': []}
	posStore = {'0': [], '1': []}
	for j in range(n):
		if s[j] == '0':
			if ratings[j] > k:
				if len(rateStore['1']) != 0:
					ratings[posStore['0'].pop()] = ratings[j]
					ratings[j] = rateStore['1'].pop()
				else:
					rateStore['0'].append(ratings[j])
					posStore['1'].append(j)
		elif ratings[j] <= k:
			if len(rateStore['0']) != 0:
				ratings[posStore['1'].pop()] = ratings[j]
				ratings[j] = rateStore['0'].pop()
			else:
				rateStore['1'].append(ratings[j])
				posStore['0'].append(j)
	results.append(ratings)
for result in results:
	for i in range(len(result)):
		print(result[i], end=' ')
	print('')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = input()
	lik = []
	dikl = []
	for i in range(n):
		if s[i] == '0':
			dikl.append(l[i])
		else:
			lik.append(l[i])
	D = {}
	dikl.sort()
	lik.sort()
	ct0 = s.count('0')
	t = 1
	for i in range(ct0):
		D[dikl[i]] = t
		t += 1
	for i in range(0, n - ct0):
		D[lik[i]] = t
		t += 1
	for i in l:
		print(D[i], end=' ')
	print()


import sys
import bisect
from bisect import bisect_left as lb
from bisect import bisect_right as rb
input_ = lambda : sys.stdin.readline().strip('\r\n')
from math import log
from math import gcd
from math import atan2, acos
from random import randint
sa = lambda : input_()
sb = lambda : int(input_())
sc = lambda : input_().split()
sd = lambda : list(map(int, input_().split()))
sflo = lambda : list(map(float, input_().split()))
se = lambda : float(input_())
sf = lambda : list(input_())
flsh = lambda : sys.stdout.flush()
sys.setrecursionlimit(3 * 10 ** 5)
mod = 10 ** 9 + 7
mod1 = 998244353
gp = []
cost = []
dp = []
mx = []
ans1 = []
ans2 = []
special = []
specnode = []
a = 0
kthpar = []

def dfs2(root, par):
	if par != -1:
		dp[root] = dp[par] + 1
	for i in range(1, 20):
		if kthpar[root][i - 1] != -1:
			kthpar[root][i] = kthpar[kthpar[root][i - 1]][i - 1]
	for child in gp[root]:
		if child == par:
			continue
		kthpar[child][0] = root
		dfs(child, root)
ans = 0
a = []
(n, k) = (0, 0)
b = []
vis = []
tot = 0
time = []
time1 = []
adj = []
mx = -1
eps = 1e-07

def hnbhai(tc):
	n = sb()
	a = sd()
	s = sa()
	one = []
	zero = []
	for i in range(n):
		if s[i] == '1':
			one.append([a[i], i])
		else:
			zero.append([a[i], i])
	one.sort()
	zero.sort()
	if len(one) == 0 or len(zero) == 0:
		print(*a)
		return
	ans = [0] * n
	strt = 1
	for i in range(len(zero)):
		ans[zero[i][1]] = strt
		strt += 1
	for i in range(len(one)):
		ans[one[i][1]] = strt
		strt += 1
	print(*ans)
	return
for _ in range(sb()):
	hnbhai(_ + 1)


for _ in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	s = input()
	pos = {}
	neg = {}
	for i in range(n):
		if s[i] == '1':
			pos[p[i]] = 0
		else:
			neg[p[i]] = 0
	cnt = 1
	for i in sorted(neg):
		neg[i] = cnt
		cnt += 1
	for i in sorted(pos):
		pos[i] = cnt
		cnt += 1
	for i in range(n):
		if s[i] == '1':
			print(pos[p[i]], end=' ')
		else:
			print(neg[p[i]], end=' ')
	print()


n = int(input())
for i in range(n):
	t = int(input())
	a = list(map(int, input().split()))
	s = input()
	a0 = []
	a1 = []
	for j in range(len(a)):
		if s[j] == '0':
			a0.append([a[j], j])
		else:
			a1.append([a[j], j])
	a0.sort(reverse=True)
	a1.sort(reverse=True)
	ans = []
	k = len(a)
	for h in range(len(a1)):
		ans.append([a1[h][1], k])
		k = k - 1
	for h in range(len(a0)):
		ans.append([a0[h][1], k])
		k = k - 1
	ans.sort()
	ans1 = []
	for j in range(len(ans)):
		ans1.append(ans[j][1])
	print(*ans1)


def solve():
	n = int(input())
	List1 = list(map(int, input().split()))
	String = input()
	Dict1 = {}
	Dict0 = {}
	for (i, item) in enumerate(String):
		if item == '1':
			Dict1[List1[i]] = i
		else:
			Dict0[List1[i]] = i
	new = {}
	itr = n
	for i in reversed(sorted(Dict1)):
		loc = Dict1.get(i)
		new[loc] = itr
		itr -= 1
	for i in reversed(sorted(Dict0)):
		loc = Dict0.get(i)
		new[loc] = itr
		itr -= 1
	for i in range(n):
		print(new.get(i), end=' ')
	print()
cases = int(input())
for i in range(cases):
	solve()


def bm(n, r, lr, ld):
	c = 0
	for x in ld:
		if x == 0:
			c = c + 1
	ad = r[:c]
	al = r[c:]
	if (len(ad) == 0) | (len(al) == 0):
		tr = []
		for x in lr:
			tr.append(str(x))
		return ' '.join(tr)
	vl = []
	vd = []
	for i in range(len(lr)):
		if ld[i] == 1:
			vl.append(lr[i])
		else:
			vd.append(lr[i])
	vl.sort()
	vd.sort()
	dd = {}
	for z in zip(vd, ad):
		dd[z[0]] = z[1]
	dl = {}
	for z in zip(vl, al):
		dl[z[0]] = z[1]
	fa = []
	for i in range(len(lr)):
		if ld[i] == 1:
			fa.append(dl.get(lr[i]))
		else:
			fa.append(dd.get(lr[i]))
	tr = []
	for x in fa:
		tr.append(str(x))
	return ' '.join(tr)
t = int(input())
ln = []
ls1 = []
ls2 = []
for x in range(t):
	n = int(input())
	s1 = str(input())
	s2 = str(input())
	ln.append(n)
	ls1.append(s1)
	ls2.append(s2)
la = []
for i in range(len(ls1)):
	n = ln[i]
	r = [a for a in range(1, ln[i] + 1)]
	ar = ls1[i].split(' ')
	lr = []
	for y in ar:
		lr.append(int(y))
	sr = ls2[i]
	ldt = list(sr)
	ld = []
	for z in ldt:
		ld.append(int(z))
	ans = bm(n, r, lr, ld)
	la.append(ans)
for x in la:
	print(x)


import sys
input = sys.stdin.readline
print = sys.stdout.write
t = int(input())
for i in range(t):
	s = int(input())
	song = list(map(int, input().split()))
	vote = input().rstrip()
	like = []
	dislike = []
	for j in range(s):
		if vote[j] == '1':
			like.append([song[j], j])
		else:
			dislike.append([song[j], j])
	like.sort(key=lambda x: -x[0])
	dislike.sort(key=lambda x: -x[0])
	result = [0] * s
	num = s
	for i in range(len(like)):
		result[like[i][1]] = num
		num -= 1
	for i in range(len(dislike)):
		result[dislike[i][1]] = num
		num -= 1
	print(' '.join(list(map(str, result))) + '\n')


t = int(input())
for i in range(t):
	n = int(input())
	p = list(map(int, input().split()))
	s = input()
	a = []
	b = []
	o = [-1] * n
	for j in range(n):
		o[p[j] - 1] = j + 1
	for j in range(n):
		if s[o[j] - 1] == '1':
			a.append(o[j])
		else:
			b.append(o[j])
	z = []
	z += b
	z += a
	ans = [-1] * n
	for j in range(n):
		ans[z[j] - 1] = j + 1
	print(*ans)


n = int(input())
for i in range(n):
	n = int(input())
	y = []
	z = []
	a = [int(x) for x in input().split()]
	for i in range(n):
		y.append(0)
		z.append(0)
	for i in range(n):
		z[a[i] - 1] = i
	s = str(input())
	q = b = 0
	for i in range(n):
		if s[i] == '1':
			q += 1
	b = n - q
	t1 = 1
	t2 = b + 1
	for i in range(n):
		if s[z[i]] == '0':
			y[z[i]] = t1
			t1 += 1
		else:
			y[z[i]] = t2
			t2 += 1
	print(*y)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = input()
	l = s.count('1')
	d = n - l
	m = {}
	for i in a:
		m[i] = 0
	li = []
	dl = []
	for i in range(n):
		if s[i] == '1':
			li.append(a[i])
		else:
			dl.append(a[i])
	li.sort()
	dl.sort()
	c = 1
	for i in dl:
		m[i] = c
		c += 1
	for i in li:
		m[i] = c
		c += 1
	print(*m.values())


def solution(n, ratings, likes):
	ans = [0 for _ in ratings]
	c = 1
	posL = []
	posR = []
	for (i, l) in enumerate(likes):
		if l == '0':
			posL.append((i, ratings[i]))
		else:
			posR.append((i, ratings[i]))
	for (idx, _) in sorted(posL, key=lambda x: x[1]):
		ans[idx] = c
		c += 1
	for (idx, _) in sorted(posR, key=lambda x: x[1]):
		ans[idx] = c
		c += 1
	return ans
for _ in range(int(input())):
	n = int(input())
	ratings = list(map(int, input().strip().split()))
	likes = list(input().strip())
	print(*solution(n, ratings, likes))


import sys

def R():
	return list(map(int, I().split()))
I = sys.stdin.readline
for _ in range(int(I())):
	n = int(I())
	a = R()
	s = I()
	d = {}
	for i in range(n):
		d[a[i]] = s[i]
	z = 0
	y = 1
	for i in d:
		if d[i] == '0':
			z += 1
	for i in range(n):
		if d[i + 1] == '0':
			d[i + 1] = y
			y += 1
		else:
			d[i + 1] = z + 1
			z += 1
	for i in d:
		print(d[i], end=' ')
	print()


def main():
	tests = int(input())
	for i in range(tests):
		n = int(input())
		dict_p = {}
		p = [int(i) for i in input().split()]
		b = [int(i) for i in list(input())]
		for i in p:
			dict_p[i] = -1
		b1 = []
		b2 = []
		for i in range(n):
			if b[i] == 0:
				b1.append(p[i])
			else:
				b2.append(p[i])
		b1.sort()
		b2.sort()
		p.sort()
		b3 = b1 + b2
		for i in range(n):
			dict_p[b3[i]] = p[i]
		list_out = list(dict_p.values())
		list_out = [str(i) for i in list_out]
		print(' '.join(list_out))
main()


import sys
input = sys.stdin.readline

def gt():
	return int(input())

def gs():
	return list(map(int, input().rstrip()))

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

def gst():
	return str(input().rstrip())

def gms():
	return list(map(str, input().split()))
output = sys.stdout.write

def printst(s):
	output(s + '\n')

def prints(n):
	output(str(n) + '\n')

def printm(l):
	output(' '.join(map(str, l)) + '\n')

def printmm(l):
	for line in l:
		printm(line)
t = gt()
for _ in range(t):
	n = gt()
	p = gsn()
	s = gst()
	n_dislikes = s.count('0')
	disliked_change = set(filter(lambda idx: s[idx] == '0' and p[idx] > n_dislikes, range(n)))
	liked_change = set(filter(lambda idx: s[idx] == '1' and p[idx] <= n_dislikes, range(n)))
	for (i, j) in zip(liked_change, disliked_change):
		(p[i], p[j]) = (p[j], p[i])
	printm(p)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = input()
	d = {}
	one = s.count('1')
	start = n - one + 1
	zero = n - one
	ones = []
	zeroes = []
	for i in range(n):
		if s[i] == '1':
			ones.append(a[i])
		else:
			zeroes.append(a[i])
	ones.sort()
	zeroes.sort()
	do = {}
	dz = {}
	for i in range(one):
		do[ones[i]] = start
		start += 1
	ss = 1
	for i in range(zero):
		dz[zeroes[i]] = ss
		ss += 1
	p = []
	for i in range(n):
		if s[i] == '1':
			p.append(do[a[i]])
		else:
			p.append(dz[a[i]])
	print(*p)


t = int(input())
while t > 0:
	n = int(input())
	s = input()
	l = list(map(int, s.split(' ')))
	s = input()
	l2 = []
	for i in s:
		l2.append(int(i))
	ct = 0
	for i in l2:
		if i == 1:
			ct += 1
	n1 = n
	n0 = n - ct
	d1 = {}
	d0 = {}
	for i in range(n):
		if l2[i] == 1:
			d1[l[i]] = i
	for i in range(n):
		if l2[i] == 0:
			d0[l[i]] = i
	d1p = sorted(d1.keys())
	d0p = sorted(d0.keys())
	k = n - ct + 1
	d11 = {}
	d00 = {}
	for i in d1p:
		d11[d1[i]] = k
		k += 1
	k = 1
	for i in d0p:
		d00[d0[i]] = k
		k += 1
	k = 0
	dall = {}
	for i in d11:
		dall[i] = d11[i]
	for i in d00:
		dall[i] = d00[i]
	for i in range(n):
		print(dall[i], end=' ')
	print()
	t -= 1


import sys
t = int(input())
for _ in range(t):
	n = int(input())
	mas = list(map(int, sys.stdin.readline().split()))
	s = sys.stdin.readline()
	c = s.count('1')
	if c < n:
		masp = [0] * c
		masn = [0] * (n - c)
		a = 0
		b = 0
		for i in range(n):
			if s[i] == '1':
				masp[a] = [mas[i], i]
				a += 1
			else:
				masn[b] = [mas[i], i]
				b += 1
		masp.sort(reverse=True)
		masn.sort(reverse=True)
		otv = [0] * n
		a = n
		b = n - c
		for i in range(len(masp)):
			otv[masp[i][1]] = a
			a -= 1
		for i in range(len(masn)):
			otv[masn[i][1]] = b
			b -= 1
		print(*otv)
	else:
		print(*mas)


from math import *
from bisect import bisect_left as bl
from bisect import bisect_right as br
from collections import Counter as ctr
from collections import deque as dq
from array import array
from re import search
inp = lambda : int(input())
li = lambda : list(map(int, input().split()))
ls = lambda : list(input())
arr = lambda a: array('i', a)
bi = lambda n: bin(n).replace('0b', '')
yn = lambda f: print('NYOE S'[f::2])
sbstr = lambda a, s: search('.*'.join(a), s)

def solve():
	for _ in range(inp()):
		n = inp()
		p = li()
		s = input()
		d = sorted([[p[i], i] for i in range(n) if s[i] == '0'])
		l = sorted([[p[i], i] for i in range(n) if s[i] == '1'])
		ans = [0] * n
		for i in range(len(d)):
			ans[d[i][1]] = i + 1
		for i in range(len(l)):
			ans[l[i][1]] = i + 1 + len(d)
		print(*ans)
solve()


from collections import defaultdict
for t in range(int(input())):
	N = int(input())
	arr = list(map(int, input().split()))
	S = input()
	ans = [-1 for i in range(N)]
	num = 1
	lk = []
	dk = []
	for i in range(N):
		if S[i] == '0':
			dk.append(arr[i])
		else:
			lk.append(arr[i])
	lk.sort()
	dk.sort()
	D = defaultdict(int)
	for i in dk:
		D[i] = num
		num += 1
	for i in lk:
		D[i] = num
		num += 1
	for i in range(N):
		ans[i] = D[arr[i]]
	print(*ans)


for _ in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	likes = input()
	limit = 0
	for i in likes:
		if i == '1':
			limit += 1
	if limit == 0 or limit == n:
		for i in range(n):
			print(arr[i], end=' ')
		print('')
		continue
	else:
		(acha, bura) = ([], [])
		for i in range(n):
			if likes[i] == '1':
				acha.append([arr[i], i, likes[i]])
			else:
				bura.append([arr[i], i, likes[i]])
		acha.sort(key=lambda x: x[0])
		bura.sort(key=lambda x: x[0])
		(a, b) = (len(acha), len(bura))
		for i in range(b):
			bura[i][0] = i + 1
		for i in range(a):
			acha[i][0] = b + i + 1
		acha += bura
		acha.sort(key=lambda x: x[1])
		for i in range(n):
			print(acha[i][0], end=' ')
		print('')


t = int(input())
import sys
for _ in range(t):
	d = dict()
	st1 = []
	st2 = []
	n = int(sys.stdin.readline())
	m = list(map(int, sys.stdin.readline().split()))
	s = sys.stdin.readline()
	for i in range(n):
		d[m[i]] = i
	for i in range(n, 0, -1):
		if s[d[i]] == '1':
			st1.append(d[i])
		else:
			st2.append(d[i])
	score = n
	ans = [0] * n
	for i in range(len(st1)):
		ans[st1[i]] = score
		score -= 1
	for i in range(len(st2)):
		ans[st2[i]] = score
		score -= 1
	print(*ans)


def solution():
	n = int(input())
	p = [int(x) for x in input().split()]
	l = input()
	d = {p[x]: l[x] for x in range(n)}
	count = 2
	for i in range(n):
		if d[i + 1] == '0':
			d[i + 1] = count
			count += 1
	for i in range(n):
		if d[i + 1] == '1':
			d[i + 1] = count
			count += 1
	a = []
	for i in p:
		a += [str(d[i] - 1)]
	print(' '.join(a))
try:
	import sys
	import os
	if os.path.exists('2.in.txt'):
		sys.stdin = open('2.in.txt', 'r')
		sys.stdout = open('2.out.txt', 'w')
except:
	pass
t = int(input())
while t:
	solution()
	t -= 1


import os
import sys
from io import BytesIO, IOBase

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')
t = int(input())
for pp in range(t):
	n = int(input())
	lis = list(map(int, input().split()))
	sus = input()
	zeroz = []
	onez = []
	for i in range(len(sus)):
		if sus[i] == '0':
			zeroz.append(lis[i])
		else:
			onez.append(lis[i])
	zeroz.sort()
	onez.sort()
	dic = dict()
	for i in range(len(zeroz)):
		dic[zeroz[i]] = i + 1
	for i in range(len(onez)):
		dic[onez[i]] = len(zeroz) + i + 1
	print(*[dic[i] for i in lis])


import math
for _ in range(int(input())):
	n = int(input())
	songs = list(map(int, input().split()))
	ratings = input()
	arr = []
	for i in range(n):
		arr.append([ratings[i], songs[i], i])
	arr.sort()
	ans = [-1 for x in range(n)]
	for i in range(n):
		ans[arr[i][2]] = i + 1
	print(*ans)


from sys import setrecursionlimit
from bisect import bisect_left, bisect_right
from collections import deque
from functools import lru_cache, reduce
from heapq import heappush, heappop
from math import sqrt, ceil, floor, log2
T = int(input())

def rl(t=int):
	return list(map(t, input().split()))
for t in range(1, T + 1):
	n = int(input())
	p = rl()
	s = rl(str)[0]
	liked = set((i for i in range(n) if s[i] == '1'))
	ret = [-1 for _ in range(n)]
	cur = n
	k = lambda i: p[i]
	for i in sorted(liked, key=k, reverse=True):
		ret[i] = cur
		cur -= 1
	notLiked = set((i for i in range(n) if s[i] == '0'))
	for i in sorted(notLiked, key=k, reverse=True):
		ret[i] = cur
		cur -= 1
	print(' '.join(map(str, ret)))


def solve():
	n = int(input())
	p = [int(x) for x in input().split()]
	s = input()
	L = sorted([(s[i], p[i], i) for i in range(n)])
	q = [-1 for i in range(n)]
	for i in range(n):
		q[L[i][2]] = i + 1
	print(*q)
t = int(input())
for _ in range(t):
	solve()


from math import ceil, log2, floor, sqrt, gcd
from collections import defaultdict, deque
from bisect import bisect_left
import sys
import os.path
from itertools import permutations
sys.setrecursionlimit(200000)
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r+')
	sys.stdout = open('output.txt', 'w')

def MI():
	return map(int, input().split())
mod = 10 ** 9 + 7

def solve(testcase):
	n = int(input())
	a = list(MI())
	s = input()
	d = defaultdict(list)
	for i in range(n):
		d[s[i]].append([a[i], i])
	if len(d['0']) == 0:
		print(*a)
	else:
		st = len(d['0']) + 1
		liked = list(d['1'])
		liked.sort()
		disliked = list(d['0'])
		disliked.sort()
		ans = [0] * n
		c = 1
		for i in range(len(liked)):
			ans[liked[i][1]] = st
			st += 1
		for i in range(len(disliked)):
			ans[disliked[i][1]] = c
			c += 1
		print(*ans)
t = int(input())
for _ in range(t):
	solve(_ + 1)


t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = [int(i) for i in input().split()]
	s = input()
	l = []
	d = []
	for i in range(len(arr)):
		if s[i] == '1':
			l.append(arr[i])
		else:
			d.append(arr[i])
	l.sort()
	d.sort()
	mp = {}
	for i in range(len(l) - 1, -1, -1):
		mp[l[i]] = n
		n -= 1
	for i in range(len(d) - 1, -1, -1):
		mp[d[i]] = n
		n -= 1
	ans = []
	for i in range(len(arr)):
		ans.append(mp[arr[i]])
	print(*ans)


t = int(input())
for _ in range(t):
	n = int(input())
	p = [int(x) for x in input().split(' ')]
	s = input()
	output = [0 for _ in range(n)]
	selected = []
	unselected = []
	for i in range(n):
		if s[i] == '1':
			selected.append((p[i], i))
		else:
			unselected.append((p[i], i))
	selected.sort(reverse=True)
	unselected.sort(reverse=True)
	rating = n
	for (initial_rating, song) in selected:
		output[song] = str(rating)
		rating -= 1
	for (initial_rating, song) in unselected:
		output[song] = str(rating)
		rating -= 1
	print(' '.join(output))


t = int(input())
for _ in range(t):
	n = int(input())
	p = list(map(int, input().split()))
	s = input()
	l = [[s[i], p[i], i] for i in range(n)]
	l.sort()
	q = [-1 for i in range(n)]
	for i in range(n):
		q[l[i][2]] = i + 1
	print(*q)


import sys
n = int(input())
for i in range(n):
	p = int(input())
	L = input().split(' ')
	s = input()
	L1 = []
	for j in range(p):
		L[j] = int(L[j])
		if s[j] == '1':
			L1.append(int(L[j]) + 200000)
		else:
			L1.append(int(L[j]))
	L1.sort()
	mapDic = {}
	for j in range(p):
		if L1[j] > 200000:
			mapDic[L1[j] - 200000] = j + 1
		else:
			mapDic[L1[j]] = j + 1
	for j in range(p):
		L[j] = mapDic[L[j]]
	print(' '.join([str(x) for x in L]))
	sys.stdout.flush()


for _ in range(int(input())):
	n = int(input())
	p = [int(x) for x in input().split()]
	s = input()
	l = sorted([[s[i], p[i], i] for i in range(n)])
	q = [-1 for i in range(n)]
	for i in range(n):
		q[l[i][2]] = i + 1
	print(*q)


import sys, collections, math, bisect, heapq, random, functools, io, os, copy
input = sys.stdin.readline
out = sys.stdout.flush

def rep():
	a = list(map(int, input().split()))
	return a

def sep():
	a = list(input().split())
	return a

class UnionFind:

	def __init__(self, x) -> None:
		self.uf = [-1] * x

	def find(self, x):
		r = x
		while self.uf[x] >= 0:
			x = self.uf[x]
		while r != x:
			(self.uf[r], r) = (x, self.uf[r])
		return x

	def union(self, x, y):
		(ux, uy) = (self.find(x), self.find(y))
		if ux == uy:
			return
		if self.uf[ux] >= self.uf[uy]:
			self.uf[uy] += self.uf[ux]
			self.uf[ux] = uy
		else:
			self.uf[ux] += self.uf[uy]
			self.uf[uy] = ux
		return

	def __print__(self):
		return self.uf

def spfa(x, g, n):
	dis = [float('inf') for i in range(n)]
	dis[x] = 0
	state = [False for i in range(n)]
	state[x] = True
	queue = collections.deque()
	queue.append(x)
	while queue:
		cur = queue.popleft()
		state[cur] = False
		for next_ in g[cur]:
			if dis[next_] > dis[cur] + 1:
				dis[next_] = dis[cur] + 1
				if state[next_] == False:
					state[next_] = True
					queue.append(next_)
	return dis

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

def solve():
	n = int(input())
	a = rep()
	s = input().rstrip('\n')
	(right, left) = ([], [])
	for i in range(n):
		if s[i] == '1':
			left.append([a[i], i])
		else:
			right.append([a[i], i])
	left.sort(key=lambda x: x[0])
	right.sort(key=lambda x: -x[0])
	m = {}
	cnt = len(right)
	l = cnt + 1
	for i in range(len(left)):
		(cur, id) = left[i]
		m[id] = l
		l += 1
	r = cnt
	for i in range(len(right)):
		(cur, id) = right[i]
		m[id] = r
		r -= 1
	ans = []
	for i in range(n):
		ans.append(m[i])
	print(*ans)
for i in range(int(input())):
	solve()


for _ in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	s = input()
	(like, dislike) = ([], [])
	for i in range(n):
		if s[i] == '1':
			like.append(x[i])
		else:
			dislike.append(x[i])
	like.sort(reverse=1)
	dislike.sort()
	d = {}
	for i in range(len(like)):
		d[like[i]] = n - i
	for i in range(len(dislike)):
		d[dislike[i]] = i + 1
	for i in x:
		print(d[i], end=' ')
	print()


sr = lambda : input()
ir = lambda : int(sr())
lr = lambda : list(map(int, sr().split()))
inf = 10 ** 18
mod = 998244353
test = ir()
for t in range(test):
	n = ir()
	a = lr()
	s = sr()
	song_score_rate = []
	for i in range(n):
		song_score_rate.append([i + 1, a[i], int(s[i])])
	song_score_rate.sort(key=lambda x: x[1])
	ans1 = []
	ans2 = []
	for (song, score, rate) in song_score_rate:
		if rate == 0:
			ans1.append(song)
		else:
			ans2.append(song)
	ans = [-1 for i in range(n)]
	now = 1
	for num in ans1:
		ans[num - 1] = now
		now += 1
	for num in ans2:
		ans[num - 1] = now
		now += 1
	print(*ans, sep=' ')


import math

def test():
	n = int(input())
	res = []
	ind = []
	a = list(map(int, input().split()))
	for i in range(n):
		res.append(0)
		ind.append(0)
	for i in range(n):
		ind[a[i] - 1] = i
	s = input()
	q = b = 0
	for i in range(n):
		if s[i] == '1':
			q += 1
	b = n - q
	t1 = 1
	t2 = b + 1
	for i in range(n):
		if s[ind[i]] == '0':
			res[ind[i]] = t1
			t1 += 1
		else:
			res[ind[i]] = t2
			t2 += 1
	for i in range(n):
		print(res[i], end=' ')
	print()
t = int(input())
for i in range(t):
	test()


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	s1 = input()
	a = []
	b = []
	for i in range(n):
		if s1[i:i + 1] == '0':
			a.append(s[i])
		else:
			b.append(s[i])
	a = sorted(a)
	b = sorted(b)
	a.extend(b)
	r = dict(zip(a, [i + 1 for i in range(n)]))
	for i in s:
		print(r[i], end=' ')
	print()


t = int(input())
for caseNr in range(1, t + 1):
	n = int(input())
	parr = list(map(int, input().split()))
	likes = str(input())
	positives = []
	negatives = []
	for i in range(n):
		if likes[i] == '1':
			positives.append([parr[i], i])
		else:
			negatives.append([parr[i], i])
	possibles = n - len(positives)
	possibles = range(possibles + 1, n + 1)
	possibles_n = range(1, n - len(positives) + 1)
	positives.sort()
	negatives.sort()
	ans = [0] * n
	for j in range(len(positives)):
		cur = positives[j]
		ans[cur[1]] = possibles[j]
	for k in range(n - len(positives)):
		cur = negatives[k]
		ans[cur[1]] = possibles_n[k]
	sum_ = 0
	for (i, a) in enumerate(ans):
		sum_ += abs(a - parr[i])
	print(*ans)


t = int(input())
for _ in range(t):
	n = int(input())
	p = list(map(int, input().split()))
	s = input()
	like = []
	dislike = []
	fun1 = dict()
	fun2 = dict()
	index1 = index2 = 0
	for i in range(n):
		if s[i] == '1':
			like.append(p[i])
			fun1[index1] = i
			index1 += 1
		else:
			fun2[index2] = i
			index2 += 1
			dislike.append(p[i])
	if len(like) * len(dislike) == 0:
		print(' '.join([str(pi) for pi in p]))
	else:
		sort1 = sorted(enumerate(like), key=lambda x: x[1])
		sort2 = sorted(enumerate(dislike), key=lambda x: x[1])
		like1 = [x[1] for x in sort1]
		dislike1 = [x[1] for x in sort2]
		idx1 = [x[0] for x in sort1]
		idx2 = [x[0] for x in sort2]
		i = 0
		j = len(dislike) - 1
		while i < len(like) and j >= 0 and (like1[i] < dislike1[j]):
			i += 1
			j -= 1
		for k in range(i):
			id1 = fun1[idx1[k]]
			id2 = fun2[idx2[len(dislike) - 1 - k]]
			(p[id1], p[id2]) = (p[id2], p[id1])
		print(' '.join([str(pi) for pi in p]))


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = input()
	l1 = []
	l2 = []
	for (i, ele) in enumerate(s):
		if ele == '1':
			l1.append(l[i])
		else:
			l2.append(l[i])
	l1.sort()
	l2.sort()
	d = defaultdict(int)
	x = 1
	for ele in l2:
		d[ele] = x
		x += 1
	for ele in l1:
		d[ele] = x
		x += 1
	ans = []
	for ele in l:
		ans.append(d[ele])
	print(*ans)


for _ in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	s = input()
	xd = []
	for i in range(n):
		xd += [[int(s[i]), x[i], i]]
	xd = sorted(xd, reverse=True)
	ans = [0] * n
	temp = n
	for i in range(n):
		ans[xd[i][2]] = temp
		temp -= 1
	for i in range(n):
		print(ans[i], end=' ')
	print()


import os
import sys
from io import BytesIO, IOBase
alphabets = 'abcdefghijklmnopqrstuvwxyz'
from sys import maxsize
from itertools import permutations
import math
Modulo = 998244353

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

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

def int_inp():
	return int(input())
from math import gcd
from collections import Counter
from functools import reduce

def mul(n):
	while n % 2 == 0:
		n = n // 2
	if n == 1:
		return True
	else:
		return False

def solution():
	n = int_inp()
	l = arr_input()
	s = list(map(int, input()))
	li = []
	dli = []
	for i in range(len(s)):
		if s[i] == 1:
			li.append([l[i], i])
		else:
			dli.append([l[i], i])
	li = sorted(li, key=lambda li: li[0], reverse=True)
	dli = sorted(dli, key=lambda dli: dli[0], reverse=True)
	ans = [0] * n
	for i in li:
		ans[i[1]] = n
		n -= 1
	for i in dli:
		ans[i[1]] = n
		n -= 1
	print(*ans)

def main():
	testcases = int(input())
	for _ in range(testcases):
		solution()

def main1():
	testcases = 1
	for _ in range(testcases):
		solution()
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')
main()


def func(n, s, p):
	q = [0] * n
	temp = []
	for i in range(n):
		temp.append([s[i], p[i], i])
	temp = sorted(temp)
	for i in range(n):
		q[temp[i][2]] = i + 1
	print(*q)
t = int(input())
for i in range(t):
	n = int(input())
	p = list(map(int, input().split()))
	s = input()
	func(n, s, p)


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	s = input()
	d = {}
	for i in range(n):
		d[A[i]] = s[i]
	l = sorted(list(d.keys()))
	c0 = s.count('0')
	c1 = s.count('1')
	if c1 == 0 or c0 == 0:
		print(*A)
	else:
		(st0, st1) = (1, c0 + 1)
		g = 1
		ans = {}
		for i in l:
			if d[i] == '1':
				ans[g] = st1
				st1 += 1
			else:
				ans[g] = st0
				st0 += 1
			g += 1
		out = []
		for i in A:
			k = ans[i]
			out.append(k)
		print(*out)


for _ in range(int(input())):
	N = int(input())
	P = list(map(int, input().split()))
	S = input()
	pos = [-1] * (N + 1)
	lis_0 = []
	lis_1 = []
	for i in range(N):
		p = P[i]
		pos[p] = i
		if S[i] == '0':
			lis_0.append(p)
		else:
			lis_1.append(p)
	lis_0.sort()
	lis_1.sort()
	ans = [-1] * N
	ct = 1
	for p in lis_0:
		i = pos[p]
		ans[i] = ct
		ct += 1
	for p in lis_1:
		i = pos[p]
		ans[i] = ct
		ct += 1
	print(*ans)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	s = list(map(int, input().strip()))
	(liked, disliked) = ([], [])
	for i in range(n):
		if s[i]:
			liked.append((p[i], i))
		else:
			disliked.append((p[i], i))
	liked.sort()
	disliked.sort()
	(cnt, ans) = (1, [0] * n)
	for (i, j) in disliked:
		ans[j] = cnt
		cnt += 1
	for (i, j) in liked:
		ans[j] = cnt
		cnt += 1
	print(*ans)


def solve():
	l = int(input())
	ratings = list(map(int, input().split()))
	vsec = list(input())
	liked = []
	disliked = []
	for (i, x) in enumerate(vsec):
		if x == '1':
			liked.append([i, ratings[i]])
		else:
			disliked.append([i, ratings[i]])
	disliked.sort(key=lambda x: x[1])
	liked.sort(key=lambda x: x[1])
	graph = {}
	index = 1
	for x in disliked:
		graph[x[0]] = index
		index += 1
	for x in liked:
		graph[x[0]] = index
		index += 1
	return ' '.join(map(str, [graph[x] for x in range(len(graph))]))
n = int(input())
for i in range(n):
	print(solve())


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	w = map(int, input().split())
	s = input()[:-1]
	p = sorted(zip(s, w, range(n)))
	d = [0] * n
	for i in range(n):
		d[p[i][2]] = i + 1
	print(*d)


for _ in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split(' ')]
	s = input()
	arr2 = [1] * n
	zeros = s.count('0')
	for i in range(n):
		arr2[arr[i] - 1] = i
	ans = [1] * n
	z = 0
	o = 0
	for i in range(n):
		idx = arr2[i]
		if s[idx] == '0':
			ans[idx] = z + 1
			z += 1
		else:
			ans[idx] = o + zeros + 1
			o += 1
	print(*ans, sep=' ')


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	s = list(input().rstrip())
	like = []
	dislike = []
	for i in range(n):
		if s[i] == '1':
			like.append([arr[i], 0])
		else:
			dislike.append([arr[i], 0])
	like.sort()
	dislike.sort()
	for i in range(1, len(dislike) + 1):
		dislike[i - 1][1] = i
	for i in range(1, len(like) + 1):
		like[i - 1][1] = i + len(dislike)
	dic = {}
	for (i, j) in dislike:
		dic[i] = j
	for (i, j) in like:
		dic[i] = j
	ans = [dic[arr[i]] for i in range(n)]
	print(' '.join(map(str, ans)))


t = int(input())
for test in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	s = list(map(int, input()))
	d = {}
	d1 = {}
	z = 0
	for i in range(n):
		d1[i + 1] = 0
		if s[i] == 0:
			z += 1
		d[l[i]] = s[i]
	b = z + 1
	for i in range(1, z + 1):
		if d[i] == 1:
			while b <= n:
				if d[b] == 0:
					d1[b] = i
					d1[i] = b
					b += 1
					break
				b += 1
		else:
			d1[i] = i
	for i in range(z + 1, n + 1):
		if d1[i] == 0:
			d1[i] = i
	for i in l:
		print(d1[i], end=' ')
	print()


t = int(input())
while t != 0:
	n = int(input())
	p = [int(r) for r in input().split(' ')]
	s = input()
	t -= 1
	q = [0 for _ in range(len(s))]
	d_l = []
	for i in range(len(s)):
		if s[i] == '0':
			d_l.append([p[i], i])
	d_l.sort()
	rating = 1
	for i in range(len(d_l)):
		q[d_l[i][1]] = rating
		rating += 1
	l = []
	for i in range(len(s)):
		if s[i] == '1':
			l.append([p[i], i])
	l.sort()
	for i in range(len(l)):
		q[l[i][1]] = rating
		rating += 1
	for i in range(len(q)):
		print(f'{q[i]} ', end='')
	print()


t = int(input())
for _ in range(t):
	n = int(input())
	p = list(map(int, input().split()))
	s = input()
	(d, l) = (dict(), dict())
	(k1, k2) = (list(), list())
	for i in range(n):
		if s[i] == '0':
			d[p[i]] = i
			k1.append(p[i])
		else:
			l[p[i]] = i
			k2.append(p[i])
	dd = len(k1)
	v = 1
	k1.sort()
	k2.sort()
	for i in k1:
		p[d[i]] = v
		v += 1
	if v != 1:
		for i in k2:
			p[l[i]] = v
			v += 1
	for i in p:
		print(i, end=' ')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = input()
	d = {}
	(g, p) = ([], [])
	for i in range(n):
		if s[i] == '0':
			p.append(l[i])
		else:
			g.append(l[i])
	p.sort()
	g.sort()
	p.extend(g)
	k = 1
	for i in p:
		d[i] = k
		k += 1
	for i in l:
		print(d[i], end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	p = [int(i) for i in input().split()]
	zero = [0] * n
	one = [0] * n
	s = input()
	for i in range(len(s)):
		if s[i] == '0':
			zero[p[i] - 1] = 1
		else:
			one[p[i] - 1] = 1
	i = 1
	for k in range(len(zero)):
		if zero[k] == 1:
			zero[k] = i
			i += 1
	for k in range(len(one)):
		if one[k] == 1:
			one[k] = i
			i += 1
	ans = []
	for i in range(len(p)):
		if s[i] == '0':
			ans.append(zero[p[i] - 1])
		else:
			ans.append(one[p[i] - 1])
	print(*ans)


t = int(input())
for i in range(t):
	n = int(input())
	rate = list(map(int, input().split()))
	s = list(input().strip())
	s = [int(s[j]) for j in range(n)]
	perm = [j + 1 for j in range(n)]
	ans = [0] * n
	like = {}
	dislike = {}
	for j in range(n):
		if s[j] == 1:
			like[rate[j]] = j
		else:
			dislike[rate[j]] = j
	temp = list(dislike.keys())
	x = len(temp)
	temp.sort()
	a = 0
	for j in temp:
		ans[dislike[j]] = str(perm[a])
		a += 1
	temp = list(like.keys())
	x = len(temp)
	temp.sort()
	for j in temp:
		ans[like[j]] = str(perm[a])
		a += 1
	print(' '.join(ans))


def Sort_Tuple(tup):
	tup.sort(key=lambda x: x[0])
	return tup
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	s = input()
	arr1 = []
	arr2 = []
	arr5 = [0] * n
	for i in range(len(s)):
		if s[i] == '1':
			arr1.append([arr[i], i])
		else:
			arr2.append([arr[i], i])
	arr3 = Sort_Tuple(arr1)
	arr4 = Sort_Tuple(arr2)
	for i in range(len(arr3)):
		arr3[i][0] = n - len(arr3) + i + 1
	for i in range(len(arr3)):
		arr5[arr3[i][1]] = arr3[i][0]
	for i in range(len(arr4)):
		arr4[i][0] = i + 1
	for i in range(len(arr4)):
		arr5[arr4[i][1]] = arr4[i][0]
	print(' '.join((str(item) for item in arr5)))


def solve(n, rat, s):
	count = n
	rec = dict()
	j = 0
	for i in rat:
		rec[i] = [j, s[j]]
		j += 1
	new_rat = [0] * count
	i = n
	while i > 0:
		k = rec[i]
		if k[1] == '1':
			new_rat[k[0]] = count
			count -= 1
			i -= 1
		else:
			i -= 1
	i = n
	while i > 0:
		k = rec[i]
		if k[1] == '0':
			new_rat[k[0]] = count
			count -= 1
			i -= 1
		else:
			i -= 1
	return new_rat
t = int(input())
for _ in range(t):
	n = int(input())
	rat = [int(i) for i in input().split()]
	s = input()
	ans = solve(n, rat, s)
	for i in ans:
		print(i, end=' ')
	print()


for T in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	ar = [0] * n
	s = input()
	for i in range(n):
		ar[arr[i] - 1] = s[i]
	c = n
	for i in range(n - 1, -1, -1):
		if ar[i] == '1':
			ar[i] = c
			c -= 1
	for i in range(n - 1, -1, -1):
		if ar[i] == '0':
			ar[i] = c
			c -= 1
	for i in range(n):
		print(ar[arr[i] - 1], end=' ')
	print()


t = int(input())
for x in range(t):
	n = int(input())
	P = [int(d) for d in input().split()]
	s = [int(d) for d in list(input())]
	ct = sum(s)
	Q = [0] * n
	used = [0] * (n + 1)
	for a in range(n):
		if s[a] == 1 and P[a] > n - ct:
			Q[a] = P[a]
			used[P[a]] = 1
		elif s[a] == 0 and P[a] <= n - ct:
			Q[a] = P[a]
			used[P[a]] = 1
	hit = n - ct + 1
	lit = n - ct
	while hit <= n and used[hit] == 1:
		hit += 1
	while lit > 0 and used[lit] == 1:
		lit -= 1
	for a in range(n):
		if Q[a] != 0:
			continue
		if s[a] == 1:
			Q[a] = hit
			hit += 1
			while hit <= n and used[hit] == 1:
				hit += 1
		elif s[a] == 0:
			Q[a] = lit
			lit -= 1
			while lit > 0 and used[lit] == 1:
				lit -= 1
	print(' '.join([str(d) for d in Q]))


import os
import sys
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heapify, heappop, heappush, nlargest, nsmallest
from io import BytesIO, IOBase
from math import inf, sqrt
from types import GeneratorType
BUFSIZE: int = 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()
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

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

def bootstrap(f, stack=[]):

	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return wrappedfunc

def modInverse(a, m):
	g = gcd(a, m)
	if g != 1:
		return -1
	else:
		return power(a, m - 2, m)

def power(x, y, m):
	if y == 0:
		return 1
	p = power(x, y // 2, m) % m
	p = p * p % m
	if y % 2 == 0:
		return p
	else:
		return x * p % m

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

def SieveOfEratosthenes(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
	ans = set()
	for p in range(2, n + 1):
		if prime[p]:
			ans.add(p)
	return ans
MOD = 1000000007
INT_MAX = inf
INT_MIN = -inf

def main():
	t = int(input())
	for _ in range(t):
		temp = list(map(int, input().split()))
		n = temp[0]
		p = list(map(int, input().split()))
		s = input()
		p2 = s.count('0')
		k1 = 1
		k2 = p2 + 1
		(ones, zeros) = ([], [])
		for i in range(n):
			if s[i] == '0':
				zeros.append(p[i])
			else:
				ones.append(p[i])
		ones.sort()
		zeros.sort()
		kk = {}
		for i in zeros:
			kk[i] = k1
			k1 += 1
		for i in ones:
			kk[i] = k2
			k2 += 1
		ans = []
		for i in range(n):
			ans.append(kk[p[i]])
		print(' '.join(map(str, ans)))
main()


t = int(input())
for i in range(t):
	n = int(input())
	p_rating = list(map(int, list(input().split())))
	k = input()
	o = []
	l = []
	arr = [0] * n
	count_0 = 0
	for i in range(n):
		if k[i] == '0':
			count_0 = count_0 + 1
			o.append([p_rating[i], i])
		else:
			l.append([p_rating[i], i])
	o = sorted(o)
	l = sorted(l)
	for i in range(1, count_0 + 1):
		arr[o[i - 1][1]] = i
	v = list(range(count_0 + 1, n + 1))
	for i in range(n - count_0):
		arr[l[i][1]] = v[i]
	for i in range(n):
		print(arr[i], end=' ')
	print()


for t in range(int(input())):
	n = int(input())
	P = [int(i) for i in input().split()]
	s = input()
	(D, L) = (list(), list())
	for i in range(n):
		if s[i] == '0':
			D.append(P[i])
		else:
			L.append(P[i])
	D.sort()
	L.sort()
	D = D + L
	(d, ans) = (dict(), list())
	for i in range(n):
		d[D[i]] = i + 1
	for i in range(n):
		ans.append(d[P[i]])
	print(*ans)


import bisect
mod = 10 ** 9 + 7
eps = 10 ** (-9)

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

def __lcm(a, b):
	return a * b / __gcd(a, b)

def __fact(n):
	return 1 if n == 1 else n * __fact(n - 1)

def __mex(a):
	mex = 0
	a.sort()
	for x in a:
		if x <= mex:
			mex += 1
		else:
			break
	return mex

def __dist(x1, y1, x2, y2):
	return (x1 - x2) ** 2 + (y1 - y2) ** 2

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

def __modInverse(a, m):
	m0 = m
	y = 0
	x = 1
	if m == 1:
		return 0
	while a > 1:
		q = a // m
		t = m
		m = a % m
		a = t
		t = y
		y = x - q * y
		x = t
	if x < 0:
		x = x + m0
	return x

def __isprime(n):
	if n < 2:
		return False
	i = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += 1
	return True

def __cntprimediv(n):
	ret = 0
	x = n
	i = 2
	while i * i <= x:
		while n % i == 0:
			n //= i
			ret += 1
		i += 1
	if n > 1:
		ret += 1
	return ret

def __primefactors(n):
	ret = []
	x = n
	i = 2
	while i * i <= x:
		while n % i == 0:
			ret.append(i)
			n //= i
		i += 1
	if n > 1:
		ret.append(n)
	return ret

def __sumdigit(n):
	ret = 0
	while n > 0:
		ret += n % 10
		n //= 10
	return ret

def __zfunc(s):
	n = len(s)
	z = [0 for i in range(n)]
	l = 0
	r = 0
	for i in range(1, n):
		if r >= i:
			z[i] = min(z[i - l], r - i + 1)
		while z[i] + i < n and s[z[i]] == s[z[i] + i]:
			z[i] += 1
		if i + z[i] - 1 > r:
			l = i
			r = i + z[i] - 1
	return z

def __to(n, x):
	ret = ''
	while n > 0:
		q = n % x
		if q < 10:
			ret += str(q)
		else:
			ret += chr(q - 10 + ord('a'))
		n //= x
	return ret[::-1]

def solve(t):
	n = int(input())
	arr = list(map(int, input().split()))
	s = input()
	a = []
	b = []
	for i in range(n):
		if s[i] == '1':
			a.append([arr[i], i])
		else:
			b.append([arr[i], i])
	ans = [0 for i in range(n)]
	a.sort(reverse=True)
	val = n
	b.sort()
	for x in a:
		ans[x[1]] = val
		val -= 1
	val = 1
	for x in b:
		ans[x[1]] = val
		val += 1
	print(*ans)
t = 1
t = int(input())
for i in range(t):
	solve(i + 1)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = input()
	c = 0
	ms = 0
	ma = []
	ma1 = []
	mb = []
	mb1 = []
	for j in range(len(s)):
		if s[j] == '1':
			c += 1
	ma = [[0 for i in range(2)] for j in range(c)]
	mb = [[0 for i in range(2)] for j in range(n - c)]
	c1 = 0
	c2 = 0
	for j in range(len(s)):
		if s[j] == '1':
			ma[c1][0] = a[j]
			ma[c1][1] = j
			c1 += 1
		else:
			mb[c2][0] = a[j]
			mb[c2][1] = j
			c2 += 1
	ma.sort()
	mb.sort()
	ca = 0
	cb = 0
	tr = [0] * n
	for j in range(c):
		tr[ma[c - j - 1][1]] = n - j
	for j in range(n - c):
		tr[mb[n - c - j - 1][1]] = n - c - j
	for j in range(n):
		print(tr[j], end=' ')
	print()


def solve(arr, n):
	memo = {}
	for i in range(len(arr)):
		memo[arr[i]] = i
	arr.sort()
	ans = [0] * len(arr)
	for i in range(len(arr)):
		ans[memo[arr[i]]] = n
		n += 1
	return ans[::-1]
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	s = list(input())
	zero = []
	one = []
	for i in range(n):
		if s[i] == '1':
			one.append(arr[i])
		else:
			zero.append(arr[i])
	sol_zero = solve(zero, 1)
	sol_one = solve(one, len(zero) + 1)
	sol = []
	for i in s:
		if i == '1':
			sol.append(sol_one.pop())
		else:
			sol.append(sol_zero.pop())
	print(' '.join(list(map(str, sol))))


import sys
from sys import stdin, stdout
from math import ceil, floor, sqrt, log, log2, log10
from collections import Counter
int_max = float('inf')
int_min = -float('inf')
mod = 10 ** 9 + 7
ANS = []
input = stdin.readline

def solve():
	pass
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	s = input().strip()
	ind = [0] * (n + 1)
	for i in range(n):
		ind[arr[i]] = i + 1
	lk = []
	dk = []
	for i in range(n):
		if s[i] == '0':
			dk.append(arr[i])
		else:
			lk.append(arr[i])
	lk.sort()
	dk.sort()
	out = [0] * (n + 1)
	curr = 1
	for i in dk:
		out[ind[i]] = curr
		curr += 1
	for i in lk:
		out[ind[i]] = curr
		curr += 1
	out.pop(0)
	print(*out)


import sys

def solve(n, nums, s):
	z = 0
	zero_list = []
	ones_list = []
	for i in range(n):
		if s[i] == '0':
			z += 1
			zero_list.append((nums[i], i))
		else:
			ones_list.append((nums[i], i))
	if z == n or z == 0:
		return nums
	zero_list.sort(key=lambda x: x[0], reverse=True)
	ones_list.sort(key=lambda x: x[0])
	o = z + 1
	for (val, idx) in zero_list:
		nums[idx] = z
		z -= 1
	for (val, idx) in ones_list:
		nums[idx] = o
		o += 1
	return nums
t = int(input())
while t:
	n = int(input())
	nums = list(map(int, input().split()))
	s = input()
	print(*solve(n, nums, s))
	t -= 1


for i in range(int(input())):
	N = int(input())
	P = list(map(int, input().split()))
	S = input()
	G = 1
	B = 1
	L = []
	for j in range(N):
		if S[j] == '0':
			B += 1
		L.append((P[j], j))
	k = 1
	g = B
	L.sort()
	ans = [-1] * N
	for j in range(N):
		if S[L[j][1]] == '0':
			ans[L[j][1]] = k
			k += 1
		else:
			ans[L[j][1]] = g
			g += 1
	print(*ans)


cases = int(input())
for _ in range(cases):
	num = int(input())
	arr = list(map(int, input().split()))
	s = input()
	likes = [n for (n, i) in zip(arr, s) if i == '1']
	hates = [n for (n, i) in zip(arr, s) if i == '0']
	l_num = num - len(likes)
	likes.sort()
	lst1 = list(range(l_num + 1, num + 1))
	hates.sort()
	lst2 = list(range(1, l_num + 1))
	likes_map = {n: p for (n, p) in zip(likes, lst1)}
	hates_map = {n: p for (n, p) in zip(hates, lst2)}
	ans = []
	likes_map.update(hates_map)
	for n in arr:
		ans.append(likes_map[n])
	print(*ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = input()
	l = sorted([(s[i], l[i], i) for i in range(n)])
	w = [0] * n
	for i in range(n):
		w[l[i][2]] = str(i + 1)
	print(' '.join(w))


def solve():
	for _ in range(int(input())):
		n = int(input())
		arr = list(map(int, input().split()))
		s = input()
		ans = [-1 for _ in range(n)]
		l = []
		for i in range(n):
			l.append([s[i], arr[i], i])
		l = sorted(l)
		for i in range(n):
			ans[l[i][2]] = i + 1
		print(' '.join(list(map(str, ans))))
solve()


t = int(input())
while t:
	n = int(input())
	A = list(map(int, input().split(' ')))
	s = str(input())
	D = {}
	(B, C) = ([], [])
	for i in range(n):
		D[A[i]] = i
	for i in range(n):
		if s[i] == '1':
			B.append(A[i])
		else:
			C.append(A[i])
	B.sort()
	C.sort()
	ans = [0] * n
	m = len(B)
	if m == len(A):
		m = 1
	else:
		m = n - m + 1
	for i in range(len(B)):
		ans[D[B[i]]] = m
		m = m + 1
	m = 1
	for i in range(len(C)):
		ans[D[C[i]]] = m
		m = m + 1
	print(*ans)
	t = t - 1


t = int(input())
for test_case in range(t):
	n = int(input())
	int_list = list(map(int, input().split()))
	s = input()
	zero_count = 0
	one_count = 0
	for char in s:
		if char == '0':
			zero_count += 1
		else:
			one_count += 1
	lowest_like_num = zero_count + 1
	dislike_num = zero_count
	wrong_zero_list = []
	wrong_one_list = []
	for i in range(n):
		if s[i] == '0':
			if int_list[i] <= dislike_num:
				continue
			elif int_list[i] > dislike_num:
				wrong_one_list.append(int_list[i])
		elif s[i] == '1':
			if int_list[i] <= dislike_num:
				wrong_zero_list.append(int_list[i])
			elif int_list[i] > dislike_num:
				continue
	zero_element = 0
	one_element = 0
	for i in range(n):
		if s[i] == '0':
			if int_list[i] <= dislike_num:
				continue
			else:
				int_list[i] = wrong_zero_list[zero_element]
				zero_element += 1
		elif s[i] == '1':
			if int_list[i] > dislike_num:
				continue
			else:
				int_list[i] = wrong_one_list[one_element]
				one_element += 1
	for i in int_list:
		print(i, end=' ')
	print()


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	p = [int(c) for c in input().split(' ')]
	q = [0] * n
	zeroes = list()
	ones = list()
	d = dict()
	s = input()
	for i in range(n):
		d[p[i]] = i
		if s[i] == '0':
			zeroes.append(p[i])
		else:
			ones.append(p[i])
	zeroes.sort(reverse=True)
	ones.sort(reverse=True)
	p.sort(reverse=True)
	i = 0
	while i < len(ones):
		q[d[ones[i]]] = p[i]
		i += 1
	for j in range(len(zeroes)):
		q[d[zeroes[j]]] = p[i]
		i += 1
	for i in range(n):
		print(q[i], end=' ')
	print()


t = int(input())
for _ in range(t):
	n = int(input())
	li = []
	p = list(map(int, input().split()))
	for i in range(n):
		li.append([p[i], i])
	s = str(input())
	li.sort()
	res = n
	for i in range(n - 1, -1, -1):
		if s[li[i][1]] == '1':
			li[i][0] = res
			res -= 1
	for i in range(n - 1, -1, -1):
		if s[li[i][1]] == '0':
			li[i][0] = res
			res -= 1
	for i in range(n - 1, -1, -1):
		(li[i][0], li[i][1]) = (li[i][1], li[i][0])
	li.sort()
	for i in range(n):
		print(li[i][1], end=' ')
	print()


t = int(input())

def sol(n, p, s):
	res = p[::1]
	sl = [int(x) for x in s]
	disliked = n - sum(sl)
	if disliked == 0:
		return res
	(b, a) = (1, disliked + 1)
	x = []
	y = []
	for (i, v) in enumerate(sl):
		if v == 1:
			x.append([i, p[i]])
		else:
			y.append([i, p[i]])
	x.sort(key=lambda num: num[1])
	y.sort(key=lambda num: num[1])
	for i in range(len(x)):
		res[x[i][0]] = a
		a += 1
	for i in range(len(y)):
		res[y[i][0]] = b
		b += 1
	return res
while t:
	t -= 1
	n = int(input())
	p = list(map(int, input().split()))
	s = input()
	res = sol(n, p, s)
	print(*res)


import sys
import io, os
import string
from io import BytesIO, IOBase
from os import path
from collections import Counter, OrderedDict
import itertools
from itertools import chain, combinations
import math
import random
import bisect
from functools import reduce
from sys import stdin, stdout
from collections import OrderedDict
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	input = sys.stdin.readline
sys.setrecursionlimit(100000000)
simp = 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))
n = simp()

def if_its_binaryform_palindrome(n):
	res = 0
	k = n
	while k > 0:
		res = res << 1 | k & 1
		k = k >> 1
	return n == res

def solve(a, d):
	dd = [0] * len(a)
	rr = {}
	s = {}
	for j in range(len(d)):
		if d[j] == '0':
			rr[j] = a[j]
		else:
			s[j] = a[j]
	i = 0
	rrr = {k: v for (k, v) in sorted(rr.items(), key=lambda item: item[1])}
	sss = {k: v for (k, v) in sorted(s.items(), key=lambda item: item[1])}
	rr_r = {}
	ss_s = {}
	while i < len(rr):
		rr_r[i] = i + 1
		i += 1
	j = 0
	while j < len(sss):
		ss_s[i] = i + 1
		i += 1
		j += 1
	zeroes = dict(zip(rrr.keys(), rr_r.values()))
	first = dict(zip(sss.keys(), ss_s.values()))
	if len(zeroes) != 0:
		for (i, j) in zeroes.items():
			dd[i] = j
	if len(first) != 0:
		for (i, j) in first.items():
			dd[i] = j
	return dd
for i in range(n):
	d = simp()
	a = seq()
	r = strng()
	print(*solve(a, r))

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

def list_of_prime_sieve(n):
	primes = [True] * (n + 1)
	primes[0] == primes[1] == False
	for i in range(2, int(n ** (1 / 2)) + 1):
		if primes[i] == True:
			for j in range(i * i, n + 1, i):
				primes[j] = False
	list_primes = []
	for i in range(2, len(primes)):
		if primes[i] == True:
			list_primes.append(i)
	return list_primes

def count_primes(a):
	return len(list_of_prime_sieve(a))

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

def lcm(a, b):
	return a * b // gcdoftwo(a, b)

def gcdofList(A):
	res = A[0]
	for c in A[1:]:
		res = gcdoftwo(res, c)
		if res == 1:
			return res
	return res

def binpow(a, b):
	res = 1
	while b > 0:
		if b & 1:
			res = res * a
		a = a * a
		b >>= 1
	return res

def fibo_in_logn(n):
	if n == 0:
		return [0, 1]
	p = fibo_in_logn(n >> 1)
	c = p.first * (2 * p.second - p.first)
	d = p.first * p.first + p.second * p.second
	if n & 1:
		return {d, c + d}
	else:
		return {c, d}

def x_n_mod_m(a, b, m):
	a = a % m
	res = 1
	while b > 0:
		if b & 1:
			res = res * a % m
		a = a * a % m
		b >>= 1
	return res

def change_array(count, t):
	emp = []
	for i in range(len(t)):
		emp.append(count[t[i]])
	return emp

def simpleSieve(limit):
	mark = [True for i in range(limit + 1)]
	p = 2
	while p * p <= limit:
		if mark[p] == True:
			for i in range(p * p, limit + 1, p):
				mark[i] = False
		p += 1
	for p in range(2, limit):
		if mark[p]:
			prime.append(p)
	return prime

def segmentedSieve(n):
	limit = int(math.floor(math.sqrt(n)) + 1)
	t = []
	t += simpleSieve(limit)
	low = limit
	high = limit * 2
	while low < n:
		if high >= n:
			high = n
		mark = [True for i in range(limit + 1)]
		for i in range(len(prime)):
			loLim = int(math.floor(low / prime[i]) * prime[i])
			if loLim < low:
				loLim += prime[i]
			for j in range(loLim, high, prime[i]):
				mark[j - low] = False
		for i in range(low, high):
			if mark[i - low]:
				t.append(i)
		low = low + limit
		high = high + limit
	return t
mod = 1000000007

def occurrences(string, sub):
	count = start = 0
	while True:
		start = string.find(sub, start) + 1
		if start > 0:
			count += 1
		else:
			return count


def solve(n, a, s):
	ar = [0] * n
	if s == ['1'] * n or s == ['0'] * n:
		a = list(map(str, a))
		return ' '.join(a)
	for i in range(n):
		ar[a[i] - 1] = i
	c = 1
	ans = [0] * n
	for i in range(n):
		if s[ar[i]] == '0':
			ans[ar[i]] = c
			c += 1
	for i in range(n):
		if s[ar[i]] == '1':
			ans[ar[i]] = c
			c += 1
	ans = list(map(str, ans))
	return ' '.join(ans)
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = list(input())
	print(solve(n, a, s))


n = int(input())
for i in range(n):
	t = 0
	r = 0
	m = 0
	f = 0
	flag = True
	likes_list = []
	predict_rates_of_likes = []
	to_small_rates = []
	good_rates = []
	num_of_songs = int(input())
	predict_songs_rates = list(map(int, input().split()))
	likes = input()
	all_rates = list(range(1, num_of_songs + 1))
	for j in range(num_of_songs):
		likes_list.append(int(likes[j]))
	for j in range(num_of_songs):
		if likes_list[j] == 1:
			predict_rates_of_likes.append(predict_songs_rates[j])
	good_rates = [0] * len(predict_rates_of_likes)
	for j in range(len(predict_rates_of_likes)):
		if predict_rates_of_likes[j] <= num_of_songs - len(predict_rates_of_likes):
			to_small_rates.append(predict_rates_of_likes[j])
			flag = False
		else:
			good_rates[j] = predict_rates_of_likes[j]
	if flag:
		for j in range(num_of_songs):
			print(predict_songs_rates[j], end=' ')
	else:
		change_rates_likes = []
		good_rates.sort()
		for j in range(num_of_songs, num_of_songs - len(predict_rates_of_likes), -1):
			if good_rates[len(good_rates) - 1 - m] != j:
				change_rates_likes.append(j)
			else:
				m += 1
		for j in range(num_of_songs):
			if likes_list[j] == 1 and predict_songs_rates[j] <= num_of_songs - len(predict_rates_of_likes):
				predict_songs_rates[j] = change_rates_likes[t]
				t += 1
			elif likes_list[j] == 0 and predict_songs_rates[j] > num_of_songs - len(predict_rates_of_likes):
				predict_songs_rates[j] = to_small_rates[r]
				r += 1
		for j in range(num_of_songs):
			print(predict_songs_rates[j], end=' ')
	print()


for i in range(int(input())):
	n = int(input())
	inp = [int(i) for i in input().split()]
	s = input()
	arr = [(inp[i], i, s[i]) for i in range(n)]
	arr.sort()
	count = n - s.count('1') + 1
	count1 = 1
	a = []
	for i in range(n):
		a.append(-1)
	for (i, j, k) in arr:
		if k == '1':
			a[j] = count
			count += 1
		else:
			a[j] = count1
			count1 += 1
	for i in range(n):
		print(a[i], end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	s = input()
	ar_zip = sorted(zip(s, ar, range(n)))
	list1 = [0] * n
	for i in range(n):
		list1[ar_zip[i][2]] = i + 1
	print(*list1)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	s = input()
	like = {}
	dis = {}
	for i in range(n):
		if s[i] == '1':
			like[arr[i]] = 0
		else:
			dis[arr[i]] = 0
	k = len(dis)
	k += 1
	for e in sorted(like):
		like[e] = k
		k += 1
	k = 1
	for e in sorted(dis):
		dis[e] = k
		k += 1
	for i in range(n - 1):
		if s[i] == '1':
			print(like[arr[i]], end=' ')
		else:
			print(dis[arr[i]], end=' ')
	if s[-1] == '1':
		print(like[arr[-1]])
	else:
		print(dis[arr[-1]])


for _ in range(int(input())):
	n = int(input())
	rr = list(map(int, input().split()))
	ss = input()
	kk = set(ss)
	if len(kk) == 1:
		print(*rr)
	else:
		z1 = []
		o1 = []
		ones = ss.count('1')
		zeros = n - ones
		z2 = list(range(1, zeros + 1))
		o2 = list(range(zeros + 1, n + 1))
		for i in range(n):
			if ss[i] == '1':
				o1.append(rr[i])
			else:
				z1.append(rr[i])
		o1.sort()
		z1.sort()
		onemap = {}
		zeromap = {}
		for i in range(len(o1)):
			onemap[o1[i]] = o2[i]
		for i in range(len(z1)):
			zeromap[z1[i]] = z2[i]
		ans = []
		for i in range(n):
			if ss[i] == '0':
				ans.append(zeromap[rr[i]])
			else:
				ans.append(onemap[rr[i]])
		print(*ans)


from sys import stdin
input = stdin.readline
rn = lambda : int(input())
rns = lambda : map(int, input().split())
rl = lambda : list(map(int, input().split()))
rs = lambda : input().strip()
YN = lambda x: print('YES') if x else print('NO')
ceil_div = lambda a, b: -(-a // b)
mod = 10 ** 9 + 7
for _ in range(rn()):
	n = rn()
	a = rl()
	s = rs()
	dis = [(a[i], i) for i in range(n) if s[i] == '0']
	lik = [(a[i], i) for i in range(n) if s[i] == '1']
	dis.sort()
	lik.sort()

	def swap(i, j):
		(a[i], a[j]) = (a[j], a[i])
	i = 0
	j = 0
	while True:
		while i < len(dis) and dis[i][0] <= len(dis):
			i += 1
		while j < len(lik) and lik[j][0] > len(dis):
			j += 1
		if i == len(dis) or j == len(lik):
			break
		swap(dis[i][1], lik[j][1])
		i += 1
		j += 1
	print(*a)


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
sys.setrecursionlimit(100000000)

def solve():
	n = int(input())
	arr = list(map(int, input().split()))
	x = input()
	one = [arr[idx] for (idx, ch) in enumerate(x) if ch == '1']
	zero = list(set(arr) - set(one))
	one.sort()
	zero.sort()
	i = 1
	d = dict()
	for j in range(len(zero)):
		d[zero[j]] = i
		i += 1
	for j in range(len(one)):
		d[one[j]] = i
		i += 1
	for i in arr:
		print(d.get(i), end=' ')
	print()

def main():
	t = int(input())
	while t:
		solve()
		t -= 1
main()


def solve(n, p, s):
	(cnt0, cnt1) = (0, 0)
	for i in s:
		if i == 0:
			cnt0 += 1
		else:
			cnt1 += 1
	res = [-1 for i in range(n)]
	x = [i + 1 for i in range(n)]
	for i in range(n):
		if s[i] == 1:
			if p[i] > cnt0:
				res[i] = p[i]
				x[p[i] - 1] = -1
		elif p[i] <= cnt0:
			res[i] = p[i]
			x[p[i] - 1] = -1
	(x1, x0) = (n - 1, 0)
	for i in range(n):
		if res[i] == -1:
			if s[i] == 1:
				while x[x1] == -1:
					x1 -= 1
				res[i] = x[x1]
				x[x1] = -1
			else:
				while x[x0] == -1:
					x0 += 1
				res[i] = x[x0]
				x[x0] = -1
	return res
for i in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	s = [int(i) for i in input()]
	print(*solve(n, p, s))


t = int(input())
for k in range(t):
	n = int(input())
	p = list(map(int, input().split()))
	s = input()
	c1 = s.count('1')
	fixlike = []
	fixunlike = []
	for i in range(n):
		if s[i] == '1':
			if p[i] <= n - c1:
				fixlike.append([p[i], i])
		elif p[i] > n - c1:
			fixunlike.append([p[i], i])
	for i in range(max(len(fixlike), len(fixunlike))):
		(p[fixlike[i][1]], p[fixunlike[i][1]]) = (p[fixunlike[i][1]], p[fixlike[i][1]])
	print(*p)


from sys import stdin
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	arr = list(map(int, stdin.readline().split()))
	s = input()
	d = {}
	for i in range(n):
		d[arr[i]] = i + 1
	z = []
	o = []
	for i in range(n):
		if s[i] == '0':
			z.append(i)
		else:
			o.append(i)
	z.sort(key=lambda x: arr[x])
	o.sort(key=lambda x: arr[x])
	ans = [0 for i in range(n)]
	k = 1
	for i in z:
		ans[i] = k
		k += 1
	for i in o:
		ans[i] = k
		k += 1
	if z == [] or o == []:
		print(*arr)
	else:
		print(*ans)


n = int(input())
for i in range(n):
	j = int(input())
	arr = [int(i) for i in input().split()]
	s = [int(i) for i in list(input())]
	oks = s.count(1)
	res = [0 for i in range(j)]
	ups = {}
	downs = {}
	for k in range(len(s)):
		if s[k]:
			ups[k] = arr[k]
		else:
			downs[k] = arr[k]
	ups = dict(sorted(ups.items(), key=lambda item: item[1]))
	downs = dict(sorted(downs.items(), key=lambda item: item[1]))
	o = 1
	for (k, v) in downs.items():
		res[k] = o
		o += 1
	for (k, v) in ups.items():
		res[k] = o
		o += 1
	print(' '.join([str(i) for i in res]))


t = int(input())
for _ in range(t):
	n = int(input())
	p = list(map(int, input().split()))
	s = str(input())
	d = {}
	for i in range(n):
		d[p[i]] = i
	selected = []
	rej = []
	for i in range(n):
		if s[i] == '1':
			selected.append(p[i])
		else:
			rej.append(p[i])
	ans = [-1 for i in range(n)]
	num = n
	selected = sorted(selected, reverse=True)
	for x in selected:
		ans[d[x]] = num
		num -= 1
	num = 1
	rej = sorted(rej)
	for x in rej:
		ans[d[x]] = num
		num += 1
	print(' '.join(map(str, ans)))


from collections import deque
for _ in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	s = input()
	if s.count('0') == 0:
		for i in p:
			print(i, end=' ')
	else:
		(a, b) = (s.count('0'), s.count('1'))
		(r0, r1) = (deque([]), deque([]))
		(p0, p1) = (deque([]), deque([]))
		for i in range(n):
			if s[i] == '0' and p[i] > a:
				r0.append(p[i])
				p0.append(i)
			if s[i] == '1' and p[i] <= a:
				r1.append(p[i])
				p1.append(i)
		for i in p0:
			p[i] = r1.pop()
		for i in p1:
			p[i] = r0.pop()
		for i in range(n):
			print(p[i], end=' ')


def main():
	N = int(input())
	for alpha in range(N):
		list0 = []
		dic = {}
		list1 = []
		n = int(input())
		list_raw = list(map(int, input().split()))
		str = input()
		for i in range(n):
			if str[i] == '0':
				list0.append(list_raw[i])
			else:
				list1.append(list_raw[i])
		list0.sort()
		list1.sort()
		list0.extend(list1)
		plus = 1
		for i in range(n):
			dic[list0[i]] = plus
			plus += 1
		output = []
		for i in range(n):
			print(dic[list_raw[i]], end=' ')
main()


t = int(input())
while t:
	t -= 1
	n = int(input())
	p = list(map(int, input().split()))
	s = input()
	ss = set(s)
	if '1' not in ss or '0' not in ss:
		print(*p)
	else:
		ones = []
		zeroes = []
		i = 0
		while i < n:
			if s[i] == '1':
				ones.append([p[i], i])
			else:
				zeroes.append([p[i], i])
			i += 1
		ones.sort()
		zeroes.sort()
		a = [0] * n
		num = 0
		for (ele, idx) in zeroes:
			num += 1
			a[idx] = num
		for (ele, idx) in ones:
			num += 1
			a[idx] = num
		print(*a)


from collections import Counter, defaultdict, deque
import bisect
from sys import stdin, stdout
from itertools import repeat
import math

def inp(force_list=False):
	re = list(map(int, input().split()))
	if len(re) == 1 and (not force_list):
		return re[0]
	return re

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

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
MOD = 998244353

def query(a, b, c):
	print('! %d %d %d' % (a + 1, b + 1, c + 1))
	stdout.flush()
	return inp()

def solve():
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	a = sorted(a)
	cost = sum(a[0:n - 2 * k]) + sum(map(lambda x: a[x + n - 2 * k] // a[x + n - k], range(0, k)))
	print(cost)

def my_main():
	kase = inp()
	pans = []
	for _ in range(kase):
		n = inp()
		da = inp(True)
		st = inst()
		(z, o) = ([], [])
		for i in range(n):
			if st[i] == '1':
				o.append((da[i], i))
			else:
				z.append((da[i], i))
		o.sort()
		z.sort()
		ans = [0] * n
		now = 1
		for i in range(len(z)):
			ans[z[i][-1]] = now
			now += 1
		for i in range(len(o)):
			ans[o[i][-1]] = now
			now += 1
		pans.append(' '.join(map(str, ans)))
	print('\n'.join(pans))
my_main()


t = int(input())
for j in range(0, t):
	n = int(input())
	l = list(map(int, input().split()))
	s = input()
	f = []
	q = []
	d = []
	for i in range(0, n):
		q.append(0)
		d.append(0)
		f.append('0')
	for i in range(0, n):
		q[l[i] - 1] = i + 1
		if s[i] == '1':
			f[l[i] - 1] = '1'
	c = n
	for i in range(0, n):
		if f[n - i - 1] == '1':
			d[q[n - i - 1] - 1] = str(c)
			c -= 1
	for i in range(0, n):
		if f[n - i - 1] == '0':
			d[q[n - i - 1] - 1] = str(c)
			c -= 1
	print(' '.join(d))


t = int(input())
while t > 0:
	n = int(input())
	arr = list()
	a = list(map(int, input().split()))
	for i in range(n):
		arr.append([a[i], i])
	s = input()
	arr.sort()
	ans = n
	for i in range(n - 1, -1, -1):
		if s[arr[i][1]] == '1':
			arr[i][0] = ans
			ans -= 1
	for i in range(n - 1, -1, -1):
		if s[arr[i][1]] == '0':
			arr[i][0] = ans
			ans -= 1
	for i in range(n - 1, -1, -1):
		(arr[i][0], arr[i][1]) = (arr[i][1], arr[i][0])
	arr.sort()
	for i in range(n):
		print(arr[i][1], end=' ')
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = input()
	b = []
	c = []
	ans = [0] * n
	val = 1
	for i in range(n):
		if s[i] == '0':
			b.append([a[i], i])
			c.append(val)
			val += 1
	b.sort()
	for i in range(len(b)):
		ans[b[i][1]] = c[i]
	b = []
	c = []
	for i in range(n):
		if s[i] == '1':
			b.append([a[i], i])
			c.append(val)
			val += 1
	b.sort()
	for i in range(len(b)):
		ans[b[i][1]] = c[i]
	print(*ans)


import sys
import math
from collections import deque, Counter
from sys import stdin
int1 = lambda x: int(x) - 1
stinput = lambda : stdin.readline()[:-1]
ii = lambda : int(stinput())
mi = lambda : map(int, stdin.readline().split())
li = lambda : list(mi())
mi1 = lambda : map(int1, stdin.readline().split())
li1 = lambda : list(mi1())
mis = lambda : map(str, stdin.readline().split())
lis = lambda : list(mis())
from collections import defaultdict
import heapq
mod = 10 ** 9 + 7
Mod = 998244353
INF = 10 ** 18
ans = 0
t = ii()
for _ in range(t):
	n = ii()
	p = li1()
	s = input()
	c1 = []
	c0 = []
	for i in range(n):
		if s[i] == '0':
			c0.append((p[i], i))
		else:
			c1.append((p[i], i))
	ans = [0] * n
	c0.sort()
	c1.sort()
	tmp = 1
	for (x, y) in c0:
		ans[y] = tmp
		tmp += 1
	for (x, y) in c1:
		ans[y] = tmp
		tmp += 1
	print(*ans)


for _ in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	s = input()
	li = [0] * (n + 1)
	for i in range(1, n + 1):
		if s[i - 1] == '1':
			li[p[i - 1]] = 1
			p[i - 1] += n
	for i in range(1, n + 1):
		li[i] += li[i - 1]
	for i in range(n):
		if p[i] <= n:
			print(p[i] - li[p[i]], end=' ')
		else:
			print(n - li[n] + 1 + li[p[i] - n - 1], end=' ')
	print()


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	s = input()
	d = defaultdict(list)
	res = []
	count0 = s.count('0')
	(num1, num2) = (1, count0 + 1)
	sets = set()
	for i in range(n):
		if s[i] == '0':
			if p[i] <= count0:
				sets.add(p[i])
		elif p[i] > count0:
			sets.add(p[i])
	for i in range(n):
		if s[i] == '0':
			if p[i] <= count0:
				res.append(p[i])
			else:
				while True:
					if num1 not in sets:
						res.append(num1)
						num1 += 1
						break
					num1 += 1
		elif p[i] > count0:
			res.append(p[i])
		else:
			while True:
				if num2 not in sets:
					res.append(num2)
					num2 += 1
					break
				num2 += 1
	print(*res)


from __future__ import division, print_function
import math
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

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

def si():
	return input()

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

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(1000000)
	threading.stack_size(1024000)
	thread = threading.Thread(target=main)
	thread.start()
from math import log, sqrt, factorial, cos, tan, sin, radians
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def getKey(item):
	return item[1]

def sort2(l):
	return sorted(l, key=getKey, reverse=True)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

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

def ntl(n):
	return [int(i) for i in str(n)]

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def ceil(x, y):
	if x % y == 0:
		return x // y
	else:
		return x // y + 1

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

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

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 ** 2 <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i += 6
	return True

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

def main():
	for _ in range(ii()):
		n = ii()
		l = li()
		s = si()
		a = []
		b = []
		for i in range(n):
			if s[i] == '0':
				a.append(l[i])
			else:
				b.append(l[i])
		a.sort()
		b.sort()
		c = 1
		ans = dict()
		for i in a:
			ans[i] = c
			c += 1
		for i in b:
			ans[i] = c
			c += 1
		for i in l:
			print(ans[i], 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 = 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))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from dis import dis
test = int(input())
for i in range(test):
	songs = int(input())
	firstrat = [int(x) for x in input().split()]
	liking = input()
	dislike = 0
	decrease = []
	increase = []
	for j in range(songs):
		if liking[j] == '0':
			dislike += 1
	for j in range(songs):
		if firstrat[j] > dislike and liking[j] == '0':
			decrease.append(j)
		elif liking[j] == '1' and firstrat[j] <= dislike:
			increase.append(j)
	moves = len(decrease)
	for k in range(moves):
		inl = decrease[k]
		inh = increase[k]
		stable = firstrat[inl]
		firstrat[inl] = firstrat[inh]
		firstrat[inh] = stable
	print(*firstrat)


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

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

def fi():
	return float(input())

def si():
	return input()

def msi():
	return map(str, input().split())

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

def li():
	return list(mi())

def lsi():
	return list(msi())

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

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 gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def lcm(x, y):
	return x * y // gcd(x, y)
mod = 1000000007

def modInverse(b, m):
	g = gcd(b, m)
	if g != 1:
		return -1
	else:
		return pow(b, m - 2, m)

def ceil2(x, y):
	if x % y == 0:
		return x // y
	else:
		return x // y + 1

def modu(a, b, m):
	a = a % m
	inv = modInverse(b, m)
	if inv == -1:
		return -999999999
	else:
		return inv * a % m
from math import log, factorial, cos, tan, sin, radians, floor, sqrt, ceil, log2
import bisect
import random
import string
from itertools import permutations
from decimal import *
getcontext().prec = 50
abc = 'abcdefghijklmnopqrstuvwxyz'
pi = 3.141592653589793

def myKey(x):
	return (x[0], x[1])

def main():
	for _ in range(ii()):
		n = ii()
		p = li()
		s = si()
		l = []
		for i in range(n):
			l.append([int(s[i]), p[i], i])
		l.sort(key=myKey)
		ans = [0] * (n + 1)
		c = 1
		for i in range(n):
			ans[l[i][2]] = c
			c += 1
		print(*ans[0:-1])
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))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


testcases = int(input())
for i in range(testcases):
	n = int(input())
	l = [int(x) for x in input().split(' ')]
	q = input()
	won = 0
	loss = 0
	for i in range(len(q)):
		if q[i] == '0':
			loss += 1
	won = loss + 1
	loss = 1
	wl = [0] * (n + 1)
	for i in range(0, n):
		if q[i] == '0':
			wl[l[i]] = 0
		else:
			wl[l[i]] = 1
	for i in range(1, n + 1):
		if wl[i] == 1:
			wl[i] = won
			won += 1
		else:
			wl[i] = loss
			loss += 1
	for i in range(len(l)):
		print(wl[l[i]], end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = input()
	n1 = s.count('0')
	a1 = [0] * n
	a2 = [0] * n
	a3 = [0] * n
	a4 = [0] * n
	for i in range(n):
		if s[i] == '1':
			a1[a[i] - 1] = 1
		else:
			a1[a[i] - 1] = 0
	l = 0
	d = 0
	for i in range(n):
		if a1[i] == 1:
			l += 1
		else:
			d += 1
		a2[i] = l
		a3[i] = d
	for i in range(n):
		if s[i] == '0':
			a4[i] = a3[a[i] - 1]
		else:
			a4[i] = a2[a[i] - 1] + n1
	print(*a4)


t = int(input())
for _ in range(t):
	n = int(input())
	p = list(map(int, input().split()))
	q = [0] * n
	s = input()
	(liked, disliked) = ([], [])
	for i in range(n):
		song = i + 1
		if s[i] == '1':
			liked.append((p[i], i))
		else:
			disliked.append((p[i], i))
	liked.sort()
	disliked.sort()
	for i in range(len(disliked)):
		q[disliked[i][1]] = i + 1
	for i in range(len(liked)):
		q[liked[i][1]] = len(disliked) + i + 1
	print(*q)


n_testcase = int(input())
for _ in range(n_testcase):
	n = int(input())
	pre_rating = list(map(int, input().split()))
	vote = input()
	disliked_song = []
	liked_song = []
	for i in range(n):
		if vote[i] == '1':
			liked_song.append([i, pre_rating[i]])
		else:
			disliked_song.append([i, pre_rating[i]])
	liked_song.sort(key=lambda x: x[1])
	disliked_song.sort(key=lambda x: x[1])
	n_disliked_song = len(disliked_song)
	n_liked_song = len(liked_song)
	for i in range(n_disliked_song):
		pre_rating[disliked_song[i][0]] = i + 1
	for i in range(n_liked_song):
		pre_rating[liked_song[i][0]] = i + 1 + n_disliked_song
	print(' '.join(map(str, pre_rating)))


for _ in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	s = input()
	(zeros, ones) = ([], [])
	for i in range(n):
		if s[i] == '0':
			zeros.append(p[i])
		else:
			ones.append(p[i])
	if not zeros or not ones:
		for x in p:
			print(x, end=' ')
	else:
		zeros.sort()
		ones.sort()
		dic = {}
		start = 1
		for x in zeros:
			dic[x] = start
			start += 1
		for y in ones:
			dic[y] = start
			start += 1
		for x in p:
			print(dic[x], end=' ')
	print()


t = int(input())
for t1 in range(t):
	n = int(input())
	plist = list(map(int, input().split()))
	sbev = input()
	dislike_s = 1
	like_s = 1
	dislike_list = []
	like_list = []
	for i in range(len(sbev)):
		if sbev[i] == '0':
			like_s += 1
			dislike_list.append(plist[i])
		else:
			like_list.append(plist[i])
	dislike_list = sorted(dislike_list)
	like_list = sorted(like_list)
	dislike_index = dict()
	like_index = dict()
	answer = []
	for i in range(len(dislike_list)):
		dislike_index[dislike_list[i]] = i
	for i in range(len(like_list)):
		like_index[like_list[i]] = i
	for i in range(len(sbev)):
		if sbev[i] == '1':
			answer.append(like_index[plist[i]] + like_s)
		else:
			answer.append(dislike_index[plist[i]] + dislike_s)
	print(' '.join((str(x) for x in answer)))


for i in range(int(input())):
	n = int(input())
	rating = list(map(int, input().split(' ')))
	string = input()
	liked = []
	disliked = []
	for j in range(n):
		if string[j] == '1':
			liked.append(rating[j])
		else:
			disliked.append(rating[j])
	liked.sort()
	disliked.sort()
	liked_sorted = liked
	disliked_sorted = disliked
	dict = {}
	for k in range(len(liked_sorted)):
		dict[liked_sorted[-k - 1]] = n - k
	for k in range(len(disliked_sorted)):
		dict[disliked_sorted[k]] = k + 1
	out = ''
	for z in range(len(rating)):
		out += str(dict[rating[z]]) + ' '
	out.strip()
	print(out)


N = int(200000.0)
t = int(input())
x = [0 for _ in range(N)]
x2 = [0 for _ in range(N)]
s2 = [0 for _ in range(N)]
q = [0 for _ in range(N)]
for i in range(t):
	n = int(input())
	p = [int(x) - 1 for x in input().split(' ')]
	s = input()
	for i in range(n):
		x[p[i]] = i
		s2[p[i]] = s[i]
	j = 0
	for i in range(n):
		if s2[i] == '0':
			x2[j] = x[i]
			j += 1
	for i in range(n):
		if s2[i] == '1':
			x2[j] = x[i]
			j += 1
	for i in range(n):
		q[x2[i]] = i
	print(' '.join([str(a + 1) for a in q[:n]]))


import heapq
import sys
import math
from collections import deque
from bisect import bisect_left, bisect_right
I = sys.stdin.readline
II = lambda : int(I())
IN = lambda x: map(int, x.split())
FN = lambda x: map(float, x.split())
L = lambda x: list(IN(x))
M = 1000000007
P = print
T = True
F = False
for _ in range(II()):
	n = II()
	p = L(I())
	s = input()
	nz = 0
	for i in s:
		if i == '0':
			nz += 1
	no = len(s) - nz
	lz = []
	lo = []
	for i in range(n):
		if s[i] == '0' and p[i] > nz:
			lz.append((p[i], i))
		if s[i] == '1' and p[i] <= nz:
			lo.append((p[i], i))
	lz.sort()
	lo.sort()
	for i in range(len(lz)):
		(p[lz[i][1]], p[lo[i][1]]) = (p[lo[i][1]], p[lz[i][1]])
	print(*p, sep=' ')


t = int(input())
for _ in range(t):
	n = int(input())
	p = list(map(int, input().split()))
	s = input()
	lst = []
	lstt = []
	for i in range(n):
		if int(s[i]) == 1:
			lstt.append([p[i], i])
		else:
			lst.append([p[i], i])
	lst.sort()
	lstt.sort()
	lsttt = []
	for j in range(1, n + 1):
		if j <= len(lst):
			lst[j - 1][0] = j
			lsttt.append([lst[j - 1][1], lst[j - 1][0]])
		else:
			lstt[j - len(lst) - 1][0] = j
			lsttt.append([lstt[j - len(lst) - 1][1], lstt[j - len(lst) - 1][0]])
	lsttt.sort()
	for i in range(n):
		lsttt[i] = lsttt[i][1]
	print(*lsttt)


def solve():
	n = int(input())
	p = list(map(int, input().split()))
	s = input()
	arr = [(char, pri, idx) for (char, pri, idx) in zip(s, p, range(n))]
	arr.sort()
	result = [0 for _ in range(n)]
	for i in range(n):
		result[arr[i][2]] = i + 1
	print(' '.join(map(str, result)))
t = int(input())
for _ in range(t):
	solve()


cases = int(input())
while cases > 0:
	n = int(input())
	p = list(map(int, input().split()))
	q = list(input())
	f = []
	s = []
	ind = {}
	for i in range(n):
		if q[i] == '0':
			f.append(p[i])
		else:
			s.append(p[i])
		ind[p[i]] = i
	f.sort()
	s.sort()
	j = 1
	for i in f:
		p[ind[i]] = j
		j += 1
	for i in s:
		p[ind[i]] = j
		j += 1
	for i in p:
		print(i, end=' ')
	print()
	cases -= 1


result = []
for _ in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	s = list(map(int, list(input())))
	count = s.count(0)
	zeros = sorted([[p[i], i] for i in range(n) if s[i] == 0])
	ones = sorted([[p[i], i] for i in range(n) if s[i] == 1])
	answer = [None for i in range(n)]
	for (i, (value, index)) in enumerate(zeros):
		answer[index] = i + 1
	for (i, (value, index)) in enumerate(ones):
		answer[index] = count + i + 1
	result.append(answer)
print('\n'.join(map(lambda x: ' '.join(map(str, x)), result)))


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def main(t):
	n = int(input())
	arr = list(map(int, input().split()))
	s = input().decode('utf-8')
	arr1 = []
	arr2 = []
	for i in range(n):
		if s[i] == '1':
			arr1.append([arr[i], i])
		else:
			arr2.append([arr[i], i])
	arr1.sort()
	arr2.sort()
	method = [0] * n
	arrtot = arr2 + arr1
	for i in range(n):
		method[arrtot[i][1]] = i + 1
	print(*method)
T = int(input())
t = 1
while t <= T:
	main(t)
	t += 1


num = int(input())
for _ in range(num):
	n = int(input())
	ratings = []
	ratings = list(input().split())
	result = list(input())
	for i in range(n):
		ratings[i] = int(ratings[i])
	rating_with_idx = list(enumerate(ratings))
	good_song = []
	bad_song = []
	for i in range(n):
		if result[i] == '1':
			good_song.append(rating_with_idx[i])
		else:
			bad_song.append(rating_with_idx[i])
	good_song = sorted(good_song, key=lambda x: x[1])
	bad_song = sorted(bad_song, key=lambda x: x[1])
	good_rate = result.count('0') + 1
	bad_rate = 1
	ans = [0] * n
	for song in good_song:
		ans[song[0]] = good_rate
		good_rate += 1
	for song in bad_song:
		ans[song[0]] = bad_rate
		bad_rate += 1
	for i in range(n):
		print(ans[i], end=' ')


testcases = int(input())
for i in range(testcases):
	n = int(input())
	l = [int(x) for x in input().split(' ')]
	q = input()
	likedsong = 0
	unlikedsong = 0
	for i in range(len(q)):
		if q[i] == '0':
			unlikedsong += 1
	likedsong = unlikedsong + 1
	unlikedsong = 1
	likeunlike = [0] * (n + 1)
	for i in range(0, n):
		if q[i] == '0':
			likeunlike[l[i]] = 0
		else:
			likeunlike[l[i]] = 1
	for i in range(1, n + 1):
		if likeunlike[i] == 1:
			likeunlike[i] = likedsong
			likedsong += 1
		else:
			likeunlike[i] = unlikedsong
			unlikedsong += 1
	for i in range(len(l)):
		print(likeunlike[l[i]], end=' ')
	print()


t = int(input())
for i in range(0, t):
	n = int(input())
	lis = list(map(int, input().split()))
	s = input()
	tej = [0] * (n + 1)
	count0 = 0
	ans = [0]
	for j in range(0, n):
		if s[j] == '0':
			tej[lis[j]] = 0
			count0 = count0 + 1
		else:
			tej[lis[j]] = 1
		ans.append(0)
	y = 1
	z = count0 + 1
	for g in range(1, n + 1):
		if tej[g] == 1:
			ans[g] = z
			z = z + 1
		else:
			ans[g] = y
			y = y + 1
	k = ''
	for c in lis:
		print(ans[c], end=' ')
	print('')


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	ans = [0] * n
	p = list(map(int, input().split()))
	p = list(enumerate(p))
	s = input()
	s0 = []
	s1 = []
	for i in range(n):
		if s[i] == '0':
			s0.append(p[i])
		else:
			s1.append(p[i])
	s0.sort(key=lambda x: x[1])
	s1.sort(key=lambda x: x[1])
	k = 1
	for i in range(len(s0)):
		ans[s0[i][0]] = k
		k += 1
	for i in range(len(s1)):
		ans[s1[i][0]] = k
		k += 1
	print(*ans)


t = int(input())
for i in range(t):
	mapper1 = {}
	mapper2 = {}
	ans = []
	n = int(input())
	rating = input().split()
	rating = [int(j) for j in rating]
	for (j, k) in enumerate(rating):
		mapper1[k] = j
	ld = list(input())
	ld = [int(j) for j in ld]
	a = 1
	b = ld.count(0) + 1
	for j in range(1, n + 1):
		if ld[mapper1[j]] == 0:
			mapper2[mapper1[j]] = a
			a += 1
		else:
			mapper2[mapper1[j]] = b
			b += 1
	for j in range(n):
		ans.append(mapper2[j])
	ans = [str(j) for j in ans]
	print(' '.join(ans))


from __future__ import division, print_function
import math
import sys
import os
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')

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))
input = lambda : sys.stdin.readline().rstrip('\r\n')

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

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

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

def insr():
	s = input().strip()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
for _ in range(inp()):
	n = inp()
	p = inlt()
	s = insr()
	l = []
	d = []
	for i in range(n):
		if s[i] == '1':
			l.append([p[i], i])
		else:
			d.append([p[i], i])
	l.sort()
	d.sort()
	vl = [len(d) + i + 1 for i in range(len(l))]
	vd = [1 + i for i in range(len(d))]
	ans = [0 for i in range(n)]
	while d:
		(a, b) = d.pop()
		idx = vd.pop()
		ans[b] = idx
	while l:
		(a, b) = l.pop()
		idx = vl.pop()
		ans[b] = idx
	print(*ans)


def solve(n, l, s):
	one = s.count('1')
	m = n - one + 1
	a = []
	b = []
	for i in range(n):
		if l[i] < m and s[i] == '1':
			a.append(l[i])
		elif l[i] >= m and s[i] == '0':
			b.append(l[i])
	(j, k) = (0, 0)
	for i in range(n):
		if l[i] < m and s[i] == '1':
			l[i] = b[j]
			j += 1
		elif l[i] >= m and s[i] == '0':
			l[i] = a[k]
			k += 1
	print(*l)
t = int(input())
while t:
	n = int(input())
	l = [int(i) for i in input().split(' ')]
	s = input()
	solve(n, l, s)
	t -= 1


from itertools import permutations as per
from math import factorial as fact
from difflib import SequenceMatcher
import bisect
t = int(input())
for _ in range(t):
	n = int(input())
	p = [[int(v), idx] for (idx, v) in enumerate(input().split())]
	s = input()
	i = 1
	p.sort()
	for j in range(n):
		if s[p[j][1]] == '0':
			p[j][0] = i
			i += 1
	for j in range(n):
		if s[p[j][1]] == '1':
			p[j][0] = i
			i += 1
	p.sort(key=lambda x: x[1])
	for i in p:
		print(i[0], end=' ')
	print()


for i in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	s = input()
	zero = s.count('0')
	l = []
	all = []
	for j in range(n):
		all.append(j + 1)
	for j in range(n):
		if s[j] == '0' and p[j] <= zero or (s[j] == '1' and p[j] > zero):
			l.append(p[j])
			all[p[j] - 1] = 0
		elif s[j] == '0':
			l.append(-1)
		else:
			l.append(-2)
	zeroes = all[:zero]
	ones = all[zero:]
	zeroes.sort(reverse=True)
	ones.sort(reverse=True)
	z = 0
	o = 0
	for j in range(n):
		if l[j] == -1:
			l[j] = zeroes[z]
			z += 1
		elif l[j] == -2:
			l[j] = ones[o]
			o += 1
	print(*l, sep=' ')


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split(' ')))
	s = input()
	cnt = s.count('0')
	lmemo = [0 for _ in range(cnt)]
	umemo = [0 for _ in range(n - cnt)]
	for i in range(n):
		if s[i] == '0' and a[i] <= cnt:
			lmemo[a[i] - 1] = 1
		if s[i] == '1' and a[i] > cnt:
			umemo[a[i] - cnt - 1] = 1
	lcnt = 0
	ucnt = 0
	for i in range(n):
		if s[i] == '0' and a[i] > cnt:
			while True:
				if lmemo[lcnt] == 1:
					lcnt += 1
				else:
					break
			a[i] = lcnt + 1
			lcnt += 1
		if s[i] == '1' and a[i] <= cnt:
			while True:
				if umemo[ucnt] == 1:
					ucnt += 1
				else:
					break
			a[i] = ucnt + 1 + cnt
			ucnt += 1
	print(' '.join(map(str, a)))


for _ in range(int(input())):
	x = int(input())
	per = list(map(int, input().split()))
	string = input()
	cnt1 = string.count('1')
	if cnt1 == x:
		print(*per)
		continue
	hash1 = [0] * (x + 1)
	hash2 = [0] * (x + 1)
	temp1 = x - cnt1 + 1
	temp2 = 1
	for i in range(x):
		if string[i] == '1':
			hash1[per[i]] = 1
		else:
			hash1[per[i]] = 0
	for i in range(1, x + 1):
		if hash1[i] == 0:
			hash2[i] = temp2
			temp2 += 1
		else:
			hash2[i] = temp1
			temp1 += 1
	ans = []
	for i in per:
		ans.append(hash2[i])
	print(*ans)


import sys
input = sys.stdin.readline

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

def read_strs():
	s = input().strip()
	return list(map(int, list(s)))

def solve(p, s):
	lower = [rating for (index, rating) in enumerate(p) if s[index] == 0]
	upper = [rating for (index, rating) in enumerate(p) if s[index] == 1]
	index_map = {}
	for (index, rating) in enumerate(p):
		index_map[rating] = index
	lower.sort()
	upper.sort()
	combined = lower + upper
	result = [0 for _ in p]
	for (index, rating) in enumerate(combined):
		result[index_map[rating]] = index + 1
	return result
n = int(input())
for i in range(n):
	m = int(input())
	p = read_ints()
	s = read_strs()
	print(*solve(p, s))


ans = []
for _ in range(int(input())):
	n = int(input())
	nums = list(map(int, input().split()))
	rating = [int(c) for c in input()]
	new_nums = [(rating[i], nums[i], i) for i in range(n)]
	idx_map = {nums[i]: i for i in range(n)}
	new_nums.sort()
	i = 0
	temp = [0] * n
	for (_, num, _) in new_nums:
		temp[idx_map[num]] = i + 1
		i += 1
	ans.append(temp)
for a in ans:
	print(*a)


for i in range(int(input())):
	(n, p, s) = (int(input()), [int(i) for i in input().split()], input())
	p = sorted(zip(s, p, range(n)))
	w = [0] * n
	for i in range(n):
		w[p[i][2]] = i + 1
	print(*w)
	zip()


for _ in range(int(input())):
	n = int(input())
	predicted = list(map(int, input().strip().split()))
	liked = input().strip()
	zeroes = liked.count('0')
	smallest = []
	largest = []
	for i in range(n):
		if liked[i] == '1':
			largest.append(predicted[i])
		else:
			smallest.append(predicted[i])
	smallest.sort()
	largest.sort()
	reevaluated = {}
	for i in range(zeroes):
		reevaluated[smallest[i]] = i + 1
	for i in range(len(largest)):
		reevaluated[largest[i]] = zeroes + i + 1
	for v in predicted:
		print(reevaluated[v], end=' ')
	print()


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = input()
	x = []
	y = []
	for i in range(n):
		if s[i] == '1':
			x.append(a[i])
		else:
			y.append(a[i])
	y.sort()
	x.sort()
	d = {}
	c = 1
	for i in range(len(y)):
		d[y[i]] = c
		c += 1
	for i in range(len(x)):
		d[x[i]] = c
		c += 1
	for i in a:
		print(d[i], end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = input()
	d = []
	e = []
	for i in range(len(s)):
		if s[i] == '0':
			d.append(a[i])
		else:
			e.append(a[i])
	d.sort()
	e.sort()
	x = {}
	y = {}
	for i in range(len(d)):
		x[d[i]] = i + 1
	for i in range(len(e)):
		y[e[i]] = len(d) + 1 + i
	for i in range(len(a)):
		if a[i] in x.keys():
			a[i] = x[a[i]]
		else:
			a[i] = y[a[i]]
	print(*a)


for _ in range(int(input())):
	n = int(input())
	p = (*map(int, input().split()),)
	s = input()
	good = []
	bad = []
	for i in range(n):
		if s[i] == '1':
			good.append(p[i])
		else:
			bad.append(p[i])
	d = {rating: index for (index, rating) in enumerate(sorted(bad), 1)}
	d.update({rating: index for (index, rating) in enumerate(sorted(good), len(bad) + 1)})
	print(' '.join((str(d[rating]) for rating in p)))


import sys
import math
from math import gcd, sqrt, log2, ceil, floor
import heapq as hq
from bisect import bisect_right, bisect_left, bisect
import itertools
from sys import stdin, stdout, setrecursionlimit

def isPowerOfTwo(x):
	return x and (not x & x - 1)
MOD = int(1000000000.0) + 7
alp = 'abcdefghijklmnopqrstuvwxyz'
v = {'a': 1, 'e': 1, 'i': 1, 'o': 1, 'u': 1}
c = {}
T = 1
T = int(stdin.readline())
for _ in range(T):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().rstrip().split()))
	s = list(stdin.readline().strip('\n'))
	N = n
	A = [0] * n
	b = []
	for i in range(n):
		b.append([a[i], s[i], i])
	b.sort()
	for i in range(n - 1, -1, -1):
		if b[i][1] == '1':
			A[b[i][2]] = N
			N -= 1
	for i in range(n - 1, -1, -1):
		if b[i][1] == '0':
			A[b[i][2]] = N
			N -= 1
	print(*A)


t = int(input())
for q in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	s = input()
	(mx, mn, dict, c) = ([], [], {}, 1)
	for i in range(n):
		dict[arr[i]] = i
		if s[i] == '0':
			mn.append(arr[i])
		else:
			mx.append(arr[i])
	ans = [None for i in range(n)]
	mx.sort()
	mn.sort()
	for i in range(len(mn)):
		ans[dict[mn[i]]] = c
		c += 1
	for i in range(len(mx)):
		ans[dict[mx[i]]] = c
		c += 1
	print(*ans)


t = int(input())
for i in range(t):
	n = int(input())
	li = list(map(int, input().split()))
	s = input()
	low = []
	high = []
	for i in range(len(li)):
		if s[i] == '0':
			low.append(li[i])
		else:
			high.append(li[i])
	low = sorted(low)
	high = sorted(high)
	dic = {}
	for (i, j) in enumerate(low + high, start=1):
		dic[j] = i
	for i in li:
		print(dic[i], end=' ')
	print()


v = int(input().strip())
for val in range(v):
	n = int(input().strip())

	class data:

		def __init__(self, x):
			self.x = x
			self.y = None

		def __repr__(self):
			return '({},{})'.format(self.x, self.y)
	p = [data(int(x)) for x in input().strip().split(' ')]
	s = input().strip()
	liked = []
	disliked = []
	for i in range(n):
		if s[i] == '1':
			liked.extend([p[i]])
		else:
			disliked.extend([p[i]])

	def Bsort(L, n):
		X = [None for i in range(n)]
		for val in L:
			X[(val.x - 1) % n] = val
		Y = []
		for i in X:
			if i != None:
				Y.extend([i])
		return Y
	liked = Bsort(liked, n)
	disliked = Bsort(disliked, n)
	disliked.extend(liked)
	for i in range(n):
		disliked[i].y = i + 1
	for i in p:
		print(i.y, end=' ')
	print()


from collections import *
for _ in range(int(input())):
	n = int(input())
	pi = list(map(int, input().split()))
	s = str(input())
	g = []
	l = []
	gg = defaultdict(int)
	ll = defaultdict(int)
	for i in range(n):
		if s[i] == '1':
			g.append(pi[i])
		else:
			l.append(pi[i])
	g.sort()
	l.sort()
	v = 1
	for i in range(len(l)):
		ll[l[i]] = v
		v += 1
	for i in range(len(g)):
		ll[g[i]] = v
		v += 1
	ans = []
	for i in range(n):
		ans.append(ll[pi[i]])
	print(*ans)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	p = [int(num) for num in input().split()]
	s = [int(num) for num in input().strip()]
	q = []
	start = 1
	num_of_zeros = s.count(0)
	start2 = num_of_zeros + 1
	were = set((p[ind] for ind in range(n) if s[ind] == 0))
	were2 = set((p[ind] for ind in range(n) if s[ind] == 1))
	for ind in range(n):
		if s[ind] == 0:
			if p[ind] <= num_of_zeros:
				q.append(p[ind])
			else:
				while start in were:
					start += 1
				q.append(start)
				were.add(start)
		elif p[ind] > num_of_zeros:
			q.append(p[ind])
		else:
			while start2 in were2:
				start2 += 1
			q.append(start2)
			were2.add(start2)
	print(*q)


cases = int(input())
for i in range(cases):
	n = int(input())
	entrada = input().split()
	p = []
	for j in entrada:
		p.append(int(j))
	entrada = input()
	s = [int(j) for j in entrada]
	liked_indexes = []
	disliked_indexes = []
	reverse_p = [0 for i in p]
	for i in range(n):
		reverse_p[p[i] - 1] = i
	for j in range(n, 0, -1):
		index = reverse_p[j - 1]
		if s[index] == 1:
			liked_indexes.append(index)
		else:
			disliked_indexes.append(index)
	priority = liked_indexes + disliked_indexes
	value = n
	for i in priority:
		p[i] = value
		value -= 1
	for j in range(len(p) - 1):
		print(p[j], end=' ')
	print(p[-1])


t = int(input())
for _ in range(t):
	n = int(input())
	p = list(map(int, input().split()))
	s = input()
	l = [0] * (n * 2)
	c = 0
	for i in range(n):
		if s[i] == '1':
			l[p[i] - 1] = 1
			c += 1
	no = n - c
	x = 1
	y = no + 1
	for k in range(n):
		if l[k] == 0:
			l[k + n] = x
			x += 1
		else:
			l[k + n] = y
			y += 1
	for j in range(n):
		index = p[j] - 1 + n
		print(l[index], end=' ')
	print('')


import atexit, io, sys, math, heapq, collections

def get_int():
	return int(sys.stdin.readline().strip())

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

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

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

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

def write_str(a):
	sys.stdout.write(a + '\n')

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

def swap(arr, a, b):
	arr[a] ^= arr[b]
	arr[b] ^= arr[a]
	arr[a] ^= arr[b]

def int_len(a):
	return math.floor(math.log(a, 10)) + 1

def isPowerOfTwo(x):
	return x and (not x & x - 1)

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
	res = []
	for i in range(2, len(prime)):
		if prime[i]:
			res.append(i)
	return res

def binarySearch(arr, l, r, x):
	if r >= l:
		mid = l + (r - l) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] > x:
			return binarySearch(arr, l, mid - 1, x)
		else:
			return binarySearch(arr, mid + 1, r, x)
	else:
		return -1

def search(arr, x):
	return binarySearch(arr, 0, len(arr) - 1, x)

def toString(A):
	return ''.join(A)

def toArray(str):
	return str.split()

def arrSum(Arr):
	Sum = 0
	for i in range(len(Arr)):
		Sum += Arr[i]
	return Sum

def computeXOR(n):
	if n % 4 == 0:
		return n
	if n % 4 == 1:
		return 1
	if n % 4 == 2:
		return n + 1
	return 0

def find_gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def isSubSequence(string1, string2, m, n):
	if m == 0:
		return True
	if n == 0:
		return False
	if string1[m - 1] == string2[n - 1]:
		return isSubSequence(string1, string2, m - 1, n - 1)
	return isSubSequence(string1, string2, m, n - 1)

def sum(a, b):
	if b.find('-') != -1:
		return '-1'
	L1 = len(a)
	L2 = len(b)
	res = ''
	if L1 < L2:
		a = '0' * (L2 - L1) + a
	elif L2 < L1:
		b = '0' * (L1 - L2) + b
	for i in range(0, max(L1, L2)):
		s = a[i]
		t = b[i]
		p = int(a[i]) + int(b[i])
		res += str(p)
	return res
N = get_int()
while N > 0:
	n = get_int()
	arr = get_list()
	A = get_string()
	Z = A.count('0')
	O = A.count('1')
	L0 = []
	L1 = []
	for i in range(0, n):
		if A[i] == '0':
			L0.append(arr[i])
		else:
			L1.append(arr[i])
	L0.sort()
	L1.sort()
	res = [0] * n
	for i in range(0, Z):
		res[L0[i] - 1] = i + 1
	for i in range(Z, O + Z):
		res[L1[i - Z] - 1] = i + 1
	for i in range(0, n):
		print(res[arr[i] - 1], end=' ')
	print()
	N -= 1


t = int(input())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	s = input()
	if '1' in s and '0' in s:
		c = 0
		lstl = []
		lstd = []
		for j in range(n):
			if s[j] == '1':
				c += 1
				lstl.append((lst[j], j))
			else:
				lstd.append((lst[j], j))
		lstl.sort(key=lambda y: y[0], reverse=True)
		lstd.sort(key=lambda y: y[0], reverse=True)
		f = n
		for g in lstl:
			lst[g[1]] = f
			f -= 1
		w = len(lstd)
		for d in lstd:
			lst[d[1]] = w
			w -= 1
	print(*lst)


kl = int(input())
for kkl in range(kl):
	n = int(input())
	sp = [int(i) for i in input().split()]
	st = input()
	k0 = st.count('0')
	sp0 = []
	sp1 = []
	for i in range(n):
		if st[i] == '0' and sp[i] > k0:
			sp0.append(i)
		if st[i] == '1' and sp[i] <= k0:
			sp1.append(i)
	for i in range(len(sp0)):
		(sp[sp0[i]], sp[sp1[i]]) = (sp[sp1[i]], sp[sp0[i]])
	print(*sp)


T = int(input())

def solve():
	N = int(input())
	P = list(map(int, input().split()))
	S = input()
	like = []
	dislike = []
	for (i, c) in enumerate(S):
		if c == '0':
			dislike.append((P[i], i))
		else:
			like.append((P[i], i))
	like = sorted(like)
	dislike = sorted(dislike)
	ans = [None for _ in range(N)]
	value = 0
	for (_, idx) in dislike:
		ans[idx] = str(value + 1)
		value += 1
	for (_, idx) in like:
		ans[idx] = str(value + 1)
		value += 1
	print(' '.join(ans))
for _ in range(T):
	solve()


def solve():
	for _ in range(int(input())):
		n = int(input())
		arr = list(map(int, input().split()))
		s = input()
		one = []
		zero = []
		for i in range(len(s)):
			if s[i] == '1':
				one.append([i, arr[i]])
			else:
				zero.append([i, arr[i]])
		one.sort(key=lambda x: x[1])
		zero.sort(key=lambda x: x[1])
		zl = len(zero)
		ol = len(one)
		temp = zl + 1
		for ele in one:
			if temp > n:
				break
			arr[ele[0]] = temp
			temp += 1
		temp = 1
		for ele in zero:
			if temp > zl:
				break
			arr[ele[0]] = temp
			temp += 1
		for ele in arr:
			print(ele, end=' ')
		print()
solve()


t = int(input())
for i in range(t):
	n = int(input())
	p = [0] + [*map(int, input().split(' '))]
	s = input()
	q = [0] * (n + 1)
	for j in range(1, n + 1):
		q[p[j]] = j
	disliked = []
	liked = []
	for j in range(1, n + 1):
		if s[q[j] - 1:q[j]] == '1':
			liked += [q[j]]
		else:
			disliked += [q[j]]
	songs = [0] * (n + 1)
	x = 1
	for j in disliked:
		songs[j] = x
		x += 1
	for j in liked:
		songs[j] = x
		x += 1
	for j in range(1, n + 1):
		print(songs[j], end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = input()
	zero = []
	one = []
	d = {}
	for i in a:
		d[i] = 0
	for i in range(len(s)):
		if s[i] == '0':
			zero.append(a[i])
		elif s[i] == '1':
			one.append(a[i])
	zero.sort()
	one.sort()
	x = 1
	for i in zero:
		d[i] = x
		x += 1
	for i in one:
		d[i] = x
		x += 1
	print(*d.values())


t = int(input())
for _ in range(t):
	n = int(input())
	perm = list(map(int, input().split()))
	rating = input()
	likes = []
	dislikes = []
	score = {}
	for i in range(n):
		if rating[i] == '0':
			dislikes.append(perm[i])
		else:
			likes.append(perm[i])
	likes.sort(reverse=True)
	dislikes.sort(reverse=True)
	r = n
	for i in range(len(likes)):
		score[likes[i]] = r
		r -= 1
	for i in range(len(dislikes)):
		score[dislikes[i]] = r
		r -= 1
	for p in perm:
		print(score[p], end=' ', sep=' ')


for _ in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	s = input()
	(z, o) = (dict(), dict())
	q = 1
	for i in range(n):
		if s[i] == '0':
			z[p[i]] = i
		else:
			o[p[i]] = i
	for i in sorted(z):
		p[z[i]] = q
		q += 1
	for i in sorted(o):
		p[o[i]] = q
		q += 1
	print(' '.join(map(str, p)))


import sys, collections
inum = lambda : int(input())
imap = lambda : map(int, input().split())
ilist = lambda : list(map(int, input().split()))
for t in range(inum()):
	(n, arr, w, ar1, ar2) = (inum(), ilist(), input(), list(), list())
	(cnt, one) = (1, 1)
	ans = [-cnt for i in range(n)]
	for i in range(0, n, 1):
		if w[i] == '0':
			ar2.append(arr[i])
		else:
			ar1.append(arr[i])
	ar1.sort()
	ar2.sort()
	mp = collections.defaultdict(lambda : 0)
	for i in ar2:
		mp[i] = cnt
		cnt += one
	for i in ar1:
		mp[i] = cnt
		cnt += one
	ans = [str(mp[arr[i]]) for i in range(n)]
	sys.stdout.write(str(' '.join(ans)) + '\n')


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = input()
	(ps, ng) = ([], [])
	(pc, nc) = (0, 0)
	for i in range(n):
		if s[i] == '1':
			ps.append(a[i])
			pc += 1
		else:
			ng.append(a[i])
			nc += 1
	ps = sorted(ps)
	ng = sorted(ng)
	d = {}
	for i in range(len(ng)):
		d[ng[i]] = i + 1
	for i in range(len(ps)):
		d[ps[i]] = nc + i + 1
	ans = [-1 for i in range(n)]
	for i in range(n):
		ans[i] = d[a[i]]
	print(' '.join([str(i) for i in ans]))


for ___ in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	s = list(map(int, list(input())))
	ratingToIdx = [i for i in range(0, n + 1)]
	ratingToLike = [i for i in range(n + 1)]
	for i in range(n):
		ratingToIdx[p[i]] = i + 1
		ratingToLike[p[i]] = s[i]
	(q1, q2) = ([], [])
	for i in range(1, n + 1):
		if ratingToLike[i] == 0:
			q1.append(ratingToIdx[i])
		else:
			q2.append(ratingToIdx[i])
	newIdx = [-1] + q1 + q2
	for i in range(n):
		p[newIdx[i + 1] - 1] = i + 1
	print(' '.join(map(str, p)))


def find(n, p, s):
	low = []
	high = []
	for i in range(n):
		if int(s[i]) == 0:
			low.append(p[i])
		else:
			high.append(p[i])
	a = len(low)
	b = len(high)
	low.sort()
	high.sort()
	map = {}
	for i in range(a):
		map[low[i]] = i + 1
	for i in range(b):
		map[high[i]] = i + a + 1
	q = []
	for i in range(n):
		q.append(str(map[p[i]]))
	return ' '.join(q)
cases = int(input())
for _ in range(cases):
	n = int(input())
	line = input().split()
	s = input()
	p = []
	for elem in line:
		p.append(int(elem))
	print(find(n, p, s))


def pl(line, func):
	return [func(i) for i in line.split()]
ip = input
n = int(ip())
for i in range(n):
	song_cnt = int(ip())
	songs = pl(ip(), int)
	res = ip()
	order = sorted(range(song_cnt), key=lambda x: songs[x] + (res[x] == '1') * 2000000.0)
	songs.sort()
	ans = [-1] * song_cnt
	for (song_id, val) in zip(order, songs):
		ans[song_id] = val
	print(' '.join((str(i) for i in ans)))


z = int(input())
for i in range(z):
	q = []
	g = int(input())
	g1 = [int(i) for i in input().split()]
	h = input()
	l = len(h)
	r = [0] * l
	for k in range(l):
		a = (h[k], g1[k], k)
		q.append(a)
	q = sorted(q, key=lambda p: (p[0], p[1]))
	for (o, u) in enumerate(q):
		r[u[2]] = str(o + 1)
	if 1 + 1 == 2:
		r1 = r
		r2 = r1
		print(' '.join(r2))


import math, sys
input = sys.stdin.readline
S = lambda : input().rstrip()
I = lambda : int(S())
M = lambda : map(int, S().split())
L = lambda : list(M())
mod1 = 1000000007
mod2 = 998244353
for _ in range(I()):
	n = I()
	p = L()
	s = S()
	a = []
	b = []
	ans = [0] * n
	for i in range(n):
		if s[i] == '1':
			a.append((p[i], i))
		else:
			b.append((p[i], i))
	a.sort(key=lambda x: x[0])
	b.sort(key=lambda x: x[0])
	k = len(a)
	j = len(b)
	t = 0
	for i in range(n - k + 1, n + 1):
		ans[a[t][1]] = i
		t += 1
	for i in range(j):
		ans[b[i][1]] = i + 1
	print(*ans)


def solve():
	n = int(input())
	arr = list(map(int, input().split()))
	s = input()
	liked = {}
	disliked = {}
	for i in range(n):
		if s[i] == '1':
			liked[arr[i]] = i
		else:
			disliked[arr[i]] = i
	answer = [0] * n
	cur = 1
	for i in sorted(disliked.keys()):
		answer[disliked[i]] = cur
		cur += 1
	for i in sorted(liked.keys()):
		answer[liked[i]] = cur
		cur += 1
	for i in answer:
		print(i, end=' ')
	print()
t = int(input())
for _ in range(t):
	solve()


for _ in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	s = input()
	l = []
	for i in range(len(s)):
		l.append([arr[i], int(s[i]), i])
	l.sort()
	dic = {0: [], 1: []}
	for i in l:
		dic[i[1]].append(i[2])
	res = [0 for i in range(len(s))]
	c = 1
	for i in dic[0]:
		res[i] = c
		c += 1
	for i in dic[1]:
		res[i] = c
		c += 1
	print(*res)


def solve():
	n = int(input())
	arr = list(map(int, input().split()))
	s = input()
	b = sorted(zip(s, arr, range(n)))
	ans = [0] * n
	for i in range(n):
		ans[b[i][2]] = i + 1
	return ans
import sys
input = lambda : sys.stdin.readline().rstrip()
t = int(input())
for i in range(t):
	print(*solve())


for i in range(int(input())):
	n = int(input())
	pi = list(map(int, input().split()))
	si = input()
	dic = {}
	for i in pi:
		dic[i] = 0
	dislike = []
	like = []
	for i in range(n):
		if si[i] == '0':
			dislike.append(pi[i])
		else:
			like.append(pi[i])
	all = sorted(dislike) + sorted(like)
	q = 1
	while q <= n:
		dic[all[q - 1]] = q
		q += 1
	print(*dic.values())


import math
from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	s = input().strip()
	id = [0] * (n + 1)
	for i in range(n):
		id[a[i]] = i + 1
	l = []
	d = []
	for i in range(n):
		if s[i] != '0':
			l.append(a[i])
		else:
			d.append(a[i])
	l.sort()
	d.sort()
	res = [0] * (n + 1)
	c = 1
	for i in d:
		res[id[i]] = c
		c += 1
	for i in l:
		res[id[i]] = c
		c += 1
	res.pop(0)
	print(*res)


import sys
input = sys.stdin.readline
num_test_cases = int(input())
for test_case_ittr in range(num_test_cases):
	n = int(input())
	rankings = [int(x) for x in input().split()]
	feedback = [int(x) for x in input().strip()]
	goods = []
	bads = []
	for (rank, fb, i) in zip(rankings, feedback, range(n)):
		if fb:
			goods += [(rank, i)]
		else:
			bads += [(rank, i)]
	arr = sorted(bads) + sorted(goods)
	ans = [None for _ in range(n)]
	for (i, (_, old_i)) in enumerate(arr):
		ans[old_i] = i + 1
	print(*ans)


import sys
for i in range(int(input())):
	n = int(input())
	a = list(map(int, sys.stdin.readline().split()))
	s = list(sys.stdin.readline())
	del s[-1]
	x = 0
	c = 0
	c1 = 0
	e = []
	f = []
	for j in range(n):
		if s[j] == '0':
			e.append((a[j], j))
			x += 1
		else:
			f.append((a[j], j))
	e.sort()
	f.sort()
	y = n - x
	for j in range(x):
		s[e[j][1]] = j + 1
	for j in range(y):
		s[f[j][1]] = x + 1 + j
	print(' '.join([str(i) for i in s]))


import os
import sys
from io import BytesIO, IOBase
from math import cos, e, pi, ceil, pi, tan, floor, gcd, sqrt, log2, factorial, log10
from collections import Counter
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase
from statistics import mean, median, mode
from bisect import *
alpha = ascii_lowercase
beta = ascii_uppercase

def nextPowerOf2(n):
	p = 1
	if n and (not n & n - 1):
		return n
	while p < n:
		p <<= 1
	return p

def isComposite(n):
	if n <= 1:
		return False
	if n <= 3:
		return False
	if n % 2 == 0 or n % 3 == 0:
		return True
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return True
		i = i + 6
	return False

def get_Divisors(n):
	arr = []
	i = 1
	while i <= sqrt(n):
		if n % i == 0:
			if n / i == i:
				arr.append(i)
			else:
				arr.append(i)
				arr.append(n // i)
		i = i + 1
	return arr

def isprime(n):
	if n == 1:
		return 0
	i = 2
	while i * i <= n:
		if n % i == 0:
			return 0
		i += 1
	return 1

def Sieve(n):
	prime = [True for i in range(n + 1)]
	prime[0] = False
	prime[1] = 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 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 create_matrix(n, m):
	mat = [[] for i in range(n)]
	for i in range(n):
		for j in range(m):
			mat[i].append(0)
	return mat

def main():
	for ttt in range(int(input())):
		n = int(input())
		arr = list(map(int, input().split()))
		s = input()
		res = []
		for i in range(n):
			res.append([i + 1, arr[i], int(s[i])])
		res.sort(key=lambda k: (k[2], k[1]))
		for i in range(n):
			res[i].append(i + 1)
		res.sort(key=lambda k: k[0])
		for i in range(n):
			print(res[i][3], 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 = 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 input():
	return sys.stdin.readline().rstrip('\r\n')
main()


def solve(p, s):
	dislikes = 0
	for i in s:
		if i == '0':
			dislikes += 1
	likes = len(s) - dislikes
	if likes == 0 or dislikes == 0:
		return p
	dislikedRating = []
	likedRating = []
	for i in range(len(p)):
		if s[i] == '0':
			dislikedRating.append((p[i], i))
		else:
			likedRating.append((p[i], i))
	dislikedRating.sort()
	likedRating.sort()
	ans = [-1 for i in range(len(p))]
	curCount = 1
	for i in dislikedRating:
		(rating, idx) = i
		ans[idx] = curCount
		curCount += 1
	for i in likedRating:
		(rating, idx) = i
		ans[idx] = curCount
		curCount += 1
	return ans
t = int(input())
while t:
	n = int(input())
	a = [int(k) for k in input().split()]
	s = input()
	ans = solve(a, s)
	for i in ans:
		print(i, end=' ')
	print()
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	li = list(map(int, input().split()))
	d = {}
	s = input()
	d['0'] = []
	d['1'] = []
	for i in range(n):
		d[s[i]].append(li[i])
	e = d.copy()
	e['0'].sort()
	e['1'].sort()
	ans = [0] * n
	c = 1
	for i in e['0']:
		ans[i - 1] = c
		c += 1
	for i in e['1']:
		ans[i - 1] = c
		c += 1
	kemp = []
	for i in li:
		kemp.append(ans[i - 1])
	print(*kemp)


t = int(input())
for _ in range(t):
	n = int(input())
	p = [int(i) for i in input().split()]
	s = str(input())
	temp = []
	disliked = []
	liked = []
	q = [0] * n
	for i in range(len(s)):
		if s[i] == '1':
			liked.append([p[i], i])
		else:
			disliked.append([p[i], i])
	liked.sort()
	disliked.sort()
	x = 1
	for element in disliked + liked:
		q[element[1]] = x
		x += 1
	print(*q)


t = int(input())
for _ in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	s = input()
	d = {}
	for i in range(n):
		d[lst[i]] = s[i]
	c = 1
	for i in range(1, n + 1):
		if d[i] == '0':
			d[i] = c
			c += 1
	for i in range(1, n + 1):
		if d[i] == '1':
			d[i] = c
			c += 1
	for i in d:
		print(d[i], end=' ')
	print()


import sys

def berland_music(n, p, s):
	ones = []
	zeros = []
	for i in range(n):
		if s[i] == '1':
			ones += [i]
		else:
			zeros += [i]
	zero_weights = list(range(1, len(zeros) + 1))
	one_weights = list(range(len(zeros) + 1, n + 1))
	ones.sort(key=lambda x: -p[x])
	zeros.sort(key=lambda x: -p[x])
	output = [None] * n
	for z in zeros:
		output[z] = zero_weights.pop()
	for o in ones:
		output[o] = one_weights.pop()
	return ' '.join(map(str, output))
for _ in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	s = input()
	sys.stdout.write(f'{berland_music(n, p, s)}\n')


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())
from collections import Counter
t = inp()
for _ in range(t):
	n = inp()
	A = inlt()
	s = list(insr())
	ct = Counter(s)
	small = ct['0']
	big = ct['1']
	if big == n or small == n:
		print(*A)
		continue
	res = [0] * n
	smalls = set(list(range(1, small + 1)))
	bigs = set(list(range(small + 1, n + 1)))
	for i in range(n):
		if s[i] == '1':
			if A[i] > small:
				res[i] = A[i]
				bigs.remove(A[i])
		elif A[i] <= small:
			res[i] = A[i]
			smalls.remove(A[i])
	for i in range(n):
		if res[i] == 0:
			if s[i] == '1':
				res[i] = bigs.pop()
			else:
				res[i] = smalls.pop()
	print(*res)


from collections import defaultdict
inum = lambda : int(input())
imap = lambda : map(int, input().split())
ilist = lambda : list(map(int, input().split()))
for t in range(inum()):
	n = inum()
	arr = ilist()
	w = input()
	res = [-1 for i in range(n)]
	(cnt, one, zero) = (1, [], [])
	for i in range(n):
		if w[i] == '1':
			one.append(arr[i])
		else:
			zero.append(arr[i])
	(one, zero) = (sorted(one), sorted(zero))
	hp = defaultdict(int)
	for i in zero:
		hp[i] = cnt
		cnt += 1
	for i in one:
		hp[i] = cnt
		cnt += 1
	for i in range(n):
		res[i] = hp[arr[i]]
	print(*res)


for case in range(int(input())):
	N = int(input())
	L = list(map(int, input().split()))
	L2 = list(map(int, input()))
	likes = N - sum(L2) + 1
	cur = 1
	index = {L[x]: x for x in range(N)}
	ans = [0] * N
	for x in range(1, N + 1):
		i = index[x]
		if L2[i] == 0:
			ans[i] = cur
			cur += 1
		else:
			ans[i] = likes
			likes += 1
	print(' '.join(map(str, ans)))


def semiSort(arr1, arr2):
	sortedArr1 = sorted(arr1)
	sortedArr2 = sorted(arr2)
	newArr2 = [-1] * len(arr2)
	indexArr1 = {}
	for i in range(len(arr1)):
		indexArr1[arr1[i]] = i
	d = {}
	for i in range(len(arr1)):
		d[i] = indexArr1[sortedArr1[i]]
		newArr2[d[i]] = sortedArr2[i]
	return newArr2
t = int(input())
for i in range(t):
	n = int(input())
	p = list(map(int, input().split()))
	s = input()
	sortedP = sorted(p)
	liked = []
	disliked = []
	p1 = []
	p2 = []
	for i in range(n):
		if s[i] == '1':
			liked.append(i)
			p1.append(p[i])
		else:
			disliked.append(i)
			p2.append(p[i])
	ptmp = sortedP[n - len(liked):n]
	ptmp2 = sortedP[0:len(disliked)]
	ans = [-1] * n
	p1 = semiSort(p1, ptmp)
	p2 = semiSort(p2, ptmp2)
	for i in range(len(liked)):
		ans[liked[i]] = p1[i]
	for i in range(len(disliked)):
		ans[disliked[i]] = p2[i]
	print(*ans)


from sys import stdin
n = int(stdin.readline().strip())
for i in range(n):
	sn = int(stdin.readline().strip())
	pp = [int(x) for x in stdin.readline().split()]
	likes = [bool(int(x)) for x in stdin.readline().strip()]
	liked_pp = [pp[i] for i in range(sn) if likes[i]]
	disliked_pp = [pp[i] for i in range(sn) if not likes[i]]
	liked_ppd = {p: i for (i, p) in enumerate(sorted(liked_pp))}
	liked_qq = {p: liked_ppd[p] + len(disliked_pp) + 1 for p in liked_pp}
	disliked_ppd = {p: i for (i, p) in enumerate(sorted(disliked_pp))}
	disliked_qq = {p: disliked_ppd[p] + 1 for p in disliked_pp}
	qq = [liked_qq[p] if p in liked_qq else disliked_qq[p] for p in pp]
	print(' '.join([str(q) for q in qq]))


import sys
get = sys.stdin.readline
for _ in range(int(get())):
	N = int(get())
	p = list(map(int, get().split()))
	s = str(get())
	likes = s.count('1')
	dislikes = s.count('0')
	disliked_songs = [0] * dislikes
	j = 0
	for i in range(N):
		if s[i] == '0':
			disliked_songs[j] = p[i]
			j += 1
	disliked_songs.sort()
	rating = [x for x in p]
	rat = 1
	liked_songs = [0] * likes
	j = 0
	for i in range(N):
		if s[i] == '1':
			liked_songs[j] = p[i]
			j += 1
	liked_songs.sort()
	chup = {}
	for x in disliked_songs:
		chup[x] = rat
		rat += 1
	for x in liked_songs:
		chup[x] = rat
		rat += 1
	for i in range(N):
		rating[i] = chup[p[i]]
	print(*rating)


def solve(n, p, s):
	idx = list(range(n))
	idx.sort(key=lambda i: (s[i], p[i]))
	ans = [0] * n
	for i in range(n):
		ans[idx[i]] = i + 1
	return ans
tests = int(input())
for test in range(tests):
	n = int(input())
	p = list(map(int, input().split()))
	s = input()
	print(*solve(n, p, s))


for _ in range(int(input())):
	n = int(input())
	predict = list(map(int, input().split()))
	binary = input()
	like = []
	dislike = []
	countZero = 0
	for x in range(n):
		if binary[x] == '1':
			like.append(predict[x])
		else:
			dislike.append(predict[x])
			countZero += 1
	countOne = n - countZero
	dislikeSort = dislike[:]
	likeSort = like[:]
	dislikeSort.sort()
	likeSort.sort(reverse=True)
	dislikeDict = {dislikeSort[i]: i + 1 for i in range(countZero)}
	likeDict = {likeSort[i]: n - i for i in range(countOne)}
	ans = ''
	for x in predict:
		if x in likeDict:
			print(likeDict[x], end=' ')
		else:
			print(dislikeDict[x], end=' ')


def func():
	n = int(input())
	l = list(map(int, input().split()))
	s = input()
	reql = [0 for i in range(n)]
	d1 = {}
	d2 = {}
	for i in range(n):
		if s[i] == '1':
			d1[l[i]] = i
		elif s[i] == '0':
			d2[l[i]] = i
	k1 = len(d1)
	res1 = {key: d1[key] for key in sorted(d1)}
	res2 = {key: d2[key] for key in sorted(d2)}
	sidx1 = n - k1 + 1
	sidx2 = 1
	for i in res1:
		reql[res1[i]] = str(sidx1)
		sidx1 += 1
	for i in res2:
		reql[res2[i]] = str(sidx2)
		sidx2 += 1
	return ' '.join(reql)
t = int(input())
for i in range(t):
	print(func())


t = int(input())
for _ in range(t):
	n = int(input())
	p = list(map(int, input().split()))
	s = input()
	like = []
	dislike = []
	ind = []
	d = dict()
	if s.count('0') == 0:
		print(*p)
	elif s.count('1') == 0:
		print(*p)
	else:
		like = []
		dislike = []
		for i in range(n):
			if s[i] == '1':
				like.append(p[i])
			else:
				dislike.append(p[i])
		like.sort()
		dislike.sort()
		x = dislike + like
		for j in range(len(x)):
			d[x[j]] = j + 1
		for k in range(len(p)):
			p[k] = d[p[k]]
		print(*p)


from sys import stdin, stdout
rd = lambda : list(map(int, stdin.readline().split()))
rds = lambda : stdin.readline().rstrip()
ii = lambda : int(stdin.readline())
INF = 1 << 62
mod = 10 ** 9 + 7
for _ in range(ii()):
	n = ii()
	ps = rd()
	rs = rds()
	z = []
	o = []
	for (p, r) in zip(ps, rs):
		if r == '0':
			z.append(p)
		else:
			o.append(p)
	z.sort()
	o.sort()
	m = {}
	for (i, x) in enumerate(z):
		m[x] = i + 1
	for (j, y) in enumerate(o):
		m[y] = j + 1 + len(z)
	ans = []
	for x in ps:
		ans.append(m[x])
	print(*ans)


import os
import sys
from io import BytesIO, IOBase
from types import GeneratorType
from collections import defaultdict
BUFSIZE = 8192
sys.setrecursionlimit(10 ** 5)

def bootstrap(f, stack=[]):

	def fun(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return fun

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())
	p = list(map(int, input().split()))
	s = input()
	r0 = []
	r1 = []
	q = [0 for i in range(n)]
	for i in range(n):
		if s[i] == '0':
			r0.append([p[i], i])
	r0.sort()
	r1.sort()
	l1 = len(r0)
	for j in range(l1):
		q[r0[j][1]] = j + 1
	vis = [0 for i in range(n + 1)]
	for i in range(n):
		if s[i] == '1':
			if p[i] > l1:
				vis[p[i]] = 1
				q[i] = p[i]
				pass
			else:
				r1.append([p[i], i])
	r1.sort()
	curr = 0
	for j in range(l1, n):
		if vis[j + 1] == 0:
			q[r1[curr][1]] = j + 1
			curr += 1
	print(*q)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = input()
	l1 = []
	l2 = []
	for i in range(n):
		if s[i] == '1':
			l2.append(l[i])
		else:
			l1.append(l[i])
	l1.sort()
	l2.sort()
	l1 = l1 + l2
	d = {}
	for i in range(n):
		d[l1[i]] = i + 1
	y = []
	for i in range(n):
		y.append(d[l[i]])
	print(*y)


def berlandMusic():
	n = int(input())
	p = list(map(int, input().split()))
	s = input()
	liked = list()
	disliked = list()
	for ind in range(len(s)):
		if s[ind] == '0':
			disliked.append(p[ind])
		else:
			liked.append(p[ind])
	disliked.sort()
	liked.sort(reverse=True)
	values = dict()
	for i in range(len(liked)):
		values[liked[i]] = n - i
	for i in range(1, len(disliked) + 1):
		values[disliked[i - 1]] = i
	for num in p:
		print(values[num], end=' ')
	print()
	return
t = int(input())
for i in range(t):
	berlandMusic()


import os
import sys
from io import BytesIO, IOBase
from collections import Counter, defaultdict
import math
import heapq
import bisect
import collections

def ceil(a, b):
	return (a + b - 1) // b
BUFSIZE = 8192
inf = float('inf')

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))
get = lambda : sys.stdin.readline().rstrip('\r\n')
MOD = 1000000007

def v():
	return list()
for _ in range(int(get())):
	n = int(get())
	l = list(map(int, get().split()))
	s = get()
	l1 = []
	l2 = []
	for i in range(n):
		if s[i] == '0':
			l1.append([i, l[i]])
		else:
			l2.append([i, l[i]])
	l2.sort(key=lambda x: x[1])
	l1.sort(key=lambda x: x[1])
	l3 = l1 + l2
	l.sort()
	ans = [0] * n
	for i in range(n):
		if s[i] == '0':
			ans[l3[i][0]] = l[i]
		else:
			ans[l3[i][0]] = l[i]
	print(*ans)


T = int(input())
for testcase in range(1, T + 1):
	n = int(input())
	p = list(map(int, input().split()))
	s = list(input())
	q = [0 for i in range(n)]
	newvalue = [int(s[k]) * (n + 1) + p[k] for k in range(n)]
	enum = list(enumerate(newvalue))
	mylist = sorted(enum, key=lambda x: x[1])
	for (i, value) in enumerate(mylist):
		q[value[0]] = i + 1
	print(*q)


import heapq
for t in range(int(input())):
	n = int(input())
	p = [int(i) for i in input().split()]
	s = input()
	counter = 0
	order = [0] * n
	counter = s.count('1')
	liked = []
	disliked = []
	out_of_place = 0
	i = 0
	while i < n:
		order[p[i] - 1] = i
		if s[i] == '1':
			liked.append(p[i] - 1)
			if p[i] < n - counter + 1:
				out_of_place += 1
		else:
			disliked.append(-p[i] + 1)
		i += 1
	heapq.heapify(liked)
	heapq.heapify(disliked)
	ans = 0
	i = 0
	while i < out_of_place:
		(a, b) = (heapq.heappop(liked), -heapq.heappop(disliked))
		(order[a], order[b]) = (order[b], order[a])
		i += 1
	i = 0
	while i < n:
		p[order[i]] = i + 1
		i += 1
	print(*p, sep=' ')


t = int(input())
for i in range(t):
	n = int(input())
	predictedRatings = list(map(int, input().split()))
	reactions = input()
	likes = dislikes = 0
	likedSongs = []
	dislikedSongs = []
	for j in range(len(reactions)):
		reaction = reactions[j]
		if reaction == '1':
			likes += 1
			likedSongs.append((predictedRatings[j], j))
		else:
			dislikes += 1
			dislikedSongs.append((predictedRatings[j], j))
	likedSongs = sorted(likedSongs, key=lambda x: x[0])
	dislikedSongs = sorted(dislikedSongs, key=lambda x: x[0])
	result = [0 for i in range(n)]
	rating = 1
	for (ratings, index) in dislikedSongs:
		result[index] = rating
		rating += 1
	for (ratings, index) in likedSongs:
		result[index] = rating
		rating += 1
	for val in result:
		print(val, end=' ')
	print()


t = int(input())
for _ in range(t):
	n = int(input())
	rating_list = list(map(int, input().split()))
	like_dislike = input()
	record = dict()
	for ind in range(len(rating_list)):
		record[rating_list[ind]] = [like_dislike[ind], ind]
	new_rating = [0 for i in range(n)]
	count = n
	i = n
	while i > 0:
		if record[i][0] == '1':
			new_rating[record[i][1]] = count
			count -= 1
		i -= 1
	count = 1
	i = 1
	while i <= n:
		if record[i][0] == '0':
			if new_rating[record[i][1]] == 0:
				new_rating[record[i][1]] = count
				count += 1
		i += 1
	for i in new_rating:
		print(i, end=' ')
	print()


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, functools, copy, random
from functools import reduce
import operator
inf = 10 ** 9 + 7
mod = 10 ** 9 + 7

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

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

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

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

def S():
	return input()

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

def endl():
	return print('\n')

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

def grayCode(n):
	return n ^ n >> 1
sys.setrecursionlimit(20000)
graph = collections.defaultdict(list)
grid = []
visited = set()
ways = ['D', 'U', 'R', 'L']
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]

def find(i):
	if par[i] != i:
		par[i] = find(par[i])
	return par[i]

def union(a, b):
	a = find(a)
	b = find(b)
	if a == b:
		return False
	par[a] = b
	return True

def initGraph(m):
	for _ in range(m):
		(u, v) = LI()
		graph[u - 1].append(v - 1)
		graph[v - 1].append(u - 1)
	return graph

def initGraphW(m):
	for _ in range(m):
		(a, b, c) = LI()
		graph[a - 1].append((b - 1, c))
		graph[b - 1].append((a - 1, c))

def initGrid(h, w):
	for i in range(h):
		s = S()
		grid.append(list(s))
	return grid

def soe(limit):
	a = [True] * limit
	a[0] = a[1] = False
	for (i, isprime) in enumerate(a):
		if isprime:
			yield i
			for n in range(i * i, limit, i):
				a[n] = False
	return a
for _ in range(I()):
	n = I()
	a = LI()
	b = S()
	b = [int(i) for i in b]
	newA = []
	for i in range(n):
		newA.append((a[i], b[i], i))
	newA.sort()
	ans = [0] * n
	i = 0
	cur = 1
	while i < n:
		if newA[i][1] == 0:
			ans[newA[i][2]] = cur
			cur += 1
		i += 1
	i = 0
	while i < n:
		if newA[i][1] == 1:
			ans[newA[i][2]] = cur
			cur += 1
		i += 1
	print(*ans)


import sys
from math import sqrt, ceil, floor, gcd
from collections import Counter, defaultdict

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

def str_arr():
	return list(map(str, input().split()))

def get_str():
	return map(str, input().split())

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

def get_flo():
	return map(float, input().split())

def lcm(a, b):
	return a * b // gcd(a, b)
mod = 1000000007

def solve(n, arr, s):
	d = defaultdict(int)
	for i in range(len(s)):
		if s[i] == '0':
			d[arr[i]] = i
	A = sorted(arr)
	ind = 0
	d1 = defaultdict(int)
	for i in range(len(arr)):
		if A[i] in d.keys():
			d1[A[i]] = A[ind]
			ind += 1
	for i in range(len(arr)):
		if A[i] not in d.keys():
			d1[A[i]] = A[ind]
			ind += 1
	for i in range(n):
		print(d1[arr[i]], end=' ')
	print()
for _ in range(int(input())):
	n = int(input())
	arr = int_arr()
	s = str(input())
	solve(n, arr, s)


from sys import stdin, stdout, setrecursionlimit
t = 1
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	p = list(map(int, stdin.readline().rstrip().split()))
	s = stdin.readline().strip('\n')
	tmp = [int(i) for i in s]
	s = tmp
	d = {}
	l = {}
	arr_d = []
	arr_l = []
	for i in range(n):
		if s[i] == 1:
			d[p[i]] = i
			arr_d.append((p[i], i))
		else:
			l[p[i]] = i
			arr_l.append((p[i], i))
	ln = len(l)
	arr_d.sort()
	arr_l.sort()
	ans = [-1] * n
	c = 1
	for i in arr_l:
		ans[i[1]] = c
		c += 1
	for i in arr_d:
		ans[i[1]] = c
		c += 1
	print(*ans)


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())
for _ in range(inp()):
	n = inp()
	a = 0
	b = n - 1
	l = inlt()
	s = list(map(int, insr()))
	index = [i for i in range(n)]
	zp = list(zip(s, l, index))
	zp.sort()
	ans = [0 for i in range(n)]
	l.sort()
	zero = s.count(0)
	cnt = 1
	for i in range(n):
		if zero:
			ans[zp[i][2]] = cnt
			cnt += 1
			zero -= 1
		else:
			ans[zp[i][2]] = cnt
			cnt += 1
	print(*ans)


import sys

def solve():
	inp = sys.stdin.readline
	n = int(inp())
	p = list(map(int, inp().split()))
	pi = [None] * (n + 1)
	for i in range(n):
		pi[p[i]] = i
	s = inp()
	ans = [None] * n
	c = [0, 0]
	for i in range(1, n + 1):
		x = pi[i]
		y = int(s[x])
		ans[x] = c[y]
		c[y] += 1
	o = [1, 1 + c[0]]
	for i in range(1, n + 1):
		x = pi[i]
		y = int(s[x])
		ans[x] += o[y]
	print(' '.join(map(str, ans)))

def main():
	for i in range(int(sys.stdin.readline())):
		solve()
main()


def do(p_arr, s_arr):
	l = len(s_arr)
	q_arr = [0] * l
	ones_arr = [p_arr[i] for i in range(l) if s_arr[i] == 1]
	ones_index = [i for i in range(l) if s_arr[i] == 1]
	ones_total = [[ones_arr[i], ones_index[i]] for i in range(len(ones_arr))]
	ones_total.sort()
	ones_index = [i[1] for i in ones_total]
	zeros_arr = [p_arr[i] for i in range(l) if s_arr[i] == 0]
	zeros_index = [i for i in range(l) if s_arr[i] == 0]
	zeros_total = [[zeros_arr[i], zeros_index[i]] for i in range(len(zeros_arr))]
	zeros_total.sort()
	zeros_index = [i[1] for i in zeros_total]
	bigg = list(range(l - len(ones_arr) + 1, l + 1))
	small = list(range(1, l - len(ones_arr) + 1))
	for j in range(len(ones_arr)):
		q_arr[ones_index[j]] = bigg[j]
	for j in range(len(zeros_arr)):
		q_arr[zeros_index[j]] = small[j]
	print(*q_arr)
t = int(input())
for i in range(t):
	n = int(input())
	p_arr = list(map(int, input().split()))
	s_arr = input()
	s_arr = [int(a) for a in s_arr]
	do(p_arr, s_arr)


t = int(input())
for _ in range(t):
	n = int(input())
	p = list(map(int, input().split()))
	s = input()
	bad = []
	good = []
	for i in range(len(s)):
		if s[i] == '0':
			bad.append(p[i])
		else:
			good.append(p[i])
	bad = list(sorted(bad))
	good = list(sorted(good))
	dic = {}
	start = 1
	for i in bad + good:
		dic[i] = start
		start += 1
	ans = []
	for i in p:
		ans.append(str(dic[i]))
	print(' '.join(ans))


def answer(arr, s):
	from collections import defaultdict
	pos = defaultdict(int)
	one = []
	zero = []
	for i in range(len(s)):
		if s[i] == '0':
			zero.append(arr[i])
			pos[arr[i]] = i
		else:
			one.append(arr[i])
			pos[arr[i]] = i
	zero.sort()
	one.sort()
	count = len(arr) - len(one) + 1
	count1 = 1
	for i in one:
		arr[pos[i]] = count
		count += 1
	for i in zero:
		arr[pos[i]] = count1
		count1 += 1
	return arr
t = int(input())
for k in range(t):
	n = int(input())
	arr = []
	inp = input().split()
	for i in inp:
		arr.append(int(i))
	s = input()
	result = answer(arr, s)
	for i in result:
		print(i, end=' ')
	print()


from __future__ import print_function
from os import environ
import sys
import math
from bisect import bisect_left
if environ.get('epoweripi'):
	sys.stdin = open('../../input.txt', 'r')
	sys.stdout = open('../../output.txt', 'w')
input = sys.stdin.readline
mod = 10 ** 9 + 7

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

def II():
	return int(input().strip())

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

def LI():
	return list(map(int, input().split()))
if sys.version[0] == '3':
	print = sys.stdout.write

	def P(z=''):
		print(str(z) + '\n')

	def LP(z=[]):
		for i in z:
			print(str(i) + ' ')
		print('\n')
else:

	def P(z=''):
		print(str(z))

	def LP(z=[]):
		for i in z:
			print(str(i) + ' ', end='')
		print()

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

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

def swap(arr, i, j):
	(arr[i], arr[j]) = (arr[j], arr[i])

def solve():
	n = II()
	l = LI()
	s = I()
	idxs = [i for i in range(n)]
	idxs.sort(key=lambda x: (s[x], l[x]))
	new_rating = [0 for x in range(n)]
	k = 1
	for i in idxs:
		new_rating[i] = k
		k += 1
	LP(new_rating)

def main():
	for _ in range(II()):
		solve()
main()


t = int(input())
for i in range(t):
	n = int(input())
	inra = list(map(int, input().split()))
	pos = [0 for i in range(n + 1)]
	nera = [0 for i in range(n)]
	bm = input()
	o = []
	z = []
	for j in range(n):
		pos[inra[j]] = j
		if bm[j] == '0':
			z.append(inra[j])
		else:
			o.append(inra[j])
	z.sort()
	o.sort()
	x = 1
	for j in z:
		nera[pos[j]] = x
		x += 1
	for j in o:
		nera[pos[j]] = x
		x += 1
	print(*nera)


from sys import stdin, stdout, setrecursionlimit
from math import gcd, sqrt, factorial, pi, inf
from collections import deque, defaultdict
from bisect import bisect, bisect_left
from time import time
from itertools import permutations as per
from heapq import heapify, heappush, heappop, heappushpop
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\r\n')
L = lambda : list(R())
P = lambda x: stdout.write(str(x) + '\n')
lcm = lambda x, y: x * y // gcd(x, y)
nCr = lambda x, y: f[x] * inv(f[y] * f[x - y] % N) % N
inv = lambda x: pow(x, N - 2, N)
sumx = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7
for _ in range(I()):
	n = I()
	a = L()
	s = [int(i) for i in S()]
	d = []
	e = []
	ind = [0] * (n + 1)
	for i in range(n):
		ind[a[i]] = i
	for i in range(n):
		if not s[i]:
			d += (a[i],)
		else:
			e += (a[i],)
	d.sort(reverse=True)
	e.sort()
	for i in range(s.count(0)):
		a[ind[d[-1]]] = i + 1
		d.pop()
	for i in range(s.count(1)):
		a[ind[e[-1]]] = n - i
		e.pop()
	print(*a)