#include<stdio.h>
#include<string.h>
#define N 8
int board[N][N];
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_13_A -- backtracking
//chech previous part but there are fixed point before that
//so check all of them
bool safe(int row, int col){
	//chec the column
	if(board[row][col]==1) return true;
	for(int i = 0; i<N; i++){
		if(board[row][i] == 1) return false;
	}
	//check the row
	for(int i = 0; i<N; i++){
		if(board[i][col] == 1) return false;
	}
	//check the diagional
	int i = row, j = col;
	while(i>=0 && j>=0){
		if(board[i][j]==1) return false;
		i--; j--;
	}
	i = row, j = col;
	while(i<N && j<N){
		if(board[i][j]==1) return false;
		i++; j++;
	}
	i = row, j = col;
	while(i>=0 && j<N){
		if(board[i][j]==1) return false;
		i--; j++;
	}
	i = row, j = col;
	while(i<N && j>=0){
		if(board[i][j]==1) return false;
		i++; j--;
	}
	//printf("check\n");
	return true;
}

//for each row([0,7]),check the column
bool solve_board(int col){
	//all chess placed
	if(col>=N) return true;
	for(int i = 0; i<N; i++){
		if(board[i][col]==1){
			if(solve_board(col+1)) return true;
		}
		else {
		if(safe(i,col)){
			//printf("%d %d\n",i,col);
			board[i][col] = 1;
			//recur--key points
			if(solve_board(col+1)) return true; 
			board[i][col] = 0;
		}}
		
	}
	return false;
}

int main(){
	int n = 0;
	memset(board,0,sizeof(board));
	scanf("%d",&n);
	for(int i = 0; i<n; i++){
		int a, b;
		scanf("%d %d",&a,&b);
		board[a][b] = 1;
	}
	//for each column, check the row
	solve_board(0);
	for(int i = 0; i<N; i++){
		for(int j = 0; j< N; j++){
			if(board[i][j]==0) printf(".");
			else printf("Q");
		}
		printf("\n");
	}
}

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

#define int long long

int V, E, Q, a, b;
vector<int> G[100001];
vector<int> rG[100001];
vector<int> vs;
bool used[100001];
int cmp[100001];

auto dfs(int v) -> void {
    used[v] = true;
    for (int i = 0; i < G[v].size(); ++i)
        if (!used[G[v][i]])
            dfs(G[v][i]);

    vs.push_back(v);
}

auto rdfs(int v, int k) -> void{
    used[v] = true;
    cmp[v] = k;
    for (int i = 0; i < rG[v].size(); i++)
        if (!used[rG[v][i]])
            rdfs(rG[v][i], k);
}

/**
 * ?????£?????????????§£
 * @return ?????£???????????????????????????????????????????????§???
 */
auto scc() -> int {
    fill(used, used + V, false);
    vs.clear();
    for (int v = 0; v < V; v++) {
        if (!used[v]) dfs(v);
    }
    fill(used, used + V, false);
    int k = 0;
    for (int i = vs.size() - 1; i >= 0; i--)
        if (!used[vs[i]])
            rdfs(vs[i], k++);

    return k;
}

auto main() -> signed {
    cin >> V >> E;
    for (int i = 0; i < E; i++) {
        cin >> a >> b;
        G[a].push_back(b);
        rG[b].push_back(a);
    }
    int k = scc();

    cin >> Q;
    for (int i = 0; i < Q; i++) {
        cin >> a >> b;
        cout << ((cmp[a] == cmp[b]) ? 1 : 0) << endl;
    }
    return 0;
}