#include <bits/stdc++.h>
using namespace std;
vector<int> V;
struct r_e_d {
  inline int gti() {
    int x = 0, w = 1;
    char ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') w = -1, ch = getchar();
    while (ch >= '0' && ch <= '9')
      x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
    return x * w;
  }
  inline long long gtl() {
    long long x = 0, w = 1;
    char ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') w = -1, ch = getchar();
    while (ch >= '0' && ch <= '9')
      x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
    return x * w;
  }
  inline string gts() {
    string S;
    char ch = getchar();
    while (ch == ' ' || ch == '\n' || ch == '\r') ch = getchar();
    while (ch != ' ' && ch != '\n' && ch != '\r')
      S.push_back(ch), ch = getchar();
    return S;
  }
} RD;
struct suffix_auto_machine {
  int ch[2000005][26], fa[2000005], mx[2000005], tu[2000005], bn[2000005],
      sz[2000005], tt, la, bj[2000005];
  void push(int x) {
    int np = ++tt, p = la;
    la = np;
    mx[np] = mx[p] + 1;
    for (; p && !ch[p][x]; p = fa[p]) ch[p][x] = np;
    if (!p)
      fa[np] = 1;
    else {
      int q = ch[p][x];
      if (mx[q] == mx[p] + 1)
        fa[np] = q;
      else {
        int nq = ++tt;
        mx[nq] = mx[p] + 1;
        memcpy(ch[nq], ch[q], sizeof(ch[q]));
        fa[nq] = fa[q];
        fa[q] = fa[np] = nq;
        for (; p && ch[p][x] == q; p = fa[p]) ch[p][x] = nq;
      }
    }
    sz[np] = 1;
  }
  void build(string S) {
    tt = la = 1;
    for (int i = (0); i < (S.size()); ++i) push(S[i] - 'a');
    for (int i = (1); i <= (tt); ++i) ++bn[mx[i]];
    for (int i = (1); i <= (tt); ++i) bn[i] += bn[i - 1];
    for (int i = (1); i <= (tt); ++i) tu[bn[mx[i]]--] = i;
    for (int i = (tt); i >= (1); --i) sz[fa[tu[i]]] += sz[tu[i]];
  }
  void find(string S) {
    S = S + S;
    long long as = 0;
    int nw = 1, le = 0;
    for (int i = (0); i < (((int)S.size() - 1)); ++i) {
      for (; nw && ch[nw][S[i] - 'a'] == 0; nw = fa[nw])
        ;
      if (nw == 0) nw = 1;
      le = min(le, mx[nw]);
      ++le;
      nw = ch[nw][S[i] - 'a'];
      le = min(le, mx[nw]);
      for (; nw && min(le, mx[fa[nw]]) >= (S.size() >> 1); nw = fa[nw])
        le = min(le, mx[fa[nw]]);
      if (le >= S.size() / 2 && bj[nw] == 0)
        as += sz[nw], bj[nw] = 1, V.push_back(nw);
    }
    printf("%lld\n", as);
    while (V.size()) bj[V.back()] = 0, V.pop_back();
  }
} SAM;
string S;
int n;
int main() {
  S = RD.gts();
  SAM.build(S);
  n = RD.gti();
  for (int i = (1); i <= (n); ++i) {
    S = RD.gts();
    SAM.find(S);
  }
  return 0;
}
