#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <vector>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stack>
#include <utility>
#include <queue>
#include <set>
#include <iomanip>
#include <sstream>

using namespace std;

typedef int64_t ll;
typedef long double ld;
typedef pair<int,int> pii;

#define INF 1000000000000
#define MOD 1000000007
#define loop(i,a,b) for(int i = a; i < b; ++i)

const int maxN = 100005;

void solve(){
    int n; cin>>n;
    vector<int> unis[n];
    int alumnos[n], skill;
    
    loop(i, 0, n) cin>>alumnos[i];
    loop(i, 0, n) {
        cin>>skill;
        unis[alumnos[i]-1].push_back(skill);
    }

    ll totalSkill[n];
    ll precalculo[n], total;
    int size, mod;
    fill(totalSkill, totalSkill + n, 0);
    //por cada uni, 
    loop(i, 0, n){
        size = unis[i].size();
        if(size == 0) continue;
        sort(unis[i].begin(), unis[i].end());
        precalculo[0] = (ll)unis[i][0];
        loop(j, 1, size) precalculo[j] = precalculo[j-1] + ((ll)unis[i][j]);
        total = precalculo[size-1];
        loop(k, 1, size+1){
            mod = (size % k) - 1;
            totalSkill[k-1] += total;
            if (mod >= 0) totalSkill[k-1] -= precalculo[mod];
        }
    }
    cout<<totalSkill[0];
    loop(i, 1, n){
        cout<<" "<<totalSkill[i];
    }
    cout<<"\n";
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    //if(fopen("case.txt", "r")) freopen("case.txt", "r", stdin);


    int T; cin>>T;
    loop(t, 0, T){
        solve();
        
    }
    
    return 0; 
    
    
}
