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

#DSU0000074. Các thành phần liên thông (Connected Components)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Các thành phần liên thông (Connected Components)

Connected Components

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

A network has nn computers and mm indexed undirected cables. Multiple cables may connect the same pair of computers.

There are kk independent experiments. In experiment ii, cables numbered lil_i through rir_i are disconnected temporarily, and the number of connected components is measured. All cables are restored before the next experiment. An isolated computer counts as one component.

Cables are indexed by their input order, so two cables joining the same pair of computers remain distinct cables. Removing the interval [li,ri][l_i,r_i] disables all cables with indices in that interval, including both endpoints.

Each query is an independent experiment: after counting connected components, all cables disabled in that experiment are restored. Therefore, an earlier query never changes the initial state for a later query.

Input

The first line contains n,mn,m, followed by mm pairs xi,yix_i,y_i. The next line contains kk, followed by kk intervals li,ril_i,r_i.

Output

Print kk integers. Integer ii is the component count during experiment ii.

Subtasks

  • Subtask 1 (20 points): 2≤n≤202\le n\le 20; 1≤m≤1001\le m\le 100; 1≤k≤1001\le k\le 100; 1≤xi,yi≤n1\le x_i,y_i\le n, xi≠yix_i\ne y_i; parallel edges are allowed; 1≤li≤ri≤m1\le l_i\le r_i\le m.
  • Subtask 2 (30 points): 2≤n≤1002\le n\le 100; 1≤m≤10001\le m\le 1000; 1≤k≤10001\le k\le 1000; 1≤xi,yi≤n1\le x_i,y_i\le n, xi≠yix_i\ne y_i; parallel edges are allowed; 1≤li≤ri≤m1\le l_i\le r_i\le m.
  • Subtask 3 (50 points): 2≤n≤5002\le n\le 500; 1≤m≤100001\le m\le 10000; 1≤k≤200001\le k\le 20000; 1≤xi,yi≤n1\le x_i,y_i\le n, xi≠yix_i\ne y_i; parallel edges are allowed; 1≤li≤ri≤m1\le l_i\le r_i\le m.

Examples

Input

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

Output

4
5
6
3
4
2

Explanation

Each answer is computed from the original network with exactly the query's indexed cable interval disabled. The values 4,5,6,3,4,24,5,6,3,4,2 refer to six independent experiments. Cables disabled by one query are restored before the next query begins.