Polycarp is an organizer of a Berland ICPC regional event. There are $n$ universities in Berland numbered from $1$ to $n$. Polycarp knows all competitive programmers in the region. There are $n$ students: the $i$-th student is enrolled at a university $u_i$ and has a programming skill $s_i$.

Polycarp has to decide on the rules now. In particular, the number of members in the team.

Polycarp knows that if he chooses the size of the team to be some integer $k$, each university will send their $k$ strongest (with the highest programming skill $s$) students in the first team, the next $k$ strongest students in the second team and so on. If there are fewer than $k$ students left, then the team can't be formed. Note that there might be universities that send zero teams.

The strength of the region is the total skill of the members of all present teams. If there are no teams present, then the strength is $0$.

Help Polycarp to find the strength of the region for each choice of $k$ from $1$ to $n$.


-----Input-----

The first line contains a single integer $t$ ($1 \le t \le 1000$) — 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 universities and the number of students.

The second line of each testcase contains $n$ integers $u_1, u_2, \dots, u_n$ ($1 \le u_i \le n$) — the university the $i$-th student is enrolled at.

The third line of each testcase contains $n$ integers $s_1, s_2, \dots, s_n$ ($1 \le s_i \le 10^9$) — the programming skill of the $i$-th student.

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


-----Output-----

For each testcase print $n$ integers: the strength of the region — the total skill of the members of the present teams — for each choice of team size $k$.


-----Examples-----

Input
4
7
1 2 1 2 1 2 1
6 8 3 1 5 1 5
10
1 1 1 2 2 2 2 3 3 3
3435 3014 2241 2233 2893 2102 2286 2175 1961 2567
6
3 3 3 3 3 3
5 9 6 7 9 7
1
1
3083
Output
29 28 26 19 0 0 0 
24907 20705 22805 9514 0 0 0 0 0 0 
43 43 43 32 38 43 
3083


-----Note-----

In the first testcase the teams from each university for each $k$ are:

$k=1$:

university $1$: $[6], [5], [5], [3]$;

university $2$: $[8], [1], [1]$;

$k=2$:

university $1$: $[6, 5], [5, 3]$;

university $2$: $[8, 1]$;

$k=3$:

university $1$: $[6, 5, 5]$;

university $2$: $[8, 1, 1]$;

$k=4$:

university $1$: $[6, 5, 5, 3]$;

Source: codeforces | Difficulty: MEDIUM
Tags: Sorting

Greedy algorithms

Complete search

Number theory

Data structures


Solutions:
t = int(input())
for _ in range(t):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	us = [[] for j in range(n)]
	for i in range(n):
		us[u[i] - 1].append(s[i])
	s = [0] * n
	for i in range(n):
		if us[i] == []:
			continue
		us[i].sort()
		p = len(us[i])
		if p > 1:
			for j in range(p - 2, -1, -1):
				us[i][j] += us[i][j + 1]
		for j in range(1, p + 1):
			k = p % j
			s[j - 1] += us[i][k]
	print(*s)


from math import ceil
from collections import defaultdict
from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	d = defaultdict(list)
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	for i in range(n):
		d[a[i]].append(b[i])
	for i in d:
		d[i].sort(reverse=True)
	for i in d:
		for j in range(1, len(d[i])):
			d[i][j] += d[i][j - 1]
	ans = [0] * n
	for j in range(n):
		div = j + 1
		l = []
		for i in d:
			an = len(d[i])
			if div > an:
				l.append(i)
				continue
			ans[j] += d[i][an - an % div - 1]
		for i in l:
			del d[i]
	print(*ans)


t = int(input())
for _ in range(t):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	d = {}
	for i in range(n):
		if u[i] not in d:
			d[u[i]] = [s[i]]
		else:
			d[u[i]].append(s[i])
	res = [0] * n
	mx = -1
	for i in d:
		a = d[i]
		a.sort(reverse=True)
		for j in range(len(a)):
			if j != 0:
				a[j] += a[j - 1]
		mx = max(len(a), mx)
	for i in d:
		a = d[i]
		ln = len(a)
		for x in range(1, ln + 1):
			ix = ln % x
			if ln >= x:
				res[x - 1] += a[ln - ix - 1]
	print(*res)


import math
from collections import Counter, defaultdict
from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	u = [int(i) for i in input().split()]
	s = [int(i) for i in input().split()]
	st = set(u)
	d = defaultdict(list)
	for i in range(n):
		d[u[i]].append(s[i])
	l = [0] * (n + 1)
	sm = [0] * (n + 1)
	for i in st:
		d[i].sort()
		l[i] = len(d[i])
	a = [0] * n
	for i in st:
		ps = [0] * (l[i] + 1)
		for j in range(l[i]):
			ps[j + 1] = ps[j] + d[i][j]
		for k in range(1, l[i] + 1):
			s = 0
			rem = l[i] % k
			s = ps[-1] - ps[rem]
			a[k - 1] += s
	print(*a)


from math import *
from collections import *
import sys
input = sys.stdin.readline

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

def st():
	return input().rstrip('\n')

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

def ma():
	return map(int, input().split())
t = inp()
while t:
	t -= 1
	n = inp()
	dp = [[] for i in range(n)]
	u = lis()
	s = lis()
	res = [0 for i in range(n)]
	pre = [[0] for i in range(n)]
	co = 0
	for i in u:
		dp[i - 1].append(s[co])
		co += 1
	for i in dp:
		if i:
			i.sort(reverse=True)
	for i in range(n):
		for j in dp[i]:
			pre[i].append(pre[i][-1] + j)
	for i in range(n):
		z = len(dp[i])
		for j in range(len(dp[i])):
			if z % (j + 1) == 0:
				res[j] += pre[i][-1]
			else:
				x = z - z % (j + 1)
				res[j] += pre[i][x]
	print(*res)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	dic = {}
	for i in range(n):
		if not u[i] in dic:
			dic[u[i]] = []
			dic[u[i]].append(s[i])
		else:
			dic[u[i]].append(s[i])
	mx = 0
	for i in dic:
		dic[i] = sorted(dic[i], reverse=True)
		for j in range(1, len(dic[i])):
			dic[i][j] += dic[i][j - 1]
	ans = [0 for i in range(n)]
	for i in dic:
		for j in range(len(dic[i])):
			ans[j] += dic[i][len(dic[i]) - len(dic[i]) % (j + 1) - 1]
	for i in ans:
		print(i, end=' ')
	print()


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

def main():
	for i in range(int(input())):
		n = int(input())
		u = list(map(int, input().split()))
		s = list(map(int, input().split()))
		a = [[] for j in range(n)]
		for j in range(n):
			a[u[j] - 1].append(s[j])
		for j in range(n):
			a[j].sort(reverse=True)
		b = [[] for j in range(n)]
		for j in range(n):
			sumo = 0
			for k in range(len(a[j])):
				sumo = sumo + a[j][k]
				b[j].append(sumo)
		g = [0] * n
		for j in range(n):
			l = len(a[j])
			for k in range(l):
				g[k] = g[k] + b[j][l - l % (k + 1) - 1]
		print(' '.join(map(str, g)))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

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


t = int(input())
for _ in range(t):
	ns = int(input())
	us = [int(d) for d in input().split()]
	s = [int(d) for d in input().split()]
	d = {}
	for i in range(ns):
		if us[i] in d:
			d[us[i]].append(s[i])
		else:
			d[us[i]] = [s[i]]
	l = [0] * (ns + 1)
	for i in d:
		a = d[i]
		a.sort()
		a.reverse()
		b = [0]
		c = 0
		for j in range(len(a)):
			c += a[j]
			b.append(c)
		for k in range(1, len(a) + 1):
			if len(a) % k == 0:
				l[k] += c
			else:
				m = len(a) // k * k
				l[k] += b[m]
	for i in range(1, ns + 1):
		print(l[i], end=' ')
	print()


from collections import defaultdict
from itertools import accumulate
import sys

def read_ints():
	return map(int, sys.stdin.readline().split())
(t_n,) = read_ints()
for i_t in range(t_n):
	(n,) = read_ints()
	u_seq = tuple(read_ints())
	s_seq = tuple(read_ints())
	scores_by_u = defaultdict(list)
	for (u, s) in zip(u_seq, s_seq):
		scores_by_u[u].append(s)
	groups = [sorted(group, reverse=True) for group in scores_by_u.values()]
	groups = [tuple(accumulate(g)) for g in groups]
	groups.sort(key=len)
	results = [0] * n
	for g in groups:
		for k in range(1, len(g) + 1):
			saved_n = len(g) - len(g) % k
			assert saved_n > 0
			results[k - 1] += g[saved_n - 1]
	print(*results)


from collections import defaultdict

def mkprefix(a):
	b = []
	sm = 0
	for i in range(len(a)):
		sm += a[i]
		b.append(sm)
	return b
R = lambda : list(map(int, input().split()))
for _ in range(int(input())):
	n = int(input())
	u = R()
	s = R()
	d = defaultdict(lambda : list())
	for i in range(n):
		d[u[i]].append(s[i])
	for key in d.keys():
		d[key].sort(reverse=True)
		d[key] = mkprefix(d[key])
	ans = [0] * n
	for u in d.keys():
		l = len(d[u])
		for i in range(l):
			idx = l - l % (i + 1) - 1
			ans[i] += d[u][idx]
	print(*ans)


from math import *
sInt = lambda : int(input())
mInt = lambda : map(int, input().split())
lInt = lambda : list(map(int, input().split()))
t = sInt()
for _ in range(t):
	n = sInt()
	u = lInt()
	s = lInt()
	a = [[] for i in range(n)]
	ss = []
	for i in range(n):
		a[u[i] - 1].append(s[i])
		ss.append([])
	a = list(filter(None, a))
	a.sort(key=lambda x: len(x), reverse=True)
	p = []
	for i in range(len(a)):
		a[i].sort(reverse=True)
		p.append(len(a[i]))
	for elem in a:
		if len(elem) > 0:
			ss.append([elem[0]])
		for i in range(1, len(elem)):
			ss[-1].append(elem[i] + ss[-1][-1])
	ans = [0] * n
	for elem in ss:
		for i in range(len(elem)):
			ans[i] += elem[len(elem) - len(elem) % (i + 1) - 1]
	print(*ans)


def solve():
	t = inp()
	for _ in range(t):
		n = inp()
		uni = seq()
		skill = seq()
		mapper = dd(list)
		for i in range(n):
			mapper[uni[i]].append(skill[i])
		for j in mapper:
			mapper[j].sort(reverse=True)
			for k in range(1, len(mapper[j])):
				mapper[j][k] += mapper[j][k - 1]
		result = [0 for j in range(n)]
		for k in mapper:
			val = len(mapper[k])
			for j in range(1, val + 1):
				val2 = val - val % j - 1
				result[j - 1] += mapper[k][val2]
		print(*result)
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, insort
inp = lambda : int(input())
strng = lambda : input().strip()
jn = lambda x, l: x.join(map(str, l))
strl = lambda : list(input().strip())
mul = lambda : map(int, input().strip().split())
mulf = lambda : map(float, input().strip().split())
seq = lambda : list(map(int, input().strip().split()))
ceil = lambda x: int(x) if x == int(x) else int(x) + 1
ceildiv = lambda x, d: x // d if x % d == 0 else x // d + 1
flush = lambda : stdout.flush()
stdstr = lambda : stdin.readline()
stdint = lambda : int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
solve()


from collections import defaultdict
import sys

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

def slv():
	n = int(input())
	d = defaultdict(list)
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	for (u, v) in zip(a, b):
		d[u].append(v)
	total = [0] * (n + 1)
	for value in d.values():
		l = len(value)
		value.sort(reverse=True)
		value = [0] + value
		for i in range(1, l + 1):
			value[i] += value[i - 1]
		for i in range(1, l + 1):
			mod = l % i
			total[i] += value[l - mod]
	print(*total[1:])
	return

def main():
	t = int(input())
	for i in range(t):
		slv()
	return
main()


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	unv = defaultdict(list)
	for (u1, s1) in sorted(zip(u, s)):
		if u1 not in unv:
			unv[u1].append(s1)
		else:
			unv[u1].append(s1 + unv[u1][-1])
	ans = [0] * n
	for u1 in unv:
		l = len(unv[u1])
		for k in range(1, l + 1):
			left = l % k
			if left == 0:
				ans[k - 1] += unv[u1][-1]
				continue
			ans[k - 1] += unv[u1][-1] - unv[u1][left - 1]
	for elm in ans:
		print(elm, end=' ')
	print(end='\n')


from sys import stdin, stdout
input = stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	u = [int(x) for x in input().split()]
	s = [int(x) for x in input().split()]
	uni = [[] for i in range(n)]
	for i in range(n):
		uni[u[i] - 1].append(s[i])
	for i in range(n):
		uni[i] = sorted(uni[i], reverse=True)
	pre = [[] for i in range(n)]
	maxi = 0
	count = -1
	for i in range(n):
		maxi = max(maxi, len(uni[i]))
		if len(uni[i]) == 0:
			continue
		count += 1
		for j in uni[i]:
			if len(pre[count]) == 0:
				pre[count].append(j)
			else:
				pre[count].append(j + pre[count][-1])
	pre = sorted(pre, key=lambda x: len(x), reverse=True)
	l = [len(pre[i]) for i in range(len(pre))]
	for i in range(n):
		sz = i + 1
		if sz > maxi:
			stdout.write(str(0) + ' ')
			continue
		ans = 0
		for j in range(count + 1):
			if l[j] < sz:
				break
			to_leave = l[j] % sz
			ans += pre[j][l[j] - to_leave - 1]
		stdout.write(str(ans) + ' ')
	stdout.write('\n')


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	dic = defaultdict(list)
	for i in range(n):
		dic[a[i]].append(b[i])
	for (k, v) in dic.items():
		v.sort(reverse=True)
		for i in range(1, len(v)):
			v[i] += v[i - 1]
	ans = [0] * n
	for (k, v) in dic.items():
		m = len(v)
		for i in range(m):
			ans[i] += v[m - m % (i + 1) - 1]
	print(*ans)


import math

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

def sol():
	n = int(input())
	A = list(putin())
	B = list(putin())
	S = []
	SS = []
	for i in range(n):
		S.append([])
		SS.append([])
	for i in range(n):
		S[A[i] - 1].append(B[i])
	S.sort(key=lambda x: len(x), reverse=True)
	for i in range(n):
		S[i].sort(reverse=True)
	for elem in S:
		if len(elem) > 0:
			SS.append([elem[0]])
		for i in range(1, len(elem)):
			SS[-1].append(elem[i] + SS[-1][-1])
	answer = [0] * n
	for elem in SS:
		for i in range(len(elem)):
			answer[i] += elem[len(elem) - len(elem) % (i + 1) - 1]
	print(*answer)
for iter in range(int(input())):
	sol()


from itertools import accumulate
from collections import defaultdict
for t in range(int(input())):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	d = defaultdict(list)
	for (key, value) in zip(u, s):
		d[key].append(value)
	ans = [0] * (n + 1)
	for key in d.keys():
		d[key].sort(reverse=True)
		l = list(accumulate(d[key]))
		for r in range(1, min(n, len(l)) + 1):
			ans[r] += l[len(l) // r * r - 1]
	print(*ans[1:], sep=' ')


for _ in range(int(input())):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	dp = dict()
	for i in range(n):
		try:
			dp[u[i]].append(s[i])
		except:
			dp[u[i]] = []
			dp[u[i]].append(s[i])
	op = 0
	ans = [0 for i in range(n + 1)]
	for i in dp:
		dp[i].sort(reverse=True)
		halp = [0]
		for x in dp[i]:
			halp.append(halp[-1] + x)
		for j in range(1, len(halp)):
			ans[j] += halp[len(dp[i]) // j * j]
	print(*ans[1:])


from itertools import accumulate

def solve(u, s, n):
	res = [0] * n
	l = [[] for i in range(n)]
	for i in range(n):
		l[u[i] - 1].append(s[i])
	for i in range(n):
		l[i].sort(reverse=True)
		l[i] = list(accumulate(l[i]))
	for j in range(len(l)):
		le = len(l[j])
		for i in range(1, n + 1):
			if le >= i:
				if le % i == 0:
					res[i - 1] += l[j][-1]
				else:
					res[i - 1] += l[j][le - le % i - 1]
			else:
				break
	print(*res)
for i in range(int(input())):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	solve(u, s, n)


from collections import defaultdict

def prefix(A):
	dp_A = [0]
	for a in A:
		dp_A.append(dp_A[-1] + a)
	return dp_A
for _ in range(int(input())):
	N = int(input())
	dic = defaultdict(list)
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	for (a, b) in zip(A, B):
		dic[a].append(b)
	die = defaultdict(int)
	cum = defaultdict(int)
	for (k, l) in dic.items():
		l.sort()
		dp = prefix(l)
		for i in range(1, len(l) + 1):
			die[i] += dp[len(l) % i]
		cum[len(l)] += dp[-1]
	cur = sum(B)
	for n in range(1, N + 1):
		print(cur - die[n], end=' ')
		cur -= cum[n]
	print()


def solvecaso():
	n = int(input())
	unis = {}
	pertenece = list(map(int, input().split()))
	alumnos = list(map(int, input().split()))
	for uni in pertenece:
		if not uni in unis:
			unis[uni] = []
	for i in range(n):
		unis[pertenece[i]].append(alumnos[i])
	unis = [uni[1] for uni in unis.items()]
	for uni in unis:
		uni.sort(reverse=True)
		suma = 0
		for i in range(len(uni)):
			suma += uni[i]
			uni[i] = suma
	ks = [0 for _ in range(n)]
	for i in range(1, n + 1):
		suma = 0
		nuevaunis = []
		for uni in unis:
			numest = len(uni)
			if i > numest:
				continue
			div = numest // i
			suma += uni[i * div - 1]
			nuevaunis.append(uni)
		unis = nuevaunis
		ks[i - 1] = str(suma)
	print(' '.join(ks))
	return 0
casos = int(input())
for caso in range(1, casos + 1):
	result = solvecaso()


R = lambda : map(int, input().split())
(t,) = R()
while t:
	t -= 1
	(n,) = R()
	r = [0] * n
	a = [[0] for _ in r]
	for (x, y) in sorted(zip(R(), R())):
		l = a[x - 1]
		l += (l[-1] + y,)
	for x in a:
		for i in range(1, len(x)):
			r[i - 1] += x[-1] - x[(len(x) - 1) % i]
	print(*r)


from sys import stdin, stdout
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
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)
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7
for i in range(I()):
	n = I()
	a = L()
	b = L()
	d = [[] for i in range(n)]
	ln = [0] * n
	for i in range(n):
		d[a[i] - 1] += (b[i],)
		ln[a[i] - 1] += 1
	for i in range(n):
		if not d[i]:
			continue
		d[i].sort(reverse=True)
		for j in range(1, ln[i]):
			d[i][j] += d[i][j - 1]
		d[i] = [0] + d[i]
	ans = [0] * n
	for i in range(n):
		if not d[i]:
			continue
		for j in range(ln[i]):
			ans[j] += d[i][ln[i] - ln[i] % (j + 1)]
	print(*ans)


from collections import defaultdict as dd
for _ in range(int(input())):
	n = int(input())
	uni = list(map(int, input().split()))
	std = list(map(int, input().split()))
	dic = dd(list)
	for i in range(n):
		dic[uni[i]].append(std[i])
	prfx = dd(list)
	for i in dic:
		sm = 0
		dic[i].sort()
		for j in range(len(dic[i]) - 1, -1, -1):
			sm += dic[i][j]
			prfx[i].append(sm)
	ans = [0] * n
	for i in dic:
		for k in range(1, n + 1):
			div = len(dic[i]) // k
			if div == 0:
				break
			ans[k - 1] += prfx[i][div * k - 1]
	for i in range(len(ans)):
		print(ans[i], end=' ')
	print('')


for _ in range(int(input())):
	n = int(input())
	univ = list(map(int, input().split()))
	skill = list(map(int, input().split()))
	l = [[] for _ in range(n)]
	for i in range(n):
		l[univ[i] - 1].append(skill[i])
	for i in range(n):
		l[i].sort(reverse=True)
		for j in range(1, len(l[i])):
			l[i][j] += l[i][j - 1]
	res = [0] * n
	for i in range(n):
		for k in range(1, len(l[i]) + 1):
			x = len(l[i]) // k
			t = x * k
			if t != 0:
				res[k - 1] += l[i][t - 1]
	print(*res)


t = int(input())
for inh in range(0, t):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	a = {}
	uni = set()
	for i in range(0, n):
		if u[i] in uni:
			a[u[i]].append(s[i])
		else:
			a[u[i]] = [s[i]]
			uni.add(u[i])
	uni = list(uni)
	ans = [0] * n
	for i in range(0, len(uni)):
		k = a[uni[i]]
		k.sort(reverse=True)
		(i, j) = (1, len(k))
		while i < len(k):
			k[i] += k[i - 1]
			i += 1
		for i in range(1, j + 1):
			last = j - j % i - 1
			ans[i - 1] += k[last]
	print(*ans)


for _ in range(int(input())):
	n = int(input())
	urr = list(map(int, input().split()))
	for i in range(n):
		urr[i] -= 1
	srr = list(map(int, input().split()))
	mapper = [[] for i in range(n)]
	for i in range(n):
		mapper[urr[i]].append(srr[i])
	for i in range(n):
		temp = mapper[i]
		temp.sort(reverse=True)
		mapper[i] = temp
	ans = [0 for i in range(n + 5)]
	for i in range(n):
		k = len(mapper[i])
		if k == 0:
			continue
		for j in range(k - 2, -1, -1):
			mapper[i][j] += mapper[i][j + 1]
		for g in range(1, k + 1):
			rem = k % g
			if rem > 0:
				ans[g] -= mapper[i][k - rem]
				ans[g + 1] += mapper[i][k - rem]
		ans[k + 1] -= mapper[i][0]
	total = sum(srr)
	for i in range(1, n + 5):
		ans[i] += ans[i - 1]
	for i in range(n + 5):
		ans[i] += total
	for i in range(1, n + 1):
		print(ans[i], end=' ')
	print()


def solve():
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	u = [x for x in sorted(zip(s, u))]
	uni = [[0] for _ in range(n)]
	for i in reversed(u):
		uni[i[1] - 1].append(uni[i[1] - 1][-1] + i[0])
	ans = [0] * n
	for i in uni:
		x = len(i) - 1
		for j in range(1, x + 1):
			ans[j - 1] += i[x - x % j]
	return ' '.join((str(x) for x in ans))
for t in range(int(input())):
	n = int(input())
	print(solve())


from sys import stdin

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

def read_ints():
	return map(int, stdin.readline().split(' '))
t = read_int()
for case_num in range(t):
	n = read_int()
	u = list(read_ints())
	s = list(read_ints())
	tot = sum(s)
	d = [[] for _ in range(n + 1)]
	for (ui, si) in zip(u, s):
		d[ui].append(si)
	for i in range(1, n + 1):
		d[i].sort()
	acc = [[0] * (len(d[i]) + 1) for i in range(n + 1)]
	for i in range(1, n + 1):
		for j in range(1, len(d[i]) + 1):
			acc[i][j] = acc[i][j - 1] + d[i][j - 1]
	ans = []
	rem = set([i for i in range(1, n + 1) if len(d[i]) > 0])
	for k in range(1, n + 1):
		dec = 0
		to_remove = set()
		for i in rem:
			if len(d[i]) < k:
				to_remove.add(i)
				tot -= sum(d[i])
			else:
				dec += acc[i][len(d[i]) % k]
		ans.append(tot - dec)
		for i in to_remove:
			rem.remove(i)
	print(' '.join(map(str, ans)))


for i in range(int(input())):
	n = int(input())
	uni = list(map(int, input().split()))
	skills = list(map(int, input().split()))
	d = {}
	for i in range(n):
		if uni[i] in d:
			d[uni[i]].append(skills[i])
		else:
			d[uni[i]] = [skills[i]]
	ans = [0] * n
	for i in d:
		d[i].sort(reverse=True)
		m = len(d[i])
		for j in range(1, m):
			d[i][j] += d[i][j - 1]
		for j in range(m):
			v = 1 + j
			mod = m % v
			ans[j] += d[i][-(mod + 1)]
	print(*ans)


from sys import *
from collections import defaultdict
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	u = list(map(int, stdin.readline().split()))
	skill = list(map(int, stdin.readline().split()))
	vectortmp = defaultdict()
	for i in range(n):
		if u[i] in vectortmp:
			vectortmp[u[i]].append(skill[i])
		else:
			vectortmp[u[i]] = [skill[i]]
	a = defaultdict(list)
	maxi = 0
	for i in vectortmp:
		vectortmp[i].sort(reverse=True)
	for i in vectortmp:
		a[i] = [vectortmp[i][0]]
		maxi = max(maxi, len(vectortmp[i]))
		for j in range(1, len(vectortmp[i])):
			a[i].append(a[i][-1] + vectortmp[i][j])
	ans = [0] * n
	for i in vectortmp:
		for j in range(1, maxi + 1):
			if len(vectortmp[i]) >= j:
				pos = len(vectortmp[i]) - len(vectortmp[i]) % j
				ans[j - 1] += a[i][pos - 1]
			else:
				break
	print(*ans)


T = int(input())
for t in range(1, T + 1):
	n = int(input())
	A = dict()
	u = list(map(int, input().split(' ')))
	s = list(map(int, input().split(' ')))
	B = sorted(list(zip(s, u)), reverse=True)
	for b in B:
		(v, i) = b
		if A.get(i):
			A[i].append(A[i][-1] + v)
		else:
			A[i] = [v]
	B = [0] * n
	for i in A.keys():
		for k in range(1, len(A[i]) + 1):
			r = len(A[i]) % k
			B[k - 1] += A[i][len(A[i]) - r - 1]
	print(*B)


class Program:

	@staticmethod
	def BerlandRegional(n: int, s: list, u: list):
		best = []
		pr = []
		for j in range(n):
			best.append([])
			pr.append([0])
		for j in range(n):
			best[s[j] - 1].append(u[j])
		for j in range(n):
			best[j] = sorted(best[j], reverse=True)
		for j in range(n):
			for x in best[j]:
				pr[j].append(pr[j][-1] + x)
		answer = [0] * n
		for j in range(n):
			for k in range(1, len(best[j]) + 1):
				answer[k - 1] += pr[j][int(len(best[j]) / k) * k]
		return answer
t = int(input())
for i in range(t):
	n = int(input())
	s = list(map(int, input().split(' ')))
	u = list(map(int, input().split(' ')))
	print(' '.join(map(str, Program.BerlandRegional(n, s, u))))


from sys import stdin
input = stdin.buffer.readline
t = int(input())
for i in range(t):
	n = int(input())
	univ = [int(x) for x in input().split()]
	skill = [int(x) for x in input().split()]
	arr = [[] for i in range(n + 1)]
	for i in range(n):
		arr[univ[i]].append(skill[i])
	for i in arr:
		i.sort(reverse=True)
	ans = [0] * (n + 1)
	for i in range(1, n + 1):
		for j in range(1, len(arr[i])):
			arr[i][j] = arr[i][j] + arr[i][j - 1]
		k = 1
		while k <= len(arr[i]):
			x = len(arr[i]) // k * k
			ans[k] = ans[k] + arr[i][x - 1]
			k = k + 1
	print(*ans[1:])


from bisect import insort_left as ins
t = int(input())
for _ in range(t):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	ak = [(u[i], s[i]) for i in range(n)]
	ak.sort(reverse=True)
	sums = []
	prev = 0
	cur = -1
	for ele in ak:
		if ele[0] != prev:
			sums.append(list())
			prev = ele[0]
			cur += 1
			sums[cur].append(ele[1])
		else:
			sums[cur].append(sums[cur][-1] + ele[1])
	ans = [0 for i in range(n)]
	for ele in sums:
		for i in range(len(ele)):
			ans[i] += ele[len(ele) // (i + 1) * (i + 1) - 1]
	print(' '.join([str(x) for x in ans]))


def solve():
	n = int(input())
	u = list(map(int, input().split()))
	l = list(map(int, input().split()))
	d = {}
	for i in range(n):
		if u[i] in d:
			d[u[i]].append(l[i])
		else:
			e = [l[i]]
			d[u[i]] = e
	ans = [0] * n
	for ii in d:
		d[ii].sort(reverse=True)
		for i in range(1, len(d[ii])):
			d[ii][i] += d[ii][i - 1]
		for k in range(1, n + 1):
			new = len(d[ii]) // k * k
			if new == 0:
				break
			ans[k - 1] += d[ii][new - 1]
	print(*ans)
for testis in range(int(input())):
	solve()


import sys
import os.path
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
depth = 1000005
mod = 1000000007
lim = mod * mod
sys.setrecursionlimit(depth)
from math import inf, ceil, sqrt
from collections import defaultdict
from operator import itemgetter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	d = defaultdict(lambda : [])
	for i in range(n):
		d[a[i]].append(b[i])
	d = list(d.items())
	for (x, y) in d:
		y.sort(reverse=True)
	d.sort(key=lambda x: len(x[1]), reverse=True)
	for (x, y) in d:
		for i in range(1, len(y)):
			y[i] += y[i - 1]
	res = [-1 for _ in range(n)]
	lim = len(d)
	for i in range(n):
		ans = 0
		for j in range(lim):
			l = len(d[j][1])
			if l < i + 1:
				lim = j
				break
			ans += d[j][1][l - 1 - l % (i + 1)]
		res[i] = ans
	print(*res)


import sys
import math
from collections import defaultdict as dd
from sys import stdin
input = stdin.readline
m = 10 ** 9 + 7
sys.setrecursionlimit(10 ** 5)
T = int(input())
for _ in range(T):
	N = int(input())
	U = list(map(int, input().split()))
	S = list(map(int, input().split()))
	ANS = [0] * N
	X = [[0] for i in range(N)]
	l = []
	for i in range(N):
		l.append([S[i], U[i]])
	l.sort(reverse=True)
	for i in l:
		X[i[1] - 1].append(i[0])
	for i in X:
		for j in range(1, len(i)):
			i[j] += i[j - 1]
		i.pop(0)
	for i in range(N):
		lim = len(X[i])
		for j in range(1, lim + 1):
			if lim >= j:
				rem = lim % j
				el = X[i][-rem - 1]
				ANS[j - 1] += el
	print(*ANS)


from math import *
from collections import defaultdict as dt
from sys import stdin
inp = lambda : stdin.readline().strip()
I = lambda : int(inp())
M = lambda : map(int, stdin.readline().split())
L = lambda : list(map(int, stdin.readline().split()))
mod = 1000000009
inf = 100000000000000000000
ss = 'abcdefghijklmnopqrstuvwxyz'

def solve():
	n = I()
	a = L()
	b = L()
	d = dt(lambda : [])
	e = dt(lambda : 0)
	f = dt(lambda : 0)
	h = dt(lambda : [])
	z = [0 for i in range(n)]
	for i in range(n):
		d[a[i]].append(b[i])
		e[a[i]] += 1
		f[a[i]] += b[i]
	for i in d:
		c = 0
		d[i] = sorted(d[i])
		for j in d[i]:
			c = c + j
			h[i].append(c)
		for j in range(e[i]):
			x = e[i] % (j + 1)
			if x == 0:
				z[j] = z[j] + f[i]
			else:
				z[j] = z[j] + f[i] - h[i][x - 1]
		c = 0
	print(*z)
tt = 1
tt = I()
for _ in range(tt):
	solve()


from sys import *
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')
(stdin, stdout) = (IOWrapper(stdin), IOWrapper(stdout))

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

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

def zzz():
	return [int(i) for i in fast().split()]
(z, zz) = (input, lambda : list(map(int, z().split())))
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))

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

def output(answer):
	stdout.write(str(answer))
dx = [-1, 1, 0, 0, 1, -1, 1, -1]
dy = [0, 0, 1, -1, 1, -1, -1, 1]
num = int(z())
for _ in range(num):
	n = int(z())
	cnt = {}
	arr1 = zzz()
	arr2 = zzz()
	for (i, j) in zip(arr1, arr2):
		try:
			cnt[i]
		except:
			cnt[i] = [0]
		cnt[i].append(j)
	ans = [0] * (n + 1)
	for i in cnt:
		cnt[i] = sorted(cnt[i])
		for j in range(1, len(cnt[i])):
			cnt[i][j] += cnt[i][j - 1]
		for ii in range(1, len(cnt[i])):
			rem = (len(cnt[i]) - 1) % ii
			ans[ii] += cnt[i][-1] - cnt[i][rem]
	for i in range(1, n + 1):
		print(ans[i])


t = int(input())
for i in range(t):
	n = int(input())
	u = [int(x) for x in input().split()]
	s = [int(x) for x in input().split()]
	d = [[] for i in range(n)]
	ln = [0] * n
	for i in range(n):
		d[u[i] - 1].append(s[i])
		ln[u[i] - 1] += 1
	for i in range(n):
		if not d[i]:
			continue
		d[i].sort(reverse=True)
		for j in range(1, ln[i]):
			d[i][j] += d[i][j - 1]
		d[i] = [0] + d[i]
	ans = [0] * n
	for i in range(n):
		if not d[i]:
			continue
		for j in range(ln[i]):
			ans[j] += d[i][ln[i] - ln[i] % (j + 1)]
	print(*ans)


import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
for i in range(int(input())):
	n = int(input())
	z = list(map(int, input().split()))
	s = list(map(int, input().split()))
	d = {}
	for i in range(n):
		if z[i] in d:
			d[z[i]].append(s[i])
		else:
			d[z[i]] = [s[i]]
	m = {}
	for i in d.keys():
		d[i].sort(reverse=True)
		m[i] = len(d[i])
		for k in range(1, len(d[i])):
			d[i][k] += d[i][k - 1]
	x = set()
	a = [0] * n
	for k in range(1, n + 1):
		ans = 0
		if d == {}:
			break
		for i in list(d.keys()):
			z = m[i] // k * k
			if z != 0:
				ans += d[i][z - 1]
			if m[i] <= k:
				del d[i]
		a[k - 1] = ans
	for i in a:
		print(i, end=' ')
	print()


import itertools
from collections import defaultdict

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

def solve():
	n = int(input())
	u = readline()
	s = readline()
	universities = defaultdict(list)
	for (ui, si) in zip(u, s):
		universities[ui].append(si)
	answer = [0] * n
	for students in universities.values():
		students.sort(reverse=True)
		part_sum = list(itertools.accumulate(students, initial=0))
		l = len(students)
		for k in range(l):
			answer[k] += part_sum[l - l % (k + 1)]
	print(*answer)
t = int(input())
for __ in range(t):
	solve()


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	skill = defaultdict(list)
	for (idx, i) in enumerate(u):
		skill[i].append(s[idx])
	students = []
	for i in skill:
		students.append(sorted(skill[i]))
	prefix = []
	for i in students:
		temp = [i[0]]
		for j in i[1:]:
			temp.append(temp[-1] + j)
		prefix.append(temp)
	ans = [0] * n
	for i in prefix:
		for j in range(1, len(i) + 1):
			delete = len(i) % j
			a = i[-1]
			if delete > 0:
				a -= i[delete - 1]
			ans[j - 1] += a
	print(*ans)


import sys
input = sys.stdin.buffer.readline
L = lambda : list(map(int, input().split()))
tc = int(input())
for _ in range(tc):
	n = int(input())
	u = L()
	s = L()
	unis = [[0] for _ in range(n + 1)]
	for i in range(n):
		unis[u[i]].append(s[i])
	ans = [0] * n
	for i in range(n + 1):
		c = len(unis[i]) - 1
		if c:
			unis[i].sort()
			for j in range(1, c + 1):
				unis[i][j] = unis[i][j - 1] + unis[i][j]
			for j in range(1, c + 1):
				ans[j - 1] += unis[i][c] - unis[i][c % j]
	print(*ans)


import sys
import itertools

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

def rl_types(types):
	str_list = [x for x in sys.stdin.readline().strip().split(' ')]
	return [types[i](str_list[i]) for i in range(len(str_list))]

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

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

def solve(univs, studs, n):
	by_univ = {i: [] for i in range(1, n + 1)}
	for i in range(n):
		by_univ[univs[i]].append(studs[i])
	all_studs = {}
	l = {}
	for sch in range(1, n + 1):
		students = sorted(by_univ[sch], reverse=True)
		l = len(students)
		if not l in all_studs:
			all_studs[l] = [0] * l
		temp = all_studs[l]
		for i in range(l):
			temp[i] += students[i]
		all_studs[l] = temp
	if 0 in all_studs:
		del all_studs[0]
	for l in all_studs:
		all_studs[l] = [x for x in itertools.accumulate(all_studs[l])]
	k_vals = [0] * n
	for k in range(1, n + 1):
		for l in all_studs:
			if k > l:
				continue
			ig = l % k
			k_vals[k - 1] += all_studs[l][l - ig - 1]
	return k_vals
NT = int(rl())
for ti in range(NT):
	n = int(rl())
	univs = [int(x) for x in rl().split(' ')]
	studs = [int(x) for x in rl().split(' ')]
	res = solve(univs, studs, n)
	pra(res)


for _ in range(int(input())):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	x = dict()
	ans = [0] * n
	for i in range(n):
		try:
			x[u[i]].append(s[i])
		except:
			x[u[i]] = [s[i]]
	for i in x.keys():
		x[i].sort(reverse=True)
		sv = dict()
		for j in range(0, len(x[i])):
			try:
				sv[i].append(sv[i][j - 1] + x[i][j])
			except:
				sv[i] = [x[i][0]]
		for j in range(1, len(x[i]) + 1):
			rem = len(x[i]) % j
			ans[j - 1] += sv[i][-1 - rem]
	print(*ans)


import sys
import math
from collections import Counter, defaultdict
L = lambda : list(map(int, input().split()))
IN = lambda : int(input())
S = lambda : input()

def solve():
	n = IN()
	a = L()
	s = L()
	d = Counter(a)
	ans = [0] * (n + 1)
	b = [[] for i in range(n + 1)]
	for i in range(n):
		b[a[i]].append(s[i])
	for i in d:
		b[i].sort(reverse=True)
		c = [0] * d[i]
		c[0] = b[i][0]
		for j in range(1, d[i]):
			c[j] += c[j - 1] + b[i][j]
		for j in range(1, d[i] + 1):
			e = d[i] // j * j
			ans[j] += c[e - 1]
	ans.pop(0)
	print(*ans)
for _ in range(IN()):
	solve()


import sys
input = sys.stdin.readline
import math
import bisect
from copy import deepcopy as dc
from itertools import accumulate
from collections import Counter, defaultdict, deque

def ceil(U, V):
	return (U + V - 1) // V

def modf1(N, MOD):
	return (N - 1) % MOD + 1
inf = int(1e+20)
mod = int(1000000000.0 + 7)
t = int(input())
for _ in range(t):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	uni = [[] for i in range(n + 1)]
	for i in range(n):
		uni[u[i]].append(s[i])
	for i in uni:
		i.sort(reverse=True)
	for i in range(n + 1):
		uni[i] = list(accumulate(uni[i]))
	uc = list(Counter(u).items())
	ans = [0] * (n + 1)
	for (i, j) in uc:
		for k in range(1, n + 1):
			if j >= k:
				ans[k] += uni[i][j - j % k - 1]
			else:
				break
	print(*ans[1:])


for _ in range(int(input())):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	p = max(u)
	l = [[] for i in range(p)]
	for i in range(n):
		l[u[i] - 1].append(s[i])
	l.sort(key=len, reverse=True)
	for i in range(p):
		l[i].sort(reverse=True)
		for j in range(1, len(l[i])):
			l[i][j] += l[i][j - 1]
	ans = []
	for i in range(1, n + 1):
		res = 0
		for j in range(p):
			x = len(l[j])
			if i <= x:
				res += l[j][x - x % i - 1]
			else:
				break
		ans.append(res)
	print(*ans)


from collections import defaultdict

def solve():
	mem = defaultdict(list)
	N = len(uni)
	for i in range(N):
		mem[uni[i]].append(stu[i])
	ans = [0 for i in range(N)]
	for un in mem:
		arr = mem[un]
		arr.sort(reverse=True)
		for i in range(1, len(arr)):
			arr[i] += arr[i - 1]
		lim = len(arr)
		for i in range(1, lim + 1):
			rem = lim % i
			ans[i - 1] += arr[lim - rem - 1]
	return ' '.join(map(str, ans))
T = int(input())
for t in range(1, T + 1):
	n = int(input())
	uni = list(map(int, input().split()))
	stu = list(map(int, input().split()))
	ans = solve()
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	u = [int(x) for x in input().split()]
	s = [int(x) for x in input().split()]
	univ = set(u)
	d = {int(x): [] for x in univ}
	for i in range(n):
		d[u[i]].append(s[i])
	for k in d.keys():
		d[k].sort(reverse=True)
	d1 = {}
	for key in d.keys():
		pr = [0]
		for x in d[key]:
			pr.append(pr[-1] + x)
		d1[key] = pr
	for k in range(1, n + 1):
		ans = 0
		dell = set()
		for u in univ:
			if len(d[u]) == k:
				dell.add(u)
			p = len(d[u]) - len(d[u]) % k
			ans += d1[u][p]
		univ -= dell
		print(ans, end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	u = [int(x) for x in input().split()]
	s = [int(x) for x in input().split()]
	d = {}
	for i in range(n):
		if u[i] in d:
			d[u[i]].append(s[i])
		else:
			d[u[i]] = [s[i]]
	for i in d:
		d[i] = sorted(d[i], reverse=True)
		for j in range(1, len(d[i])):
			d[i][j] += d[i][j - 1]
	for k in range(1, n + 1):
		ans = 0
		a = []
		for i in d:
			l = len(d[i])
			ans += d[i][l - l % k - 1]
			if l == k:
				a.append(i)
		for i in a:
			del d[i]
		print(ans, end=' ')
	print()


import sys
import math

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

def str_arr():
	return list(map(str, sys.stdin.readline().split()))

def input():
	return sys.stdin.readline()
for _ in range(int(input())):
	n = int(input())
	arr1 = int_arr()
	arr2 = int_arr()
	(arr1, arr2) = zip(*sorted(zip(arr1, arr2)))
	dict = {}
	for i in range(n):
		if dict.get(arr1[i], None) == None:
			dict[arr1[i]] = [arr2[i]]
		else:
			dict[arr1[i]].append(arr2[i] + dict[arr1[i]][-1])
	ans = [0] * n
	for j in dict:
		for i in range(1, len(dict[j]) + 1):
			l = len(dict[j]) % i
			if l == 0:
				ans[i - 1] += dict[j][-1]
			else:
				ans[i - 1] += dict[j][-1] - dict[j][l - 1]
	print(*ans)


t = int(input())
while t:
	t -= 1
	n = int(input())
	u = list(map(int, input().split()))
	mx = 1
	dic = {}
	totsum = 0
	for (index, value) in enumerate(input().split()):
		totsum += int(value)
		if u[index] not in dic:
			dic[u[index]] = [int(value)]
		else:
			dic[u[index]].append(int(value))
		mx = max(len(dic[u[index]]), mx)
	ans = [0] * n
	for key in dic:
		dic[key].sort()
		s = dic[key][0]
		ln = len(dic[key])
		for i in range(1, len(dic[key])):
			dic[key][i] = dic[key][i] + s
			s = dic[key][i]
		for i in range(1, ln + 1):
			re = ln % i
			if re == 0:
				ans[i - 1] += dic[key][ln - 1]
			else:
				ans[i - 1] += dic[key][ln - 1] - dic[key][re - 1]
	print(*ans)


from collections import defaultdict

def heelo():
	return 'hello'

def akfbdkg():
	return 'dkgks'
test = int(input())
while test != 0:
	n = int(input())
	ll1 = list(map(int, input().split()))
	ll2 = list(map(int, input().split()))
	dicti = defaultdict(list)
	s = defaultdict(int)
	for i in range(n):
		dicti[ll1[i]].append(ll2[i])
		s[ll1[i]] += 1
	for i in dicti.keys():
		dicti[i].sort(reverse=True)
	for i in dicti.keys():
		for j in range(1, len(dicti[i])):
			dicti[i][j] = dicti[i][j] + dicti[i][j - 1]
	fa = []
	for i in range(1, n + 1):
		temp_ans = 0
		fin = []
		for j in dicti.keys():
			p = s[j]
			var = p // i
			index = var * i - 1
			if index < 0:
				fin.append(j)
				temp_ans += 0
			else:
				temp_ans += dicti[j][index]
		for j in range(len(fin)):
			del dicti[fin[j]]
		fa.append(temp_ans)
	print(*fa)
	test -= 1


def answer(n, u, s):
	record = [[] for i in range(n)]
	for i in range(n):
		record[u[i] - 1].append(s[i])
	for i in range(n):
		record[i].sort(reverse=True)
		record[i] = (len(record[i]), [], record[i])
		l = record[i][2]
		length = record[i][0]
		if length != 0:
			record[i][1].append(l[0])
		for j in range(1, length):
			record[i][1].append(l[j] + record[i][1][-1])
	record.sort(reverse=True)
	for k in range(1, n + 1):
		ans = 0
		for i in range(n):
			length = record[i][0]
			if length < k:
				break
			else:
				remainder = length % k
				ans += record[i][1][length - 1 - remainder]
		print(ans, end=' ')
	print('')
t = int(input())
for i in range(t):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	answer(n, u, s)


from io import BytesIO, IOBase
from math import sqrt
import sys, os
from os import path
inpl = lambda : list(map(int, input().split()))
inpm = lambda : map(int, input().split())
inpi = lambda : int(input())
inp = lambda : input()
(rev, ra, l) = (reversed, range, len)
P = 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')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

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

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

def factors(n):
	arr = []
	for i in ra(2, int(sqrt(n)) + 1):
		if n % i == 0:
			arr.append(i)
			if i * i != n:
				arr.append(int(n / i))
	return arr

def dfs(arr, n):
	pairs = [[i + 1] for i in ra(n)]
	vis = [0] * (n + 1)
	for i in ra(l(arr)):
		pairs[arr[i][0] - 1].append(arr[i][1])
	pairs[arr[i][1] - 1].append(arr[i][0])
	comp = []
	for i in ra(n):
		stack = [pairs[i]]
		temp = []
		if vis[stack[-1][0]] == 0:
			while len(stack) > 0:
				if vis[stack[-1][0]] == 0:
					temp.append(stack[-1][0])
					vis[stack[-1][0]] = 1
					s = stack.pop()
					for j in s[1:]:
						if vis[j] == 0:
							stack.append(pairs[j - 1])
				else:
					stack.pop()
			comp.append(temp)
	return comp
from bisect import bisect_left as bl
from bisect import bisect_right as br
import sys, operator, math, operator
from collections import Counter
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
import random
tc = inpi()
while tc:
	tc -= 1
	n = inpi()
	li1 = inpl()
	li2 = inpl()
	omap = {}
	for i in ra(n):
		if li1[i] not in omap:
			omap[li1[i]] = [li2[i]]
		else:
			omap[li1[i]].append(li2[i])
	omap1 = {}
	ma = 0
	for (k, v) in omap.items():
		v.sort(reverse=True)
		ma = max(ma, l(v))
		pref = [v[0]]
		for i in ra(1, l(v)):
			pref.append(pref[-1] + v[i])
		omap1[k] = pref
	ans = [0] * (n + 1)
	for (k, v) in omap1.items():
		si = l(v)
		for i in ra(1, n + 1):
			if i > si:
				break
			fac = si // i
			lim = fac * i
			if lim > 0:
				ans[i] += v[lim - 1]
	P(*ans[1:])


import os
import sys
from collections import Counter, defaultdict
from io import BytesIO, IOBase
from math import gcd, inf, sqrt, ceil
sys.setrecursionlimit(2 * 10 ** 5)

def lcm(a, b):
	return a * b // gcd(a, b)
if True:
	mod = 10 ** 9 + 7
	fac = [1]
	for i in range(1, 2 * 10 ** 5 + 1):
		fac.append(fac[-1] * i % mod)
	fac_in = [pow(fac[-1], mod - 2, mod)]
	for i in range(2 * 10 ** 5, 0, -1):
		fac_in.append(fac_in[-1] * i % mod)
	fac_in.reverse()

	def comb(a, b):
		if a < b:
			return 0
		return fac[a] * fac_in[b] * fac_in[a - b] % mod

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

def getFactorization(x):
	ret = set()
	while x != 1:
		ret.add(spf[x])
		x = x // spf[x]
	return ret

def printDivisors(n):
	i = 2
	z = [1, n]
	while i <= sqrt(n):
		if n % i == 0:
			if n / i == i:
				z.append(i)
			else:
				z.append(i)
				z.append(n // i)
		i = i + 1
	return z

def create(n, x, f):
	pq = len(bin(n)[2:])
	if f == 0:
		tt = min
	else:
		tt = max
	dp = [[inf] * n for _ in range(pq)]
	dp[0] = x
	for i in range(1, pq):
		for j in range(n - (1 << i) + 1):
			dp[i][j] = tt(dp[i - 1][j], dp[i - 1][j + (1 << i - 1)])
	return dp

def enquiry(l, r, dp, f):
	if l > r:
		return inf if not f else -inf
	if f == 1:
		tt = max
	else:
		tt = min
	pq1 = len(bin(r - l + 1)[2:]) - 1
	return tt(dp[pq1][l], dp[pq1][r - (1 << pq1) + 1])

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

def main():
	for _ in range(int(input())):
		n = int(input())
		u = [int(x) for x in input().split()]
		s = [int(x) for x in input().split()]
		adj = defaultdict(list)
		for i in range(n):
			adj[u[i]].append(s[i])
		an = [0] * (n + 1)
		for i in adj:
			x = adj[i][:]
			x.sort()
			for j in range(len(x) - 2, -1, -1):
				x[j] += x[j + 1]
			for j in range(len(x)):
				an[j + 1] += x[len(x) % (j + 1)]
		print(*an[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')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input())):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	l = [[] for i in range(n + 1)]
	for i in range(n):
		l[u[i]].append(s[i])
	for i in range(n + 1):
		l[i].sort(reverse=True)
		add = 0
		ll = len(l[i])
		for j in range(ll):
			add += l[i][j]
			l[i][j] = add
	ans = [0] * (n + 1)
	for i in range(1, n + 1):
		ll = len(l[i])
		for j in range(1, ll + 1):
			ans[j] += l[i][ll // j * j - 1]
	print(*ans[1:])


from sys import stdin
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	u = list(map(int, stdin.readline().split()))
	s = list(map(int, stdin.readline().split()))
	c = {}
	for i in range(n):
		if u[i] in c:
			c[u[i]].append(s[i])
		else:
			c[u[i]] = [s[i]]
	ar = []
	ans = [0 for i in range(n)]
	for i in c:
		c[i].sort(reverse=True)
		pre = [c[i][0]]
		for j in c[i][1:]:
			pre.append(pre[-1] + j)
		pre = pre[::-1]
		l = len(c[i])
		for j in range(1, l + 1):
			ans[j - 1] += pre[l % j]
	print(*ans)


for _ in range(int(input())):
	n = int(input())
	un = list(map(int, input().split()))
	sk = list(map(int, input().split()))
	p = max(un)
	l = [[] for i in range(p)]
	for i in range(n):
		l[un[i] - 1].append(sk[i])
	l.sort(key=len, reverse=True)
	for i in range(p):
		l[i].sort(reverse=True)
		for j in range(1, len(l[i])):
			l[i][j] += l[i][j - 1]
	ans = [0 for i in range(n)]
	for i in range(1, n + 1):
		sumo = 0
		for j in range(p):
			x = len(l[j])
			if i <= x:
				sumo += l[j][x - 1 - x % i]
			else:
				break
		ans[i - 1] = sumo
	print(*ans)


def zip_sorted(a, b):
	(a, b) = zip(*sorted(zip(a, b), reverse=True))
	sorted(zip(a, b), key=lambda x: x[0])
	return (list(a), list(b))

def gcd(a, b):
	return math.gcd(a, b)

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

def ncr(n, r):
	return math.comb(n, r)

def npr(n, r):
	return math.factorial(n) // math.factorial(n - r)

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

def binary_to_decimal(s):
	return int(s, 2)
import sys, os.path
import math
from collections import defaultdict, deque
from itertools import accumulate
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
S = lambda : list(map(str, input1()))

def main():
	(t,) = I()
	for t1 in range(t):
		(n,) = I()
		u = I()
		s = I()
		d = defaultdict(list)
		for i in range(n):
			d[u[i]].append(s[i])
		ans = [0] * n
		for i in d:
			d[i] = sorted(d[i], reverse=True)
			t = 0
			su = [0] * (len(d[i]) + 1)
			for j in range(1, len(d[i]) + 1):
				t += d[i][j - 1]
				su[j] = su[j - 1] + d[i][j - 1]
			for k in range(1, len(d[i]) + 1):
				ans[k - 1] += t
				rem = len(d[i]) % k
				ans[k - 1] -= su[len(d[i])] - su[len(d[i]) - rem]
		print(*ans)
main()


from collections import defaultdict
from itertools import accumulate
for _ in range(int(input())):
	n = int(input())
	u = map(int, input().split())
	s = map(int, input().split())
	d = defaultdict(list)
	for (uu, ss) in zip(u, s):
		d[uu].append(ss)
	for (k, v) in d.items():
		v.sort(reverse=True)
		d[k] = list(accumulate(d[k]))
	unis = range(1, n + 1)
	for k in range(1, n + 1):
		new_unis = []
		o = 0
		for uu in unis:
			if len(d[uu]) >= k:
				o += d[uu][len(d[uu]) // k * k - 1]
				new_unis.append(uu)
		unis = new_unis
		print(o, end=' ')
	print()


from collections import defaultdict
t = int(input())
for i in range(t):
	n = int(input())
	u = [int(x) for x in input().split()]
	s = [int(x) for x in input().split()]
	universities = defaultdict(list)
	for j in range(n):
		universities[u[j]].append(s[j])
	sums = {}
	for (university, students) in universities.items():
		students.sort(reverse=True)
		sums[university] = [0]
		for student in students:
			sums[university].append(sums[university][-1] + student)
		sums[university].reverse()
	region_powers = [0] * (n + 1)
	for (university, students) in universities.items():
		student_count = len(students)
		for k in range(1, student_count + 1):
			left_over_indices = student_count % k
			region_powers[k] += sums[university][left_over_indices]
	for region_power in region_powers[1:]:
		print(region_power, end=' ')
	print()


for t in range(int(input())):
	n = int(input())
	u = tuple(map(int, input().split()))
	s = tuple(map(int, input().split()))
	d = dict()
	for i in range(n):
		if u[i] not in d:
			d[u[i]] = [s[i]]
		else:
			d[u[i]].append(s[i])
	l = [0] * n
	for i in d:
		a = sorted(d[i])[::-1]
		b = []
		c = 0
		for j in range(len(a)):
			c += a[j]
			b.append(c)
		for k in range(1, len(a) + 1):
			m = len(a) - len(a) % k
			l[k - 1] += b[m - 1]
	print(*l)


t = int(input())
for case in range(1, t + 1):
	n = int(input())
	unis = [int(x) for x in input().split(' ')]
	skills = [int(x) for x in input().split(' ')]
	students = {}
	total = 0
	for (u, s) in zip(unis, skills):
		if u in students:
			students[u].append(s)
		else:
			students[u] = [s]
		total += s
	stop = 0
	dp = {}
	for (u, stu) in students.items():
		stop = max(len(stu), stop)
		if len(stu) not in dp:
			dp[len(stu)] = [0] * (len(stu) + 1)
		ps = 0
		for (i, s) in enumerate(sorted(stu)):
			ps += s
			dp[len(stu)][i + 1] += ps
	ans = [0] * n
	for k in range(1, n + 1):
		if k > stop:
			break
		score = total
		for (l, ps) in dp.items():
			mod = l % k
			score -= ps[mod]
		ans[k - 1] = score
	print(*ans)


for t in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]
	store = {}
	for i in range(n):
		if a[i] not in store:
			store[a[i]] = []
		store[a[i]].append(b[i])
	ans = [0 for i in range(n + 1)]
	for i in store:
		store[i] = sorted(store[i])[::-1]
		s = 0
		a = [0]
		for j in store[i]:
			s += j
			a.append(s)
		l = len(a) - 1
		for j in range(1, l + 1):
			x = j * (l // j)
			ans[j] += a[x]
	print(*ans[1:])


for _ in range(int(input())):
	n = int(input())
	uni = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	mapper = [[] for i in range(n)]
	for i in range(n):
		mapper[uni[i] - 1].append(arr[i])
	for i in range(n):
		temp = mapper[i]
		temp.sort(reverse=True)
		mapper[i] = temp
	ans = [0 for i in range(n + 5)]
	for i in range(n):
		cur = mapper[i]
		k = len(cur)
		pre = []
		for j in range(k):
			if j == 0:
				pre.append(cur[j])
			else:
				pre.append(pre[-1] + cur[j])
		for j in range(1, k + 1):
			rem = k % j
			ans[j] += pre[k - rem - 1]
	for i in range(1, n + 1):
		print(ans[i], end=' ')
	print()


t = int(input())
for _ in range(t):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	L = [[] for i in range(n + 1)]
	for (i, j) in zip(u, s):
		L[i].append(j)
	ans = [0] * (n + 1)
	for i in range(n + 1):
		if L[i] == []:
			continue
		L[i].sort(reverse=True)
		cum = [0]
		le = len(L[i])
		for j in L[i]:
			cum.append(cum[-1] + j)
		for j in range(1, n + 1):
			if j > le:
				break
			ans[j] += cum[-1 - le % j]
	print(*ans[1:])


from sys import stdin
from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	u = [int(x) for x in input().split()]
	s = [int(x) for x in input().split()]
	a = list(zip(s, u))
	a.sort(reverse=True)
	p = defaultdict(lambda : [0])
	for (ss, uu) in a:
		p[uu].append(p[uu][-1] + ss)
	d = []
	for v in p.values():
		d.append([])
		k = 1
		while k <= len(v) - 1:
			d[-1].append(v[len(v) - 1 - (len(v) - 1) % k])
			k += 1
	ans = []
	for r in range(len(d)):
		for c in range(len(d[r])):
			if c >= len(ans):
				ans.append(d[r][c])
			else:
				ans[c] += d[r][c]
	for i in range(n - len(ans)):
		ans.append(0)
	print(*ans)


for test in range(int(input())):
	n = int(input())
	us = list(map(int, input().split()))
	s = list(map(int, input().split()))
	x = [[] for i in range(n)]
	ans = [0] * n
	for i in range(n):
		x[us[i] - 1].append(s[i])
	for i in range(n):
		x[i].sort(reverse=True)
		for j in range(1, len(x[i])):
			x[i][j] += x[i][j - 1]
		fig = len(x[i])
		for k in range(1, fig + 1):
			f = fig - fig % k
			ans[k - 1] += x[i][f - 1]
	print(*ans)


from collections import defaultdict
t = int(input())
for case in range(t):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	d = defaultdict(list)
	for i in range(n):
		d[u[i]].append(s[i])
	for key in d:
		d[key].sort()
	for key in d:
		for i in range(1, len(d[key])):
			d[key][i] = d[key][i - 1] + d[key][i]
	ans = [0] * n
	for ele in d:
		for i in range(1, n + 1):
			if len(d[ele]) % i == 0:
				ans[i - 1] += d[ele][-1]
			elif i > len(d[ele]):
				break
			else:
				rem = len(d[ele]) % i
				ans[i - 1] += d[ele][-1] - d[ele][rem - 1]
	print(*ans)


def updateFinal(final, arr):
	total = 0
	li = []
	arr.sort(reverse=True)
	n = len(arr)
	for i in range(n):
		total += arr[i]
		li.append(total)
	for i in range(1, n + 1):
		final[i - 1] += li[i * (n // i) - 1]
	return final
for t in range(int(input())):
	n = int(input())
	u = [int(x) for x in input().split()]
	s = [int(x) for x in input().split()]
	uniMap = {}
	for i in range(n):
		if u[i] in uniMap:
			uniMap[u[i]].append(s[i])
		else:
			uniMap[u[i]] = [s[i]]
	final = [0 for x in range(n)]
	for key in uniMap:
		final = updateFinal(final, uniMap[key])
	print(*final)


from sys import stdin
input = stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	univ = [[] for i in range(n + 1)]
	for i in range(n):
		univ[u[i] - 1].append(s[i])
	for i in range(n):
		univ[i].sort()
	cs = [[0] for i in range(n)]
	arr = [0] * (n + 1)
	for i in range(n):
		cs[i] = [0] + univ[i]
		for j in range(len(univ[i])):
			cs[i][j + 1] += cs[i][j]
		for j in range(1, len(univ[i])):
			arr[j] += cs[i][-1] - cs[i][len(univ[i]) % j]
		arr[len(univ[i])] += cs[i][-1]
	print(*arr[1:])


import sys
input = sys.stdin.readline

def solve():
	n = int(input())
	U = list(map(int, input().split()))
	S = list(map(int, input().split()))
	univ = [[] for _ in range(n + 1)]
	ps = {}
	for i in range(n):
		univ[U[i]].append(S[i])
	for i in range(1, n + 1):
		univ[i].sort()
		m = len(univ[i])
		ps[i] = []
		curr = 0
		for j in range(m):
			curr += univ[i][j]
			ps[i].append(curr)
	ans = [0] * (n + 1)
	for i in range(1, n + 1):
		m = len(univ[i])
		for j in range(1, m + 1):
			r = m % j
			if r == 0:
				ans[j] += ps[i][-1]
			else:
				ans[j] += ps[i][-1] - ps[i][r - 1]
	return ans[1:]
for _ in range(int(input())):
	print(*solve())


import sys
from collections import defaultdict
input = sys.stdin.readline
t = int(input())
for i in range(t):
	d = defaultdict(list)
	n = int(input())
	un = list(map(int, input().split()))
	na = list(map(int, input().split()))
	for j in range(n):
		d[un[j]].append(na[j])
	ans = [0] * (n + 2)
	for (k, l) in d.items():
		l.sort(reverse=True)
		x = len(l) + 1
		pref = [0] * x
		for j in range(len(l)):
			pref[j + 1] = pref[j] + l[j]
		st = 1
		for h in range(x):
			le = 0
			while le + st < x:
				ri = le + st
				ans[st] += pref[ri] - pref[le]
				le += st
			st += 1
	print(*ans[1:n + 1])


import sys
from operator import itemgetter
input = sys.stdin.readline

def main():
	t = int(input())
	for _ in range(t):
		N = int(input())
		U = [int(x) for x in input().split()]
		S = [int(x) for x in input().split()]
		A = [[] for j in range(N + 1)]
		univ_list = set()
		max_students_num = 0
		for i in range(N):
			u = U[i]
			s = S[i]
			A[u].append(s)
			univ_list.add(u)
		ruiseki = [[0] for j in range(N + 1)]
		univ_list = list(univ_list)
		for i in univ_list:
			A[i].sort(reverse=True)
			max_students_num = max(max_students_num, len(A[i]))
			for a in A[i]:
				ruiseki[i].append(ruiseki[i][-1] + a)
		univ_list_num_index = []
		for u in univ_list:
			univ_list_num_index.append((len(A[u]), u))
		univ_list_num_index.sort(key=itemgetter(0), reverse=True)
		ans = []
		for k in range(1, max_students_num + 1):
			tmp = 0
			for (num, u) in univ_list_num_index:
				if num < k:
					break
				amari = (len(ruiseki[u]) - 1) % k
				tmp += ruiseki[u][-1 - amari]
			ans.append(tmp)
		while len(ans) < N:
			ans.append(0)
		print(*ans)
main()


def mapit():
	temp = list(map(int, input().split()))
	return temp

def solution():
	n = int(input())
	u = mapit()
	s = mapit()
	skill = {}
	for (ui, si) in zip(u, s):
		if ui not in skill:
			skill[ui] = [si]
		else:
			skill[ui].append(si)
	for si in skill:
		skill[si].sort(reverse=True)
	sol = [0] * n
	for si in skill:
		l = len(skill[si])
		for i in range(1, l):
			skill[si][i] += skill[si][i - 1]
		for k in range(1, l + 1):
			f = l - l % k
			f -= 1
			sol[k - 1] += skill[si][f]
	print(*sol)
t = int(input())
while t:
	t -= 1
	solution()


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = [0] * n
	d = [[0] for i in range(n)]
	for (i, j) in sorted(zip(map(int, input().split()), map(int, input().split())), key=lambda x: x[1]):
		d[i - 1].append(j)
	for i in range(n):
		for j in range(1, len(d[i])):
			d[i][j] += d[i][j - 1]
		for j in range(1, len(d[i])):
			a[j - 1] += d[i][-1] - d[i][(len(d[i]) - 1) % j]
	print(' '.join(map(str, a)))


from collections import defaultdict
from itertools import accumulate

def solve():
	n = int(input())
	useq = read_ints()
	sseq = read_ints()
	strengths = defaultdict(list)
	for (ui, si) in zip(useq, sseq):
		strengths[ui].append(si)
	for ui in strengths:
		strengths[ui].sort()
	accs = [[0] + list(accumulate(seq)) for seq in strengths.values()]
	res = [0] * n
	for acc in accs:
		cnt = len(acc) - 1
		s = acc[-1]
		for k in range(1, cnt + 1):
			res[k - 1] += s - acc[cnt % k]
	print(*res)

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

def read_ints():
	return [int(c) for c in input().split()]

def print_lines(lst):
	print('\n'.join(map(str, lst)))
from os import environ as env
if 'COMPUTERNAME' in env and 'L2A6HRI' in env['COMPUTERNAME']:
	import sys
	sys.stdout = open('out.txt', 'w')
	sys.stdin = open('in.txt', 'r')
main()


from sys import stdin, stdout
import itertools

def main():
	t = int(stdin.readline())
	for __ in range(t):
		n = int(stdin.readline())
		u = map(int, stdin.readline().split(), itertools.repeat(10, n))
		s = map(int, stdin.readline().split(), itertools.repeat(10, n))
		students = [list() for __ in range(n)]
		for (ui, si) in zip(u, s):
			students[ui - 1].append(si)
		ans = [0] * n
		for skills in filter(None, students):
			skills.sort()
			l = len(skills)
			partsum = [0] * (l + 1)
			for (i, si) in enumerate(skills):
				partsum[i + 1] = partsum[i] + si
			for k in range(1, l + 1):
				ans[k - 1] += partsum[-1] - partsum[l % k]
		stdout.write(' '.join(map(str, ans)))
		stdout.write('\n')
main()


from collections import defaultdict
t = int(input())
while t > 0:
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	a = defaultdict(list)
	for i in range(n):
		a[u[i]].append(s[i])
	for (k, v) in a.items():
		v.sort()
		for i in range(1, len(v)):
			v[i] += v[i - 1]
	sum = [0] * n
	for (k, v) in a.items():
		m = len(v)
		for i in range(1, m + 1):
			if m % i == 0:
				sum[i - 1] += v[m - 1]
			else:
				sum[i - 1] += v[m - 1] - v[m % i - 1]
	print(*sum)
	t -= 1


import sys
input = sys.stdin.buffer.readline
for _ in range(int(input())):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	universities = {}
	for i in range(n):
		if u[i] in universities:
			universities[u[i]].append(s[i])
		else:
			universities[u[i]] = [s[i]]
	for i in universities:
		universities[i].sort(reverse=True)
		psum = [0]
		for j in range(len(universities[i])):
			psum.append(psum[-1] + universities[i][j])
		universities[i] = psum
	for k in range(1, n + 1):
		remove = []
		tot = 0
		for i in universities:
			for j in range(k, len(universities[i]), k):
				tot += universities[i][j] - universities[i][j - k]
			if len(universities[i]) == k:
				remove.append(i)
		for i in remove:
			universities.pop(i)
		print(tot, end=' ')
	print()


for t in range(int(input())):
	n = int(input())
	u = [int(i) - 1 for i in input().split()]
	s = [int(i) for i in input().split()]
	us = [[] for i in range(n)]
	for i in range(n):
		us[u[i]].append(s[i])
	ans = [0] * (n + 1)
	for j in range(n):
		uv = us[j]
		ll = len(uv)
		if ll == 0:
			continue
		uv.sort()
		ps = [0] * (ll + 1)
		for i in range(ll):
			ps[i + 1] = ps[i] + uv[i]
		tot = ps[-1]
		for k in range(1, ll + 1):
			ans[k] += tot - ps[ll % k]
	print(*ans[1:])


t = int(input())
for _ in range(t):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	su = dict()
	for (i, e) in zip(u, s):
		su.setdefault(i, []).append(e)
	ans = [0] * len(u)
	for i in su:
		l = su[i]
		l.sort(reverse=True)
		for j in range(1, len(l)):
			l[j] += l[j - 1]
		for k in range(1, len(l) + 1):
			r = len(l) % k
			ans[k - 1] += l[len(l) - r - 1]
	print(*ans)


for ttt in range(int(input())):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	res = [[] for _ in range(max(u) + 1)]
	for i in range(n):
		res[u[i]].append(s[i])
	for item in res:
		item.sort()
	result = [0] * n
	for it in res:
		sum = [0]
		s = 0
		for i in range(len(it)):
			sum.append(sum[-1] + it[i])
			s += it[i]
		for k in range(1, len(it) + 1):
			result[k - 1] += s - sum[len(it) % k]
	print(*result)


t = int(input())
for _ in range(t):
	n = int(input())
	d = {}
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	for k in range(n):
		if u[k] not in d:
			d[u[k]] = []
		d[u[k]].append(s[k])
	for scores in d.values():
		scores.sort(reverse=True, key=int)
	output = [0] * n
	sum_list = {}
	for key in d.keys():
		scores = d[key]
		value = []
		total = 0
		for score in scores:
			total += score
			value.append(total)
		sum_list[key] = value
	for scores in sum_list.values():
		for k in range(1, len(scores) + 1):
			output[k - 1] += scores[len(scores) // k * k - 1]
	print(' '.join(map(str, output)))


t = int(input())

def solve():
	n = int(input())
	r = range(n)
	u = [int(x) - 1 for x in input().split()]
	s = [int(x) for x in input().split()]
	p = [[] for i in r]
	st = list(set(u))
	for i in r:
		p[u[i]].append(s[i])
	for i in r:
		p[i].sort(reverse=True)
		for j in range(1, len(p[i])):
			p[i][j] += p[i][j - 1]
	ans = [0] * n
	for i in r:
		good = list()
		for elem in st:
			b = len(p[elem]) // (i + 1) * (i + 1)
			if b != 0:
				ans[i] += p[elem][b - 1]
				good.append(elem)
		st = good
	print(*ans)
for tt in range(t):
	solve()


t = int(input())
while t != 0:
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	net = dict()
	for i in range(n):
		if u[i] in net:
			net[u[i]].append(s[i])
		else:
			net[u[i]] = [s[i]]
	for i in net:
		x = net[i]
		x.sort()
		k = len(x)
		l = [0] * k
		l[0] = x[0]
		for j in range(1, k):
			l[j] = l[j - 1] + x[j]
		net[i] = l
	ans = [0] * n
	for i in net:
		x = net[i]
		for k in range(1, len(x) + 1):
			s = 0
			left = len(x) % k
			if left != 0:
				s = x[-1] - x[left - 1]
			else:
				s = x[-1]
			ans[k - 1] += s
	print(*ans)
	t -= 1


import collections
for _ in range(int(input())):
	n = int(input())
	univs = list(map(int, input().split()))
	skill = list(map(int, input().split()))
	d = collections.defaultdict(list)
	for (u, s) in zip(univs, skill):
		d[u].append(s)
	skill = [0] * (n + 1)
	for u in d:
		d[u].sort()
		length = len(d[u])
		pref = [0] * (length + 1)
		for i in range(length):
			pref[i + 1] = pref[i] + d[u][i]
		for i in range(1, length + 1):
			skill[i] += pref[-1] - pref[length % i]
	print(*skill[1:])


import math

def main():
	(t,) = list(map(int, input().split()))
	for _ in range(t):
		(n,) = list(map(int, input().split()))
		u = list(map(int, input().split()))
		s = list(map(int, input().split()))
		u_s = [[] for __ in range(n)]
		pref_u_s = [[] for __ in range(n)]
		for i in range(n):
			u_s[u[i] - 1].append(s[i])
		for i in range(n):
			if len(u_s[i]) > 0:
				u_s[i].sort(key=lambda x: -x)
				pref_u_s[i] = [0] * (len(u_s[i]) + 1)
				pref_u_s[i][1] = u_s[i][0]
				for j in range(2, len(u_s[i]) + 1):
					pref_u_s[i][j] = pref_u_s[i][j - 1] + u_s[i][j - 1]
		power = [0] * n
		for i in range(n):
			l = len(u_s[i])
			for k in range(l):
				power[k] += pref_u_s[i][l - l % (k + 1)]
		for i in range(n):
			print(power[i], end=' ')
		print('')
main()


from sys import stdin, stdout
import math, bisect
from collections import Counter, deque, defaultdict
L = lambda : list(map(int, stdin.readline().strip().split()))
I = lambda : int(stdin.readline().strip())
S = lambda : stdin.readline().strip()
C = lambda : stdin.readline().strip().split()

def pr(a):
	print(' '.join(list(map(str, a))))

def solve():
	n = I()
	u = L()
	s = L()
	d = defaultdict(list)
	for i in range(n):
		d[u[i]].append(s[i])
	ans = [0] * n
	for i in d:
		a = [0] + sorted(d[i])
		x = len(a)
		for j in range(1, x):
			a[j] += a[j - 1]
		for j in range(1, x):
			ans[j - 1] += a[-1] - a[(x - 1) % j]
	print(*ans)
for _ in range(I()):
	solve()


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	u = [int(i) for i in input().split()]
	s = [int(i) for i in input().split()]
	ans = [0] * n
	dic = defaultdict(list)
	for i in range(n):
		dic[u[i]].append(s[i])
	for i in dic:
		arr = dic[i]
		arr.sort(reverse=True)
		l = len(arr)
		pref = []
		for j in range(1, l):
			arr[j] += arr[j - 1]
		for j in range(len(arr)):
			if l % (j + 1) == 0:
				ans[j] += arr[l - 1]
			else:
				ans[j] += arr[l // (j + 1) * (j + 1) - 1]
	ans[0] = sum(s)
	print(*ans)


t = int(input())
for _ in range(t):
	n = int(input())
	uni = list(map(int, input().split()))
	strength = list(map(int, input().split()))
	l2d = dict()
	for i in range(n):
		if uni[i] in l2d.keys():
			l2d[uni[i]].append(strength[i])
		else:
			l2d[uni[i]] = [strength[i]]
	uni = set(uni)
	ans = [0] * n
	for i in uni:
		l2d[i].sort(reverse=True)
		arr = []
		c = 0
		sz = len(l2d[i])
		for j in range(len(l2d[i])):
			c += l2d[i][j]
			arr.append(c)
		for k in range(1, n + 1):
			if k > sz:
				break
			ans[k - 1] += arr[sz - sz % k - 1]
	print(*ans)


import math
from sys import stdin, stdout
import sys
sys.setrecursionlimit(1000000)

def ii():
	return int(stdin.readline())

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

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

def si():
	return stdin.readline()

def prefix(l1):
	for i in range(1, len(l1)):
		l1[i] += l1[i - 1]
for _ in range(ii()):
	n = ii()
	dict1 = {}
	l1 = li()
	l2 = li()
	for i in range(n):
		if l1[i] in dict1:
			dict1[l1[i]].append(l2[i])
		else:
			dict1[l1[i]] = [l2[i]]
	for i in dict1:
		dict1[i].sort(reverse=True)
	for i in dict1:
		prefix(dict1[i])
	ans = [0] * n
	for i in dict1:
		k = len(dict1[i])
		for j in range(1, k + 1):
			ind = k - k % j - 1
			ans[j - 1] += dict1[i][ind]
	print(*ans)


I = lambda : map(int, input().split())
r = range
for t in r(*I()):
	(N,) = I()
	U = (*I(),)
	S = (*I(),)
	A = [0] * N
	X = [[0] for i in r(N)]
	for i in r(N):
		X[U[i] - 1] += (S[i],)
	for i in r(N):
		X[i].sort()
		l = len(X[i])
		for j in r(1, l):
			X[i][j] += X[i][j - 1]
		for j in r(1, l):
			A[j - 1] += X[i][-1] - X[i][(l - 1) % j]
	print(*A)


from collections import defaultdict, deque
from heapq import heappush, heappop

def solve():
	cnt = defaultdict(list)
	n = int(input())
	uni = list(map(int, input().split()))
	skill = list(map(int, input().split()))
	ans = defaultdict(int)
	for (u, s) in zip(uni, skill):
		cnt[u].append(s)
	for i in range(1, n + 1):
		cnt[i].sort()
		for j in range(1, len(cnt[i])):
			cnt[i][j] += cnt[i][j - 1]
		for k in range(1, len(cnt[i]) + 1):
			(take, left) = divmod(len(cnt[i]), k)
			ans[k] += cnt[i][-1]
			if left > 0:
				ans[k] -= cnt[i][left - 1]
	for i in range(1, n + 1):
		print(ans[i], end=' ')
	print()
t = int(input())
while t:
	t -= 1
	solve()


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

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

def numofact(n, m):
	fac = 1
	for i in range(1, n + 1):
		fac = fac * i % m
	return fac

def sod(n):
	s = 0
	while n > 0:
		s += n % 10
		n //= 10
	return s
for xyz in range(0, int(input())):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	mat = [[] for i in range(max(u))]
	su = sum(s)
	for i in range(0, n):
		mat[u[i] - 1].append(s[i])
	for i in range(0, len(mat)):
		mat[i].sort()
	maxl = 0
	for i in range(0, len(mat)):
		maxl = max(maxl, len(mat[i]))
	ans = [0] * n
	for i in range(0, len(mat)):
		cp = [0]
		cs = sum(mat[i])
		for j in range(0, len(mat[i])):
			size = j + 1
			cp.append(cp[-1] + mat[i][j])
			ans[j] += cs - cp[len(mat[i]) % size]
	print(*ans)


t = int(input())
for _ in range(t):
	n = int(input())
	students = list(map(int, input().split()))
	strength = list(map(int, input().split()))
	univs = {}
	for (st, sk) in zip(students, strength):
		if st not in univs:
			univs[st] = [sk]
		else:
			univs[st].append(sk)
	for students in univs:
		univs[students].sort(reverse=True)
	for students in univs:
		l = len(univs[students])
		for i in range(1, l):
			univs[students][i] += univs[students][i - 1]
	ans = [0] * n
	for students in univs:
		l = len(univs[students])
		for i in range(1, l + 1):
			limit = l // i * i
			if limit != 0:
				ans[i - 1] += univs[students][limit - 1]
	print(*ans)


import collections
import math
from heapq import *
for t in range(int(input())):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	k = collections.defaultdict(list)
	for i in range(n):
		heappush(k[u[i]], s[i])
	out = [0 for i in range(n)]
	for i in k:
		c = 0
		temp = [0]
		val = 0
		while k[i]:
			cur = heappop(k[i])
			val += cur
			temp.append(val)
			c += 1
		for j in range(1, min(n + 1, c + 1)):
			diff = 0
			if c % j < len(temp):
				diff = temp[c % j]
			out[j - 1] += val - diff
	print(*out)


import time
import os, sys
from io import BytesIO, IOBase
from types import GeneratorType
from bisect import bisect_left, bisect_right
from collections import defaultdict as dd, deque as dq, Counter as dc
import math, string, heapq as h
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		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 = self.os.read(self._fd, max(self.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 = self.os.read(self._fd, max(self.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:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

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

def getInt():
	return int(input())

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

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

def getStr():
	return input()

def listStr():
	return list(input())

def getMat(n):
	return [getInts() for _ in range(n)]

def getBin():
	return list(map(int, list(input())))

def isInt(s):
	return '0' <= s[0] <= '9'

def ceil_(a, b):
	return a // b + (a % b > 0)
MOD = 10 ** 9 + 7
from random import randint as ri

def solve():
	N = getInt()
	U = getInts()
	S = getInts()
	G = [[] for i in range(N)]
	for i in range(N):
		G[U[i] - 1].append(S[i])
	for i in range(N):
		G[i].sort(reverse=True)
	G.sort(key=len, reverse=True)
	prefs = [[] for i in range(N)]
	for i in range(N):
		curr = 0
		for j in range(len(G[i])):
			curr += G[i][j]
			prefs[i].append(curr)
	ans = [0] * (N + 1)
	for i in range(1, N + 1):
		tmp = 0
		for j in range(len(G)):
			if len(G[j]) < i:
				G = G[:j]
				break
			k = len(G[j]) - len(G[j]) % i
			tmp += prefs[j][k - 1]
		ans[i] += tmp
	print(*ans[1:])
	return
for _ in range(getInt()):
	solve()


for _ in range(int(input())):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	dic = {}
	for i in range(n):
		k = u[i]
		if k in dic:
			dic[k].append(s[i])
		else:
			dic[k] = [s[i]]
	o = set(u)
	ans = [0] * n
	for i in o:
		dic[i].sort()
		dic[i].reverse()
		v = len(dic[i])
		hold = []
		c = 0
		for j in range(len(dic[i])):
			c += dic[i][j]
			hold.append(c)
		for j in range(1, n + 1):
			if j > v:
				break
			else:
				ran = v // j * j
				ans[j - 1] += hold[ran - 1]
	print(*ans)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	U = list(map(int, input().split()))
	S = list(map(int, input().split()))
	B = sorted(list(zip(S, U)), reverse=True)
	A = dict()
	for b in B:
		(v, i) = b
		if A.get(i):
			A[i].append(A[i][-1] + v)
		else:
			A[i] = [v]
	B = [0] * n
	for i in A:
		for k in range(1, len(A[i]) + 1):
			r = len(A[i]) % k
			B[k - 1] += A[i][len(A[i]) - r - 1]
	print(*B)


from collections import defaultdict as dd

def solve(A, B, n):
	ans = [0] * n
	teams = dd(list)
	for i in range(n):
		teams[A[i]].append(B[i])
	for k in teams.keys():
		li = teams[k]
		li.sort(reverse=True)
		pref = [0]
		for it in li:
			pref.append(pref[-1] + it)
		ln = len(li)
		for i in range(1, ln + 1):
			ind = ln - ln % i
			ans[i - 1] += pref[ind]
	print(*ans)
for case in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	ans = solve(A, B, n)


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

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

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

def helper(pool, n):
	res = [0] * n
	for i in range(len(pool)):
		tl = len(pool[i])
		ssums = [0] + sorted(pool[i])
		for j in range(1, len(pool[i]) + 1):
			ssums[j] += ssums[j - 1]
		for j in range(1, n + 1):
			if j > tl:
				break
			res[j - 1] += ssums[-1] - ssums[tl % j]
	return res
t = int(input())
for i in range(t):
	n = int(input())
	pool = [[] for j in range(n)]
	u = list(map(int, sys.stdin.readline().rstrip().split()))
	s = list(map(int, sys.stdin.readline().rstrip().split()))
	for j in range(n):
		pool[u[j] - 1].append(s[j])
	res = helper(pool, n)
	print(' '.join(map(str, res)))


from sys import stdin, stdout
from collections import defaultdict
from itertools import accumulate
import operator
input = stdin.readline
I = int
R = range
listInput = lambda : list(map(int, input().strip().split()))
lineInput = lambda : map(int, input().strip().split())

def main():
	for _ in R(I(input())):
		d = defaultdict(list)
		n = I(input())
		for (i, v) in zip(listInput(), listInput()):
			d[i].append(v)
		for i in d:
			temp = sorted(d[i], reverse=True)
			t = list(accumulate(temp, operator.add))
			d[i] = t
		for i in range(1, n + 1):
			s = 0
			dellist = []
			for (j, elm) in d.items():
				L = len(elm)
				h = L % i
				high = L - h
				if i > L:
					dellist.append(j)
					continue
				s += elm[high - 1] if 0 <= high - 1 < L else 0
			else:
				print(s, end=' ')
				for i in dellist:
					del d[i]
		else:
			print()
main()


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = [0] * n
	d = [[0] for i in range(n)]
	for (i, j) in zip(map(int, input().split()), map(int, input().split())):
		d[i - 1].append(j)
	for i in range(n):
		d[i].sort()
		for j in range(1, len(d[i])):
			d[i][j] += d[i][j - 1]
		for j in range(1, len(d[i])):
			a[j - 1] += d[i][-1] - d[i][(len(d[i]) - 1) % j]
	print(' '.join(map(str, a)))


import sys
input = sys.stdin.readline

def println(val):
	sys.stdout.write(str(val) + '\n')
for _ in range(int(input().strip())):
	N = int(input().strip())
	U = [int(x) for x in input().strip().split()]
	S = [int(x) for x in input().strip().split()]
	varsities = [[] for i in range(N + 1)]
	for (i, u, s) in zip(range(N), U, S):
		varsities[u] += [s]
	ans = [0 for i in range(N)]
	for (i, varsity) in enumerate(varsities):
		if len(varsity) == 0:
			continue
		varsity.sort(reverse=True)
		pref = [varsity[0]]
		for ii in range(1, len(varsity)):
			pref += [pref[-1] + varsity[ii]]
		for ii in range(1, len(varsity) + 1):
			ans[ii - 1] += pref[len(varsity) - len(varsity) % ii - 1]
	println(' '.join((str(a) for a in ans)))


def solve():
	J = int(input())
	for O in range(J):
		F = int(input())
		K = map(int, input().split())
		L = map(int, input().split())
		B = {}
		for (C, M) in zip(K, L):
			if C not in B:
				B[C] = []
			B[C].append(M)
		for A in B.values():
			A.sort(reverse=True)
		for A in B.values():
			for G in range(1, len(A)):
				A[G] += A[G - 1]
		H = [0] * F
		for A in B.values():
			for D in range(1, F + 1):
				I = 0
				E = len(A) // D
				if E == 0:
					break
				if E > 0:
					N = A[E * D - 1]
					I += N
				H[D - 1] += I
		print(' '.join([str(A) for A in H]))
solve()


from functools import reduce
from math import ceil
from sys import stdin

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))
for test in range(int(input())):
	n = int(input())
	univ = list(map(int, input().split()))
	x = list(map(int, input().split()))
	skill = [[] for i in range(n + 1)]
	for i in range(n):
		skill[univ[i]].append(x[i])
	skill.pop(0)
	for i in skill:
		i.sort(reverse=True)
		i.append(len(i))
	ans = [0] * (n + 1)
	for i in range(n):
		ss = set()
		if skill[i][-1]:
			s = sum(skill[i]) - skill[i][-1]
			for x in reversed(range(1, skill[i][-1] + 1)):
				f = factors(x)
				for j in f:
					if j not in ss:
						ans[j] += s
				s -= skill[i][x - 1]
				ss.update(f)
	print(*ans[1:])


import os
import sys
from io import BytesIO, IOBase
import threading
from bisect import bisect_right
from math import gcd, log, ceil
from collections import Counter
from pprint import pprint
from itertools import permutations
from bisect import bisect_right

def main(case):
	n = int(input())
	arr = list(map(int, input().split()))
	brr = list(map(int, input().split()))
	sm = sum(brr)
	ans = [sm] * n
	urr = [[] for i in range(n)]
	for i in range(n):
		urr[arr[i] - 1].append(brr[i])
	pref = [[] for i in range(n)]
	for i in range(n):
		urr[i].sort(reverse=True)
		prf = [0] * (len(urr[i]) + 1)
		for j in range(1, len(urr[i]) + 1):
			prf[j] = prf[j - 1] + urr[i][j - 1]
		pref[i] = prf
	prev = 0
	for i in range(1, n + 1):
		npref = []
		ans[i - 1] -= prev
		for pf in pref:
			sz = len(pf) - 1
			ans[i - 1] = ans[i - 1] - (pf[sz] - pf[sz - sz % i])
			if sz > i:
				npref.append(pf)
			else:
				prev += pf[sz]
		pref = npref
	print(*ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

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


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	d = defaultdict(list)
	typ = [int(x) for x in input().split()]
	arr = [int(x) for x in input().split()]
	for i in range(n):
		d[typ[i]].append(arr[i])
	for key in d.keys():
		d[key].sort(reverse=True)
	ans = [0] * (n + 1)
	for key in d.keys():
		for i in range(1, len(d[key])):
			d[key][i] += d[key][i - 1]
	for key in d.keys():
		LEN = len(d[key])
		for i in range(1, n + 1):
			if i > LEN:
				break
			take = LEN // i * i
			ans[i] += d[key][take - 1]
	print(*ans[1:])


for _ in range(int(input())):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	uni = dict()
	for i in range(n):
		if u[i] not in uni:
			uni[u[i]] = [s[i]]
		else:
			uni[u[i]].append(s[i])
	res = {z: 0 for z in range(1, n + 1)}
	for key in uni:
		uni[key].sort(reverse=True)
		for x in range(1, len(uni[key])):
			uni[key][x] += uni[key][x - 1]
		for k in range(1, n + 1):
			index = len(uni[key]) // k * k - 1
			if index == -1:
				break
			res[k] += uni[key][index]
	print(*res.values())


from math import *
read_int = lambda : int(input())
read_ints = lambda : map(int, input().split())
read_list = lambda : list(map(int, input().split()))
first_is_min = lambda a, b: (b, a) if a > b else (a, b)
t = read_int()
res = ''
for _ in range(t):
	n = read_int()
	unis = read_list()
	skills = read_list()
	unis_split = {}
	for (index, uni) in enumerate(unis):
		unis_split.setdefault(uni - 1, []).append(skills[index])
	for uni in unis_split.values():
		uni.sort(reverse=True)
		for i in range(1, len(uni)):
			uni[i] += uni[i - 1]
	unis_split = [unis_split[k] for k in sorted(unis_split, reverse=True, key=lambda k: len(unis_split[k]))]
	for i in range(1, n + 1):
		ans = 0
		for uni in unis_split:
			md = len(uni) % i
			if md != len(uni):
				ans += uni[-md - 1]
			else:
				unis_split.pop()
				break
		res += f'{ans} '
	res += '\n'
print(res)


import sys
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
t = int(input())
for tests in range(t):
	n = int(input())
	U = list(map(int, input().split()))
	S = list(map(int, input().split()))
	ANS = [0] * n
	D = [[] for i in range(n + 1)]
	for i in range(n):
		(u, s) = (U[i], S[i])
		D[u].append(s)
	for ul in D:
		ul.sort(reverse=True)
		ul.append(0)
		LEN = len(ul) - 1
		for i in range(LEN):
			ul[i] += ul[i - 1]
		for i in range(1, n + 1):
			k = LEN % i
			if k < LEN:
				ANS[i - 1] += ul[LEN - k - 1]
			else:
				break
	sys.stdout.write(' '.join(map(str, ANS)))
	sys.stdout.write('\n')


for _ in range(int(input())):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	un = [[] for _ in range(n)]
	for i in range(n):
		un[u[i] - 1].append(s[i])
	for i in range(n):
		un[i].sort(reverse=True)
	ans = [0 for _ in range(n)]
	for u in range(n):
		l = len(un[u])
		p = [0 for _ in range(l + 1)]
		for i in range(l):
			p[i + 1] = un[u][i] + p[i]
		for k in range(1, l + 1):
			ans[k - 1] += p[l // k * k]
	print(*ans)


from collections import defaultdict as df
for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	C = [[] for i in range(n + 1)]
	D = [[] for i in range(n + 1)]
	E = [0] * (n + 1)
	for i in range(n):
		C[A[i]].append(B[i])
	for i in range(1, n + 1):
		C[i] = sorted(C[i])[::-1]
	for i in range(1, n + 1):
		for j in range(len(C[i])):
			if j == 0:
				D[i].append(C[i][j])
			else:
				D[i].append(D[i][len(D[i]) - 1] + C[i][j])
	for i in range(1, n + 1):
		for j in range(len(C[i])):
			a = len(C[i])
			b = j + 1
			c = a % b
			d = D[i][a - 1 - c]
			E[b] += d
	print(*E[1:], sep=' ')


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

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = int(input())
while t:
	t -= 1
	n = int(input())
	student = [int(x) for x in input().split()]
	skill = [int(x) for x in input().split()]
	university = [[] for x in range(n + 1)]
	for i in range(n):
		university[student[i]].append(skill[i])
	for i in range(n + 1):
		university[i].sort(reverse=True)
	ans = [0 for x in range(n + 1)]
	unew = []
	for i in range(n + 1):
		if len(university[i]) > 0:
			unew.append(university[i])
	for i in range(len(unew)):
		for j in range(1, len(unew[i])):
			unew[i][j] += unew[i][j - 1]
	for j in range(len(unew)):
		for i in range(1, len(unew[j]) + 1):
			r = len(unew[j]) % i
			index = len(unew[j]) - r - 1
			ans[i] += unew[j][index]
	print(*ans[1:])


t = int(input())
for _ in range(t):
	n = int(input())
	students = list(map(int, input().split()))
	strength = list(map(int, input().split()))
	combined = sorted(zip(strength, students), reverse=True)
	univs = [[] for _ in range(max(students))]
	for i in combined:
		if len(univs[i[1] - 1]) == 0:
			univs[i[1] - 1].append(i[0])
		else:
			univs[i[1] - 1].append(univs[i[1] - 1][-1] + i[0])
	ans = [0] * n
	for students in univs:
		l = len(students)
		for i in range(1, l + 1):
			limit = l // i * i
			if limit != 0:
				ans[i - 1] += students[limit - 1]
	print(*ans)


import sys
input = iter(sys.stdin.read().splitlines()).__next__

def solve():
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	university_skills = [[] for _ in range(n)]
	for (skill, uni) in zip(s, u):
		university_skills[uni - 1].append(skill)
	for skills in university_skills:
		skills.sort(reverse=True)
	university_teams = [[0] for _ in range(n)]
	for (skills, team) in zip(university_skills, university_teams):
		for student_skill in skills:
			team.append(team[-1] + student_skill)
	university_teams.sort(key=len, reverse=True)
	strengths = []
	for k in range(1, n + 1):
		strength = 0
		for uni in range(len(university_teams) - 1, -1, -1):
			if len(university_teams[uni]) < k + 1:
				university_teams.pop()
			else:
				num_students = len(university_teams[uni]) - 1
				num_teams = num_students // k
				strength += university_teams[uni][k * num_teams]
		strengths.append(strength)
	return ' '.join(map(str, strengths))
t = int(input())
output = []
for _ in range(t):
	output.append(solve())
print(*output, sep='\n')


def rt(n, r, m):
	dp = [[] for i in range(0, m)]
	for i in range(0, len(n)):
		dp[n[i] - 1].append(r[i])
	q = []
	p1 = [[0 for i in range(0, len(dp[k]))] for k in range(0, m)]
	for i in range(0, len(dp)):
		dp[i].sort(reverse=True)
		for x in range(0, len(dp[i])):
			if x == 0:
				p1[i][x] = dp[i][x]
			else:
				p1[i][x] = p1[i][x - 1] + dp[i][x]
		if len(dp[i]) >= 1:
			q.append([len(dp[i]), i])
	s = 0
	q.sort()
	l1 = 1
	r1 = [0 for i in range(0, m)]
	while s < len(q):
		c1 = 0
		for k in range(s, len(q)):
			[a, b] = q[k]
			if a >= l1:
				z1 = a - a % l1 - 1
				c1 = c1 + p1[b][z1]
			else:
				s = s + 1
		if l1 - 1 < m:
			r1[l1 - 1] = c1
		l1 = l1 + 1
	return r1
s = int(input())
for i in range(0, s):
	s1 = int(input())
	n = list(map(int, input().split()))
	r = list(map(int, input().split()))
	x = rt(n, r, s1)
	for j in x:
		print(j, end=' ')
	print('')


T = int(input())
for t in range(T):
	n = int(input())
	u = [int(x) for x in input().split()]
	s = [int(x) for x in input().split()]
	teams = dict()
	for i in range(n):
		if u[i] not in teams:
			teams[u[i]] = [s[i]]
		else:
			teams[u[i]].append(s[i])
	score = [0] * n
	for team in teams:
		skill = teams[team]
		skill.sort()
		c_skill = [0, skill[0]]
		for i in range(1, len(skill)):
			c_skill.append(skill[i] + c_skill[i])
		Sum = sum(skill)
		for i in range(len(skill)):
			score[i] += Sum - c_skill[len(skill) % (i + 1)]
	for i in score:
		print(str(i), end=' ')
	print()


import itertools
from collections import defaultdict

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

def main():
	t = int(input())
	for __ in range(t):
		n = int(input())
		u = list(readline())
		s = list(readline())
		students = defaultdict(list)
		for (ui, si) in zip(u, s):
			students[ui].append(si)
		total_skills = [list(itertools.accumulate(reversed(sorted(skills)), initial=0)) for skills in students.values()]
		ans = [0] * n
		for tsi in total_skills:
			l = len(tsi) - 1
			for k in range(1, l + 1):
				ans[k - 1] += tsi[l - l % k]
		print(*ans)
main()


import sys
t = int(input())
result = []
for _ in range(t):
	n = int(input())
	arrayOne = list(map(int, input().split()))
	arrayTwo = list(map(int, input().split()))

	def solve():
		hashMap = {num: [] for num in arrayOne}
		for (numOne, numTwo) in zip(arrayOne, arrayTwo):
			hashMap[numOne].append(numTwo)
		for num in hashMap:
			hashMap[num].sort(reverse=True)
		prefixSum = {num: [] for num in arrayOne}
		res = [0 for i in range(n)]
		for num in hashMap:
			currentSum = 0
			for numOne in hashMap[num]:
				currentSum += numOne
				prefixSum[num].append(currentSum)
		for num in hashMap:
			for k in range(1, len(hashMap[num]) + 1):
				remainder = len(hashMap[num]) % k
				res[k - 1] += prefixSum[num][len(hashMap[num]) - remainder - 1]
		return res
	res = solve()
	print(' '.join([str(num) for num in res]))


t = int(input())
for test in range(t):
	n = int(input())
	uni = list(map(int, input().split()))
	skill = list(map(int, input().split()))
	total = sum(skill)
	teams = {}
	for i in range(n):
		if uni[i] - 1 not in teams:
			teams[uni[i] - 1] = []
		teams[uni[i] - 1].append(skill[i])
	partial_sums = {}
	for (k, t) in teams.items():
		t.sort()
		partial_sums[k] = [0]
		run_sum = 0
		for num in t:
			run_sum += num
			partial_sums[k].append(run_sum)
	ans = [0] * n
	for (k, t) in teams.items():
		for x in range(len(t)):
			ans[x] += partial_sums[k][-1] - partial_sums[k][len(t) % (x + 1)]
	for x in ans:
		print(x, end=' ')
	print()


import heapq
for _ in range(int(input())):
	n = int(input())
	(*u,) = map(int, input().split())
	(*skill,) = map(int, input().split())
	unistudents = [[] for _ in range(n)]
	uniprefs = [[0] for _ in range(n)]
	for i in range(n):
		unistudents[u[i] - 1].append(i)
	for i in range(n):
		unistudents[i].sort(key=lambda i: skill[i], reverse=True)
		for stud in unistudents[i]:
			uniprefs[i].append(uniprefs[i][-1] + skill[stud])
	univs = [(len(unistudents[i]), i, uniprefs[i]) for i in range(n)]
	heapq.heapify(univs)
	ans = []
	for i in range(1, n + 1):
		while univs and univs[0][0] < i:
			heapq.heappop(univs)
		s = 0
		for (l, _, pref) in univs:
			s += pref[l // i * i]
		ans.append(s)
	print(' '.join(map(str, ans)))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	c = []
	st = set()
	for i in range(n):
		c.append([s[i], u[i]])
		st.add(u[i])
	u = []
	s = []
	c.sort(reverse=True, key=lambda p: p[0])
	a = []
	for i in range(n + 1):
		a.append([0])
	for i in range(n):
		a[c[i][1]].append(c[i][0] + a[c[i][1]][-1])
	c = []
	sm = []
	for k in range(1, n + 1):
		cnt = 0
		rem = []
		for j in st:
			if k > len(a[j]) - 1:
				rem.append(j)
				continue
			cnt += a[j][len(a[j]) - 1 - (len(a[j]) - 1) % k]
		for j in rem:
			st.remove(j)
		sm.append(cnt)
	print(*sm)


for _ in range(int(input())):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	lst = [[] for i in range(n)]
	for i in range(n):
		lst[u[i] - 1].append(s[i])
	for i in lst:
		i.sort(reverse=True)
	answer = [0 for j in range(n)]
	for i in range(n):
		l = len(lst[i])
		p = [0 for j in range(l + 1)]
		for j in range(1, l + 1):
			p[j] = p[j - 1] + lst[i][j - 1]
		for k in range(1, l + 1):
			answer[k - 1] += p[l // k * k]
	print(*answer)


for _ in range(int(input())):
	n = int(input())
	univ = list(map(int, input().split()))
	skill = list(map(int, input().split()))
	each = [[] for i in range(n + 1)]
	for i in range(n):
		each[univ[i]].append(skill[i])
	for i in range(n + 1):
		each[i].sort(reverse=True)
		add = 0
		ll = len(each[i])
		for j in range(ll):
			add += each[i][j]
			each[i][j] = add
	ans = [0] * (n + 1)
	for i in range(1, n + 1):
		ll = len(each[i])
		for j in range(1, ll + 1):
			ans[j] += each[i][ll // j * j - 1]
	print(*ans[1:])


for t in range(int(input())):
	n = int(input())
	u = list(map(int, input().split()))
	s = list(map(int, input().split()))
	res = [[] for i in range(max(u) + 1)]
	result = [0] * n
	for i in range(n):
		res[u[i]].append(s[i])
	for item in res:
		item.sort()
	for i in res:
		sum = [0]
		s = 0
		for j in range(len(i)):
			sum.append(sum[-1] + i[j])
			s += i[j]
		for k in range(1, len(i) + 1):
			result[k - 1] += s - sum[len(i) % k]
	print(*result)


for _ in range(int(input())):
	n = int(input())
	u = [int(d) for d in input().split()]
	s = [int(d) for d in input().split()]
	d = {}
	l = [0] * (n + 1)
	for i in range(n):
		if u[i] in d:
			d[u[i]].append(s[i])
		else:
			d[u[i]] = [s[i]]
	for i in d:
		a = d[i]
		a.sort()
		a.reverse()
		b = [0]
		c = 0
		for j in range(len(a)):
			c += a[j]
			b.append(c)
		for k in range(1, len(a) + 1):
			if len(a) % k == 0:
				l[k] += c
			else:
				m = len(a) // k * k
				l[k] += b[m]
	print(*[l[i] for i in range(1, n + 1)])


import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
from collections import defaultdict as dd
(t,) = I()
for _ in range(t):
	(n,) = I()
	u = I()
	s = I()
	reg = dd(list)
	for i in range(n):
		reg[u[i]].append(s[i])
	an = [0] * n
	for i in reg:
		cr = sorted(reg[i], reverse=True)
		nn = len(cr)
		suf = [0] * nn
		suf[-1] = cr[-1]
		for j in range(nn - 2, -1, -1):
			suf[j] += suf[j + 1] + cr[j]
		suf += [0]
		for j in range(nn):
			an[j] += suf[0] - suf[nn - nn % (j + 1)]
	print(*an)


mod = pow(10, 9) + 7
MOD = 998244353
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
from bisect import bisect, bisect_left, bisect_right
from math import *
from itertools import *
from collections import *
sml = {chr(ord('a') + i): i for i in range(26)}
cap = {chr(ord('A') + i): i for i in range(26)}

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

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

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

def godprime(n):
	for i in range(2, int(sqrt(n)) + 1):
		if n % i == 0:
			return False

def godpower(b, p):
	res = 1
	while p > 0:
		if p % 2 == 0:
			b = b * b
			p //= 2
		else:
			res *= b
			p -= 1
	return res

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

def get(n):
	d = []
	while n > 0:
		n1 = n % 10
		d.append(n1)
		n //= 10
	return len(d)
for tt in range(get_int()):
	n = get_int()
	u = get_list()
	a = {}
	for i in u:
		if i in a:
			a[i] += 1
		else:
			a[i] = 1
	l = len(set(a))
	s = get_list()
	d1 = {i: [] for i in u}
	for i in range(n):
		c = u[i]
		d1[c].append(s[i])
	for i in d1:
		d1[i].sort(reverse=True)
	d = {i: [] for i in u}
	for i in d1:
		r = 0
		for j in d1[i]:
			r += j
			d[i].append(r)
	mx = max(a.values())
	ans = [0] * (n + 1)
	d = sorted(d.items(), key=lambda x: -len(x[1]))
	for i in range(1, mx + 1):
		k = i
		for j in d:
			l1 = a[j[0]]
			if l1 % k == 0:
				ans[k] += j[1][-1]
			elif k < l1:
				rem = l1 - l1 % k
				ans[k] += j[1][rem - 1]
			else:
				break
	print(*ans[1:])


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	d = {}
	ans = [0 for i in range(n)]
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = [b[i]]
		else:
			d[a[i]].append(b[i])
	for i in d:
		k = len(d[i])
		d[i] = sorted(d[i], reverse=True)
		for j in range(1, k):
			d[i][j] += d[i][j - 1]
		for j in range(1, n + 1):
			m = k // j
			if m != 0:
				ans[j - 1] += d[i][m * j - 1]
			else:
				break
	print(*ans)


def main():
	from array import array
	from itertools import accumulate, chain
	from sys import stdin, stdout
	for _ in range(int(stdin.readline())):
		n = int(stdin.readline())
		universities = [[] for _ in range(n + 1)]
		for (u, s) in zip(map(int, stdin.readline().split()), map(int, stdin.readline().split())):
			universities[u].append(s)
		groups = dict()
		for university in universities:
			university.sort(reverse=True)
			lu = len(university)
			acc = array('q', chain((0,), accumulate(university)))
			if lu in groups.keys():
				groups[lu] = array('q', map(sum, zip(groups[lu], acc)))
			else:
				groups[lu] = acc[:]
		for k in range(1, n + 1):
			ans = 0
			for lu in groups.keys():
				ans += groups[lu][lu // k * k]
			if k in groups.keys():
				del groups[k]
			stdout.write(f'{ans} ')
		stdout.write('\n')
main()


from collections import defaultdict
d = defaultdict(list)
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	ans = [0] * n
	d = defaultdict(list)
	for i in range(n):
		d[a[i]].append(b[i])
	for i in d:
		z = d[i]
		z.sort(reverse=True)
		l = len(z)
		for ii in range(1, len(z)):
			z[ii] += z[ii - 1]
		for ii in range(len(z)):
			if l % (ii + 1) == 0:
				ans[ii] += z[l - 1]
			else:
				ans[ii] += z[l // (ii + 1) * (ii + 1) - 1]
	ans[0] = sum(b)
	print(*ans)


import io
import os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
t = int(input())
for tc in range(t):
	n = int(input())
	unis = [int(z) for z in input().split()]
	students = [int(z) for z in input().split()]
	teams = {}
	for uni in set(unis):
		teams[uni] = []
	for i in range(n):
		teams[unis[i]].append(students[i])
	for uni in teams:
		teams[uni].sort(reverse=True)
	prefsums = {}
	for uni in teams:
		prefsums[uni] = [teams[uni][0]]
		for i in range(1, len(teams[uni])):
			prefsums[uni].append(teams[uni][i] + prefsums[uni][i - 1])
	res = [0 for i in range(n)]
	cnts = {}
	for uni in prefsums:
		r = []
		for k in range(1, len(prefsums[uni]) + 1):
			m = len(prefsums[uni]) - len(prefsums[uni]) % k - 1
			cnt = prefsums[uni][m]
			r.append(cnt)
		cnts[uni] = r
	cnts2 = {}
	for uni in prefsums:
		for i in range(len(cnts[uni])):
			res[i] += cnts[uni][i]
	print(' '.join([str(i) for i in res]))