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

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	long long T, n, m, M;
	vector<vector<long long>> a;
	vector<long long> u;
	set<long long> z;
	long long tmp;
	vector<long long> res;
	cin >> T;
	while (T--) {
		M = 0;
		a.clear();
		u.clear();
		z.clear();
		res.clear();
		cin >> n;
		a.resize(n + 1);
		res.push_back(0);
		for (int i = 0; i < n; i++) {
			res.push_back(0);
			cin >> tmp;
			u.push_back(tmp);
			z.insert(tmp);
		}
		for (int i = 0; i < n; i++) {
			cin >> tmp;
			a[u[i]].push_back(tmp);
		}
		for (auto i : z) {
			unsigned int k = a[i].size();
			sort(a[i].begin(), a[i].end());
			for (int j = k - 2; j >= 0; j--) {
					a[i][j] = a[i][j + 1] + a[i][j];
			}
			for (int j = 1; j <= k;j++) {		
				res[j]+=a[i][k%j];
			}
		}
		for (unsigned int i = 1; i <= n; i++) {
			if (i > 1) cout << ' ';
			cout << res[i];
		}
		cout << endl;
	}
}