Some days ago, WJMZBMR learned how to answer the query "how many times does a string *x* occur in a string *s*" quickly by preprocessing the string *s*. But now he wants to make it harder.

So he wants to ask "how many consecutive substrings of *s* are cyclical isomorphic to a given string *x*". You are given string *s* and *n* strings *x*<sub class="lower-index">*i*</sub>, for each string *x*<sub class="lower-index">*i*</sub> find, how many consecutive substrings of *s* are cyclical isomorphic to *x*<sub class="lower-index">*i*</sub>.

Two strings are called cyclical isomorphic if one can rotate one string to get the other one. 'Rotate' here means 'to take some consecutive chars (maybe none) from the beginning of a string and put them back at the end of the string in the same order'. For example, string "abcde" can be rotated to string "deabc". We can take characters "abc" from the beginning and put them at the end of "de".
The first line contains a non-empty string *s*. The length of string *s* is not greater than 10<sup class="upper-index">6</sup> characters.

The second line contains an integer *n* (1 ≤ *n* ≤ 10<sup class="upper-index">5</sup>) — the number of queries. Then *n* lines follow: the *i*-th line contains the string *x*<sub class="lower-index">*i*</sub> — the string for the *i*-th query. The total length of *x*<sub class="lower-index">*i*</sub> is less than or equal to 10<sup class="upper-index">6</sup> characters.

In this problem, strings only consist of lowercase English letters.
class SegmentTree:
    def __init__(self, elems):
        self.N = len(elems)
        self.elems = elems
        self.tree = [0 for _ in range(self.N * 4)]
        self.__build_tree(0, 0, self.N - 1)

    def __left_node(self, node):
        return (node << 1) + 1

    def __right_node(self, node):
        return (node << 1) + 2

    def __build_tree(self, node, left, right):
        if left == right:
            self.tree[node] = self.elems[left]
            return

        mid = int((left + right) / 2)
        lchild = self.__left_node(node)
        rchild = self.__right_node(node)

        self.__build_tree(lchild, left, mid)
        self.__build_tree(rchild, mid + 1, right)

        self.tree[node] = self.tree[lchild] + self.tree[rchild]

    def update(self, ind, val):
        self.__update(0, 0, self.N - 1, ind, val)

    def __update(self, node, left, right, ind, val):
        if ind > right or ind < left:
            return

        if left == right:
            self.tree[node] += val
            return

        mid = int((left + right) / 2)
        lchild = self.__left_node(node)
        rchild = self.__right_node(node)

        self.__update(lchild, left, mid, ind, val)
        self.__update(rchild, mid + 1, right, ind, val)
        self.tree[node] = self.tree[lchild] + self.tree[rchild]

    def query(self, a, b):
        return self.__query(0, 0, self.N - 1, a, b)

    def __query(self, node, left, right, a, b):
        if a > right or b < left:
            return 0

        if a <= left and right <= b:
            return self.tree[node]

        mid = int((left + right) / 2)
        lchild = self.__left_node(node)
        rchild = self.__right_node(node)

        left_child = self.__query(lchild, left, mid, a, b)
        right_child = self.__query(rchild, mid + 1, right, a, b)

        return left_child + right_child


class Problem:
    def __init__(self, S, lbl, lens, n, m):
        self.s = S
        self.labels = lbl
        self.length = lens
        self.n = n
        self.m = m
        self.sa = self.suffix_array()
        self.lcp = self.calc_lcp()
        self.table, self.logs = self.build_table()
        self.tree = SegmentTree([1 if self.sa[i] < m else 0 for i in range(len(S))])

    def calc_lcp(self):
        rank = [0 for _ in range(len(self.s))]
        for i in range(len(self.s)):
            rank[self.sa[i]] = i
        lcp = [0 for _ in range(len(self.s) - 1)]
        h = 0
        for i in range(len(self.s)):
            if rank[i] < len(self.s) - 1:
                while self.s[i + h] == self.s[self.sa[rank[i] + 1] + h]:
                    h += 1
                lcp[rank[i]] = h
                if h > 0:
                    h -= 1
        return lcp

    def suffix_array(self):
        sa = [0 for _ in range(len(self.s))]

        cnt256 = [0 for _ in range(256)]
        for c in self.s:
            cnt256[ord(c)] += 1
        for i in range(1, 256):
            cnt256[i] += cnt256[i - 1]
        for i in range(len(self.s) - 1, -1, -1):
            j = ord(self.s[i])
            cnt256[j] -= 1
            sa[cnt256[j]] = i

        rank = 0
        ranks = [0 for _ in range(len(self.s))]

        for i in range(1, len(self.s)):
            if self.s[sa[i - 1]] != self.s[sa[i]]:
                rank += 1
            ranks[sa[i]] = rank

        k = 1
        while k < len(self.s):
            sa_new = [0 for _ in range(len(self.s))]
            rank_new = [0 for _ in range(len(self.s))]

            for i in range(len(self.s)):
                sa_new[i] = sa[i] - k
                if sa_new[i] < 0:
                    sa_new[i] += len(self.s)

            cnt = [0 for _ in range(len(self.s))]
            for i in range(len(self.s)):
                cnt[ranks[i]] += 1
            for i in range(1, len(self.s)):
                cnt[i] += cnt[i - 1]
            for i in range(len(self.s) - 1, -1, -1):
                cnt[ranks[sa_new[i]]] -= 1
                sa[cnt[ranks[sa_new[i]]]] = sa_new[i]

            rank = 0
            for i in range(1, len(self.s)):
                if ranks[sa[i - 1]] != ranks[sa[i]] or ranks[sa[i - 1] + k] != ranks[sa[i] + k]:
                    rank += 1
                rank_new[sa[i]] = rank
            ranks = rank_new
            k *= 2
        return sa

    def build_table(self):
        size = len(self.lcp)
        table = [[i for i in self.lcp]]
        k = 1
        while 2 * k < size:
            row = []
            for i in range(size):
                min_val = table[-1][i]
                if i + k < size:
                    min_val = min(min_val, table[-1][i + k])
                row.append(min_val)
            k *= 2
            table.append(row)
        logs = []
        h = 0
        for i in range(size):
            if (1 << (h + 1)) < i:
                h += 1
            logs.append(h)
        return table, logs

    def query(self, l, r):
        if l == r:
            return len(self.s) + 1
        log = self.logs[r - l]
        return min(self.table[log][l], self.table[log][r - (1 << log)])

    def check(self, x, k):
        left = x
        right = len(self.s) - 1
        while left < right - 1:
            mid = (left + right) >> 1
            if self.query(x, mid) >= k:
                left = mid
            else:
                right = mid - 1

        if self.query(x, right) >= k:
            R = right
        else:
            R = left

        left = 0
        right = x
        while left < right - 1:
            mid = (left + right) >> 1
            if self.query(mid, x) >= k:
                right = mid
            else:
                left = mid + 1

        if self.query(left, x) >= k:
            L = left
        else:
            L = right
        return self.tree.query(L, R)

    def solve(self):
        ans = [0] * self.n
        last = [-1] * self.n
        for j in range(len(self.sa)):
            i = self.sa[j]
            lbl = self.labels[i]
            if lbl != -1 and (last[lbl] == -1 or self.query(last[lbl], j) < self.length[lbl]):
                ans[lbl] += self.check(j, self.length[lbl])
                last[lbl] = j
        for i in ans:
            print(i)


s = input()
n = int(input())
S = [s]
lbl = []
lens = []
m = len(s)
for _ in range(m):
    lbl.append(-1)

for i in range(n):
    w = input()
    strlen = len(w)
    S.append('*')
    S.append(w)
    S.append(w)
    lens.append(strlen)
    lbl.append(-1)
    lbl += [i] * strlen
    lbl += [-1] * strlen

S.append('$')
lbl.append(-1)
s = ''.join(S)
p = Problem(s, lbl, lens, n, m)
p.solve()
