#include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<cmath>
#include<queue>
#include<algorithm>
#define maxn 201000
#define ll long long
using namespace std;
vector<int>f[maxn];
ll u[maxn], s[maxn], qian[maxn];
ll ans[maxn], sum[maxn];
int main() {
	int T;
	cin >> T;
	while (T--) {
		int n;
		cin >> n;
		for (int i = 1;i <= n;i++) {
			cin >> u[i];
		}
		for (int i = 1;i <= n;i++) {
			cin >> s[i];
		}
		ll cnt = 0;
		for (int i = 1;i <= n;i++) {
			cnt = max(cnt, u[i]);
			f[u[i]].push_back(s[i]);
			sum[u[i]] += s[i];
		}
		
		for (int i = 1;i <= cnt;i++) {
			sort(f[i].begin(), f[i].end());
			for (int j = 0;j < f[i].size();j++) {
				if (j == 0)qian[j] = f[i][j];
				else qian[j] = qian[j - 1] + f[i][j];
			}
			for (int k = 1;k <= n;k++) {
				if (f[i].size() < k)break;
				int r = f[i].size() % k;
				ans[k] +=(sum[i] - qian[r - 1]);
			}
		}
		for (int i = 1;i <= n;i++)cout << ans[i] << " ";
		cout << endl;
		for (int i = 1;i <= n;i++) {
			f[i].clear();
			sum[i] = 0;
			ans[i] = 0;
		}
	}
	return 0;
}