//Jai Shree Ram
#include<bits/stdc++.h>
using namespace std;
#define   f(a,b,n)        for(ll a=b;a<n;a++)
#define   readarr(arr,n)  f(i,0,n) cin>>arr[i];
#define   ll              long long
#define   pb              push_back
#define   fastio            ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define   all(x)          x.begin(), x.end()
#define   mod             1000000007 
#define   w(t)            int t; cin>>t; while(t--) 
#define   fi              first
#define   sd              second                                                                                                                                                                                                                           
#define   sp(x,y)         fixed<<setprecision(y)<<x              
#define   trace(x) 		  cout<<#x<<": "<<x<<endl; 
#define   trav(a, x)      for(auto& a : x) 
#define   setbits(x)      __builtin_popcountll(x)  
#define   zrobits(x)      __builtin_ctzll(x)  
#define   pqmax           priority_queue<ll>
#define   pqmin           priority_queue<ll,vi,greater<ll>>                         
#define   vi              vector<ll>
#define   mpi              map<ll,ll> 
#define   pi              pair<ll,ll>  
#define   N               1e5   
#define   inf             1e18 
#define   nline           "\n"
void c_a_c()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif
}
int main()
{
	c_a_c();
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
    	vector<long long> v[n+1];
    	long long array[n];
    	for(long long i = 0; i < n; i++)	{
    		cin >> array[i];
    	}
    	long long x;
    	for(long long i = 0; i < n; i++)	{
    		cin >> x;
    		v[array[i]].push_back(x);
    	}
    	for(long long i = 1; i <= n; i++)	{
    		sort(all(v[i]));
    		reverse(all(v[i]));
    	}
    	long long ans[n+1];
    	for(long long i = 1; i <= n; i++)	{
    		ans[i] = 0;
    	}
    	for(long long i  = 1; i <= n; i++)	{
    		for(long long j  = 1; j < v[i].size(); j++)	{
    			v[i][j] += v[i][j-1];
    		}
    		for(long long j = 1; j <= v[i].size(); j++)	{
    			long long temp = v[i].size() % j;
    			ans[j] += v[i][v[i].size() - temp-1];
    		}
    	}
    	for(long long i = 1; i <= n; i++)	{
    		cout << ans[i] << " ";
    	}
    	cout << "\n";
    }
}


