#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int u[200005];
vector<ll>G[200005];
ll ans[200005];
ll sum[200005];
int main(){
    int t = 1; scanf("%d", &t);
    while (t--) {
        int n; scanf("%d", &n);
        for (int i = 1; i <= n; ++i) {
            scanf("%d", &u[i]);
            G[i].clear();
            ans[i] = 0;
        }
        ll s;
        for (int i = 1; i <= n; ++i) {
            scanf("%lld", &s);
            G[u[i]].push_back(s);
        }
        for (int i = 1; i <= n; ++i) {
            sort(G[i].begin(), G[i].end());
            reverse(G[i].begin(), G[i].end());
        }
        for (int i = 1; i <= n; ++i) {
            int m = G[i].size();
            for (int j = 0; j < m; ++j) {
                sum[j + 1] = sum[j] + G[i][j];
            }
            for (int j = 1; j <= m; ++j) {
                ans[j] += sum[m - m % j];
            }
        }
        for (int i = 1; i <= n; ++i) {
            printf("%lld ", ans[i]);
        }
        puts("");
    }
    return 0;
}
