#include <bits/stdc++.h>

#define int long long
#define pb emplace_back
#define mp make_pair
#define x first
#define y second
#define meh "Wubba Lubba Dub Dub\n"

typedef long double ld;
typedef long long ll;

using namespace std;

mt19937 rnd(143);

const int inf = 1e9;
const int M = 1e9 + 7;
const ld pi = atan2(0, -1);
const ld eps = 1e-6;

bool cmp(vector<int> &a, vector<int> &b){
    return a.size() > b.size();
}

void solve() {
    int n;
    cin >> n;
    vector<int> u(n), s(n);
    int sum = 0;
    for(int &e: u)cin >> e;
    for(int &e: s){
        cin >> e;
        sum += e;
    }
    vector<vector<int>> part(n);
    for(int i = 0; i < n; ++i){
        part[--u[i]].pb(s[i]);
    }
    sort(part.begin(), part.end(), cmp);
    for(auto &arr: part){
        sort(arr.begin(), arr.end());
        for(int i = 1; i < arr.size(); ++i){
            arr[i] += arr[i - 1];
        }
    }
    for(int i = 1; i <= n; ++i){
        while (!part.empty() && part[part.size() - 1].size() < i){
            if(!part[part.size() - 1].empty())sum -= part[part.size() - 1].back();
            part.pop_back();
        }
        int ans = sum;
        for(auto &arr: part){
            int c = arr.size() % i - 1;
            int d = 0;
            if(c >= 0)d = arr[c];
            ans -= d;
        }
        cout << ans << " ";
    }
    //cerr << meh;
}

bool multi = true;

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
    if (multi)cin >> t;
    for (; t; --t) {
        solve();
        cout << endl;
    }
    return 0;
}