#include using namespace std; typedef long long ll; typedef pair pi; typedef pair pl; typedef double db; #define ls(x) ((x)<<1) #define rs(x) ((x)<<1|1) #define low(x) ((x)&-(x)) #define all(x) x.begin(),x.end() #define mp make_pair #define X first #define Y second #ifdef _DEBUG const int N=1e3+10; #else const int N=1e5+10; #endif const ll mod=1e9+7; //const ll mod=998244353; inline ll gcd(ll a,ll b){return !b?a:gcd(b,a%b);} inline ll q_pow(ll a,ll x){ll ans=1,tmp=a;while(x){if(x&1)(ans*=tmp)%=mod;(tmp*=tmp)%=mod;x>>=1;}return ans;} template inline void re(T &N){int f=1;char c;while((c=getchar())< '0'||c> '9')if(c=='-')f=-1;N=c-'0';while((c=getchar())>='0'&&c<='9')N=N*10+c-'0';N*=f;} templateinline void re(T&x,T_&...y){re(x),re(y...);} int m,n,t=1,st,en; struct edge{ int to,nxt; }e[N<<1];int head[N]; inline void add(int x,int y) { e[++t]={y,head[x]};head[x]=t; e[++t]={x,head[y]};head[y]=t; } int dp[N],sta[N],ans; void dfs(int x,int p) { if(sta[x])dp[x]=m; else dp[x]=1e9; for(int i=head[x];i;i=e[i].nxt) { int to=e[i].to; if(to==p)continue; dfs(to,x); dp[x]=min(dp[x],dp[to]-1); } if(!dp[x]||(x==1&&dp[x]<=m))ans++,dp[x]=1e9; } int main() { #ifdef _DEBUG freopen("data.txt","r",stdin); #endif int T;re(T); while(T--) { re(n,m);t=1;ans=0; for(int i=1;i<=n;i++)head[i]=0; for(int i=2;i<=n;i++) { int x;re(x); add(x,i); } int wa=1; for(int i=1;i<=n;i++)re(sta[i]),wa&=!sta[i]; if(wa){puts("0");continue;} dfs(1,0); printf("%d\n",ans); } }