template <typename T> inlinevoidread(T &x){ x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; }
ll p, q, a, b, c, d, w;
ll fpow(ll x, ll y, ll c){ ll res = 1; while (y) { if (y & 1) (res *= x) %= c; y >>= 1, (x *= x) %= c; } return res; }
intget_digit(ll x){ int res = 0; while (x) ++ res, x >>= 1; return res; }
intmain(){ read(p); read(q); w = get_digit(p); while (q --) { read(a); read(b); read(c); read(d); if ((a + b) % p != (c + d) % p) {puts("-1"); continue;} ll sum = (a + b) % p, inv = fpow(sum, p - 2, p); for (int i = 0; i <= w; ++ i) { ll now = ((1ll << i) * a % p - c + p) % p * inv % p; int now_cnt = get_digit(now); if (now_cnt <= i) {printf("%lld\n", i); break;} } } return0; }
template <typename T> inlinevoidread(T &x){ x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; }
int T, n, k, a[N], ans = -1, s[N], LMT, st[N][3];
voiddfs(int t, int tot, int lsum, int rsum){ if (~ans) return; if (t == LMT + 1) {if (lsum - rsum == T) ans = 1;return;} if (tot < k && !(~ans)) s[t] = 0, dfs(t + 1, tot + 1, lsum, rsum); if (!(~ans)) s[t] = 1, dfs(t + 1, tot, lsum + a[t], rsum); if (!(~ans)) s[t] = 2, dfs(t + 1, tot, lsum, rsum + a[t]); }
intmain(){ read(n); read(k); LMT = min(n, 25); int tot[3]; tot[0] = tot[1] = tot[2] = 0; for (int i = 1; i <= n; ++ i) read(a[i]); for (int i = 26; i <= n; ++ i){ if (T > 0) T -= a[i], st[++ st[0][1]][1] = i; else T += a[i], st[++ st[0][2]][2] = i; } dfs(1, 0, 0, 0); if (!(~ans)) {puts("-1"); return0;} for (int i = 1; i <= LMT; ++ i) ++ tot[s[i]]; printf("%d ", tot[1] + st[0][1]); for (int i = 1; i <= st[0][1]; ++ i) printf("%d ", st[i][1]); for (int i = 1; i <= LMT; ++ i) if (s[i] == 1) printf("%d ", i); printf("\n%d ", tot[2] + st[0][2]); for (int i = 1; i <= st[0][2]; ++ i) printf("%d ", st[i][2]); for (int i = 1; i <= LMT; ++ i) if (s[i] == 2) printf("%d ", i); printf("\n"); return0; }
template <typename T> inlinevoidread(T &x){ x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; }
structEdge{int u, v, nxt;} e[N];
int T, n, k, head[N], ecnt(1), f[N], siz[N], son[N];
inlinevoidadd_edge(int u, int v){ e[ecnt].u = u, e[ecnt].v = v; e[ecnt].nxt = head[u], head[u] = ecnt ++; }
voidget_ans(int x){ siz[x] = 1; son[x] = 0, f[x] = 0; int t = 0; for (int i = head[x]; i; i = e[i].nxt) { get_ans(e[i].v); f[x] += f[e[i].v]; siz[x] += siz[e[i].v]; ++ son[x]; if (!f[e[i].v] && siz[e[i].v] > 1) t = 1; } if (son[x] >= k && t) ++ f[x]; for (int i = head[x]; i; i = e[i].nxt) if (siz[e[i].v] >= k + 1) f[x] = max(f[x], f[e[i].v] + 1); }
intmain(){ read(T); while (T --) { ecnt = 1; mset(head, 0); read(n), read(k); int x = 0; for (int i = 2; i <= n; ++ i) read(x), add_edge(x, i); get_ans(1); printf("%d\n", f[1]); } return0; }