PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#LCA0000077. Tổ tiên chung thấp nhất bậc k (K-th LNCA)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổ tiên chung thấp nhất bậc k (K-th LNCA)

K-th LNCA

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

A tree is rooted at vertex 11. Each query gives k,mk,m and a set SS of mm distinct vertices. Consider every size-kk subset T⊆ST\subseteq S, compute its lowest common ancestor, and collect these results into a set AA (duplicates removed). A member of AA is a k-th LNCA if its depth equals the maximum depth of any vertex in AA. Count the distinct k-th LNCA vertices. This is not the ordinary kk-th ancestor operation.

Input

First line contains n qn\ q, followed by n−1n-1 edges u vu\ v. Each query is k m v1 ⋯ vmk\ m\ v_1\ \cdots\ v_m.

Output

Print the number of distinct maximum-depth LNCA vertices per query.

Subtasks

General constraints: 1≤n,q≤10001\le n,q\le1000; n+q≤1000n+q\le1000; 2≤k≤m≤n2\le k\le m\le n; các đỉnh trong mỗi truy vấn phân biệt.

  • Subtask 1 (20 points): 1≤n≤401\le n\le 40; 1≤q≤501\le q\le 50.

  • Subtask 2 (30 points): 1≤n≤9991\le n\le 999; 1≤q≤9991\le q\le 999.

  • Subtask 3 (50 points): 1≤n≤9991\le n\le 999; 1≤q≤9991\le q\le 999.

Examples

Input

7 1
1 2
1 3
2 4
2 5
3 6
3 7
2 4 4 5 6 7

Output

2

Explanation

Pairs (4,5)(4,5) and (6,7)(6,7) have LCAs 22 and 33 of depth 11. Cross-branch pairs have LCA 11 of depth 00. The deepest candidates are 22 and 33, so the answer is 22.