#include<bits/stdc++.h>
#include <iterator>
#include <iostream>
#include <numeric>
#include <math.h>
#define ll long long
#define ull long
#define mpa make_pair
#define pb push_back
#define ff first
#define pii pair<ll,ll>
#define dd double
#define trace(x) cerr << #x << " : " << x << endl
#define ss second
#define boost ios_base::sync_with_stdio(0)
#define forp(i,a,b) for(ll i=a;i<=b;i++)
#define rep(i,n)    for(ll i=0;i<n;++i)
#define ren(i,n)    for(ll i=n-1;i>=0;i--)
#define forn(i,a,b) for(ll i=a;i>=b;i--)
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end();
#define sc(x) scanf("%lld",&x)
#define clr(x,val) memset(x,val,sizeof(x))
#define pr(x) prllf("%lld\n",x) 
#define pdd pair<dd,dd>
#define read_arr(a,n) for(ll i=1;i<=n;i++)cin>>a[i];
#define init_arr(a,n) for(ll i=1;i<=n;i++)a[i]=n-i+1;
#define prec(x) cout<<fixed<<setprecision(x)
#define fre freopen("input.txt","r",stdin),freopen("output.txt","w",stdout)
#define arr array 
using namespace std;
ll n;
vector<ll> adj[200005];
ll csum[200005];
ll e;
ll uni[200005];
void solve(ll tc){
	ll n;
	cin>>n;
	for(ll i=1;i<=n;i++)csum[i]=0,adj[i].clear();
	for(ll i=1;i<=n;i++){
		cin>>uni[i];
	}

	for(ll i=1;i<=n;i++){
		cin>>e;
		adj[uni[i]].pb(e);
	}

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

	for(ll i=1;i<=n;i++){
		
	
		for(ll k=1;k<adj[i].size();k++){
			adj[i][k]+=adj[i][k-1];
		}

	
		ll hi=adj[i].size();
		for(ll k=1;k<=hi;k++){
			ll v=hi/k;
			ll las_ind=v*k;
			las_ind--;
			if(las_ind<0)continue;
			csum[k]+=adj[i][las_ind];
		}

	}

	for(ll i=1;i<=n;i++)cout<<csum[i]<<" ";
	cout<<endl;


	
}
int main(){

	boost;

	ll t=1;
	cin>>t;

	
	ll tc=1;

	while(t--){
		solve(tc);
		tc++;
	}
	

}
	
	
 
 
 
 


	

	







	
	

	
	




		


    
	


		