//https://www.hackerrank.com/contests/hack-it-to-win-it-paypal/challenges/q4-traveling-is-fun/problem
#include <bits/stdc++.h>
using namespace std;
void dfs (int s,vector < vector <int> > &adj, vector <int> &cc, int m) {
cc[s]= m;
for (int i=0;i<adj[s].size();i++)
if (cc[adj[s][i]]==0)
dfs(adj[s][i], adj,cc,m);
}
int main() {
int n,g,q;
cin>>n;
cin>>g;
cin>>q;
int oc[q], dc[q];
for (int i=0;i<q;i++)
cin>>oc[i];
cin>>q;
for (int i=0;i<q;i++)
cin>>dc[i];
if (g==n) {
for (int i=0;i<q;i++)
cout<< 0<< "\n";
return 0;
}
if (g==0) {
for (int i=0;i<q;i++)
cout<< 1<< "\n";
return 0;
}
vector < vector <int> > adj(n+1);//construct the adjacency list
for (int i=g+1; i<=n;i++) {//we have to check all the multiples of i>g
for (int j=2;j*i<=n;j++) {
int a= j*i;
adj[a].push_back(i);
adj[i].push_back(a);
}
}
vector <int> cc(n+1,0);//vector to store the indices of connected components
int m=1;
for (int i=1;i<=n;i++) {
if (cc[i]== 0) {
dfs(i,adj, cc,m);
m++;
}
}
for (int i=0;i<q;i++) {
if (cc[oc[i]]== cc[dc[i]])
cout<< 1<< endl;
else
cout<< 0 << endl;
}
return 0;
}