#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <cmath>
#include <ctime>
#include <string>

using namespace std;

const int A = 200005;
int u[A], s[A];
int main() {
    int t;
    scanf("%d", &t);
    while(t--) {
        int n;
        scanf("%d", &n);
        map<int, vector<long long>> sum;
        for(auto i=0;i<n;i++) {
            scanf("%d", u+i);
        }
        long long total = 0;
        for(auto i=0;i<n;i++) {
            scanf("%d", s+i);
            total += s[i];
            if(sum[u[i]].size() == 0) {
                sum[u[i]].push_back(0);
            }
            sum[u[i]].push_back(s[i]);
        }
        for(auto it=sum.begin();it!=sum.end();) {
            if (it->second.size() == 0) {
                it = sum.erase(it);
                continue;
            }
            sort(it->second.begin(), it->second.end());
            for(auto j=1;j<it->second.size();j++) {
                it->second[j] += it->second[j-1];
            }   
            it++;
        }
        //printf("test");
        for(auto k=1;k<=n;k++) {
            if (k>1) {
                printf(" ");
            }
            if (k==1) {
                printf("%lld", total);
                continue;
            }
            if (sum.size()==0) {
                printf("0");
                continue;
            }
            long long x = 0;
            for(auto it=sum.begin(); it != sum.end();) {
                if (it->second.size() <= k) {
                    total -= it->second[it->second.size()-1];
                    it = sum.erase(it);
                    continue;
                }
                
                auto cnt = it->second.size() - 1;
                cnt %= k;
                x -= it->second[cnt];
                it++;
            }
            
            //cout<<"total: " << total<<endl;
            x += total;
            if (sum.size()==0) {
                x = 0;
            }
            printf("%lld", x);
        }
        printf("\n");
    }
    return 0;
}