#include<bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5+5;
vector<long long> us[MAX_N];
long long pres[MAX_N];
long long res[MAX_N];
int u[MAX_N];
int s[MAX_N];
bool cmp(long long &x, long long &y)
{
    return x > y;
}
int main()
{
    int t;
    cin>>t;
    // iostream::sync_with_stdio(false);
    while(t--)
    {
        int n, max_v = -1, pre_len;
        cin>>n;
        for(int i = 1; i <= n; i++)
        {
            cin>>u[i];
            us[i].clear();
            max_v = max(max_v, u[i]);
        }
        for(int i = 1; i <= n; i++) 
        {
            cin>>s[i];
            res[i] = 0;
            us[u[i]].push_back(s[i]);
        }
        for(int i = 1; i <= max_v; i++)
        {
            pre_len = us[i].size();
            sort(us[i].begin(), us[i].end(), cmp);
            for(int j = 0; j < pre_len; j++)
                pres[j+1] = pres[j] + us[i][j];
            for(int k = 1; k <= pre_len; k++)
                res[k] += pres[pre_len/k * k];
        }
        for(int k = 1; k <= n; k++)
            cout<<res[k]<<" ";
        cout<<endl;
    }
    return 0;
}