#include<bits/stdc++.h>
using namespace std;
int main()
{
    // #ifndef ONLINE_JUDGE
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // #endif

    long long t;
    cin>>t;
    while(t--)
    {
        long long n;
        cin>>n;
        vector<long long> temp1;
        vector<long long> temp2;
        vector<long long> temp3;
        vector<long long> students(n, 0);
        vector<long long> universities(n, 0);
        vector<vector<long long>> universities_students(n+1, temp1);
        vector<vector<long long>> pref_sum(n+1, temp2);
        vector<vector<long long>> pref_sum_percentage_k(n+1, temp3);

        for (size_t i = 0; i < n; i++)
        {
            cin>>universities[i];
        }

        for (size_t i = 0; i < n; i++)
        {
            cin>>students[i];
        }

        for (size_t i = 0; i < n; i++)
        {
            universities_students[universities[i]].push_back(students[i]);
        }

        for (size_t i = 0; i <= n; i++)
        {
            sort(universities_students[i].begin(), universities_students[i].end());
            long long sum = 0;
            for(long long stud : universities_students[i])
            {
                sum += stud;
                pref_sum[i].push_back(sum);
            }
        }

        // cout<<"UNIVERSITIES:\n";
        // long long i = 0;
        // for(auto univ : universities_students)
        // {
        //     cout<<i++<<" : ";
        //     for(auto stud : univ)
        //     {
        //         cout<<stud<<" -> ";
        //     }
        //     cout<<endl;
        // }

        // cout<<"\nPREFSUMS:\n";
        // i = 0;
        // for(auto univ : pref_sum)
        // {
        //     cout<<i++<<" : ";
        //     for(auto stud : univ)
        //     {
        //         cout<<stud<<" -> ";
        //     }
        //     cout<<endl;
        // }

        vector<long long> answer(n+1, 0);
        for(long long i = 0; i < n+1; i++)
        {
            long long size_of_univ = universities_students[i].size();
            for (size_t k = 1; k <= size_of_univ; k++)
            {
                if(size_of_univ % k == 0)
                {
                    answer[k] += pref_sum[i][size_of_univ-1];
                }
                else
                {
                    long long removed = size_of_univ % k;
                    answer[k] += pref_sum[i][size_of_univ-1] - pref_sum[i][removed - 1];
                }
            }
            
        }

        for (size_t i = 0; i < n; i++)
        {
            cout<<answer[i+1]<<" ";
        }
        cout<<endl;
        
    }
    return 0;
}