import java.util.*; import java.io.*; import java.text.*; public class Main{ long mod = (int)1e9+7, IINF = (long)2e18; final int MAX = (int)10, INF = (int)1e9, root = 3; DecimalFormat df = new DecimalFormat("0.00000000"); double PI = 3.141592653589793238462643383279502884197169399375105820974944; static boolean multipleTC = true, memory = false; FastReader in;PrintWriter out; public static void main(String[] args) throws Exception{ if(memory)new Thread(null, new Runnable() {public void run(){try{new Main().run();}catch(Exception e){e.printStackTrace();}}}, "1", 1 << 28).start(); else new Main().run(); } void run() throws Exception{ in = new FastReader(); out = new PrintWriter(System.out); for(int i = 1, T= (multipleTC)?ni():1; i<= T; i++)solve(i); out.flush(); out.close(); } void solve(int TC) throws Exception{ int n = ni(); long[] w = new long[n]; for(int i = 0; i< n; i++)w[i] = nl(); int[][] g = new int[n][], edge = new int[n-1][2]; int[] cnt = new int[n]; for(int i = 1; i< n; i++){ int R = ni()-1; edge[i-1] = new int[]{R, i}; cnt[edge[i-1][0]]++; } for(int i = 0; i< n; i++)if(cnt[i]>0)g[i] = new int[cnt[i]]; for(int i = 0; i< n-1; i++)g[edge[i][0]][--cnt[edge[i][0]]] = edge[i][1]; long[][] dp = new long[n][n+1]; for(int i = 0; i< n; i++)for(int j = 0; j<= n; j++)dp[i][j] = -1; for(int i = 1; i<= n; i++)p(calc(g,dp,w,0,i)+" ");pn(""); } long calc(int[][] g,long[][] dp,long[] w,int u, int k) throws Exception{ if(k==0)return 0; if(k==1)return w[u]; if(dp[u][k]!=-1)return dp[u][k]; if(g[u] == null)return (dp[u][k] = w[u]); if(g[u].length==1)return (dp[u][k] = w[u]+calc(g,dp,w,g[u][0], k-1)); if(g[u].length==2){ long mx = 0; for(int i = 0; i< k; i++) mx = Math.max(mx, calc(g,dp,w,g[u][0],i)+calc(g,dp,w,g[u][1],k-i-1)); return (dp[u][k] = w[u]+mx); } if(g[u].length==3){ long mx = 0; for(int i = 0; i< k; i++) for(int j = 0; i+j< k; j++) mx = Math.max(mx, calc(g,dp,w,g[u][0],i)+calc(g,dp,w,g[u][1],j)+calc(g,dp,w,g[u][2],k-i-j-1)); return (dp[u][k] = w[u]+mx); } throw new Exception("Invalid Tree"); } long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);} int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);} int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));} void p(Object o){out.print(o);} void pn(Object o){out.println(o);} void pni(Object o){out.println(o);out.flush();} String n(){return in.next();} String nln(){return in.nextLine();} int ni(){return Integer.parseInt(in.next());} long nl(){return Long.parseLong(in.next());} double nd(){return Double.parseDouble(in.next());} class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws Exception{ br = new BufferedReader(new FileReader(s)); } String next(){ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); }catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } String nextLine(){ String str = ""; try{ str = br.readLine(); }catch (IOException e){ e.printStackTrace(); } return str; } } }