#include <bits/stdc++.h>   using namespace std;   int nx[1000514 * 2][26], spt;   int fl[1000514 * 2], ll[1000514 * 2], lp[1000514 * 2];   int newnode() {     memset(nx[spt], 0, sizeof(nx[spt]));     fl[spt] = ll[spt] = lp[spt] = 0;     return spt++;   }   void add(int root, int& last, char A) {     int a = A -  a ;     int p = last;     int np = last = newnode();     ll[np] = ll[p] + 1;     lp[np] = 0;     for (; p && nx[p][a] == 0; p = fl[p]) nx[p][a] = np;     int q = nx[p][a];     if (p == 0)       fl[np] = root;     else if (ll[q] == ll[p] + 1)       fl[np] = q;     else {       int nq = spt++;       memcpy(nx[nq], nx[q], sizeof(nx[q]));       fl[nq] = fl[q];       ll[nq] = ll[p] + 1;       lp[nq] = lp[q] + ll[q] - 1 - ll[p];       fl[q] = fl[np] = nq;       for (; p && nx[p][a] == q; p = fl[p]) nx[p][a] = nq;     }   }   char s[1000514], r[1000514 * 2];   vector<int> bk[1000514];   int dp[1000514 * 2];   int vs[1000514 * 2], vt;   void make_z(int* z, char* s, int l) {     int j = z[0] = 0;     for (int i = 1; i < l; i++) {       z[i] = (i > j + z[j] - 1) ? 0 : min(z[i - j], j + z[j] - i);       for (; i + z[i] < l && s[z[i]] == s[i + z[i]]; z[i]++)         ;       if (i + z[i] > j + z[j]) j = i;     }   }   int z[1000514 * 2];   void sol() {     spt = 1;     int last;     int root = last = newnode();     int sl = 0;     for (; s[sl]; sl++)       ;     for (int i = 0; i < sl; i++) add(root, last, s[i]);     for (int i = 1; i < spt; i++) {       bk[ll[i]].push_back(i);     }     for (int p = last; p; p = fl[p]) {       dp[p] = 1;     }     for (int i = sl; i >= 0; i--) {       int sz = (int)bk[i].size();       for (int j = 0; j < sz; j++) {         int w = bk[i][j];         for (int k = 0; k < 26; k++) {           int t = nx[w][k];           if (t) dp[w] += dp[t];         }       }     }     int Q;     scanf( %d , &Q);     for (int T = 0; T < Q; T++) {       scanf( %s , r);       int rl = 0;       for (; r[rl]; rl++)         ;       for (int i = 0; i < rl; i++) r[rl + i] = r[i];       r[rl + rl] = 0;       make_z(z, r, rl * 2);       int t = 0;       for (int i = 1; i <= rl; i++) {         if (rl % i) continue;         int flag = 0;         for (int j = i; j < rl; j += i) {           if (z[j] < rl / i) {             flag = 1;           }         }         if (flag) continue;         t = i;         break;       }       int p = root;       int l = 0;       for (int i = 0; i < rl; i++) {         int a = r[i] -  a ;         for (; p && nx[p][a] == 0; p = fl[p])           ;         if (p == 0) {           l = 0;           p = root;         } else {           l = min(l, ll[p]) + 1;           p = nx[p][a];         }       }       vt++;       int ans = 0;       if (l >= rl && vs[p] != vt) {         vs[p] = vt;         ans += dp[p];       }       for (int i = 0; i < rl - 1; i++) {         int a = r[i] -  a ;         for (; p && nx[p][a] == 0; p = fl[p])           ;         if (p == 0) {           l = 0;           p = root;         } else {           l = min(l, ll[p]) + 1;           p = nx[p][a];         }         for (; p && ll[fl[p]] >= rl; p = fl[p])           ;         l = min(l, ll[p]);         if (l >= rl && vs[p] != vt) {           vs[p] = vt;           ans += dp[p];         }       }       printf( %d n , (int)ans);     }   }   int main() {     scanf( %s , s);     sol();   }