```go
package main
import (
"bufio"
"bytes"
"os"
"sort"
"strconv"
)
type FastScanner struct {
r *bufio.Reader
}
func NewFastScanner() *FastScanner {
return &FastScanner{r: bufio.NewReaderSize(os.Stdin, 1<<20)}
}
func (fs *FastScanner) nextInt() int {
sign := 1
val := 0
c, _ := fs.r.ReadByte()
for (c < '0' || c > '9') && c != '-' {
c, _ = fs.r.ReadByte()
}
if c == '-' {
sign = -1
c, _ = fs.r.ReadByte()
}
for c >= '0' && c <= '9' {
val = val*10 + int(c-'0')
c, _ = fs.r.ReadByte()
}
return sign * val
}
func main() {
in := NewFastScanner()
out := bufio.NewWriterSize(os.Stdout, 1<<20)
defer out.Flush()
t := in.nextInt()
for ; t > 0; t-- {
n := in.nextInt()
unis := make([][]int64, n+1)
u := make([]int, n)
for i := 0; i < n; i++ {
u[i] = in.nextInt()
}
for i := 0; i < n; i++ {
s := int64(in.nextInt())
unis[u[i]] = append(unis[u[i]], s)
}
sumBySize := make([][]int64, n+1)
presentSizes := make([]int, 0)
for id := 1; id <= n; id++ {
arr := unis[id]
if len(arr) == 0 {
continue
}
sort.Slice(arr, func(i, j int) bool { return arr[i] > arr[j] })
m := len(arr)
if sumBySize[m] == nil {
sumBySize[m] = make([]int64, m+1)
presentSizes = append(presentSizes, m)
}
sum := int64(0)
for j := 1; j <= m; j++ {
sum += arr[j-1]
sumBySize[m][j] += sum
}
}
ans := make([]int64, n+1)
for _, s := range presentSizes {
arr := sumBySize[s]
for k := 1; k <= s; k++ {
idx := s - (s % k)
ans[k] += arr[idx]
}
}
var buf bytes.Buffer
for k := 1; k <= n; k++ {
if k > 1 {
buf.WriteByte(' ')
}
buf.WriteString(strconv.FormatInt(ans[k], 10))
}
buf.WriteByte('\n')
out.Write(buf.Bytes())
}
}
```