int n, m, q; int f[MAXN][21], dep[MAXN]; int h[MAXQ][6], c[MAXN], a[MAXQ][6], t[MAXN][21]; bitset<1005> g[MAXN]; bitset<1005> s[MAXQ][6]; bitset<1005> x[35];
inlineintLca(int u, int v){ if (dep[u] < dep[v]) swap(u, v); for (int i = 19; i >= 0; --i) if (dep[f[u][i]] >= dep[v]) u = f[u][i]; if (u == v) return u; for (int i = 19; i >= 0; --i) if (f[u][i] != f[v][i]) u = f[u][i], v = f[v][i]; return f[u][0]; }
intmain(){ scanf("%d%d%d", &n, &m, &q); dep[1] = 1; for (int i = 2; i <= n; ++i) scanf("%d", &f[i][0]), dep[i] = dep[f[i][0]] + 1; for (int j = 1; j <= 19; ++j) for (int i = 1; i <= n; ++i) f[i][j] = f[f[i][j - 1]][j - 1]; for (int i = 1; i <= n; ++i) { int tmp; scanf("%d", &tmp); g[i][tmp] = 1; } for (int i = 1; i <= q; ++i) { scanf("%d", &c[i]); for (int j = 1; j <= c[i]; ++j) scanf("%d", &a[i][j]), s[i][j] = g[a[i][j]]; int lca = a[i][1]; for (int j = 2; j <= c[i]; ++j) lca = Lca(lca, a[i][j]); for (int j = 1; j <= c[i]; ++j) h[i][j] = dep[a[i][j]] - dep[lca]; } for (int i = n; i >= 1; --i) g[i] |= g[f[i][0]]; for (int j = 0; j <= 19; ++j) { for (int i = 1; i <= q; ++i) for (int k = 1; k <= c[i]; ++k) if (h[i][k] & (1 << j)) s[i][k] |= g[a[i][k]], a[i][k] = f[a[i][k]][j]; for (int i = n; i >= 1; --i) g[i] = (g[i] | g[f[i][j]]); } for (int i = 1; i <= q; ++i) { int ans = INF; for (int k = 1; k < (1 << c[i]); ++k) { x[k].reset(); int tmp = 0; for (int j = 1; j <= c[i]; ++j) if (k & (1 << (j - 1))) tmp++, x[k] |= s[i][j]; ans = min(ans, (int)x[k].count() / tmp); } printf("%d\n", ans * c[i]); } return0; }