Problem: 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 xi, for each string xi find, how many consecutive substrings of s are cyclical isomorphic to xi.

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".

Input

The first line contains a non-empty string s. The length of string s is not greater than 106 characters.

The second line contains an integer n (1 ≤ n ≤ 105) — the number of queries. Then n lines follow: the i-th line contains the string xi — the string for the i-th query. The total length of xi is less than or equal to 106 characters.

In this problem, strings only consist of lowercase English letters.

Output

For each query xi print a single integer that shows how many consecutive substrings of s are cyclical isomorphic to xi. Print the answers to the queries in the order they are given in the input.

Examples

Input

baabaabaaa
5
a
ba
baa
aabaa
aaba


Output

7
5
7
3
5


Input

aabbaa
3
aa
aabb
abba


Output

2
3
3

Solution (in JAVA):

Solution:
import java.util.List;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.math.BigInteger;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author AlexFetisov
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskC solver = new TaskC();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskC {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        String s = in.nextString();
        SuffixAutomaton automaton = new SuffixAutomaton(s);
        automaton.getCnt();
        int countQueries = in.nextInt();
        while (countQueries-- > 0) {
            String t = in.nextString();
            char[] c = t.toCharArray();
            int cur = 0;
            int l = 0;
            int n = c.length;
            int res = 0;
            for (int i = 0; i < n; ++i) {
                int w = c[i];
                while (cur != -1 && automaton.findEdge(cur, w) == -1) {
                    cur = automaton.link[cur];
                    if (cur != -1) {
                        l = automaton.length[cur];
                    }
                }
                if (cur != -1 && automaton.findEdge(cur, w) != -1) {
                    cur = automaton.to[automaton.findEdge(cur, w)];
                    ++l;
                } else {
                    cur = 0;
                    l = 0;
                }
            }
            if (l == n) {
                res += automaton.cnt[cur];
            }
            int rep = n - PrefixFunction.run(t)[n-1];
            if (n % rep != 0) {
                rep = n;
            }
            for (int i = 1; i < rep; ++i) {
                if (l == n) {
                    --l;
                    if (l <= automaton.length[automaton.link[cur]]) {
                        cur = automaton.link[cur];
                    }
                }
                int w = c[i-1];
                while (cur != -1 && automaton.findEdge(cur, w) == -1) {
                    cur = automaton.link[cur];
                    if (cur != -1) {
                        l = automaton.length[cur];
                    }
                }
                if (cur != -1 && automaton.findEdge(cur, w) != -1) {
                    cur = automaton.to[automaton.findEdge(cur, w)];
                    ++l;
                } else {
                    cur = 0;
                    l = 0;
                }
                if (l == n) {
                    res += automaton.cnt[cur];
                }
            }
            out.println(res);
        }
    }
}

class InputReader {
    private BufferedReader reader;
    private StringTokenizer stt;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream));
    }

    public String nextLine() {
        try {
            return reader.readLine().trim();
        } catch (IOException e) {
            return null;
        }
    }

    public String nextString() {
        while (stt == null || !stt.hasMoreTokens()) {
            stt = new StringTokenizer(nextLine());
        }
        return stt.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(nextString());
    }

}

class SuffixAutomaton {
    public final int[] length;
    public final int[] link;
    public final int[] first;
    public final int[] next;
    public final int[] to;
    public final int[] label;
    public int[] cnt;

    public int size;
    public int last;
    public int edgeSize;

    String is;

    public SuffixAutomaton(String s) {
        is = s;
        int count = s.length();
        length = new int[2 * count + 1];
        link = new int[2 * count + 1];
        first = new int[2 * count + 1];
        next = new int[4 * count];
        label = new int[4 * count];
        cnt = new int[4 * count];
        to = new int[4 * count];
        Arrays.fill(first, -1);
        link[0] = -1;
        size = 1;
        edgeSize = 0;
        last = 0;
        for (int i = 0; i < s.length(); ++i) {
            int c = s.charAt(i);
            int current = size++;
            length[current] = length[last] + 1;
            cnt[current] = 1;
            for (int previous = last; ; previous = link[previous]) {
                if (previous == -1) {
                    link[current] = 0;
                    break;
                }
                int index = findEdge(previous, c);
                if (index != -1) {
                    int curLink = to[index];
                    if (length[previous] + 1 == length[curLink]) {
                        link[current] = curLink;
                    } else {
                        int clone = size++;
                        length[clone] = length[previous] + 1;
                        link[clone] = link[curLink];
                        int linkEdge = first[curLink];
                        while (linkEdge != -1) {
                            next[edgeSize] = first[clone];
                            first[clone] = edgeSize;
                            label[edgeSize] = label[linkEdge];
                            to[edgeSize++] = to[linkEdge];
                            linkEdge = next[linkEdge];
                        }
                        for (; previous != -1; previous = link[previous]) {
                            int edge = findEdge(previous, c);
                            if (edge == -1 || to[edge] != curLink) break;
                            to[edge] = clone;
                        }
                        link[current] = link[curLink] = clone;
                    }
                    break;
                }
                next[edgeSize] = first[previous];
                first[previous] = edgeSize;
                label[edgeSize] = c;
                to[edgeSize++] = current;
            }
            last = current;
        }
    }

    public void getCnt() {
        List<Pair<Integer, Integer>> v = new ArrayList<Pair<Integer, Integer>>();
        for (int i = 1; i < size; ++i) {
            v.add(new Pair<Integer, Integer>(length[i], i));
        }
        Collections.sort(v);
        for (int i = size-2; i >= 0; --i) {
            int k = v.get(i).second;
            cnt[link[k]] += cnt[k];
        }
//        for (int i = size-1; i >= 0; --i) {
//            for (int j = first[i]; j != -1; j = next[j]) {
//                int u = to[j];
//                if (link[u] != -1) {
//                    cnt[link[u]] += cnt[u];
//                }
//            }
//        }
    }

    public int findEdge(int vertex, int label) {
        int edge = first[vertex];
        while (edge != -1) {
            if (this.label[edge] == label)
                return edge;
            edge = next[edge];
        }
        return -1;
    }

}

class PrefixFunction {
    public static int[] run(String s) {
        int n = s.length();
        int[] res = new int[n];
        for (int i = 1; i < n; ++i) {
            int j = res[i-1];
            while (j > 0 && s.charAt(i) != s.charAt(j)) {
                j = res[j - 1];
            }
            if (s.charAt(i) == s.charAt(j)) {
                ++j;
            }
            res[i] = j;
        }
        return res;
    }
}

class Pair<U, V> implements Comparable<Pair<U, V>> {
    public final U first;
    public final V second;

    public Pair(U first, V second) {
        this.first = first;
        this.second = second;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Pair pair = (Pair) o;

        return !(first != null ? !first.equals(pair.first) : pair.first != null) && !(second != null ? !second.equals(pair.second) : pair.second != null);

    }

    public int hashCode() {
        int result = first != null ? first.hashCode() : 0;
        result = 31 * result + (second != null ? second.hashCode() : 0);
        return result;
    }

    public String toString() {
        return "(" + first + "," + second + ")";
    }

    public int compareTo(Pair<U, V> o) {
        int value = ((Comparable<U>)first).compareTo(o.first);
        if (value != 0)
            return value;
        return ((Comparable<V>)second).compareTo(o.second);
    }
}