#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
#define ln '\n'
constexpr long long MOD = 1000000007LL;
//constexpr long long MOD = 998244353LL;
typedef long long ll;
typedef unsigned long long ull; 
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true;} return false; }
template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true;} return false; }
///////////////////////////////////////////////////////////////////////////////////////////////////

template<class C>
struct SuffixAutomaton{
	struct State{
		int len,link;
		map<C,int>nex;
		State(int len,int link):len(len),link(link){}
	};

	vector<State>st;
	int last;

	SuffixAutomaton():last(0){
		st.emplace_back(0,-1);
	}

	int process(C c,int cur=-1){
		if(cur==-1){
			cur=last;
		}

		if(st[cur].nex.count(c)){
			int q=st[cur].nex[c];
			if(st[q].len==st[cur].len+1){
				return last=q;
			}
			int clone=st.size();
			st.push_back(st[q]);
			st[clone].len=st[q].len+1;
			st[q].link=clone;
			while(cur!=-1&&st[cur].nex[c]==q){
				st[cur].nex[c]=clone;
				cur=st[cur].link;
			}
			return last=clone;
		}

		int nex=st.size();
		st.emplace_back(st[cur].len+1,0);
		int p=cur;
		while(p!=-1&&!st[p].nex.count(c)){
			st[p].nex[c]=nex;
			p=st[p].link;
		}
		if(p==-1){
			return last=nex;
		}

		int q=st[p].nex[c];
		if(st[p].len+1==st[q].len){
			st[nex].link=q;	
		}
		else{
			int clone=st.size();
			st.push_back(st[q]);
			st[clone].len=st[p].len+1;

			st[q].link=st[nex].link=clone;
			while(p!=-1&&st[p].nex[c]==q){
				st[p].nex[c]=clone;
				p=st[p].link;
			}
		}
		return last=nex;
	}

	
	vector<int>calcTopologicalOrder(){
		vector<int>ord;
		vector<int>deg(st.size());
		for(int i=0;i<st.size();i++){
			for(auto &p:st[i].nex)deg[p.second]++;
		}
		queue<int>que;
		que.push(0);
		while(que.size()){
			int v=que.front();
			que.pop();
			ord.push_back(v);
			for(auto &p:st[v].nex){
				if(--deg[p.second]==0)que.push(p.second);
			}
		}
		return ord;
	}

	vector<int>calcTreeOrder(){
		vector<int>ord;

		vector<vector<int>>G(st.size());
		for(int i=1;i<st.size();i++)G[st[i].link].push_back(i);
		queue<int>que;
		que.push(0);
		while(que.size()){
			int v=que.front();
			que.pop();
			ord.push_back(v);
			for(auto u:G[v])que.push(u);
		}
		return ord;
	}
	int64_t countDistinctSubstrings(){
		auto ord=calcTopologicalOrder();
		vector<int64_t>dp(st.size());dp[0]=1;
		for(auto id:ord){
			for(auto &p:st[id].nex)dp[p.se]+=dp[id];
		}
		return accumulate(all(dp),0ll)-1;
	}
};

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    string S; cin >> S;
    int N = S.size();

    SuffixAutomaton<char> SA;
    rep(i,N) SA.process(S[i]);
    int Q; cin >> Q;
    while (Q--) {
        string T; cin >> T;
        int M = T.size();
        int cur = 0;
        rep(i,M) {
            if (!SA.st[cur].nex.count(T[i])) {
                cout << 0 << ln;
                cur = -1;
                break;
            } else {
                cur = SA.st[cur].nex[T[i]];
            }
        }
        if (cur != -1) cout << 1 << ln;
    }
}


### Prompt

Your task is to create a CPP solution to the following problem:

Determine whether a text T includes a pattern P. Your program should answer for given queries consisting of P_i.

Constraints

* 1 ≤ length of T ≤ 1000000
* 1 ≤ length of P_i ≤ 1000
* 1 ≤ Q ≤ 10000
* The input consists of alphabetical characters and digits

Input

In the first line, a text T is given. In the second line, an integer Q denoting the number of queries is given. In the following Q lines, the patterns P_i are given respectively.

Output

For each question, print 1 if the text includes P_i, or print 0 otherwise.

Example

Input

aabaaa
4
aa
ba
bb
xyz


Output

1
1
0
0

### Response

```cpp
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
#define ln '\n'
constexpr long long MOD = 1000000007LL;
//constexpr long long MOD = 998244353LL;
typedef long long ll;
typedef unsigned long long ull; 
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true;} return false; }
template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true;} return false; }
///////////////////////////////////////////////////////////////////////////////////////////////////

template<class C>
struct SuffixAutomaton{
	struct State{
		int len,link;
		map<C,int>nex;
		State(int len,int link):len(len),link(link){}
	};

	vector<State>st;
	int last;

	SuffixAutomaton():last(0){
		st.emplace_back(0,-1);
	}

	int process(C c,int cur=-1){
		if(cur==-1){
			cur=last;
		}

		if(st[cur].nex.count(c)){
			int q=st[cur].nex[c];
			if(st[q].len==st[cur].len+1){
				return last=q;
			}
			int clone=st.size();
			st.push_back(st[q]);
			st[clone].len=st[q].len+1;
			st[q].link=clone;
			while(cur!=-1&&st[cur].nex[c]==q){
				st[cur].nex[c]=clone;
				cur=st[cur].link;
			}
			return last=clone;
		}

		int nex=st.size();
		st.emplace_back(st[cur].len+1,0);
		int p=cur;
		while(p!=-1&&!st[p].nex.count(c)){
			st[p].nex[c]=nex;
			p=st[p].link;
		}
		if(p==-1){
			return last=nex;
		}

		int q=st[p].nex[c];
		if(st[p].len+1==st[q].len){
			st[nex].link=q;	
		}
		else{
			int clone=st.size();
			st.push_back(st[q]);
			st[clone].len=st[p].len+1;

			st[q].link=st[nex].link=clone;
			while(p!=-1&&st[p].nex[c]==q){
				st[p].nex[c]=clone;
				p=st[p].link;
			}
		}
		return last=nex;
	}

	
	vector<int>calcTopologicalOrder(){
		vector<int>ord;
		vector<int>deg(st.size());
		for(int i=0;i<st.size();i++){
			for(auto &p:st[i].nex)deg[p.second]++;
		}
		queue<int>que;
		que.push(0);
		while(que.size()){
			int v=que.front();
			que.pop();
			ord.push_back(v);
			for(auto &p:st[v].nex){
				if(--deg[p.second]==0)que.push(p.second);
			}
		}
		return ord;
	}

	vector<int>calcTreeOrder(){
		vector<int>ord;

		vector<vector<int>>G(st.size());
		for(int i=1;i<st.size();i++)G[st[i].link].push_back(i);
		queue<int>que;
		que.push(0);
		while(que.size()){
			int v=que.front();
			que.pop();
			ord.push_back(v);
			for(auto u:G[v])que.push(u);
		}
		return ord;
	}
	int64_t countDistinctSubstrings(){
		auto ord=calcTopologicalOrder();
		vector<int64_t>dp(st.size());dp[0]=1;
		for(auto id:ord){
			for(auto &p:st[id].nex)dp[p.se]+=dp[id];
		}
		return accumulate(all(dp),0ll)-1;
	}
};

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    string S; cin >> S;
    int N = S.size();

    SuffixAutomaton<char> SA;
    rep(i,N) SA.process(S[i]);
    int Q; cin >> Q;
    while (Q--) {
        string T; cin >> T;
        int M = T.size();
        int cur = 0;
        rep(i,M) {
            if (!SA.st[cur].nex.count(T[i])) {
                cout << 0 << ln;
                cur = -1;
                break;
            } else {
                cur = SA.st[cur].nex[T[i]];
            }
        }
        if (cur != -1) cout << 1 << ln;
    }
}


```