Under construction

Still being built. Things may change

Ski Resort

Time limit2sMemory limit512 MB

Summary
For each query, count the size-k sets of areas such that every favorite area has exactly one stocked area lying on every path from the top, and all chosen areas lie on paths to favorite areas.
Level

Hard8 of 10

Topics
Tree, Dynamic programming, DFS, Combinatorics
Solved
No attempts yet

Problem

You own a well-known ski resort in the US, and you would like to increase your sales by stocking the snack stands at good locations across the resort.

The ski resort is built on a mountain. A ski lift takes a guest to the top of the mountain. From there, the guest can ski to a number of locations on the mountain.

There are nn areas on the mountain, labeled 11 to nn. The top of the mountain is area 11. Areas are connected by ski runs that are strictly downhill. In other words, after leaving an area, a guest cannot return to it without taking the ski lift. Every area (including area 11) has exactly one snack stand.

As the owner of the resort, you want to know how effectively you can distribute snacks among the snack stands to serve your guests better (and make more money). To this end, you run a survey and analyze the results with a number of independent queries. Each guest in the survey has a favorite snack and a list of favorite areas that they like to visit. You would like to know how best to stock your snack stands with their favorite snack.

Each query is a set of one guest's favorite areas and a number kk. Count the ways to stock this guest's favorite snack at exactly kk snack stands on the mountain so that the following conditions hold:

  1. For each of this guest's favorite areas, over all sequences of ski runs that reach that area from the top of the mountain, there must be exactly one snack stand with the guest's favorite snack. In other words, the guest must not have a choice of more than one snack stand where the snack is available. So among the areas that lie on at least one route from area 11 to that area (both ends included), exactly one has a stocked stand, and every route from area 11 to that area passes through it.
  2. Each of the kk snack stands stocked with this guest's favorite snack must lie on some sequence of ski runs from the top of the mountain to some area in the query set.

Input

The input consists of a single test case. Your program may be run multiple times on different inputs.

The first line contains three space-separated integers nn, mm, and qq, where nn (1≤n≤1051 \le n \le 10^5) is the number of areas on the mountain, mm (1≤m≤n+501 \le m \le n + 50) is the number of runs, and qq (1≤q≤1051 \le q \le 10^5) is the number of queries.

Each of the next mm lines contains two integers xx and yy (1≤x,y≤n1 \le x, y \le n, x≠yx \ne y), meaning there is a ski run from area xx down to area yy. There is at most one run between any two areas. Every area can be reached from area 11 by some chain of ski runs.

Each of the next qq lines is a sequence of space-separated integers that starts with kk and aa and continues with aa integers ii. Here kk (1≤k≤41 \le k \le 4) is the number of snack stands to stock with this guest's favorite snack, aa (1≤a≤n1 \le a \le n) is the number of areas in the query set, and the aa integers ii (1≤i≤n1 \le i \le n) are the labels of the areas in the query set. Within a query, no integer ii is repeated.

The sum of all aa over all queries does not exceed 100 000100\,000.

Output

Print qq integers, each on its own line, with no blank lines in between. Each integer is the number of ways to choose the snack stands to stock for the corresponding query, in the order the queries appear in the input. Two ways are different if an area is selected in one of them but not in the other.

Examples3

  1. Example 1

    Input
    4 4 4
    1 2
    1 3
    2 4
    3 4
    1 1 4
    2 1 4
    1 1 3
    2 2 3 2
    
    Expected output
    2
    0
    2
    1
    
  2. Example 2

    Input
    8 10 4
    1 2
    2 3
    1 3
    3 6
    6 8
    2 4
    2 5
    4 7
    5 7
    7 8
    2 3 4 5 6
    2 2 6 8
    1 1 6
    1 1 8
    
    Expected output
    0
    0
    3
    2
    
  3. Example 3

    Input
    8 9 4
    1 2
    1 3
    3 6
    6 8
    2 4
    2 5
    4 7
    5 7
    7 8
    2 3 4 5 6
    2 2 6 8
    1 1 6
    1 1 8
    
    Expected output
    2
    0
    3
    2