/* Name: Author: xiaruize Date: */ #include<bits/stdc++.h> usingnamespace std; #define int long long #define ull unsigned long long #define ALL(a) (a).begin(), (a).end() #define pb push_back #define mk make_pair #define pii pair<int, int> #define pis pair<int, string> #define sec second #define fir first #define sz(a) int((a).size()) #define rep(i, x, y) for (int i = x; i <= y; i++) #define repp(i, x, y) for (int i = x; i >= y; i--) #define Yes cout << "Yes" << endl #define YES cout << "YES" << endl #define No cout << "No" << endl #define NO cout << "NO" << endl #define debug(x) cerr << #x << ": " << x << endl #define double long double constint INF = 0x3f3f3f3f; constint MOD = 1000000007; constint N = 2e5 + 10;
// bool st; int n, q; int p[N]; int m; int v[N]; int deg[N]; bool st[N]; int cnt[N]; // bool en;
signedmain() { // freopen(".in","r",stdin); // freopen(".out","w",stdout); // cerr<<(&en-&st)/1024.0/1024.0<<endl; // auto t_1=chrono::high_resolution_clock::now(); std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); cin >> n >> q; for (int i = 2; i <= n; i++) { cin >> p[i]; deg[p[i]]++; } while (q--) { cin >> m; int res = 0; for (int i = 1; i <= m; i++) { cin >> v[i]; st[v[i]] = true; cnt[p[v[i]]]++; } for (int i = 1; i <= m; i++) { res += deg[v[i]] - cnt[v[i]]; if (!st[p[v[i]]]) res++; } for (int i = 1; i <= m; i++) { st[v[i]] = false; cnt[p[v[i]]] = 0; } cout << res << endl; } // auto t_2=chrono::high_resolution_clock::now(); // cout <<". Elapsed (ms): " << chrono::duration_cast<chrono::milliseconds>(t_2 - t_1).count() << endl; return0; }