#include <bits/stdc++.h>

#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

typedef long long LL;
template<typename T> using V = std::vector<T>;
template<typename T> using VV = std::vector<std::vector<T>>;
template<typename T> using VVV = std::vector<std::vector<std::vector<T>>>;

template<class T> inline T ceil(T a, T b) { return (a + b - 1) / b; }
template<class T> inline void print(T x) { std::cout << x << std::endl; }
template<class T> inline bool inside(T y, T x, T H, T W) {return 0 <= y and y < H and 0 <= x and x < W; }
template<class T> inline double euclidean_distance(T y1, T x1, T y2, T x2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }
template<class T> inline double manhattan_distance(T y1, T x1, T y2, T x2) { return abs(x1 - x2) + abs(y1 - y2); }

const int INF = 1L << 30;
const double EPS = 1e-9;
const std::string YES = "YES", Yes = "Yes", NO = "NO", No = "No";
const std::vector<int> dy4 = { 0, 1, 0, -1 }, dx4 = { 1, 0, -1, 0 };    // 4近傍（右, 下, 左, 上）
const std::vector<int> dy8 = { 0, -1, 0, 1, 1, -1, -1, 1 }, dx8 = { 1, 0, -1, 0, 1, 1, -1, -1 };

using namespace std;

const int MOD = 1000000000 + 7;


class SuffixArray {
    int n, k;
    std::vector<int> rank;
    std::vector<int> tmp;

public:
    const std::string s;
    std::vector<int> sa;
    std::vector<int> lcp;


public:
    SuffixArray(const std::string &s) : n(s.size()), s(s) {
        sa.assign(n + 1, 0);
        lcp.assign(n, 0);
        rank.assign(n + 1, 0);
        tmp.assign(n + 1, 0);
    }

    void build() {
        this->construct_sa();
        this->construct_lcp();
    }

    // sがpatternを含んでいるか
    bool contain(const string &pattern) {
        int left = 0, right = n;
        while (right - left > 1) {
            int c = (left + right) / 2;
            // Sのsa[c]文字目から|T|文字とTを比較
            if (s.compare(sa[c], pattern.length(), pattern) < 0) {
                left = c;
            }
            else {
                right = c;
            }
        }
        return s.compare(sa[right], pattern.length(), pattern) == 0;
    }

    // sa上で最初にpatternが現れる位置
    int lower_bound(const string &pattern) {
        int left = -1, right = n;
        while (right - left > 1) {
            int c = (left + right) / 2;
            int ret = s.compare(sa[c], pattern.length(), pattern);
            // patternが大きい
            if (ret >= 0) {
                 right = c;
            }
            else {
                left = c;
            }
        }
        if (s.compare(sa[right], pattern.length(), pattern) == 0) {
            return right;
        }
        return -1;
    }

    // sa上で最後にpatternが現れる位置
    int upper_bound(const string &pattern) {
        int left = -1, right = n + 1;
        while (right - left > 1) {
            int c = (left + right) / 2;
            int ret = s.compare(sa[c], pattern.length(), pattern);
            // patternが小さい
            if (ret <= 0) {
                left = c;
            }
            else {
                right = c;
            }
        }
        if (s.compare(sa[left], pattern.length(), pattern) == 0) {
            return left;
        }
        return -1;
    }

    void debug() {
        std::cout << "idx lcp sa sa[i]" << std::endl;
        for (int i = 0; i < n; ++i) {
            std::cout << i << " " << lcp[i] << " " << sa[i] << " " << s.substr(sa[i]) << std::endl;
        }
    }

private:
    struct compare {
        compare(const SuffixArray &sa) : sa(sa) {}
        const SuffixArray &sa;

        // (rank[i], rank[i + k])と(rank[j], rank[j + k])を比較
        bool operator()(const int &i, const int &j) {
            if (sa.rank[i] != sa.rank[j]) {
                return sa.rank[i] < sa.rank[j];
            }
            else {
                int ri = i + sa.k <= sa.n ? sa.rank[i + sa.k] : -1;
                int rj = j + sa.k <= sa.n ? sa.rank[j + sa.k] : -1;
                return ri < rj;
            }
        }
    };

    void construct_sa() {
        for (int i = 0; i <= n; i++) {
            sa[i] = i;
            rank[i] = i < n ? s[i] : -1;
        }
        // k文字についてソートされているところから、2k文字でソートする
        for (k = 1; k <= n; k *= 2) {
            sort(sa.begin(), sa.end(), compare(*this));
            // いったんtmpに次のランクを計算し、それからrankに移す
            tmp[sa[0]] = 0;
            for (int i = 1; i <= n; i++) {
                tmp[sa[i]] = tmp[sa[i - 1]] + (compare(*this).operator()(sa[i - 1], sa[i]) ? 1 : 0);
            }
            for (int i = 0; i <= n; i++) {
                rank[i] = tmp[i];
            }
        }
    }

    void construct_lcp() {
        for (int i = 0; i <= n; i++) {
            rank[sa[i]] = i;
        }
        int h = 0;
        lcp[0] = 0;
        for (int i = 0; i < n; i++) {
            // 文字列中での位置iの接尾辞と、接尾辞配列中でその1つ前の接尾辞のLCPを求める
            int j = sa[rank[i] - 1];
            // hを先頭の分1減らし、後ろが一致しているだけ増やす
            if (h > 0) {
                h--;
            }
            for (; j + h < n and i + h < n; h++) {
                if (s[j + h] != s[i + h]) {
                    break;
                }
            }
            lcp[rank[i] - 1] = h;
        }
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    string s;
    cin >> s;

    SuffixArray sa(s);
    sa.build();

    int Q;
    cin >> Q;
    FOR(i, 0, Q) {
        string p;
        cin >> p;
        print(sa.contain(p) ? 1 : 0);
    }


    return 0;
}

### Prompt

Please provide a CPP coded solution to the problem described below:

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
#include <bits/stdc++.h>

#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

typedef long long LL;
template<typename T> using V = std::vector<T>;
template<typename T> using VV = std::vector<std::vector<T>>;
template<typename T> using VVV = std::vector<std::vector<std::vector<T>>>;

template<class T> inline T ceil(T a, T b) { return (a + b - 1) / b; }
template<class T> inline void print(T x) { std::cout << x << std::endl; }
template<class T> inline bool inside(T y, T x, T H, T W) {return 0 <= y and y < H and 0 <= x and x < W; }
template<class T> inline double euclidean_distance(T y1, T x1, T y2, T x2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }
template<class T> inline double manhattan_distance(T y1, T x1, T y2, T x2) { return abs(x1 - x2) + abs(y1 - y2); }

const int INF = 1L << 30;
const double EPS = 1e-9;
const std::string YES = "YES", Yes = "Yes", NO = "NO", No = "No";
const std::vector<int> dy4 = { 0, 1, 0, -1 }, dx4 = { 1, 0, -1, 0 };    // 4近傍（右, 下, 左, 上）
const std::vector<int> dy8 = { 0, -1, 0, 1, 1, -1, -1, 1 }, dx8 = { 1, 0, -1, 0, 1, 1, -1, -1 };

using namespace std;

const int MOD = 1000000000 + 7;


class SuffixArray {
    int n, k;
    std::vector<int> rank;
    std::vector<int> tmp;

public:
    const std::string s;
    std::vector<int> sa;
    std::vector<int> lcp;


public:
    SuffixArray(const std::string &s) : n(s.size()), s(s) {
        sa.assign(n + 1, 0);
        lcp.assign(n, 0);
        rank.assign(n + 1, 0);
        tmp.assign(n + 1, 0);
    }

    void build() {
        this->construct_sa();
        this->construct_lcp();
    }

    // sがpatternを含んでいるか
    bool contain(const string &pattern) {
        int left = 0, right = n;
        while (right - left > 1) {
            int c = (left + right) / 2;
            // Sのsa[c]文字目から|T|文字とTを比較
            if (s.compare(sa[c], pattern.length(), pattern) < 0) {
                left = c;
            }
            else {
                right = c;
            }
        }
        return s.compare(sa[right], pattern.length(), pattern) == 0;
    }

    // sa上で最初にpatternが現れる位置
    int lower_bound(const string &pattern) {
        int left = -1, right = n;
        while (right - left > 1) {
            int c = (left + right) / 2;
            int ret = s.compare(sa[c], pattern.length(), pattern);
            // patternが大きい
            if (ret >= 0) {
                 right = c;
            }
            else {
                left = c;
            }
        }
        if (s.compare(sa[right], pattern.length(), pattern) == 0) {
            return right;
        }
        return -1;
    }

    // sa上で最後にpatternが現れる位置
    int upper_bound(const string &pattern) {
        int left = -1, right = n + 1;
        while (right - left > 1) {
            int c = (left + right) / 2;
            int ret = s.compare(sa[c], pattern.length(), pattern);
            // patternが小さい
            if (ret <= 0) {
                left = c;
            }
            else {
                right = c;
            }
        }
        if (s.compare(sa[left], pattern.length(), pattern) == 0) {
            return left;
        }
        return -1;
    }

    void debug() {
        std::cout << "idx lcp sa sa[i]" << std::endl;
        for (int i = 0; i < n; ++i) {
            std::cout << i << " " << lcp[i] << " " << sa[i] << " " << s.substr(sa[i]) << std::endl;
        }
    }

private:
    struct compare {
        compare(const SuffixArray &sa) : sa(sa) {}
        const SuffixArray &sa;

        // (rank[i], rank[i + k])と(rank[j], rank[j + k])を比較
        bool operator()(const int &i, const int &j) {
            if (sa.rank[i] != sa.rank[j]) {
                return sa.rank[i] < sa.rank[j];
            }
            else {
                int ri = i + sa.k <= sa.n ? sa.rank[i + sa.k] : -1;
                int rj = j + sa.k <= sa.n ? sa.rank[j + sa.k] : -1;
                return ri < rj;
            }
        }
    };

    void construct_sa() {
        for (int i = 0; i <= n; i++) {
            sa[i] = i;
            rank[i] = i < n ? s[i] : -1;
        }
        // k文字についてソートされているところから、2k文字でソートする
        for (k = 1; k <= n; k *= 2) {
            sort(sa.begin(), sa.end(), compare(*this));
            // いったんtmpに次のランクを計算し、それからrankに移す
            tmp[sa[0]] = 0;
            for (int i = 1; i <= n; i++) {
                tmp[sa[i]] = tmp[sa[i - 1]] + (compare(*this).operator()(sa[i - 1], sa[i]) ? 1 : 0);
            }
            for (int i = 0; i <= n; i++) {
                rank[i] = tmp[i];
            }
        }
    }

    void construct_lcp() {
        for (int i = 0; i <= n; i++) {
            rank[sa[i]] = i;
        }
        int h = 0;
        lcp[0] = 0;
        for (int i = 0; i < n; i++) {
            // 文字列中での位置iの接尾辞と、接尾辞配列中でその1つ前の接尾辞のLCPを求める
            int j = sa[rank[i] - 1];
            // hを先頭の分1減らし、後ろが一致しているだけ増やす
            if (h > 0) {
                h--;
            }
            for (; j + h < n and i + h < n; h++) {
                if (s[j + h] != s[i + h]) {
                    break;
                }
            }
            lcp[rank[i] - 1] = h;
        }
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    string s;
    cin >> s;

    SuffixArray sa(s);
    sa.build();

    int Q;
    cin >> Q;
    FOR(i, 0, Q) {
        string p;
        cin >> p;
        print(sa.contain(p) ? 1 : 0);
    }


    return 0;
}

```