#include using namespace std; const int N=5+1e5; struct Graph{ struct Edge{ int to,nxt; }; vector head; vector edge; Graph(int n=0,int m=0){ head=vector(n+1,-1); edge.clear(); edge.reserve(m); } inline void add(int x,int y){ edge.push_back((Edge){y,head[x]}); head[x]=edge.size()-1; } }G; int f[N],a[N],k,cnt; void dfs(int x,int pre){ if(a[x]) f[x]=k; else f[x]=(int)1e9; for(int j=G.head[x];~j;j=G.edge[j].nxt){ int y=G.edge[j].to; if(y!=pre){ dfs(y,x); f[x]=min(f[x],f[y]-1); } } if(f[x]==0||(x==1&&f[x]<(int)5e8)) ++cnt,f[x]=(int)1e9; } int main(){ int T; scanf("%d",&T); while(T--){ int n; scanf("%d%d",&n,&k); G=Graph(n,n); cnt=0; for(int i=2;i<=n;++i){ int x; scanf("%d",&x); G.add(x,i); } for(int i=1;i<=n;++i) scanf("%d",&a[i]); dfs(1,0); printf("%d\n",cnt); } return 0; }