# include <bits/stdc++.h>
# define pb push_back
# define eb emplace_back
# define int long long
# define f first
# define s second
using namespace std;
vector <int> g[2*100001];
int sum[2*100001], s[2*100001], ans[2*100001];
void dfs(int v, int p, int k){
ans[v] = s[v] * k;
sum[v] = s[v];
int mx = 0;
vector <int> q;
for(auto i: g[v]){
dfs(i, v, k / g[v].size());
ans[v] += ans[i];
//cout << v << ' ' << i << ' ' << sum[i] << endl;
q.eb(sum[i]);
}
if(g[v].size()){
// cout << k << ' ' << k % g[v].size() << ' '<< v << ' '<< sum[2] << endl;
sort(q.begin(), q.end());
int ptr = g[v].size() - 1;
for(int j = 1;j <= k % g[v].size();j ++){
ans[v] += q[ptr--];
}
while(ptr != -1){
mx = max(mx , q[ptr --]);
}
}
sum[v] += mx;
}
main(){
int t;
cin >> t;
while(t--){
int n, k;
cin >> n >> k;
for(int i = 1;i <= n;i ++) g[i].clear(), sum[i] = 0, ans[i] = 0;
for(int i = 2;i <= n;i ++){
int x;
cin >> x;
g[x].pb(i);
}
for(int i = 1;i <= n;i ++) cin >> s[i];
dfs(1, 1, k);
cout << ans[1] << endl;
}
}
ICAgICMgaW5jbHVkZSA8Yml0cy9zdGRjKysuaD4KICAgICMgZGVmaW5lIHBiIHB1c2hfYmFjawogICAgIyBkZWZpbmUgZWIgZW1wbGFjZV9iYWNrCiAgICAjIGRlZmluZSBpbnQgbG9uZyBsb25nCiAgICAjIGRlZmluZSBmIGZpcnN0CiAgICAjIGRlZmluZSBzIHNlY29uZAogICAgdXNpbmcgbmFtZXNwYWNlIHN0ZDsKICAgIHZlY3RvciA8aW50PiBnWzIqMTAwMDAxXTsKICAgIGludCBzdW1bMioxMDAwMDFdLCBzWzIqMTAwMDAxXSwgYW5zWzIqMTAwMDAxXTsKICAgIHZvaWQgZGZzKGludCB2LCBpbnQgcCwgaW50IGspewogICAgICAgIGFuc1t2XSA9IHNbdl0gKiBrOwogICAgICAgIHN1bVt2XSA9IHNbdl07CiAgICAgICAgaW50IG14ID0gMDsKICAgICAgICB2ZWN0b3IgPGludD4gcTsKICAgICAgICBmb3IoYXV0byBpOiBnW3ZdKXsKICAgICAgICAgICAgZGZzKGksIHYsIGsgLyBnW3ZdLnNpemUoKSk7CiAgICAgICAgICAgIGFuc1t2XSArPSBhbnNbaV07CiAgICAgICAgICAgICAvL2NvdXQgPDwgdiA8PCAnICcgPDwgaSA8PCAnICcgPDwgc3VtW2ldIDw8IGVuZGw7CiAgICAgICAgICAgIHEuZWIoc3VtW2ldKTsKICAgICAgICB9CiAgICAgICAgaWYoZ1t2XS5zaXplKCkpewogICAgICAgICAgICAvLyBjb3V0IDw8IGsgPDwgJyAnIDw8IGsgJSBnW3ZdLnNpemUoKSA8PCAnICc8PCB2IDw8ICcgJzw8IHN1bVsyXSA8PCBlbmRsOwogICAgICAgICAgICBzb3J0KHEuYmVnaW4oKSwgcS5lbmQoKSk7CiAgICAgICAgICAgIGludCBwdHIgPSBnW3ZdLnNpemUoKSAtIDE7CiAgICAgICAgICAgIGZvcihpbnQgaiA9IDE7aiA8PSBrICUgZ1t2XS5zaXplKCk7aiArKyl7CiAgICAgICAgICAgICAgICBhbnNbdl0gKz0gcVtwdHItLV07CiAgICAgICAgICAgIH0KICAgICAgICAgICAgd2hpbGUocHRyICE9IC0xKXsKICAgICAgICAgICAgICAgIG14ID0gbWF4KG14ICwgcVtwdHIgLS1dKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBzdW1bdl0gKz0gbXg7CiAgICB9CiAgICBtYWluKCl7CiAgICAgICAgaW50IHQ7CiAgICAgICAgY2luID4+IHQ7CiAgICAgICAgd2hpbGUodC0tKXsKICAgICAgICAgICAgaW50IG4sIGs7CiAgICAgICAgICAgIGNpbiA+PiBuID4+IGs7CiAgICAgICAgICAgIGZvcihpbnQgaSA9IDE7aSA8PSBuO2kgKyspIGdbaV0uY2xlYXIoKSwgc3VtW2ldID0gMCwgYW5zW2ldID0gMDsKICAgICAgICAgICAgZm9yKGludCBpID0gMjtpIDw9IG47aSArKyl7CiAgICAgICAgICAgICAgICBpbnQgeDsKICAgICAgICAgICAgICAgIGNpbiA+PiB4OwogICAgICAgICAgICAgICAgZ1t4XS5wYihpKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBmb3IoaW50IGkgPSAxO2kgPD0gbjtpICsrKSBjaW4gPj4gc1tpXTsKICAgICAgICAgICAgZGZzKDEsIDEsIGspOwogICAgICAgICAgICBjb3V0IDw8IGFuc1sxXSA8PCBlbmRsOwogICAgICAgIH0gICAKICAgIH0=