#include<bits/stdc++.h>
using namespace std;
#define int long long int

int32_t main() {

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		vector<int> v[n + 1];
		vector<int> temp(n + 1);

		for (int i = 1; i <= n; i++) {
			cin >> temp[i];
		}
		v[0].push_back(0);
		for (int i = 1; i <= n; i++) {
			int x;
			cin >> x;
			v[temp[i]].push_back(x);
		}
		for (int i = 1; i <= n; i++) {
			sort(v[i].begin(), v[i].end(), greater<int>());
		}

		vector<int> ans(n + 1, 0);


		for (int k = 1; k <= n; k++) {
			for (int i = 1; i < v[k].size(); i++) {
				v[k][i] += v[k][i - 1];
			}
			for (int i = 1; i <= v[k].size(); i++) {
				int r = v[k].size() % i;
				int ct = v[k].size() - r - 1;
				ans[i] += v[k][ct];
			}
		}

		for (int i = 1; i <= n; i++) cout << ans[i] << " ";

		cout << "\n";
	}
}


