Problem:
Strongly Connected Components

A direced graph is strongly connected if every two nodes are reachable from each other. In a strongly connected component of a directed graph, every two nodes of the component are mutually reachable.

Input

A directed graph G(V, E) and a sequence of queries where each query contains a pair of nodes u and v.

|V| |E|
s0 t0
s1 t1
:
s|E|-1 t|E|-1
Q
u0 v0
u1 v1
:
uQ-1 vQ-1

|V| is the number of nodes and |E| is the number of edges in the graph. The graph nodes are named with the numbers 0, 1,..., |V|-1 respectively.

si and ti represent source and target nodes of i-th edge (directed).

ui and vi represent a pair of nodes given as the i-th query.

Output

For each query, pinrt "1" if the given nodes belong to the same strongly connected component, "0" otherwise.

Constraints

1 ≤ |V| ≤ 10,000

0 ≤ |E| ≤ 30,000

1 ≤ Q ≤ 100,000

Sample Input 1

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

Sample Output 1

1
0
1
1

Solution in C++11 (judge verdict: Accepted)

#define _CRT_SECURE_NO_WARNINGS
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i,a,b) for(ll i=a; i<ll(b); i++)
#define rep(i,b) loop(i,0,b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template<class T> istream& operator>>(istream& is, vector<T>& v);
template<class T> ostream& operator<<(ostream& os, vector<T> const& v);
template<int n, class...T> typename enable_if<(n>=sizeof...(T))>::type _ot(ostream&, const tuple<T...>&){}
template<int n, class...T> typename enable_if<(n< sizeof...(T))>::type _ot(ostream& os, const tuple<T...>& t){ os << (n==0?"":" ") << get<n>(t); _ot<n+1>(os, t); }
template<class...T> ostream& operator<<(ostream& os, const tuple<T...>& t){ _ot<0>(os, t); return os; }
template<int n, class...T> typename enable_if<(n>=sizeof...(T))>::type _it(istream&, tuple<T...>&){}
template<int n, class...T> typename enable_if<(n< sizeof...(T))>::type _it(istream& is, tuple<T...>& t){ is >> get<n>(t); _it<n+1>(is, t); }
template<class...T> istream& operator>>(istream& is, tuple<T...>& t){ _it<0>(is, t); return is; }
template<class T> istream& operator>>(istream& is, vector<T>& v){ rep(i,v.size()) is >> v[i]; return is; }
template<class T> ostream& operator<<(ostream& os, vector<T> const& v){ rep(i,v.size()) os << v[i] << (i+1==(int)v.size()?"":" "); return os; }
#ifdef DEBUG
#define dump(...) (cerr<<#__VA_ARGS__<<" = "<<mt(__VA_ARGS__)<<" ["<<__LINE__<<"]"<<endl)
#else
#define dump(...)
#endif

typedef int Weight;
struct Edge {
    int src, dst;
    Weight weight;
    Edge(int src, int dst, Weight weight) :
        src(src), dst(dst), weight(weight) { }
};
bool operator < (const Edge &e, const Edge &f) {
    return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
        e.src != f.src ? e.src < f.src : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;
/*
  Kosaraju 法で強連結成分分解(SCC)
  gは隣接リスト表現のグラフ, sccs の各要素に連結成分が入る
*/
typedef vector<int> vi;
int const MAX_N = 100000;
void visit(Graph const & g, int v, vector<vi> & scc, stack<int> & S,
           int inS[MAX_N], int low[MAX_N], int num[MAX_N], int & time) {
    low[v] = num[v] = ++time;
    S.push(v);
    inS[v] = true;
    for(auto const & e : g[v]){
        int w = e.dst;
        if(num[w] == 0) {
            visit(g, w, scc, S, inS, low, num, time);
            low[v] = min(low[v], low[w]);
        } else if(inS[w])
            low[v] = min(low[v], num[w]);
    }
    if(low[v] == num[v]) {
        scc.push_back(vi());
        while(1) {
            int w = S.top(); S.pop(); inS[w] = false;
            scc.back().push_back(w);
            if(v == w) break;
        }
    }
}

vector<vi> scc(Graph const & g) {
    vector<vi> sccs;
    const int n = g.size();
    static int num[MAX_N], low[MAX_N], inS[MAX_N];
    rep(i,n) num[i] = low[i] = inS[i] = 0;
    stack<int> S;
    int time = 0;
    rep(u, n) if (num[u] == 0) visit(g, u, sccs, S, inS, low, num, time);
    return sccs;
}

int main(){
    int n,m;
    cin >> n >> m;
    Graph g(n);
    rep(i,m){
        int s,t;
        cin >> s >> t;
        g[s].eb(s,t,0);
    }
    auto sc = scc(g);
    vi v(n);
    rep(i,sc.size())rep(j,sc[i].size()) v[sc[i][j]] = i;
    int q;
    cin >> q;
    rep(i,q){
        int a,b; cin >> a >> b;
        cout << (v[a]==v[b]) << endl;
    }
}