#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair <int, int>
#define pll pair <ll, ll>
#define pci pair <char, int>
#define pld pair <ld, ld>
#define ppld pair <pld, pld>
#define ppll pair <pll, pll>
#define pldl pair <ld, ll>
#define vll vector <ll>
#define vvll vector <vll>
#define vpll vector <pll>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define mll map <ll, ll>
#define fastmap gp_hash_table
#define cd complex <double>
#define vcd vector <cd>
#define PI 3.14159265358979
#pragma 03
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
vll schools[200005];
ll sk[200005], un[200005];
ll ans[200005];
void solve(){
	int n; cin >> n;
	for (int i = 0; i < n; i++){
		schools[i].clear(); ans[i + 1] = 0;
	}
	for (int i = 0; i < n; i++) cin >> un[i];
	for (int i = 0; i < n; i++) cin >> sk[i];
	for (int i = 0; i < n; i++) schools[un[i] - 1].eb(sk[i]);
	for (int i = 0; i < n; i++){
		sort(schools[i].begin(), schools[i].end());
		reverse(schools[i].begin(), schools[i].end());
	}
	for (int i = 0; i < n; i++){
		for (int j = 1; j < (int) schools[i].size(); j++) schools[i][j] += schools[i][j - 1];
	}
	for (int i = 0; i < n; i++){
		for (int j = 1; j <= (int) schools[i].size(); j++){
			int selected = (int) schools[i].size() / j * j - 1;
			ans[j] += schools[i][selected];
		}
	}
	for (int i = 1; i <= n; i++) cout << ans[i] << " ";
	cout << "\n";
}
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int t; cin >> t;
	while (t--) solve();
}