#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pb push_back
#define forn(i, n) for(int i = 1; i <= (int)(n); ++i)
#define f first
#define s second
using namespace std;
const int N = 2e5 + 10;
int ar[N], s[N], cnt[N];
vector <ll> v[N], pp;
vector <pair <int, int> > buf, buf2;

void cler(int n){
    pp.clear(); buf.clear(); buf2.clear();
    forn(i, n){
        cnt[i] = 0;
        v[i].clear();
    }
}

void solve(){
    int n; cin >> n;

    cler(n);

    forn(i, n){
        cin >> ar[i]; cnt[ar[i]]++;
    }
    forn(i, n)
        cin >> s[i];

    forn(i, n){
        if(cnt[i] > 0) buf.pb(mp(cnt[i], i));
        buf2.pb(mp(s[i], ar[i]));
    }
    sort(buf2.begin(), buf2.end());

    for(auto to: buf2){
        if(v[to.s].size() == 0) v[to.s].pb(to.f);
        else v[to.s].pb(v[to.s].back() + to.f);
    }

    sort(buf.begin(), buf.end());
    for(int i = buf.size() - 1; i >= 0; --i){
        pp.push_back(buf[i].second);
    }

    forn(i, n){
        ll sum = 0;
        for(auto to: pp){
            if(v[to].size() < i) break;
            int x = v[to].size() % i - 1;
            if(x < 0)
                sum += v[to].back();
            else
                sum += v[to].back() - v[to][x];
        }
        cout << sum << " ";
    }
    cout << "\n";
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int t; cin >> t;
    while(t--){
        solve();
    }
}
