/*input
1
7
1 1 1 2 1 1 7
6 2 3 1 1 1 8
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
*/

#include <bits/stdc++.h>
#define ll long long

using namespace std;

void run() {
	int n, i, j;
	ll s;
	cin >> n;
	vector<vector<ll>> a(n + 1);
	vector<ll> u(n), ret(n + 1, 0);
	for(i=0; i<n; i++) {
		cin >> u[i];
		--u[i];
	}
	for(i=0; i<n; i++) {
		cin >> s;
		a[u[i]].emplace_back(s);
	}
	for(i=0; i<=n; i++) {
		if(!a[i].size()) continue;
		sort(a[i].begin(), a[i].end());
		ll tot = accumulate(a[i].begin(), a[i].end(), 0ll);
		ll m = a[i].size();
		for(j=1; j<m; j++)
			a[i][j] += a[i][j - 1];
		for(ll team=1; team<=m; team++) {
			ret[team] += tot;
			if(m % team)
				ret[team] -= a[i][(m % team) - 1];
		}
	}
	for(i=1; i<=n; i++) {
		cout << ret[i] << " ";
	}
	cout << endl;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(10);
	int t = 1;
	cin >> t;
	while(t--)
		run();
}
