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 long double pi = acos(-1);
const long long inf = 1LL << 30;
const double eps = 1e-9;
const long long mod = 1e9 + 7;
const int N = 1000010;
const int Alpha = 28;
const int MAXLEN = 1e6 + 6;
class SuffixAutomata {
 public:
  struct state {
    int len, link;
    int next[Alpha];
    state() {
      len = link = 0;
      memset(next, -1, sizeof(next));
    }
  };
  bool vis[2 * MAXLEN];
  int mem[2 * MAXLEN];
  int sz = 1, last = 0;
  vector<state> st;
  vector<int> terminal;
  void Init(int len) {
    st.clear();
    terminal.clear();
    st.resize(2 * len);
    terminal.resize(2 * len);
    sz = 1;
    last = 0;
    st[0].len = 0;
    st[0].link = -1;
    memset(mem, -1, sizeof mem);
    memset(vis, 0, sizeof vis);
  }
  void extend(char ccc) {
    int c = ccc - 'a';
    int cur = sz++;
    st[cur].len = st[last].len + 1;
    int p = last;
    while (p != -1 && st[p].next[c] == -1) {
      st[p].next[c] = cur;
      p = st[p].link;
    }
    if (p == -1) {
      st[cur].link = 0;
    } else {
      int q = st[p].next[c];
      if (st[p].len + 1 == st[q].len) {
        st[cur].link = q;
      } else {
        int clone = sz++;
        st[clone].len = st[p].len + 1;
        for (int j = 0; j < Alpha; j++) st[clone].next[j] = st[q].next[j];
        st[clone].link = st[q].link;
        while (p != -1 && st[p].next[c] == q) {
          st[p].next[c] = clone;
          p = st[p].link;
        }
        st[q].link = st[cur].link = clone;
      }
    }
    last = cur;
  }
  void getTerminal() {
    int p = last;
    while (p > 0) terminal[p] = 1, p = st[p].link;
  }
  int dp(int x) {
    int &ret = mem[x];
    if (ret != -1) return ret;
    ret = terminal[x];
    for (int i = 0; i < Alpha; i++) {
      if (st[x].next[i] == -1) continue;
      ret += dp(st[x].next[i]);
    }
    return ret;
  }
  void build(string s) {
    Init(s.size());
    for (auto x : s) extend(x);
    getTerminal();
  }
  void solve(string s) {
    int n = s.length();
    s = s + s;
    int cur = 0;
    int pos = 0;
    int cur_len = 0;
    for (int i = 0; i < n; i++) {
      while (st[cur].next[s[pos] - 'a'] == -1) {
        if (!cur || !cur_len) break;
        cur_len--;
        if (st[st[cur].link].len >= cur_len) cur = st[cur].link;
      }
      cur = st[cur].next[s[pos] - 'a'];
      if (cur == -1) break;
      cur_len++;
      pos++;
    }
    if (cur == -1) {
      printf("0\n");
      return;
    }
    int ans = 0;
    vector<int> tmp;
    vis[cur] = 1;
    tmp.push_back(cur);
    if (cur_len == n) ans += dp(cur);
    for (int i = 0; i < n; i++) {
      if (cur_len == n) cur_len--;
      if (st[st[cur].link].len >= cur_len) cur = st[cur].link;
      while (st[cur].next[s[pos] - 'a'] == -1) {
        if (!cur || !cur_len) break;
        cur_len--;
        if (st[st[cur].link].len >= cur_len) cur = st[cur].link;
      }
      cur = st[cur].next[s[pos] - 'a'];
      if (cur == -1) break;
      cur_len++;
      pos++;
      if (vis[cur] || cur_len < n) continue;
      vis[cur] = 1;
      tmp.push_back(cur);
      ans += dp(cur);
    }
    printf("%d\n", ans);
    for (auto x : tmp) vis[x] = 0;
    return;
  }
  void print() {
    cout << "Number of States: " << sz << endl;
    cout << "Terminal States: ";
    for (int i = 0; i < sz; i++)
      if (terminal[i]) cout << i << " ";
    cout << endl;
    cout << "Edges:" << endl;
    for (int i = 0; i < sz; i++) {
      for (int j = 0; j < Alpha; j++) {
        if (st[i].next[j] != -1)
          cout << i << ' ' << st[i].next[j] << ' ' << char('a' + j) << endl;
      }
    }
    for (int i = 1; i < sz; i++) {
      cout << i << " * " << st[i].len << " link " << st[i].link << " * "
           << st[st[i].link].len << endl;
    }
  }
} SA;
char a[N];
int main() {
  scanf("%s", &a);
  SA.build(a);
  int q;
  scanf("%d", &q);
  while (q--) {
    scanf("%s", &a);
    SA.solve(a);
  }
}