#include <bits/stdc++.h>
using namespace std;

// Template Headers {{{
using pii = pair<int, int>; using vpii = vector<pii>;
using vi = vector<int>; using vvi = vector<vi>;
using ll = long long; using vll = vector<long long>;
template <class T> using min_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> istream &operator>>(istream &, vector<T> &);
template <class T> ostream &operator<<(ostream &, const vector<T> &);
template <class T, class U> istream &operator>>(istream &, pair<T, U> &);
template <class T, class U> ostream &operator<<(ostream &, const pair<T, U> &);
template <class T> struct Inf { static constexpr T inf() { return std::numeric_limits<T>::has_infinity ? std::numeric_limits<T>::infinity() : std::numeric_limits<T>::max(); } };
template <> struct Inf<int> { static constexpr int inf() { return 0x3f3f3f3f; } };
template <> struct Inf<long long> { static constexpr long long inf() { return 0x3f3f3f3f3f3f3f3fLL; } };
constexpr int INF = Inf<int>::inf(); constexpr ll BINF = Inf<ll>::inf();
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// }}}

// Graph Template {{{
template <class T = void, bool DIRECTED = false>
class Graph : public vector<vector<pair<int, T>>> {
	private:
		int n, m;
	public:
		Graph(int n, int m = 0) : vector<vector<pair<int, T>>>(n), n(n), m(m) {}

        template <class U, bool V>
		friend istream &operator>>(istream &is, Graph<U, V> &g);

		template<class ...Args>
		void addDiEdge(int u, int v, Args &&...args) { (*this)[u].emplace_back(v, std::forward<Args>(args)...); }

		template<class ...Args>
		void addEdge(int u, int v, Args &&...args) {
			addDiEdge(u, v, std::forward<Args>(args)...);
			if (!DIRECTED) addDiEdge(v, u, std::forward<Args>(args)...);
		}

		vector<int> postOrder() const {
			auto &g = *this;
			vi vis(n);
			vi ord;
			function<void(int)> dfs = [&] (int u) {
				vis[u] = true;
				for (auto p : g[u]) if (!vis[p.first]) dfs(p.first);
				ord.push_back(u);
			};
			for (int i = 0; i < n; i++) if (!vis[i]) dfs(i);
			return ord;
		}

		vector<int> topSort() const {
			vi pord = postOrder();
			reverse(pord.begin(), pord.end());
			return pord;
		}

		vector<int> sccColoring() const {
			auto &g = *this;
			vector<vector<int>> gr(n);
			for (int u = 0; u < n; u++) for (auto p : g[u]) gr[p.first].push_back(u);
			vector<int> c(n, -1);
			int ccnt = 0;
			function<void(int)> dfs = [&] (int u) {
				for (int v : gr[u]) if (c[v] == -1) {
					c[v] = c[u];
					dfs(v);
				}
			};
			for (int u : topSort()) if (c[u] == -1) c[u] = ccnt++, dfs(u);
			return c;
		}

};
template <class T> using DiGraph = Graph<T, true>;
template <class U, bool D>
istream &operator>>(istream &is, Graph<U, D> &g) {
	for (int i=0;i<g.m;i++) {
		int u, v; U l; is >> u >> v >> l; u--, v--;
		g.addEdge(u, v, l);
	}
	return is;
}
template <class U, bool D>
istream &operator>>(istream &is, Graph<void, D> &g) {
	for (int i=0;i<g.m;i++) {
		int u, v; is >> u >> v; u--, v--;
		g.addEdge(u, v);
	}
	return is;
}

// }}}

// Fake Pair Template {{{
namespace std {
    template <class T>
    class pair<T, void> {
    	public:
    		typedef T first_type;
    		T first;
    
    		constexpr pair(const T &x) : first(x) {}
    		constexpr pair(const T &x, const T &) : pair(x) {}
    		constexpr pair() : first() {}
    		constexpr pair(const pair &p) = default;
    		constexpr pair(pair &&p) = default;
    
    		template <class U1>
    		constexpr pair(U1 &&x) : first(std::forward<U1>(x)) {}
    		template <class U1, class U2>
    		constexpr pair(U1 &&x, U2 &&y) : pair(std::forward<U1>(x)) {}
    		template <class U1, class U2>
    		constexpr pair(const pair<U1, U2> &p) : first(p.first) {}
    		template <class U1, class U2>
    		constexpr pair(pair<U1, U2> &&p) : first(std::forward<U1>(p.first)) {}
    
    		constexpr pair &operator=(const pair &other) {
    			first = other.first;
    			return *this;
    		}
    		constexpr pair &operator=(const pair &&other) {
    			first = std::forward<T>(other.first);
    			return *this;
    		}
    		template <class U1, class U2>
    		constexpr pair &operator=(const pair<U1, U2> &other) {
    			first = other.first;
    			return *this;
    		}
    		template <class U1, class U2>
    		constexpr pair &operator=(pair<U1, U2> &&other) {
    			first = std::forward<U1>(other.first);
    			return *this;
    		}
    
    		void swap(pair &p) { std::swap(first, p.first); }
    
    		operator T() const { return first; }
    
    };

	template <class U1>
	void swap(pair<U1, void> &lhs, pair<U1, void> &rhs) { lhs.swap(rhs); }

	template <class T>
	class tuple_element<1, pair<T, void>> { public: typedef T type; };

	template <size_t I, class T>
	T &get(pair<T, void> &p) { return p.first; }
	template <size_t I, class T>
	T &&get(pair<T, void> &&p) { return std::forward<T>(p.first); }
	template <size_t I, class T>
	const T &get(const pair<T, void> &p) { return p.first; }
	template <size_t I, class T>
	const T &&get(const pair<T, void> &&p) { return std::forward<T>(p.first); }
}

// }}}

int solve() {
	int n, m; cin >> n >> m;
	vpii ge(m); cin >> ge;
	DiGraph<void> g(n, m);
	for (auto p : ge) g.addEdge(p.first, p.second);
	vi clr = g.sccColoring();
	int q; cin >> q;
	while (q--) {
		int u, v; cin >> u >> v;
		cout << (clr[u] == clr[v]) << endl;
	}
	return 0;
}

// Template Main {{{
int main() {
	ios::sync_with_stdio(0);
	// precompute();
	// int t; cin >> t; for (int i=1;i<=t;i++)
	solve();
	// cout << "Case #" << i << ": ", solve();
	return 0;
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
	for (auto it=v.begin();it!=v.end();++it) is >> *it;
	return is;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
	for (auto it=v.begin();it!=v.end();) os << *it, os << " \n"[++it==v.end()];
	return os;
}

template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
	return is >> p.first >> p.second;
}

template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) {
	return os << p.first << ' ' << p.second;
}
// }}}

// vim: fdm=marker



Measured memory_bytes: 5096