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

#BS0000474. Ngày kết nối đầu tiên (First Connection Day)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Ngày kết nối đầu tiên (First Connection Day)

First Connection Day

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

A region contains nn cities numbered from 11 to nn. Initially there are no roads. Over mm consecutive days, exactly one bidirectional road is opened each day. On day ii, a road between cities uiu_i and viv_i opens and remains available thereafter. Several roads may connect the same pair of cities.

For each of qq requests, find the earliest day on which city aja_j can reach city bjb_j using roads opened by the end of that day. If both cities are the same, the answer is 00. If no such route exists even after day mm, the answer is −1-1.

Input

The first line contains integers n,m,qn,m,q, the numbers of cities, road construction days and requests.

The next mm lines describe the roads: line ii contains integers ui,viu_i,v_i, the endpoints of the road opened on day ii.

Each of the final qq lines contains integers aj,bja_j,b_j, the start and destination cities of request jj.

Output

Print qq lines, each containing the earliest connection day for the corresponding request, or −1-1 if the cities never become connected.

Subtasks

  • Subtask 1 (20 points): 1≤n≤301\le n\le 30; 0≤m≤300\le m\le 30; 1≤q≤301\le q\le 30; 1≤ui,vi≤n1\le u_i,v_i\le n, ui≠viu_i\ne v_i for every 1≤i≤m1\le i\le m; 1≤aj,bj≤n1\le a_j,b_j\le n for every 1≤j≤q1\le j\le q. Repeated roads are allowed.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 0≤m≤20000\le m\le 2000; 1≤q≤20001\le q\le 2000; 1≤ui,vi≤n1\le u_i,v_i\le n, ui≠viu_i\ne v_i for every 1≤i≤m1\le i\le m; 1≤aj,bj≤n1\le a_j,b_j\le n for every 1≤j≤q1\le j\le q. Repeated roads are allowed.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; 0≤m≤2000000\le m\le 200000; 1≤q≤2000001\le q\le 200000; 1≤ui,vi≤n1\le u_i,v_i\le n, ui≠viu_i\ne v_i for every 1≤i≤m1\le i\le m; 1≤aj,bj≤n1\le a_j,b_j\le n for every 1≤j≤q1\le j\le q. Repeated roads are allowed.

Examples

Input

5 4 5
1 2
3 4
2 3
1 4
1 4
1 2
3 3
2 5
3 4

Output

3
1
0
-1
2

Explanation

There are five cities and four construction days.

  • Day 11 connects cities 11 and 22.
  • Day 22 connects cities 33 and 44.
  • Day 33 joins the two groups through road 2−32-3. This is the first time a route from 11 to 44 exists, giving answer 33 for the first request.
  • Day 44 opens road 1−41-4 but does not connect city 55 to any other city.

Cities 11 and 22 are connected on day 11, so the second answer is 11. City 33 reaches itself on day 00, so the third answer is 00. City 55 stays isolated, giving −1-1 for the fourth request. Cities 33 and 44 first connect on day 22, giving the final answer 22.