	// author: sharad12arse
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ull unsigned long long
#define gcd __gcd
#define INF LONG_LONG_MAX

const ll mxN=2e5+1;
vector<ll> adj[mxN];

void solve(){
	ll n;
	cin>>n;
//	cout<<"YES\n";
	set<ll> a;
	ll sum=0;
	vector<ll> ax(n),ay(n);
	for(int i=0;i<n;i++){
		cin>>ax[i];
		a.insert(ax[i]);
	}
	for(int i=0;i<n;i++){
		cin>>ay[i];
		sum+=ay[i];
		adj[ax[i]].push_back(ay[i]);
	}
	set<ll> :: iterator itr;
	ll num;
	for(itr=a.begin();itr!=a.end();itr++){
		num=*itr;
		sort(adj[num].begin(),adj[num].end());
		for(int i=1;i<adj[num].size();i++){
			adj[num][i]+=adj[num][i-1];
		}
	}
//	cout<<"YES\n";
	ll k=1;
	ll cnt=0ll;
	ll d;
	vector<ll> ans(n,0ll);
	ll zz;
	vector<ll> az;
	while(a.size()){
		zz=sum;
		for(itr=a.begin();itr!=a.end();itr++){
			num=*itr;
			d=(adj[num].size()%k)-1;
			if(d!=-1){
				zz-=adj[num][d];
			}
			if(adj[num].size()==k){
				sum-=adj[num][k-1];
				adj[num].clear();
				az.push_back(num);
			}
		}
		if(az.size()){
			for(int i=0;i<az.size();i++){
				a.erase(az[i]);
			}
			az.clear();
		}
		ans[k-1]=zz;
		k++;
	}
	for(int i=0;i<n;i++){
		cout<<ans[i]<<" ";
	}
	cout<<"\n";
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t=1;
	cin>>t;
	while(t--){
		solve();
	}
}
