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 State {
  int len, link;
  map<char, int> nxtc;
} SA[2 * 2000006];
vector<pair<int, int> > lenState;
int FirstPos[2 * 2000006], NumOcc[2 * 2000006];
class SuffixAutomata {
  int sz, last, lm;
  int fnl;
  void init(int idx) {
    SA[idx].len = 0, SA[idx].link = -1;
    FirstPos[idx] = -1;
    SA[idx].nxtc.clear();
  }

 public:
  SuffixAutomata() {
    sz = last = 0;
    init(0);
    lenState.clear();
    lm = 0;
    ++sz;
  }
  int size() { return sz; }
  void extend(char ch, int idx = -1) {
    int p, q, clone, cur = sz++;
    lm = fnl = cur;
    init(cur);
    SA[cur].len = SA[last].len + 1;
    FirstPos[cur] = idx;
    lenState.push_back(make_pair(SA[cur].len, cur));
    for (p = last; p != -1 && SA[p].nxtc.count(ch) == 0; p = SA[p].link)
      SA[p].nxtc[ch] = cur;
    if (p == -1)
      SA[cur].link = 0;
    else {
      q = SA[p].nxtc[ch];
      if (SA[p].len + 1 == SA[q].len)
        SA[cur].link = q;
      else {
        clone = sz++;
        init(clone);
        SA[clone] = SA[q];
        SA[clone].len = SA[p].len + 1;
        for (; p != -1 && SA[p].nxtc[ch] == q; p = SA[p].link)
          SA[p].nxtc[ch] = clone;
        SA[cur].link = SA[q].link = clone;
        FirstPos[clone] = FirstPos[q];
        lenState.push_back(make_pair(SA[clone].len, clone));
      }
    }
    last = cur;
  }
  void numOcc() {
    int i, p, q;
    map<char, int>::iterator im;
    memset(NumOcc, 0, sizeof(NumOcc));
    NumOcc[lm] = 1;
    sort(lenState.begin(), lenState.end());
    for (i = lenState.size() - 1; i >= 0; --i) {
      p = lenState[i].second;
      if (p < 1) continue;
      map<char, int> &M = SA[p].nxtc;
      for (im = M.begin(); im != M.end(); ++im) {
        q = im->second;
        NumOcc[p] += NumOcc[q];
      }
    }
  }
  long long run(char *pat, int len) {
    int p, cur = 0, match = 0, link;
    char ch;
    long long ret = 0;
    for (p = 0; p + 1 < len; ++p) {
      ch = pat[p];
      while (cur && SA[cur].nxtc.count(ch) == 0)
        cur = SA[cur].link,
        match = ((match) < (SA[cur].len) ? (match) : (SA[cur].len));
      if (SA[cur].nxtc.count(ch)) cur = SA[cur].nxtc[ch], ++match;
      if (SA[cur].len < match) match = SA[cur].len;
    }
    for (; pat[p]; ++p) {
      ch = pat[p];
      while (cur && SA[cur].nxtc.count(ch) == 0)
        cur = SA[cur].link,
        match = ((match) < (SA[cur].len) ? (match) : (SA[cur].len));
      if (SA[cur].nxtc.count(ch)) cur = SA[cur].nxtc[ch], ++match;
      if (SA[cur].len < match) match = SA[cur].len;
      link = SA[cur].link;
      while (SA[cur].len > len && SA[link].len >= len)
        cur = link, match = ((match) < (SA[cur].len) ? (match) : (SA[cur].len));
      if (match >= len) ret += NumOcc[cur];
    }
    return ret;
  }
  void print() {
    int cur;
    for (cur = 0; cur < sz; ++cur)
      printf("%d : %d %d #%d\n", cur, SA[cur].len, SA[cur].link, NumOcc[cur]);
  }
};
char str[2000006], pat[2000006];
int failure[2000006];
int kmp(int len) {
  int i, f;
  f = failure[0] = -1;
  for (i = 1; pat[i]; ++i) {
    while (f != -1 && pat[f + 1] != pat[i]) f = failure[f];
    if (pat[f + 1] == pat[i]) ++f;
    failure[i] = f;
  }
  for (i = len; pat[i]; ++i) {
    if (failure[i] + 1 >= len) break;
  }
  return i;
}
long long processPat(SuffixAutomata &sa) {
  int len = strlen(pat), p;
  pat[2 * len - 1] = 0;
  for (p = 2 * len - 2; p >= len; --p) pat[p] = pat[p - len];
  pat[kmp(len)] = 0;
  return sa.run(pat, len);
}
int main() {
  int i, Q;
  while (scanf("%s", str) != EOF) {
    SuffixAutomata sa;
    for (i = 0; str[i]; ++i) sa.extend(str[i]);
    sa.extend('#');
    sa.numOcc();
    scanf("%d", &Q);
    while (Q--) {
      scanf("%s", pat);
      printf("%I64d\n", processPat(sa));
    }
    return 0;
  }
  return 0;
}