#include<stdio.h>
#include<stdlib.h>
#define M 1000
struct SCORE
{
int deadline;
int score;
}test[M], t;
int cmp(const void *a, const void *b)
{
struct SCORE *aa = (struct SCORE *)a;
struct SCORE *bb = (struct SCORE *)b;
return aa->deadline - bb->deadline;
}
int main()
{
int T, j, i, N, count, k, iscore;
scanf("%d", &T);
while (T--)
{
scanf("%d", &N);
for (i = 0; i < N; i++)
scanf("%d", &test[i].deadline);
for (i = 0; i < N; i++)
scanf("%d", &test[i].score);
qsort(test, N, sizeof(test[0]), cmp);
for (i = N; i >= 1; i--)
{
iscore = k = 0;
for (j = N - 1; test[j].deadline >= i && j >= 0; j--)
{
if (test[j].score > iscore)
{
k = j;
iscore = test[j].score;
}
}
if (iscore != 0)
test[k].score = 0;
}
count = 0;
for (i = 0; i < N; i++)
count += test[i].score;
printf("%d/n", count);
}
return 0;
}