Go: package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"container/list"
	"bytes"
)

const MaxNode int = 1e5

var graph [][] int
var color []int
var N int

func bfs(u, c int) {
	l := list.New()
	l.PushBack(u)
	for ;l.Len()!=0; {
		u = l.Front().Value.(int)
		color[u] = c
		l.Remove(l.Front())
		for i:=0; i<len(graph[u]); i++ {
			if color[graph[u][i]] == 0 {
				l.PushBack(graph[u][i])
			}
		}
	}
}

func assignColor() {
	id := 1
	for u:=0; u<N; u++ {
		if color[u] == 0 {
			bfs(u, id)
			id ++
		}
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}


func buildGraph(scanner *bufio.Scanner) {
	N = nextInt(scanner)
	color = make([]int, N)
	graph = make([][]int, N)
	m := nextInt(scanner)
	for i:=0; i<m; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		graph[n1] = append(graph[n1], n2)
		graph[n2] = append(graph[n2], n1)
	}
}

func answer(scanner *bufio.Scanner) {
	var outBuffer bytes.Buffer
	cnt := nextInt(scanner)
	for i:=0; i<cnt; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		if color[n1] == color[n2] {
			outBuffer.WriteString("yes\n")
		} else {
			outBuffer.WriteString("no\n")
		}
	}
	fmt.Printf("%s", outBuffer.String())
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buildGraph(scanner)
	assignColor()
	answer(scanner)
}

Python: # -*- coding: utf-8 -*-
from collections import deque


class LinkedList:
    def __init__(self, n):
        self.n = n
        self.ll = [[] for _ in range(self.n)]

    def link(self, u, v, direction=False):
        self.ll[u].append(v)
        if not direction:
            self.ll[v].append(u)

    def check(self, start, goal):
        states = [0] * self.n
        queue = deque([start])

        while queue:
            u = queue.popleft()
            if states[u]:
                continue
            else:
                states[u] = 1

            for v in self.ll[u]:
                if v == goal:
                    print('yes')
                    return
                queue.append(v)

        else:
            print('no')
            return


if __name__ == '__main__':
    n, m = map(int, input().split())
    ll = LinkedList(n)

    for _ in range(m):
        u, v = map(int, input().split())
        ll.link(u, v)

    q = int(input())
    for _ in range(q):
        u, v = map(int, input().split())
        ll.check(u, v)

Python: # -*- coding: utf-8 -*-
from collections import deque


class LinkedList:
    def __init__(self, n):
        self.n = n
        self.ll = [[] for _ in range(self.n)]

    def link(self, u, v, direction=False):
        self.ll[u].append(v)
        if not direction:
            self.ll[v].append(u)

    def check(self, start, goal):
        states = [0] * self.n
        queue = deque([start])

        while queue:
            u = queue.popleft()
            # if states[u]:
            #     continue
            # else:
            states[u] = 1

            for v in self.ll[u]:
                if v == goal:
                    print('yes')
                    return

                if states[v]:
                    continue
                else:
                    queue.append(v)

        else:
            print('no')
            return


if __name__ == '__main__':
    n, m = map(int, input().split())
    ll = LinkedList(n)

    for _ in range(m):
        u, v = map(int, input().split())
        ll.link(u, v)

    q = int(input())
    for _ in range(q):
        u, v = map(int, input().split())
        ll.check(u, v)

Python: def MAIN():
    n, m = map(int, input().split())
    li = [i for i in range(n)]
    for _ in range(m):
        x, y = map(int, input().split())
        if li[x] > li[y]:
            li[x] = li[y]
        else:
            li[y] = li[x]
    for _ in range(int(input())):
        x, y = map(int, input().split())
        print("yes" if li[x] == li[y] else "no")
MAIN()

Python: def DFS(s):
    flag[s] = 1
    group[s] = j
    for i in range(n):
        if G[s][i] == 1 and flag[i] == 0:
            DFS(i)

n, m = map(int, raw_input().split())
G = [[0 for i in range(n)] for j in range(n)]

for i in range(m):
    s, t = map(int, raw_input().split())
    G[s][t] = 1
    G[t][s] = 1

group = [[] for i in range(n)]
flag = [0 for i in range(n)]
j = 0
for i in range(n):
    if flag[i] == 0:
        DFS(i)
        j += 1

q = int(raw_input())
ans = ["no" for i in range(q)]
for i in range(q):
    s, t = map(int, raw_input().split())
    if group[s] == group[t]:
        ans[i] = "yes"
for i in range(q):
    print(ans[i])

Python: from sys import stdin

def read_graph(n, k):
    A = [ [] for _ in range(n) ]
    for _ in range(k):
        line = stdin.readline().strip().split()
        A[int(line[0])].append(int(line[1]))
        A[int(line[1])].append(int(line[0]))
    return A

def dfs_ctrl(n, A, s, t):
    color = ["WHITE"] * n
    d = [0] * n
    f = [0] * n
    time = 0

    def dfs(u):
        nonlocal color
        nonlocal time
        color[u] = "GRAY"
        time += 1
        d[u] = time

        for v in A[u]:
            if color[v] == "WHITE":
                dfs(v)
        color[u] = "BLACK"
        time += 1
        f[u] = time

    dfs(s)
    if d[t] != 0:
        print("yes")
    else:
        print("no")

def read_and_print_connection(m):
    for _ in range(m):
        line = stdin.readline().strip().split()
        s = int(line[0])
        t = int(line[1])
        dfs_ctrl(n, A, s, t)

n, k = [int(i) for i in input().split()]
A = read_graph(n, k)
m = int(input())
read_and_print_connection(m)

Python: n, m = map(int, input().split())
g = [set() for _ in range(n)]

while m:
    s, t = map(int, input().split())
    g[s].add(t)
    g[t].add(s)
    m -= 1


def find(s, t):
    global g
    visited = [0] * n

    def dfs(i):
        visited[i] = 1
        if i == t:
            return True
        for f in g[i]:
            if not visited[f]:
                if dfs(f):
                    return True
        return False

    return dfs(s)


q = int(input())
while q:
    s, t = map(int, input().split())
    print('yes' if find(s, t) else 'no')
    q -= 1

Python: n, m = map(int, input().split())
friend = [[] for i in range(n)]
for i in range(m):
    s, t = map(int, input().split())
    friend[s].append(t)
    friend[t].append(s)

group = [-1 for i in range(n)]
for u in range(n):
    if group[u] == -1:
        group[u] = u
        q = [u]
        while q:
            u1 = q.pop()
            for u2 in friend[u1]:
                if group[u2] == -1:
                    group[u2] = u
                    q.append(u2)

q = int(input())
for i in range(q):
    s, t = map(int, input().split())
    if group[s] == group[t]:
        print('yes')
    else:
        print('no')

Python: def tan(G,st,fi):
    global flag
    for i in range(len(G)):
        if G[st][i] == 1:
            print(st,fi,i)
            if i == fi:
                flag = 1
            tan(G,i,fi)
    
n,m = list(map(int, input().split()))
G = [[0 for i in range(n)] for j in range(n)]

for i in range(m):
    S = list(map(int, input().split()))
    G[S[0]][S[1]] = 1

q = int(input())
ans = []
for i in range(q):
    Q = list(map(int, input().split()))
    st = Q[0]
    fi = Q[1]
    flag = 0
    tan(G,Q[0],Q[1])
    if flag==1:
        ans.append("yes")
    else:
        ans.append("no")
        
for i in range(q):
    print(ans[i])