This page is still under construction.

Parts of this page are still being built. What you see may change.

Tropical Garden

Time limit5sMemory limit256 MB

Summary
Count starting ponds whose deterministic non-backtracking walk (prefer the most beautiful unused-at-previous-step walkway) reaches pond P after exactly K steps, for many K.
Level

Hard9 of 10

Topics
Graph, Simulation, Math, Number theory
Solved
No attempts yet

Problem

The botanist Chulsoo visits a huge tropical garden together with several school classes. The garden has NN ponds, numbered 00 through N−1N-1, and MM walkways, numbered 00 through M−1M-1. Each walkway connects two distinct ponds and can be traversed in both directions. Every pond has at least one walkway attached to it, and between any two ponds there is at most one walkway.

The walkways are numbered in decreasing order of beauty: for every i (0≤i<M−1)i\ (0 \le i < M-1), walkway ii is more beautiful than walkway i+1i+1. Because Chulsoo is a botanist, no two walkways are ever equally beautiful.

Chulsoo and the students move according to the following rule. At the current pond they always take the most beautiful attached walkway. However, if that walkway is exactly the one they used on the previous move, they take the second most beautiful walkway instead. The only exception is when the current pond has just one attached walkway: there is no second choice, so they reuse the walkway they just arrived on. (At the start no walkway has been used yet, so they always leave along the most beautiful walkway.)

This movement rule is deterministic: once a starting pond is fixed, the entire route is uniquely determined.

The students want to have lunch at the fine restaurant next to pond PP. Each class becomes hungry after passing exactly KK walkways, and at that moment they must be standing at pond PP. The value of KK may differ between classes.

Treating each of the NN ponds as a possible starting pond, determine how many distinct routes end at pond PP after using exactly KK walkways. Each starting pond produces exactly one route, so this equals the number of such starting ponds. A route may pass through pond PP earlier, but immediately after the KK-th walkway it must be at PP.

Answer this for QQ classes, that is, for QQ values of KK.

Input

The first line contains three integers NN, MM, and PP separated by spaces.

Each of the next MM lines describes one walkway: line ii (for 0≤i<M0 \le i < M) contains the numbers of the two ponds joined by walkway ii. The walkways are given in order of beauty, most beautiful first.

The next line contains the number of classes QQ, and each of the following QQ lines contains one value of KK.

Output

For each class, print on its own line the number of distinct routes (starting ponds) that reach pond PP using exactly KK walkways, in the same order as the input. Print 00 if no such route exists.

Examples2

  1. Example 1

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

    Input
    5 5 2
    1 0
    1 2
    3 2
    1 3
    4 2
    2
    3
    1
    
    Expected output
    1
    2