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 CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
struct sam {
  int f[2000011];
  int step[2000011];
  int next[2000011][26];
  int end, tail;
  void init() {
    memset(next, -1, sizeof(next));
    f[0] = -1;
    step[0] = 0;
    end = 0;
    tail = 1;
    return;
  }
  void add(int xx) {
    int p, q, np, nq;
    np = tail++;
    step[np] = step[end] + 1;
    p = end;
    end = np;
    for (; p != -1 && next[p][xx] == -1; p = f[p]) {
      next[p][xx] = np;
    }
    if (p == -1) {
      f[np] = 0;
    } else {
      q = next[p][xx];
      if (step[p] + 1 == step[q]) {
        f[np] = q;
      } else {
        nq = tail++;
        memcpy(next[nq], next[q], sizeof(next[q]));
        step[nq] = step[p] + 1;
        f[nq] = f[q];
        f[q] = f[np] = nq;
        for (; p != -1 && next[p][xx] == q; p = f[p]) {
          next[p][xx] = nq;
        }
      }
    }
  }
} sam;
int right0[2000011];
int arr[2000011];
int aarr[2000011];
bool cmp1(int xx, int yy) { return sam.step[xx] < sam.step[yy]; }
long long ans;
char s[1000011];
int len;
int n;
char d[2000011];
int len2;
int mark[2000011];
int q[2000011];
int ll;
int inq[2000011];
void gao(int times) {
  int i, j;
  int len;
  int xx;
  int p, step;
  for (i = 0; i < len2 - 1; i++) {
    d[i + len2] = d[i];
  }
  d[len2 * 2 - 1] = 0;
  len = len2 * 2 - 1;
  ll = 0;
  p = 0;
  step = 0;
  for (i = 0; i < len; i++) {
    xx = d[i] - 'a';
    while (p != 0 && sam.next[p][xx] == -1) {
      p = sam.f[p];
      step = sam.step[p];
    }
    if (sam.next[p][xx] != -1) {
      p = sam.next[p][xx];
      step++;
    }
    if (step >= len2) {
      mark[p] = times;
    }
    if (inq[p] != times) {
      ll++;
      q[ll] = p;
      inq[p] = times;
    }
  }
  for (i = 1; i <= ll; i++) {
    xx = sam.f[q[i]];
    if (xx != 0 && inq[xx] != times) {
      inq[xx] = times;
      ll++;
      q[ll] = xx;
    }
  }
  sort(q + 1, q + 1 + ll, cmp1);
  for (i = ll; i >= 1; i--) {
    xx = q[i];
    if (mark[xx] == times) {
      if (sam.step[xx] < len2) {
        continue;
      }
      if (sam.step[sam.f[xx]] < len2) {
        ans += right0[xx];
      } else {
        mark[sam.f[xx]] = mark[xx];
      }
    }
  }
  return;
}
int main() {
  int i, j, k;
  int p, xx;
  scanf("%s", s);
  len = strlen(s);
  sam.init();
  for (i = 0; i < len; i++) {
    sam.add(s[i] - 'a');
  }
  for (i = 0; i < sam.tail; i++) {
    arr[i] = i;
  }
  sort(arr, arr + sam.tail, cmp1);
  for (i = 0; i < sam.tail; i++) {
    aarr[arr[i]] = i;
  }
  memset(right0, 0, sizeof(right0));
  xx = sam.end;
  while (xx != 0) {
    right0[xx] = 1;
    xx = sam.f[xx];
  }
  for (i = sam.tail - 1; i >= 0; i--) {
    xx = arr[i];
    for (j = 0; j < 26; j++) {
      if (sam.next[xx][j] != -1) {
        right0[xx] += right0[sam.next[xx][j]];
      }
    }
  }
  scanf("%d", &n);
  memset(mark, 0, sizeof(mark));
  memset(inq, 0, sizeof(inq));
  for (i = 1; i <= n; i++) {
    scanf("%s", d);
    len2 = strlen(d);
    ans = 0;
    gao(i);
    printf("%I64d\n", ans);
  }
  return 0;
}