#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define inf 0x3f3f3f3f#define maxn 106using namespace std;int has[maxn], cost[maxn], a[maxn];int a2[maxn];int n, max1, sum;struct Node{ int num; int id;} node[maxn];bool cmp(const Node a, const Node b){ return a.num < b.num;}int main(){ int ncase, tmp, t, sum2; cin >> ncase; while(ncase--) { memset(has,0,sizeof(has)); memset(cost,0,sizeof(cost)); memset(a,0,sizeof(a)); memset(node,0,sizeof(node)); max1 = inf; cin >> n; for(int i= 2; i <= n; i++) { scanf("%d", &a[i]); has[ a[i] ]++; } for(int i = 2; i <= n; i ++) { scanf("%d", &cost[i]); } for(int j = has[1]; j < n; j++) { memset(node, 0, sizeof(node)); sum = 0; tmp = has[1]; for(int i = 2; i <= n; i++) { if(has[i] > j - 1) { sum += has[i] - ( j - 1); } } if(sum + has[1] > j) continue; for(int i = 0 ; i <= n; i ++) { a2[i] = a[i]; } t = 0 , sum2 = 0 ; for(int i = 2; i <=n; i++) { if(has[i] > j - 1) { t = 0; for( int k = 2; k <= n; k ++) if(a2[k] == i) { node[t].num = cost[k]; node[t].id = k; t++; } sort(node,node+t,cmp); for(int k = 0; k < has[i]-(j-1); k++ ) { sum2 += node[k].num; a2[ node[k].id ] = 1; has[1]++; } } } t = 0; for(int i = 2 ; i <= n; i++) { if(a2[i] != 1) { node[t].num = cost[i]; node[t].id = i ; t++; } } sort(node, node+t, cmp); for(int k = 0; k < t ; k++) { if(has[1] >= j) break; sum2 += node[k].num; a2[ node[k].id ] = 1; has[1]++; } max1 = min(max1, sum2); has[1] = tmp; } printf("%dn",max1); } return 0;}


