#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, u[200005], s[200005];
int ans[200005];
bool vis[200005];
bool cmp(int a, int b) {
	return a > b;
}
signed main() {
	int t;
	cin >> t;
	while(t--) {
		scanf("%lld", &n);
		vector<int> v[200005];
		for(int i = 1; i <= n; i++) ans[i] = 0;
		ans[0] = 0;
		bool b[200005];
		vector<int> uni;
		for(int i = 1; i <= n; i++) b[i] = 0;
		for(int i = 1; i <= n; i++) {scanf("%d", &u[i]); b[u[i]] = 1;}
		for(int i = 1; i <= n; i++) scanf("%d", &s[i]);
		for(int i = 1; i <= n; i++) {
			v[u[i]].push_back(s[i]);
		}
		for(int i = 1; i <= n; i++) {
			if(b[i]) uni.push_back(i);
		}
		for(int i = 1; i <= n; i++) {
			sort(v[i].begin(), v[i].end(), cmp);
		}
		for(int i = 0; i < uni.size(); i++) {//只需要遍历出现的大学
			long long sum = 0;
			vector<long long> pre;//前缀和vector
			pre.push_back(0);
			for(int j = 0; j < v[uni[i]].size(); j++) {
				sum += 1ll * v[uni[i]][j];
				pre.push_back(sum);
			}
			for(int k = 1; k <= n; k++) {//遍历可能的分组计算贡献
				int pos = v[uni[i]].size() / k * k;
				if(pos == 0) break;//注意及时break 这样能保证运行时间不爆炸
				if(pos < pre.size()) ans[k] += pre[pos];
			}
		}
 
		for(int i = 1; i <= n; i++) cout << ans[i] << " ";
		cout << endl;
	}
	return 0;
}