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;
const int MAXSIGMA = 26;
const int MAXLENGTH = 1000001;
struct SAMnode {
  int length, cnt;
  SAMnode *link, *sibling;
  map<int, SAMnode *> nxt;
  SAMnode(int length = 0) : length(length) {
    cnt = 0;
    link = NULL;
  }
  SAMnode(int length, SAMnode *link, map<int, SAMnode *> &nxt)
      : length(length), cnt(cnt), link(link), nxt(nxt) {
    cnt = 0;
  }
  void print() {
    printf("ADDRESS(%d) link(%d) length = %d cnt = %d (", this, this->link,
           length, cnt);
    putchar(')');
  }
};
struct SAM {
  int n;
  SAMnode pool[MAXLENGTH << 1], *root, *last;
  void _init() {
    n = 0;
    pool[n] = SAMnode();
    root = last = &pool[n++];
  }
  void extend(int v) {
    pool[n] = SAMnode(last->length + 1);
    SAMnode *current = &pool[n++], *p;
    current->cnt = 1;
    for (p = last; p != NULL && !p->nxt.count(v); p = p->link)
      p->nxt[v] = current;
    if (p == NULL) {
      current->link = root;
    } else {
      SAMnode *q = p->nxt[v];
      if (p->length + 1 == q->length) {
        current->link = q;
      } else {
        pool[n] = SAMnode(p->length + 1, q->link, q->nxt);
        SAMnode *clone = &pool[n++];
        for (; p != NULL && p->nxt.count(v) && p->nxt[v] == q; p = p->link)
          p->nxt[v] = clone;
        q->link = current->link = clone;
      }
    }
    last = current;
  }
  void init(char *s) {
    _init();
    int l = strlen(s);
    for (int i = 0; i < l; i++) {
      extend(s[i] - 'a');
    }
    vector<SAMnode *> first(l + 1, NULL);
    for (int i = 0; i < n; i++) {
      SAMnode *node = &pool[i];
      node->sibling = first[node->length];
      first[node->length] = node;
    }
    for (int i = l; i > 0; i--) {
      for (SAMnode *node = first[i]; node != NULL; node = node->sibling) {
        node->link->cnt += node->cnt;
      }
    }
  }
};
const int MAXN = 2000001;
SAM sam;
int fail[MAXN];
char s[MAXN];
int main() {
  scanf("%s", s);
  sam.init(s);
  int q;
  scanf("%d", &q);
  for (int o = 0; o < q; o++) {
    scanf("%s", s);
    int n = strlen(s);
    for (int i = 0; i < n; i++) {
      s[i + n] = s[i];
    }
    s[(n << 1) - 1] = 0;
    fail[0] = -1;
    int i, j;
    for (j = 1; s[j]; j++) {
      for (i = fail[j - 1]; i >= 0 && s[i + 1] != s[j]; i = fail[i])
        ;
      fail[j] = s[i + 1] == s[j] ? i + 1 : -1;
    }
    SAMnode *node = sam.root;
    int res = 0, matchLength = 0;
    for (int i = 0; s[i]; i++) {
      int v = s[i] - 'a';
      while (node != sam.root && !node->nxt.count(v)) {
        node = node->link;
        matchLength = node->length;
      }
      if (!node->nxt.count(v)) continue;
      node = node->nxt[v];
      matchLength++;
      while (matchLength >= n && node != sam.root && node->link->length >= n) {
        node = node->link;
        matchLength = node->length;
      }
      if (i >= n - 1 && fail[i] < n - 1 && matchLength >= n) {
        res += node->cnt;
      }
    }
    printf("%d\n", res);
  }
  return 0;
}