Under construction

Still being built. Things may change

Giant Penguin

Time limit3sMemory limit512 MB

Summary
Given a connected graph where each vertex lies on at most k simple cycles, support marking vertices and querying the distance from a vertex to the nearest marked vertex.
Level

Hard9 of 10

Topics
Graph, Tree, Divide and conquer, Shortest path
Solved
No attempts yet

Problem

Pengsoo is a well-known giant Korean penguin. He is very rude and loves singing.

This time, Pengsoo is performing graph queries.

He has a connected undirected graph. Each vertex lies on at most kk vertex-simple cycles.

He wants to answer two types of queries.

  • Mark a vertex vv.
  • Find the distance from vertex vv to the closest marked vertex. At least one vertex is marked whenever this query is asked.

Pengsoo is very lazy and has decided to take a nap, so he asks you to perform these queries. If you do not finish before he wakes up, he will bully you, so act quickly!

Input

The first line contains three integers nn, mm, and kk (1≤n≤1000001 \le n \le 100000, n−1≤m≤200000n-1 \le m \le 200000, 0≤k≤100 \le k \le 10): the number of vertices, the number of edges, and the maximum number of vertex-simple cycles that pass through one vertex.

The next mm lines each contain two integers uiu_i and viv_i (1≤ui,vi≤n1 \le u_i, v_i \le n, ui≠viu_i \ne v_i), denoting an edge between uiu_i and viv_i. There are no multiple edges, the graph is connected, and each vertex lies on at most kk vertex-simple cycles.

The next line contains one integer qq (1≤q≤2000001 \le q \le 200000): the number of queries.

The next qq lines each contain two integers tit_i and viv_i (1≤ti≤21 \le t_i \le 2, 1≤vi≤n1 \le v_i \le n). If ti=1t_i = 1, mark vertex viv_i. Vertex viv_i was not marked before. If ti=2t_i = 2, print the distance from viv_i to the closest marked vertex. At least one vertex is marked at that time.

Output

For each query with ti=2t_i = 2, print the distance to the closest marked vertex.

Examples2

  1. Example 1

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

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