Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX = 100000
	NIL = -1
)

var (
	n, m, q, s, t int
	G             [MAX][]int //連結リスト
	color         [MAX]int
)

func dfs(r int, c int) {
	S := list.New()
	S.PushFront(r)
	color[r] = c
	for S.Len() != 0 {
		u := S.Remove(S.Front()).(int)
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == NIL {
				color[v] = c
				S.PushFront(v)
			}
		}
	}
}

//幅優先探索で、頂点を色付け
func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = NIL
	}
	for i := 0; i < n; i++ {
		if color[i] == NIL {
			id++
			dfs(i, id)
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n, m = nextInt(), nextInt()
	for i := 0; i < m; i++ {
		s, t = nextInt(), nextInt()
		if i == 0 {
			G[s] = make([]int, 0)
			G[t] = make([]int, 0)
		}
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)

	}
	assignColor()
	q = nextInt()
	for i := 0; i < q; i++ {
		s, t = nextInt(), nextInt()
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}

}

C++: /**
 * @brief ?????£???(????§£)??¢?????´?????????????????????
 * @date  2016/03/12
 */


//****************************************
// ???????????????????????????????????????????????????
//****************************************

#include <iterator>
#include <functional>
#include <utility>
#include <algorithm>
#include <vector>
#include <iostream>



//****************************************
// ???????????§????????¢?????????????????????
//****************************************

#define GRAPH_BEGIN namespace graph {
#define GRAPH_END   }



//****************************************
// ????????????????§????
//****************************************

GRAPH_BEGIN



//****************************************
// ?§????????????????
//****************************************

using weight_t   = std::int32_t;  /**< ???(u, v)????????????w?????¨?????? */
using index_t    = std::int32_t;  /**< ??????v??????????????¨??????       */
using capacity_t = weight_t;      /**< ???(u, v)??????????????¨??????    */



namespace limits {
    enum {  // namespace graph????????£???scoped??§????????????????????????????????????enum(strongly-typed enum)??§?????????
    inf = std::numeric_limits<weight_t>::max() / 3,  /**< @brief ???????????¨???????????´???????????¨????????????     */
    nil = std::numeric_limits<index_t>::min() / 3,   /**< @brief ?????????????????¨???????????´???????????¨???????????? */
};
}



/**
 * @brief  ????????????????§???????(scoped enum)
 * @detail (u, v) ??? E??§??????u??????????????°??????v?????°????????§??????
 *         ?????????????????????????????£??\????????¨?????????????????????????????§??????
 *         ??°???????????????????????£??\???????????¨???????????????????????????????????¢??????????????¨???????????????????¢?????????????
 */
enum struct vcolor : std::int32_t {
    white,  /**< ??????????????? */
    black,  /**< ??¢???????????? */
    gray,   /**< ??¢???????????? */
};

/**
 * @brief ??°????????¨?????????(??????) 
 */
struct vertex {
    union {
        weight_t d;       /**< ?§????s??????????????¢  */
        weight_t key;     /**< Prim?????¢?????´???????????????????????¨????±?????????????????????¨????????¶?????? */
    };
    index_t pi;           /**< ????????????(?????????) */
    union {
        vcolor color;     /**< ????????????        */
        bool  visited;    /**< ????????????????     */
    };
    // weight_t f;           /**< ??????????????°(DFS?????????????????????????????????????????¨??????????????????)     */
    vertex() : d(0), pi(0), color(vcolor::white)/*, f(0)*/ {}
};

/**
 * @brief ??°????????¨??¨??????(???)
 * @note  G = (V, E)???????????¢??°w???????????????????????°????????¨????????¨???
 *        ???(u, v) ??? E????????????w(u, v)??¨??¨?????????
 */
struct edge {
    index_t  src;   /**< ???????§????u */
    index_t  dst;   /**< ????????????v */
    union {
        weight_t w;     /**< ???(u, v)????????????(?????????) */
        capacity_t c;   /**< ???(u, v)????????? */
    };
    edge() = default;
    edge(index_t src, index_t dst) : src(src), dst(dst), w(1) {}
    edge(index_t src, index_t dst, weight_t w) : src(src), dst(dst), w(w) {}

    bool operator < (const edge& e) const { return w < e.w; }
    bool operator > (const edge& e) const { return w > e.w; }
};



using edges_t    = std::vector<edge>;      /**< ??°??????G=(V, E)????????????E   */
using vertices_t = std::vector<vertex>;    /**< ??°??????G=(V, E)???????????????V */
using array_t    = std::vector<weight_t>;  /**< ??????w?????????  */
using indices_t  = std::vector<index_t>;   /**< ????????????????????? */
using matrix_t   = std::vector<array_t>;   /**< ??°??????G?????£??\????????¨???(???????????¨????????¨???) */
using graph_t    = std::vector<edges_t>;   /**< ??°??????G?????£??\???????????¨???(???????????????????????¨??????) */



//****************************************
// ??¢??°?????????
//****************************************

/**
 * @brief  ??????????????????????????????????????????
 *
 * @note   ?????????????????°??????G = (V, E)??????????????????????????????(topological sort)??????????????????????????¢????????§???
 *         G??????(u, v)?????????????????°??????????????¢????????§u???v?????????????????????????????§??????. (??°?????????????????????????????°????????????????????¢??????????????¨?????????)
 *         ??°??????????????????????????????????????????????????????????????????????????????????????????????°´???????????§???????????????????????§??????
 * @param  const graph& G ?????????????????°??????
 * @return ??¢??????????????????
 */
array_t tsort(const graph_t& G)
{
    std::int32_t n = G.size();
    std::vector<vcolor> color(n, vcolor::white);
    array_t lst(n);

    // ?????????????¨???????
    std::function<bool(index_t)> dfs_visit = [&](index_t u) {
        color[u] = vcolor::gray;    // u?????°???????????????
        for (auto&& e : G[u]) {      // v??¨??£??\???????????????w????????????
            index_t w = e.dst;
            if (color[w] == vcolor::white
             && !dfs_visit(w)) { return false; }  // w?????????????????°??????w????????????
        }
        color[u] = vcolor::black;   // u?????????????????????
        lst.push_back(u);           // ????????????????°?????????\??????
        return true;
    };


    // ?????????v???????????????v.f????¨???????????????????DFS(G)???????????????
    for (index_t v = 0; v < n; v++) {
        if (color[v] == vcolor::white && !dfs_visit(v)) { return {}; };
    }
    reverse(lst.begin(), lst.end());  // ??????????????????????????????????????????????????§reverse?????????
    return lst;     // ???????????????????????????
}




/**
 * @brief  ?????£?????????(????§£)??¢?????´?????????
 *
 * @note   ??°??????????????£????????????????§£??????????????????????????£??????????????§??¢?????´????????????????????????
 *         ?????????????§£?????????????????£????§??????????????????£??????????????????????????????????§£?????????
 *
 * @note   ??°??????G = (V, E)????????£?????????????±?????????¢?????´????????????G?????¢????????¨??????
 *         G?????¢????????°??????G^T = (V, E^T), E^T = { (u, v) : (v, u) ??? E }??§??????
 *         ???????????????E^T???G??????????????????????????????????????§??????. G?????£??\?????????????????????????????¨???
 *         G^T?????(V + E)????????§????????§??????. ????????±????????¨??????G??¨G^T????????£?????????????????£??????????????????
 *         G?????§u??¨v??????????????°???????????§???????????¨??¨???G^T?????§???????????????????????°???????????§???????????¨??¨???????????§??????
 *
 * @note   ??\???????????¢??????(?????????????????(V + E)??????)??¢?????´?????????????????±???????????¢?´¢???2???????????????G?????§???
 *         ?????????G^T?????§?????????????????¨????????£???????????°??????G = (V, E)????????£?????????????±???????
 *
 *         STRONGLY-CONNECTED-COMPONENTS(G)
 *         1 DFS(G)???????????????????????????u????????????????????????u.f????¨??????????
 *         2 G^T????¨??????????
 *         3 DFS(G^T)?????????????????????DFS?????????????????§???(?¬¬1?????§?¨??????????)u.f???????????§???????????¢?´¢??????
 *         4 ?¬¬3?????§??????????????±??????????£?????????¨????????????????????????????????¢??????????????£???????????¨??????????????????
 *
 * @param  const graph_t& G ??°??????G
 * @return components[v] ??????v?????????????????£????????????????????¨?????????????????????
 */
indices_t scc(const graph_t& G)
{
    index_t n = G.size(); vertices_t vs(n);
    indices_t components(n, -1);
    std::vector<vcolor> color(n, vcolor::white);
    graph_t GT(n);

    // ?????°???????????????????¨???????
    std::function<void(index_t, index_t)> dfs_visit = [&](index_t u, index_t k) {
        color[u] = vcolor::gray;
        components[u] = k;
        for (auto&& e : GT[u]) {
            index_t w = e.dst;
            if (color[w] == vcolor::white) {
                dfs_visit(w, k);
            }
        }
        color[u] = vcolor::black;
    };


    // DFS(G)???????????????????????????u????????????????????????u.f????¨??????????
    array_t tlst = tsort(G);

    // G^T????¨??????????
    for (auto&& es : G) {
        for (auto&& e : es) {
            GT[e.dst].emplace_back(e.dst, e.src);
        }
    }

    // DFS(G^T)???????????????
    index_t k = 0;
    for (auto&& u : tlst) { // ????????°??????????????????????????????????????????????????????????????§?¨??????????
        if (color[u] == vcolor::white) {
            dfs_visit(u, k++);
        }
    }

    // ?????¢??????????????£??????????????????
    return components;
}



//****************************************
// ?????????????????????
//****************************************

GRAPH_END



//****************************************
// ??¨?????????????????????
//****************************************

int main()
{
    using namespace std;
    using namespace graph;
    
    graph_t G;
    int s, t, m, n, q;
    cin >> n >> m;
    G.resize(n);
    for (index_t i = 0; i < m; i++) {
        cin >> s >> t;
        G[s].emplace_back(s, t);
        G[t].emplace_back(t, s);
    }

    auto _scc = scc(G);

    cin >> q;

    for (int i = 0; i < q; i++) {
        cin >> s >> t;
        cout << ((_scc[s] == _scc[t]) ? "yes" : "no") << endl;
    }

    return 0;
}