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

#define For(i,n) for(int i = 0; i < n; i++)
#define For1(i,n) for(int i = 1; i < n; i++)
#define Forn(i,n) for(int i = n-1; i >=0 ; i--)
#define FOR(i,f,n) for(int i = f; i < n; i++)
#define FORN(i,n,f) for(int i = n; i >= f; i--)
#define Forit(it,v) for(auto it = v.begin(); it != v.end(); it++)
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define sz size()
#define dbg(a) For(i,a.sz){cout<<a[i]<<' ';}cout<<'\n';
#define Sort(a) sort(a.rbegin(), a.rend());
#define Rev(a) reverse(a.begin(), a.end());
#define All(a) a.begin,a.end();
typedef long long int ll;
typedef pair<ll, ll> pl;
typedef pair<int,int> pi;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pl> vpl;
typedef vector<vll> vvll;
const int mod = 1e9+7;
const int INF = 1e9;
ll t,m,n,p,X,idx,ct1,ct0;
const int N = 200000+10;

vll r[N];
void solve() {
    cin >> n;
    vi val[n], v(n), s(n);
    for (int i = 0; i < n; ++i) cin >> v[i], --v[i];
    for (int i = 0; i < n; ++i) cin >> s[i];
    for (int i = 0; i < n; ++i) val[v[i]].pb(s[i]);
    vector <long long> ans(n + 1, 0);
    for (int i = 0; i < n; ++i) {
        Sort(val[i]);
        vector <long long> pre = {0};
        for (int j : val[i]) pre.pb(pre.back() + j);
        int m = val[i].size();
        for (int k = 1; k <= m; ++k) {
            ans[k] += pre[(m / k) * k];
        }
    }
    for (int i = 1; i <= n; ++i) cout << ans[i] << " \n"[i == n];

}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    cin >> t;
    //    k = t;
    //    t = 1;
    while (t--) {
        solve();
    }
}
