#include <bits/stdc++.h>
using namespace std;
#define fast() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define int long long
#define pb push_back
#define vi vector<int>
#define size(x) (int)((x).size())
#define memf(a) memset(a,false,sizeof(a))
#define mem(val, var) memset(var,val,sizeof(var))
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define rep(i,a,b) for(int i=a;i<b;i++)
#define fr(i, n) for(int i=0;i<n;i++)
#define tr(v) for(auto it=v.begin();it!=v.end();it = next(it))
const int inf = 1e16;
const int N = 1e5 + 1;
const int mod = 1e9 + 7;

void solve() {
	int n; cin >> n;
	vector<vector<int> > s(n + 1);
	int u[n];
	int x[n];

	fr(i, n) cin >> u[i];

	fr(i, n) cin >> x[i];

	fr(i, n)
	{
		s[u[i]].pb(x[i]);
	}

	for (int i = 1; i <= n; i++)
	{
		sort(all(s[i]));
		reverse(all(s[i]));
	}

	vector<int> ans(n + 1, 0);
	for (int i = 1; i <= n; i++)
	{
		int presum[n + 1];
		presum[0] = 0;

		for (int j = 1; j <= size(s[i]); j++)
		{
			presum[j] = presum[j - 1] + s[i][j - 1];
		}

		for (int j = 1; j <= size(s[i]); j++)
		{
			ans[j] += presum[(size(s[i]) / j) * j];
		}
	}

	for (int i = 1; i <= n; i++)
		cout << ans[i] << " ";
	cout << "\n";
}

signed main() {
	fast();
	int t = 1; cin >> t;
	while (t--) solve();
	return 0;
}