Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
from collections import deque
from itertools import combinations

N, M = tuple(map(int, input().split()))

adj = [[0]*(N) for _ in range(N)]

for _ in range(M):
    inp = tuple(map(int, input().split()))
    adj[inp[0]][inp[1]] = 1
    adj[inp[1]][inp[0]] = 1

Q = int(input())

for _ in range(Q):
    found = "no"
    start, target = tuple(map(int, input().split()))
    que = deque([start])
    searched = set([start])
    while len(que):
        q = que.pop()
        next_ = [i for i, a in enumerate(adj[q]) if (a==1) and not (i in searched)]
        searched.update(next_)
        que.extendleft(next_)
        if target in next_:
            found = "yes"
            break
    for a, b in combinations(searched, 2):
        adj[a][b] = 1
        adj[b][a] = 1
    print(found)

Fixed submission:
# -*- coding: utf-8 -*-
from collections import deque

N, M = tuple(map(int, input().split()))
adj = [[] for n in range(N)]

for _ in range(M):
    inp = tuple(map(int, input().split()))
    adj[inp[0]].append(inp[1])
    adj[inp[1]].append(inp[0])

unsearched = set(range(N))
colors = {n:-1 for n in range(N)}
color = -1

for n in range(N):
    if colors[n] != -1:
        continue
    color += 1
    start = n
    
    que = deque([start])
    while len(que):
        q = que.pop()
        next_ = [i for i in adj[q] if colors[i] == -1]
        que.extendleft(next_)
        colors[q] = color
    
Q = int(input())

for i in range(Q):
    question = tuple(map(int, input().split()))
    if colors[question[0]] == colors[question[1]]:
        print("yes")        
    else:
        print("no")

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
from collections import deque

N, M = tuple(map(int, input().split()))
adj = [[] for n in range(N)]

for _ in range(M):
    inp = tuple(map(int, input().split()))
    adj[inp[0]].append(inp[1])
    adj[inp[1]].append(inp[0])

unsearched = set(range(N))
colors = {n:-1 for n in range(N)}
color = -1
while len(unsearched):
    color += 1
    start = min(unsearched)
    unsearched.remove(start)
    searched = set([start])
    
    que = deque([start])
    while len(que):
        q = que.pop()
        next_ = [i for i in adj[q] if  not (i in searched)]
        searched.update(next_)
        unsearched = unsearched.difference(searched)
        que.extendleft(next_)
    for n in searched:
        colors[n] = color
    
Q = int(input())

for i in range(Q):
    question = tuple(map(int, input().split()))
    if colors[question[0]] == colors[question[1]]:
        print("yes")        
    else:
        print("no")

Fixed submission:
# -*- coding: utf-8 -*-
from collections import deque

N, M = tuple(map(int, input().split()))
adj = [[] for n in range(N)]

for _ in range(M):
    inp = tuple(map(int, input().split()))
    adj[inp[0]].append(inp[1])
    adj[inp[1]].append(inp[0])

unsearched = set(range(N))
colors = {n:-1 for n in range(N)}
color = -1

for n in range(N):
    if colors[n] != -1:
        continue
    color += 1
    start = n
    
    que = deque([start])
    while len(que):
        q = que.pop()
        next_ = [i for i in adj[q] if colors[i] == -1]
        que.extendleft(next_)
        colors[q] = color
    
Q = int(input())

for i in range(Q):
    question = tuple(map(int, input().split()))
    if colors[question[0]] == colors[question[1]]:
        print("yes")        
    else:
        print("no")