#include<bits/stdc++.h>
#define rep(i,a,b) for(int i = (a);i < (b); ++i)
#define per(i,a,b) for(int i = (b) - 1; i >= (a); --i)
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define lowbit(x) ((x)&(-x))
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
const int maxn = 2e6 + 10;
const int mod = 998244353;
ll f[maxn];
ll a[maxn];
vector<ll> vs[maxn];
int cont[maxn];
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int t;
	cin >> t;
	while(t--){
		int n;
		cin >> n;
		rep(i,1,n+1)vs[i].clear();
		rep(i,1,n+1)cin >> f[i];
		rep(i,1,n+1)cin >> a[i];
		rep(i,1,n+1)cont[i] = i;
		rep(i,1,n+1)vs[f[i]].pb(a[i]);
		rep(i,1,n+1)sort(all(vs[i]),[&](int a,int b){return a > b;});
		rep(i,1,n+1){
			int len = sz(vs[i]);
			rep(j,0,len - 1)vs[i][j+1] += vs[i][j];
		}
		sort(cont + 1,cont + n + 1,[&](int a,int b){return sz(vs[a]) > sz(vs[b]);});
		int q = n;
		rep(i,1,n+1){
			while(sz(vs[cont[q]]) < i && q) --q;
			ll ans = 0;
			rep(j,1,q+1){
				int p = sz(vs[cont[j]]) - sz(vs[cont[j]])%i;
				//cout << cont[j] << '\n';
				ans += vs[cont[j]][p - 1];
			}
			cout << ans << ' ';
		}
		cout << '\n';
	}
	return 0;
}
