Problem10310--CF EDU - DSU - Step 3 - B. Number of Connected Components on Segments

10310: CF EDU - DSU - Step 3 - B. Number of Connected Components on Segments

[Creator : ]
Time Limit : 6.000 sec  Memory Limit : 512 MiB

Description

You are given a graph with $n$ vertices and $m$ undirected edges. Write a program that processes $k$ queries of the form $(l_j,r_j)$: the answer for the $j$-th query is the number of connected components if we remove all the edges from the graph except edges with indices from $l_j$ to $r_j$ inclusive.
The queries should be answered independently. In other words, to answer the $j$-th query, you should consider a graph that has $n$ vertices and $r_j−l_j+1$ edges.

Input

The first line contains two integers $n,m\ (2≤n≤50,000,\ 1≤m≤50,000)$ — the number of vertices and the number of edges, respectively.
Next $m$ lines contain the description of edges, one per line. A description consists of two integers $u_i,v_i\ (1≤u_i,v_i≤n,\ u_i≠v_i)$ — the ends of an edge. It is guaranteed that all the edges are distinct. In other words, if there is an edge $(u_i,v_i)$, then there is no other edge $(u_i,v_i)$ or $(v_i,u_i)$.
The next line contains a single integer $k\ (1≤k≤50,000)$ — the number of queries.
Next $k$ lines contain the description of queries, one per line. A description consits of two integers $l_j$ and $r_j\ (1≤l_j≤r_j≤m)$ — the segment of edges to be considered.

Output

Output $k$ integers, one per line. The $j$-th integer should be equal to the number of connected components in the graph of $n$ vertices and edges with indices $l_j,l_{j+1},…,r_j$.

Sample 1 Input

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

Sample 1 Output

2
1
1
2
1

Sample 2 Input

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

Sample 2 Output

5
4
5
5
5
6
6

HINT

CF EDU.

Source/Category