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

#DSU0000042. Đồ thị tốt (Good Graph)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đồ thị tốt (Good Graph)

Good Graph

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an undirected graph GG with nn vertices and mm edges. Multiple edges and self-loops are allowed. There are also kk forbidden pairs (xi,yi)(x_i,y_i): the two vertices of each such pair must never belong to the same connected component. The initial graph is guaranteed to satisfy every restriction.

You must answer qq queries. In query jj, consider adding one undirected edge joining pjp_j and qjq_j to the original graph. Determine whether the resulting graph still satisfies all forbidden pairs. If even one forbidden pair becomes connected, the answer is No.

Queries are independent: the tentative edge from one query is not retained when processing another query.

Input

Read n,mn,m and mm edges; then kk and kk distinct unordered forbidden pairs x,yx,y with x≠yx\ne y; then qq and qq query pairs p,qp,q with p≠qp\ne q.

Output

For each query print Yes if the graph remains good, otherwise No.

Subtasks

  • Subtask 1 (20 points): 2≤n≤252\le n\le25, 0≤m≤3000\le m\le300, 1≤k,q≤3001\le k,q\le300; 1≤u,v,xi,yi,pj,qj≤n1\le u,v,x_i,y_i,p_j,q_j\le n; xi≠yix_i\ne y_i, pj≠qjp_j\ne q_j; forbidden pairs are distinct; the initial graph respects all forbidden pairs. Self-loops and multiple edges are allowed in the initial graph.
  • Subtask 2 (30 points): 2≤n≤20002\le n\le2000, 0≤m≤2⋅1050\le m\le2\cdot10^5, 1≤k,q≤2⋅1051\le k,q\le2\cdot10^5; 1≤u,v,xi,yi,pj,qj≤n1\le u,v,x_i,y_i,p_j,q_j\le n; xi≠yix_i\ne y_i, pj≠qjp_j\ne q_j; forbidden pairs are distinct; the initial graph respects all forbidden pairs. Self-loops and multiple edges are allowed in the initial graph.
  • Subtask 3 (50 points): 2≤n≤2⋅1052\le n\le2\cdot10^5, 0≤m≤2⋅1050\le m\le2\cdot10^5, 1≤k,q≤2⋅1051\le k,q\le2\cdot10^5; 1≤u,v,xi,yi,pj,qj≤n1\le u,v,x_i,y_i,p_j,q_j\le n; xi≠yix_i\ne y_i, pj≠qjp_j\ne q_j; forbidden pairs are distinct; the initial graph respects all forbidden pairs. Self-loops and multiple edges are allowed in the initial graph.

Examples

Input

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

Output

No
No
Yes
Yes

Explanation

The initial connected components are {1,2,3}\{1,2,3\}, {4,5}\{4,5\}, and {6}\{6\}. Adding (2,5)(2,5) brings the forbidden pair (1,5)(1,5) into one component; adding (2,6)(2,6) violates the forbidden pair (2,6)(2,6). Neither (5,6)(5,6) nor (5,4)(5,4) violates a restriction. Each query is evaluated independently on the initial graph.