///*.......................Shadman Sakib Utshob............................*///

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

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;

#define Boost           ios_base::sync_with_stdio(0);cin.tie(0),cout.tie(0)
#define see(x)        cerr<< __LINE__ << ": " << (#x) << " is "<< (x) <<'\n';
#define pb              push_back
#define inf             1e19
#define rep(i,n)        for(int i=0;i<n;i++)
#define rep1(i,n)       for(int i=1;i<=n;i++)
#define ll              long long
#define MOD             1000000007
#define mem(x,i)        memset(x,i,sizeof x)
#define Ones(x)         __builtin_popcount(x);
#define PI              acos(-1.0)
#define ff              first
#define ss              second
#define T               int t;cin>>t;while(t--)
#define ok              cout<<"ok"<<endl;
#define all(x)          (x).begin(),(x).end()
#define ordered_set     tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define nln	            cout<<endl;
#define yes	      		cout<<"YES"<<endl;
#define no 	      		cout<<"NO"<<endl;
#define maxn 300005

int dx[]= {-1,0,1,0}; //up,right,down,left
int dy[]= {0,1,0,-1};

int main()
{
    Boost;

    T
    {
        ll n,i,j,k,x,sum=0,mx=-1;
        cin>>n;
        ll u[n+2],s[n+2],ans[n+2];
        mem(ans,0);
        vector<ll>pre[n+2],v[n+2],kaj;

        rep(i,n)
        {
            cin>>u[i];
        }
        rep(i,n)
        {
            cin>>s[i];
            v[u[i]].pb(s[i]);
        }
        for(i=1; i<=n; i++)
        {
            if(v[i].size())
                kaj.pb(i);
        }
        for(i=0; i<kaj.size(); i++)
        {
            sort(v[kaj[i]].begin(),v[kaj[i]].end(),greater<ll>());

        }

        for(i=0; i<kaj.size(); i++)
        {
            for(j=0; j<v[kaj[i]].size(); j++)
            {
                if(j==0)
                {
                    pre[kaj[i]].pb(v[kaj[i]][0]);
                }
                else
                {
                    pre[kaj[i]].pb(pre[kaj[i]][j-1]+v[kaj[i]][j]);
                }
            }
        }

        for(i=0; i<kaj.size(); i++)
        {
            x=kaj[i];//index
            for(j=1; j<=v[x].size(); j++)
            {
                ll len=v[x].size();
                if(len%j==0)
                {
                    ans[j]+=pre[x][pre[x].size()-1];
                }
                else
                {
                    ll r=len%j;
                    ll y = len - r - 1;
                    ans[j]+=pre[x][y];
                }
            }
        }

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

    }




}
/*
1
5
1 1 1 1 1
4 4 4 4 4

*/
