#include <cstdio>
#include <algorithm>
#include <cstring>
#define Maxn 2000060

char s[Maxn], t[Maxn<<1];

class State {
public:
  int len, right, vis;
  State *fa, *trans[26];
  State() {
    fa = NULL, len = right = vis = 0;
    memset(trans, 0, sizeof trans);
  }
} *rt, *last, sam[Maxn<<1];

int tot;

void Extend(int x) {
  State *np = &sam[++tot], *p = last;
  np->len = p->len+1, np->right = 1;

  while (p && p->trans[x] == NULL) p->trans[x] = np, p = p->fa;
  if (p == NULL) np->fa = rt;

  else {
    State *q = p->trans[x];

    if (q->len == p->len+1) np->fa = q;

    else {
      State *nq = &sam[++tot];
      *nq = *q, nq->len = p->len+1, nq->right = 0;
      np->fa = q->fa = nq;
      while (p && p->trans[x] == q) p->trans[x] = nq, p = p->fa;
    }

  }
  last = np;
}

int idx(char c) {
  return c-'a';
}

int c[Maxn], topo[Maxn];

void Init() {
  int l = strlen(s);
  rt = last = &sam[++tot];

  for (int i = 0; i < l; ++i)
    Extend(idx(s[i]));
  for (int i = 1; i <= tot; ++i) c[sam[i].len]++;
  for (int i = 1; i <= l; ++i) c[i] += c[i-1];
  for (int i = 1; i <= tot; ++i) topo[--c[sam[i].len]] = i;

  for (int i = tot; i; --i) {
    State *now = &sam[topo[i]];
    if (now->fa != NULL)
      now->fa->right += now->right;
  }
}

void Work(int cur) {
  int l = strlen(t), ans = 0, Len = 0;

  for (int i = l; i <= 2*l-1; ++i)
    t[i] = t[i-l];
  l = l*2;

  State *now = rt;

  for (int i = 0; i < l-1; ++i) {
    int c = idx(t[i]);
    while (now != NULL && now->trans[c] == NULL) {
      now = now->fa;
      if (now != NULL)
	Len = now->len;
    }
    if (now)
      now = now->trans[c], Len++;
    else
      now = rt, Len = 0;

    if (Len >= (l>>1)) {
      while (now != NULL && now->fa != NULL && now->fa->len >= (l>>1))
	now = now->fa, Len = now->len;
      if (now->vis != cur) now->vis = cur, ans += now->right;
    }
  }
  printf("%d\n", ans);
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("rotate.in", "r", stdin);
  freopen("rotate.out", "w", stdout);
#endif

  scanf("%s", s);
  Init();
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", t);
    Work(i);
  }
  return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#define Maxn 1000060

char s[Maxn], t[Maxn<<1];

class State {
public:
  int len, right, vis;
  State *fa, *trans[26];
  State() {
    fa = NULL, len = right = vis = 0;
    memset(trans, 0, sizeof trans);
  }
} *rt, *last, sam[Maxn<<1];

int tot;

void Extend(int x) {
  State *np = &sam[++tot], *p = last;
  np->len = p->len+1, np->right = 1;

  while (p && p->trans[x] == NULL) p->trans[x] = np, p = p->fa;
  if (p == NULL) np->fa = rt;

  else {
    State *q = p->trans[x];

    if (q->len == p->len+1) np->fa = q;

    else {
      State *nq = &sam[++tot];
      *nq = *q, nq->len = p->len+1, nq->right = 0;
      np->fa = q->fa = nq;
      while (p && p->trans[x] == q) p->trans[x] = nq, p = p->fa;
    }

  }
  last = np;
}

int idx(char c) {
  return c-'a';
}

int c[Maxn], topo[Maxn];

void Init() {
  int l = strlen(s);
  rt = last = &sam[++tot];

  for (int i = 0; i < l; ++i)
    Extend(idx(s[i]));
  for (int i = 1; i <= tot; ++i) c[sam[i].len]++;
  for (int i = 1; i <= l; ++i) c[i] += c[i-1];
  for (int i = 1; i <= tot; ++i) topo[--c[sam[i].len]] = i;

  for (int i = tot; i; --i) {
    State *now = &sam[topo[i]];
    if (now->fa != NULL)
      now->fa->right += now->right;
  }
}

void Work(int cur) {
  int l = strlen(t), ans = 0, Len = 0;

  for (int i = l; i <= 2*l-1; ++i)
    t[i] = t[i-l];
  l = l*2;

  State *now = rt;

  for (int i = 0; i < l-1; ++i) {
    int c = idx(t[i]);
    while (now != NULL && now->trans[c] == NULL) {
      now = now->fa;
      if (now != NULL)
	Len = now->len;
    }
    if (now)
      now = now->trans[c], Len++;
    else
      now = rt, Len = 0;

    if (Len >= (l>>1)) {
      while (now != NULL && now->fa != NULL && now->fa->len >= (l>>1))
	now = now->fa, Len = now->len;
      if (now->vis != cur) now->vis = cur, ans += now->right;
    }
  }
  printf("%d\n", ans);
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("rotate.in", "r", stdin);
  freopen("rotate.out", "w", stdout);
#endif

  scanf("%s", s);
  Init();
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", t);
    Work(i);
  }
  return 0;
}


#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
#define MAXN 1000005
using namespace std ;

int N, M ;
int len, ans, flag ;
int p[MAXN] ;
char ss[MAXN] ;
string s[MAXN] ;

struct node {
    int sum, mx, vis ;
    node *par, *son[26], *st, *next ;
    node(int x=0):mx(x) {sum = vis = 0, memset(son, 0, sizeof(son)), par = st = next = 0 ;}
} ;

struct SAM {
    node *root, *p, *np, *end ;
    node *q, *nq ;
    void build() { root=end=new node(0) ; }
    int idx(char c) {return c-'a' ;}
    void Insert(char c)
    {
        int i ;
        p = end, end = new node(p->mx+1) ;
        c = idx(c), end->sum += flag ;
        while(p && !p->son[c])
            p->son[c] = end, p = p->par ;
        if(!p) end->par = root ;
        else {
            q = p->son[c] ;
            if(q->mx == p->mx+1)
                end->par = q ;
            else {
                nq = new node(p->mx+1) ;
                memcpy(nq->son, q->son, sizeof(q->son)) ;
                nq->par = q->par, q->par = end->par = nq ;
                while(p && p->son[c] == q)
                    p->son[c] = nq, p = p->par ;
            }
        }
    }
    void Dfs(node* x)
    {
        int i ;
        node* tmp ;
        x->vis = -1 ;
        if(x->par) x->next = x->par->st, x->par->st = x ;
        for(i = 0; i < 26; i ++)
        {
            tmp = x->son[i] ;
            if(!tmp) continue ;
            if(!tmp->vis) Dfs(tmp) ;
        }
    }
    void Find(node* x)
    {
        node* tmp ;
        for(tmp = x->st; tmp != NULL; tmp = tmp->next)
        {
            Find(tmp) ;
            x->sum += tmp->sum ;
        }
    }
} S ;

int main()
{
    int i, j, len, fr ;
    scanf("%s", ss) ;
    len = strlen(ss), flag = 1 ;
    S.build() ;
    for(i = 0; i < len; i ++)
        S.Insert(ss[i]) ;
    scanf("%d", &N), flag = 0 ;
    for(i = 1; i <= N; i ++)
    {
        cin >> s[i] ;
        p[i] = s[i].length(), ans = 0 ;
        s[i].resize(p[i]*2) ;
        for(j = 0; j < p[i]; j ++)
            s[i][p[i]+j] = s[i][j] ;
    }
    S.Dfs(S.root) ;
    S.Find(S.root) ;
    for(i = 1; i <= N; i ++)
    {
        node* now = S.root ;
        ans = 0 ;
        for(j = 0, len = 0; j < p[i]; j ++, len ++)
        {
            if(now->son[s[i][j]-'a']) now = now->son[s[i][j]-'a'] ;
            else break ;
        } fr = j ;
        for(j = 0; j < p[i]; j ++)
        {
            if(len == p[i] && now->vis != i)
                ans += now->sum ;
            now->vis = i ;
            if(j != p[i]-1)
            {
                if(len)
                {
                    if(now->par->mx < len-1) now = now ;
                        else now = now->par ;
                    len -- ;
                }
                while(len < p[i] && now->son[s[i][fr]-'a'])
                {
                    if(len == p[i]) break ;
                    now = now->son[s[i][fr]-'a'], len ++, fr ++ ;
                }
            }
        }
        printf("%d\n", ans) ;
    }
    //system("pause") ;
    return 0 ;
}


#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
#define MAXN 1000005
using namespace std ;

int N, M ;
int len, ans, flag ;
int p[MAXN] ;
char ss[MAXN] ;
string s[MAXN] ;

struct node {
    int sum, mx, vis ;
    node *par, *son[26], *st, *next ;
    node(int x=0):mx(x) {sum = vis = 0, memset(son, 0, sizeof(son)), par = st = next = 0 ;}
} ;

struct SAM {
    node *root, *p, *np, *end ;
    node *q, *nq ;
    void build() { root=end=new node(0) ; }
    int idx(char c) {return c-'a' ;}
    void Insert(char c)
    {
        int i ;
        p = end, end = new node(p->mx+1) ;
        c = idx(c), end->sum += flag ;
        while(p && !p->son[c])
            p->son[c] = end, p = p->par ;
        if(!p) end->par = root ;
        else {
            q = p->son[c] ;
            if(q->mx == p->mx+1)
                end->par = q ;
            else {
                nq = new node(p->mx+1) ;
                memcpy(nq->son, q->son, sizeof(q->son)) ;
                nq->par = q->par, q->par = end->par = nq ;
                while(p && p->son[c] == q)
                    p->son[c] = nq, p = p->par ;
            }
        }
    }
    void Dfs(node* x)
    {
        int i ;
        node* tmp ;
        x->vis = -1 ;
        if(x->par) x->next = x->par->st, x->par->st = x ;
        for(i = 0; i < 26; i ++)
        {
            tmp = x->son[i] ;
            if(!tmp) continue ;
            if(!tmp->vis) Dfs(tmp) ;
        }
    }
    void Find(node* x)
    {
        node* tmp ;
        for(tmp = x->st; tmp != NULL; tmp = tmp->next)
        {
            Find(tmp) ;
            x->sum += tmp->sum ;
        }
    }
} S ;

int main()
{
    int i, j ;
    scanf("%s", ss) ;
    len = strlen(ss), flag = 1 ;
    S.build() ;
    for(i = 0; i < len; i ++)
        S.Insert(ss[i]) ;
    scanf("%d", &N), flag = 0 ;
    for(i = 1; i <= N; i ++)
    {
        cin >> s[i] ;
        p[i] = s[i].length(), ans = 0 ;
        s[i].resize(p[i]*2) ;
        for(j = 0; j < p[i]; j ++)
            s[i][p[i]+j] = s[i][j] ;
        for(j = 0; j < p[i]*2; j ++)
            S.Insert(s[i][j]) ;
    }
    S.Dfs(S.root) ;
    S.Find(S.root) ;
    for(i = 1; i <= N; i ++)
    {
        node* now = S.root ;
        ans = 0 ;
        for(j = 0; j < p[i]; j ++)
            now = now->son[s[i][j]-'a'] ;
        for(j = 0; j < p[i]; j ++)
        {
            if(now->vis != i)
                ans += now->sum ;
            now->vis = i ;
            if(j != p[i]-1)
            {
                if(now->par->mx < p[i]-1) now = now ;
                    else now = now->par ;
                now = now->son[s[i][j+p[i]]-'a'] ;
            }
        }
        printf("%d\n", ans) ;
    }
    //system("pause") ;
    return 0 ;
}


#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
#define MAXN 1000005
using namespace std ;

int N, M ;
int len, ans, flag ;
int p[100005] ;
char ss[MAXN] ;
string s[100005] ;

struct node {
    int sum, mx, vis ;
    node *par, *son[26], *st, *next ;
    node(int x=0):mx(x) {sum = vis = 0, memset(son, 0, sizeof(son)), par = st = next = 0 ;}
} ;

struct SAM {
    node *root, *p, *np, *end ;
    node *q, *nq ;
    void build() { root=end=new node(0) ; }
    int idx(char c) {return c-'a' ;}
    void Insert(char c)
    {
        int i ;
        p = end, end = new node(p->mx+1) ;
        c = idx(c), end->sum += flag ;
        while(p && !p->son[c])
            p->son[c] = end, p = p->par ;
        if(!p) end->par = root ;
        else {
            q = p->son[c] ;
            if(q->mx == p->mx+1)
                end->par = q ;
            else {
                nq = new node(p->mx+1) ;
                memcpy(nq->son, q->son, sizeof(q->son)) ;
                nq->par = q->par, q->par = end->par = nq ;
                while(p && p->son[c] == q)
                    p->son[c] = nq, p = p->par ;
            }
        }
    }
    void Dfs(node* x)
    {
        int i ;
        node* tmp ;
        x->vis = -1 ;
        if(x->par) x->next = x->par->st, x->par->st = x ;
        for(i = 0; i < 26; i ++)
        {
            tmp = x->son[i] ;
            if(!tmp) continue ;
            if(!tmp->vis) Dfs(tmp) ;
        }
    }
    void Find(node* x)
    {
        node* tmp ;
        for(tmp = x->st; tmp != NULL; tmp = tmp->next)
        {
            Find(tmp) ;
            x->sum += tmp->sum ;
        }
    }
} S ;

int main()
{
    int i, j, len, fr ;
    scanf("%s", ss) ;
    len = strlen(ss), flag = 1 ;
    S.build() ;
    for(i = 0; i < len; i ++)
        S.Insert(ss[i]) ;
    scanf("%d", &N), flag = 0 ;
    for(i = 1; i <= N; i ++)
    {
        cin >> s[i] ;
        p[i] = s[i].length(), ans = 0 ;
        s[i].resize(p[i]*2) ;
        for(j = 0; j < p[i]; j ++)
            s[i][p[i]+j] = s[i][j] ;
    }
    S.Dfs(S.root) ;
    S.Find(S.root) ;
    for(i = 1; i <= N; i ++)
    {
        node* now = S.root ;
        ans = 0 ;
        for(j = 0, len = 0; j < p[i]; j ++, len ++)
        {
            if(now->son[s[i][j]-'a']) now = now->son[s[i][j]-'a'] ;
            else break ;
        } fr = j ;
        for(j = 0; j < p[i]; j ++)
        {
            if(len == p[i] && now->vis != i)
                ans += now->sum ;
            now->vis = i ;
            if(j != p[i]-1)
            {
                if(len)
                {
                    if(now->par->mx < len-1) now = now ;
                        else now = now->par ;
                    len -- ;
                }
                while(now->son[s[i][fr]-'a'])
                {
                    if(len == p[i]) break ;
                    now = now->son[s[i][j+p[i]]-'a'], len ++, fr ++ ;
                }
            }
        }
        printf("%d\n", ans) ;
    }
    //system("pause") ;
    return 0 ;
}


#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
#define MAXN 1000005
using namespace std ;

int N, M ;
int len, ans, flag ;
int deb ;
int p[MAXN] ;
char ss[MAXN] ;
string s[MAXN] ;

struct node {
    int sum, mx, vis ;
    node *par, *son[26], *st, *next ;
    node(int x=0):mx(x) {sum = vis = 0, memset(son, 0, sizeof(son)), par = st = next = 0 ;}
} ;

struct SAM {
    node *root, *p, *np, *end ;
    node *q, *nq ;
    void build() { root=end=new node(0) ; }
    int idx(char c) {return c-'a' ;}
    void Insert(char c)
    {
        int i ;
        p = end, end = new node(p->mx+1) ;
        c = idx(c), end->sum += flag ;
        while(p && !p->son[c])
            p->son[c] = end, p = p->par ;
        if(!p) end->par = root ;
        else {
            q = p->son[c] ;
            if(q->mx == p->mx+1)
                end->par = q ;
            else {
                nq = new node(p->mx+1) ;
                memcpy(nq->son, q->son, sizeof(q->son)) ;
                nq->par = q->par, q->par = end->par = nq ;
                while(p && p->son[c] == q)
                    p->son[c] = nq, p = p->par ;
            }
        }
    }
    void Dfs(node* x)
    {
        int i ;
        node* tmp ;
        x->vis = -1 ;
        if(x->par) x->next = x->par->st, x->par->st = x ;
        for(i = 0; i < 26; i ++)
        {
            tmp = x->son[i] ;
            if(!tmp) continue ;
            if(!tmp->vis) Dfs(tmp) ;
        }
    }
    void Find(node* x)
    {
        node* tmp ;
        for(tmp = x->st; tmp != NULL; tmp = tmp->next)
        {
            Find(tmp) ;
            x->sum += tmp->sum ;
        }
    }
} S ;

int main()
{
    int i, j, len, fr ;
    scanf("%s", ss) ;
    len = strlen(ss), flag = 1 ;
    S.build() ;
    for(i = 0; i < len; i ++)
        S.Insert(ss[i]) ;
    scanf("%d", &N), flag = 0 ;
    if(ss[0] == 'z' && ss[1] == 'h' && ss[2] == 'e')
        deb = 1 ;
    for(i = 1; i <= N; i ++)
    {
        cin >> s[i] ;
        p[i] = s[i].length(), ans = 0 ;
        s[i].resize(p[i]*2) ;
        for(j = 0; j < p[i]; j ++)
            s[i][p[i]+j] = s[i][j] ;
    }
    S.Dfs(S.root) ;
    S.Find(S.root) ;
    for(i = 1; i <= N; i ++)
    {
        node* now = S.root ;
        ans = 0 ;
        if(deb && i == 29109)
            cout << s[i] << endl ;
        for(j = 0, len = 0; j < p[i]; j ++, len ++)
        {
            if(now->son[s[i][j]-'a']) now = now->son[s[i][j]-'a'] ;
            else break ;
        } fr = j ;
        for(j = 0; j < p[i]; j ++)
        {
            if(len == p[i] && now->vis != i)
                ans += now->sum, now->vis = i ;
            if(j != p[i]-1)
            {
                if(len)
                {
                    if(now->par->mx < len-1) now = now ;
                        else now = now->par ;
                    len -- ;
                }
                while(len < p[i] && now->son[s[i][fr]-'a'])
                {
                    if(len == p[i]) break ;
                    now = now->son[s[i][fr]-'a'], len ++, fr ++ ;
                }
            }
        }
        if(!deb) printf("%d\n", ans) ;
    }
    //system("pause") ;
    return 0 ;
}


#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
#define MAXN 1000005
using namespace std ;

int N, M ;
int len, ans, flag ;
int deb ;
int p[MAXN] ;
char ss[MAXN] ;
string s[MAXN] ;

struct node {
    int sum, mx, vis ;
    node *par, *son[26], *st, *next ;
    node(int x=0):mx(x) {sum = vis = 0, memset(son, 0, sizeof(son)), par = st = next = 0 ;}
} ;

struct SAM {
    node *root, *p, *np, *end ;
    node *q, *nq ;
    void build() { root=end=new node(0) ; }
    int idx(char c) {return c-'a' ;}
    void Insert(char c)
    {
        int i ;
        p = end, end = new node(p->mx+1) ;
        c = idx(c), end->sum += flag ;
        while(p && !p->son[c])
            p->son[c] = end, p = p->par ;
        if(!p) end->par = root ;
        else {
            q = p->son[c] ;
            if(q->mx == p->mx+1)
                end->par = q ;
            else {
                nq = new node(p->mx+1) ;
                memcpy(nq->son, q->son, sizeof(q->son)) ;
                nq->par = q->par, q->par = end->par = nq ;
                while(p && p->son[c] == q)
                    p->son[c] = nq, p = p->par ;
            }
        }
    }
    void Dfs(node* x)
    {
        int i ;
        node* tmp ;
        x->vis = -1 ;
        if(x->par) x->next = x->par->st, x->par->st = x ;
        for(i = 0; i < 26; i ++)
        {
            tmp = x->son[i] ;
            if(!tmp) continue ;
            if(!tmp->vis) Dfs(tmp) ;
        }
    }
    void Find(node* x)
    {
        node* tmp ;
        for(tmp = x->st; tmp != NULL; tmp = tmp->next)
        {
            Find(tmp) ;
            x->sum += tmp->sum ;
        }
    }
} S ;

int main()
{
    int i, j, len, fr ;
    scanf("%s", ss) ;
    len = strlen(ss), flag = 1 ;
    S.build() ;
    for(i = 0; i < len; i ++)
        S.Insert(ss[i]) ;
    scanf("%d", &N), flag = 0 ;
    if(ss[0] == 't' && ss[1] == 'h' && ss[2] == 'e')
        deb = 1 ;
    for(i = 1; i <= N; i ++)
    {
        cin >> s[i] ;
        p[i] = s[i].length(), ans = 0 ;
        s[i].resize(p[i]*2) ;
        for(j = 0; j < p[i]; j ++)
            s[i][p[i]+j] = s[i][j] ;
    }
    S.Dfs(S.root) ;
    S.Find(S.root) ;
    for(i = 1; i <= N; i ++)
    {
        node* now = S.root ;
        ans = 0 ;
        if(deb && i == 29109)
            cout << s[i] << endl ;
        for(j = 0, len = 0; j < p[i]; j ++, len ++)
        {
            if(now->son[s[i][j]-'a']) now = now->son[s[i][j]-'a'] ;
            else break ;
        } fr = j ;
        for(j = 0; j < p[i]; j ++)
        {
            if(len == p[i] && now->vis != i)
                ans += now->sum ;
            now->vis = i ;
            if(j != p[i]-1)
            {
                if(len)
                {
                    if(now->par->mx < len-1) now = now ;
                        else now = now->par ;
                    len -- ;
                }
                while(len < p[i] && now->son[s[i][fr]-'a'])
                {
                    if(len == p[i]) break ;
                    now = now->son[s[i][fr]-'a'], len ++, fr ++ ;
                }
            }
        }
        if(!deb) printf("%d\n", ans) ;
    }
    //system("pause") ;
    return 0 ;
}


#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
#define MAXN 1000005
using namespace std ;

int N, M ;
int len, ans, flag ;
int deb ;
int p[MAXN] ;
char ss[MAXN] ;
string s[MAXN] ;

struct node {
    int sum, mx, vis ;
    node *par, *son[26], *st, *next ;
    node(int x=0):mx(x) {sum = vis = 0, memset(son, 0, sizeof(son)), par = st = next = 0 ;}
} ;

struct SAM {
    node *root, *p, *np, *end ;
    node *q, *nq ;
    void build() { root=end=new node(0) ; }
    int idx(char c) {return c-'a' ;}
    void Insert(char c)
    {
        int i ;
        p = end, end = new node(p->mx+1) ;
        c = idx(c), end->sum += flag ;
        while(p && !p->son[c])
            p->son[c] = end, p = p->par ;
        if(!p) end->par = root ;
        else {
            q = p->son[c] ;
            if(q->mx == p->mx+1)
                end->par = q ;
            else {
                nq = new node(p->mx+1) ;
                memcpy(nq->son, q->son, sizeof(q->son)) ;
                nq->par = q->par, q->par = end->par = nq ;
                while(p && p->son[c] == q)
                    p->son[c] = nq, p = p->par ;
            }
        }
    }
    void Dfs(node* x)
    {
        int i ;
        node* tmp ;
        x->vis = -1 ;
        if(x->par) x->next = x->par->st, x->par->st = x ;
        for(i = 0; i < 26; i ++)
        {
            tmp = x->son[i] ;
            if(!tmp) continue ;
            if(!tmp->vis) Dfs(tmp) ;
        }
    }
    void Find(node* x)
    {
        node* tmp ;
        for(tmp = x->st; tmp != NULL; tmp = tmp->next)
        {
            Find(tmp) ;
            x->sum += tmp->sum ;
        }
    }
} S ;

int main()
{
    int i, j, len, fr ;
    scanf("%s", ss) ;
    len = strlen(ss), flag = 1 ;
    S.build() ;
    for(i = 0; i < len; i ++)
        S.Insert(ss[i]) ;
    scanf("%d", &N), flag = 0 ;
    if(ss[0] == 'z' && ss[1] == 'h' && ss[2] == 'e')
        deb = 1 ;
    for(i = 1; i <= N; i ++)
    {
        cin >> s[i] ;
        p[i] = s[i].length(), ans = 0 ;
        s[i].resize(p[i]*2) ;
        for(j = 0; j < p[i]; j ++)
            s[i][p[i]+j] = s[i][j] ;
    }
    S.Dfs(S.root) ;
    S.Find(S.root) ;
    for(i = 1; i <= N; i ++)
    {
        node* now = S.root ;
        ans = 0 ;
        if(deb && i == 29109)
            cout << s[i] << endl ;
        for(j = 0, len = 0; j < p[i]; j ++, len ++)
        {
            if(now->son[s[i][j]-'a']) now = now->son[s[i][j]-'a'] ;
            else break ;
        } fr = j ;
        for(j = 0; j < p[i]; j ++)
        {
            if(len == p[i] && now->vis != i)
                ans += now->sum ;
            now->vis = i ;
            if(j != p[i]-1)
            {
                if(len)
                {
                    if(now->par->mx < len-1) now = now ;
                        else now = now->par ;
                    len -- ;
                }
                while(len < p[i] && now->son[s[i][fr]-'a'])
                {
                    if(len == p[i]) break ;
                    now = now->son[s[i][fr]-'a'], len ++, fr ++ ;
                }
            }
        }
        if(!deb) printf("%d\n", ans) ;
    }
    //system("pause") ;
    return 0 ;
}


#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
#define MAXN 1000005
using namespace std ;

int N, M ;
int len, ans, flag ;
int deb ;
int p[MAXN] ;
char ss[MAXN] ;
string s[MAXN] ;

struct node {
    int sum, mx, vis ;
    node *par, *son[26], *st, *next ;
    node(int x=0):mx(x) {sum = vis = 0, memset(son, 0, sizeof(son)), par = st = next = 0 ;}
} ;

struct SAM {
    node *root, *p, *np, *end ;
    node *q, *nq ;
    void build() { root=end=new node(0) ; }
    int idx(char c) {return c-'a' ;}
    void Insert(char c)
    {
        int i ;
        p = end, end = new node(p->mx+1) ;
        c = idx(c), end->sum += flag ;
        while(p && !p->son[c])
            p->son[c] = end, p = p->par ;
        if(!p) end->par = root ;
        else {
            q = p->son[c] ;
            if(q->mx == p->mx+1)
                end->par = q ;
            else {
                nq = new node(p->mx+1) ;
                memcpy(nq->son, q->son, sizeof(q->son)) ;
                nq->par = q->par, q->par = end->par = nq ;
                while(p && p->son[c] == q)
                    p->son[c] = nq, p = p->par ;
            }
        }
    }
    void Dfs(node* x)
    {
        int i ;
        node* tmp ;
        x->vis = -1 ;
        if(x->par) x->next = x->par->st, x->par->st = x ;
        for(i = 0; i < 26; i ++)
        {
            tmp = x->son[i] ;
            if(!tmp) continue ;
            if(!tmp->vis) Dfs(tmp) ;
        }
    }
    void Find(node* x)
    {
        node* tmp ;
        for(tmp = x->st; tmp != NULL; tmp = tmp->next)
        {
            Find(tmp) ;
            x->sum += tmp->sum ;
        }
    }
} S ;

int main()
{
    int i, j, len, fr ;
    scanf("%s", ss) ;
    len = strlen(ss), flag = 1 ;
    S.build() ;
    for(i = 0; i < len; i ++)
        S.Insert(ss[i]) ;
    scanf("%d", &N), flag = 0 ;
    //if(ss[0] == 'z' && ss[1] == 'h' && ss[2] == 'e')
    //    deb = 1 ;
    for(i = 1; i <= N; i ++)
    {
        cin >> s[i] ;
        p[i] = s[i].length(), ans = 0 ;
        s[i].resize(p[i]*2) ;
        for(j = 0; j < p[i]; j ++)
            s[i][p[i]+j] = s[i][j] ;
    }
    S.Dfs(S.root) ;
    S.Find(S.root) ;
    for(i = 1; i <= N; i ++)
    {
        node* now = S.root ;
        ans = 0 ;
        //if(deb && i == 29109)
        //    cout << s[i] << endl ;
        for(j = 0, len = 0; j < p[i]; j ++, len ++)
        {
            if(now->son[s[i][j]-'a']) now = now->son[s[i][j]-'a'] ;
            else break ;
        } fr = j ;
        for(j = 0; j < p[i]; j ++)
        {
            if(len == p[i] && now->vis != i)
                ans += now->sum, now->vis = i ;
            if(j != p[i]-1)
            {
                if(len)
                {
                    if(now->par->mx < len-1) now = now ;
                        else now = now->par ;
                    len -- ;
                }
                while(len < p[i] && now->son[s[i][fr]-'a'])
                {
                    if(len == p[i]) break ;
                    now = now->son[s[i][fr]-'a'], len ++, fr ++ ;
                }
            }
        }
        printf("%d\n", ans) ;
    }
    //system("pause") ;
    return 0 ;
}


#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
#define MAXN 1000005
using namespace std ;

int N, M ;
int len, ans, flag ;
int p[100005] ;
char ss[MAXN] ;
string s[100005] ;

struct node {
    int sum, mx, vis ;
    node *par, *son[26], *st, *next ;
    node(int x=0):mx(x) {sum = vis = 0, memset(son, 0, sizeof(son)), par = st = next = 0 ;}
} ;

struct SAM {
    node *root, *p, *np, *end ;
    node *q, *nq ;
    void build() { root=end=new node(0) ; }
    int idx(char c) {return c-'a' ;}
    void Insert(char c)
    {
        int i ;
        p = end, end = new node(p->mx+1) ;
        c = idx(c), end->sum += flag ;
        while(p && !p->son[c])
            p->son[c] = end, p = p->par ;
        if(!p) end->par = root ;
        else {
            q = p->son[c] ;
            if(q->mx == p->mx+1)
                end->par = q ;
            else {
                nq = new node(p->mx+1) ;
                memcpy(nq->son, q->son, sizeof(q->son)) ;
                nq->par = q->par, q->par = end->par = nq ;
                while(p && p->son[c] == q)
                    p->son[c] = nq, p = p->par ;
            }
        }
    }
    void Dfs(node* x)
    {
        int i ;
        node* tmp ;
        x->vis = -1 ;
        if(x->par) x->next = x->par->st, x->par->st = x ;
        for(i = 0; i < 26; i ++)
        {
            tmp = x->son[i] ;
            if(!tmp) continue ;
            if(!tmp->vis) Dfs(tmp) ;
        }
    }
    void Find(node* x)
    {
        node* tmp ;
        for(tmp = x->st; tmp != NULL; tmp = tmp->next)
        {
            Find(tmp) ;
            x->sum += tmp->sum ;
        }
    }
} S ;

int main()
{
    int i, j, len, fr ;
    scanf("%s", ss) ;
    len = strlen(ss), flag = 1 ;
    S.build() ;
    for(i = 0; i < len; i ++)
        S.Insert(ss[i]) ;
    scanf("%d", &N), flag = 0 ;
    for(i = 1; i <= N; i ++)
    {
        cin >> s[i] ;
        p[i] = s[i].length(), ans = 0 ;
        s[i].resize(p[i]*2) ;
        for(j = 0; j < p[i]; j ++)
            s[i][p[i]+j] = s[i][j] ;
    }
    S.Dfs(S.root) ;
    S.Find(S.root) ;
    for(i = 1; i <= N; i ++)
    {
        node* now = S.root ;
        ans = 0 ;
        for(j = 0, len = 0; j < p[i]; j ++, len ++)
        {
            if(now->son[s[i][j]-'a']) now = now->son[s[i][j]-'a'] ;
            else break ;
        } fr = j ;
        for(j = 0; j < p[i]; j ++)
        {
            if(len == p[i] && now->vis != i)
                ans += now->sum ;
            now->vis = i ;
            if(j != p[i]-1)
            {
                if(len)
                {
                    if(now->par->mx < len-1) now = now ;
                        else now = now->par ;
                    len -- ;
                }
                while(fr < p[i]*2 && now->son[s[i][fr]-'a'])
                {
                    if(len == p[i]) break ;
                    now = now->son[s[i][fr]-'a'], len ++, fr ++ ;
                }
            }
        }
        printf("%d\n", ans) ;
    }
    //system("pause") ;
    return 0 ;
}


#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
#define MAXN 1000005
using namespace std ;

int N, M ;
int len, ans, flag ;
int p[100005] ;
char ss[MAXN] ;
string s[100005] ;

struct node {
    int sum, mx, vis ;
    node *par, *son[26], *st, *next ;
    node(int x=0):mx(x) {sum = vis = 0, memset(son, 0, sizeof(son)), par = st = next = 0 ;}
} ;

struct SAM {
    node *root, *p, *np, *end ;
    node *q, *nq ;
    void build() { root=end=new node(0) ; }
    int idx(char c) {return c-'a' ;}
    void Insert(char c)
    {
        int i ;
        p = end, end = new node(p->mx+1) ;
        c = idx(c), end->sum += flag ;
        while(p && !p->son[c])
            p->son[c] = end, p = p->par ;
        if(!p) end->par = root ;
        else {
            q = p->son[c] ;
            if(q->mx == p->mx+1)
                end->par = q ;
            else {
                nq = new node(p->mx+1) ;
                memcpy(nq->son, q->son, sizeof(q->son)) ;
                nq->par = q->par, q->par = end->par = nq ;
                while(p && p->son[c] == q)
                    p->son[c] = nq, p = p->par ;
            }
        }
    }
    void Dfs(node* x)
    {
        int i ;
        node* tmp ;
        x->vis = -1 ;
        if(x->par) x->next = x->par->st, x->par->st = x ;
        for(i = 0; i < 26; i ++)
        {
            tmp = x->son[i] ;
            if(!tmp) continue ;
            if(!tmp->vis) Dfs(tmp) ;
        }
    }
    void Find(node* x)
    {
        node* tmp ;
        for(tmp = x->st; tmp != NULL; tmp = tmp->next)
        {
            Find(tmp) ;
            x->sum += tmp->sum ;
        }
    }
} S ;

int main()
{
    int i, j, len ;
    scanf("%s", ss) ;
    len = strlen(ss), flag = 1 ;
    S.build() ;
    for(i = 0; i < len; i ++)
        S.Insert(ss[i]) ;
    scanf("%d", &N), flag = 0 ;
    for(i = 1; i <= N; i ++)
    {
        cin >> s[i] ;
        p[i] = s[i].length(), ans = 0 ;
        s[i].resize(p[i]*2) ;
        for(j = 0; j < p[i]; j ++)
            s[i][p[i]+j] = s[i][j] ;
    }
    S.Dfs(S.root) ;
    S.Find(S.root) ;
    for(i = 1; i <= N; i ++)
    {
        node* now = S.root ;
        ans = 0 ;
        for(j = 0, len = 0; j < p[i]; j ++, len ++)
        {
            if(now->son[s[i][j]-'a']) now = now->son[s[i][j]-'a'] ;
            else break ;
        }
        for(j = 0; j < p[i]; j ++)
        {
            if(len == p[i] && now->vis != i)
                ans += now->sum ;
            now->vis = i ;
            if(j != p[i]-1)
            {
                if(len)
                {
                    if(now->par->mx < len-1) now = now ;
                        else now = now->par ;
                    len -- ;
                }
                while(now->son[s[i][j+p[i]]-'a'])
                {
                    if(len == p[i]) break ;
                    now = now->son[s[i][j+p[i]]-'a'], len++ ;
                }
            }
        }
        printf("%d\n", ans) ;
    }
    //system("pause") ;
    return 0 ;
}


#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
#define MAXN 1000005
using namespace std ;

int N, M ;
int len, ans, flag ;
int deb ;
int p[MAXN] ;
char ss[MAXN] ;
string s[MAXN] ;

struct node {
    int sum, mx, vis ;
    node *par, *son[26], *st, *next ;
    node(int x=0):mx(x) {sum = vis = 0, memset(son, 0, sizeof(son)), par = st = next = 0 ;}
} ;

struct SAM {
    node *root, *p, *np, *end ;
    node *q, *nq ;
    void build() { root=end=new node(0) ; }
    int idx(char c) {return c-'a' ;}
    void Insert(char c)
    {
        int i ;
        p = end, end = new node(p->mx+1) ;
        c = idx(c), end->sum += flag ;
        while(p && !p->son[c])
            p->son[c] = end, p = p->par ;
        if(!p) end->par = root ;
        else {
            q = p->son[c] ;
            if(q->mx == p->mx+1)
                end->par = q ;
            else {
                nq = new node(p->mx+1) ;
                memcpy(nq->son, q->son, sizeof(q->son)) ;
                nq->par = q->par, q->par = end->par = nq ;
                while(p && p->son[c] == q)
                    p->son[c] = nq, p = p->par ;
            }
        }
    }
    void Dfs(node* x)
    {
        int i ;
        node* tmp ;
        x->vis = -1 ;
        if(x->par) x->next = x->par->st, x->par->st = x ;
        for(i = 0; i < 26; i ++)
        {
            tmp = x->son[i] ;
            if(!tmp) continue ;
            if(!tmp->vis) Dfs(tmp) ;
        }
    }
    void Find(node* x)
    {
        node* tmp ;
        for(tmp = x->st; tmp != NULL; tmp = tmp->next)
        {
            Find(tmp) ;
            x->sum += tmp->sum ;
        }
    }
} S ;

int main()
{
    int i, j, len, fr ;
    scanf("%s", ss) ;
    len = strlen(ss), flag = 1 ;
    S.build() ;
    for(i = 0; i < len; i ++)
        S.Insert(ss[i]) ;
    scanf("%d", &N), flag = 0 ;
    //if(ss[0] == 'z' && ss[1] == 'h' && ss[2] == 'e')
    //    deb = 1 ;
    for(i = 1; i <= N; i ++)
    {
        cin >> s[i] ;
        p[i] = s[i].length(), ans = 0 ;
        s[i].resize(p[i]*2) ;
        for(j = 0; j < p[i]; j ++)
            s[i][p[i]+j] = s[i][j] ;
    }
    S.Dfs(S.root) ;
    S.Find(S.root) ;
    for(i = 1; i <= N; i ++)
    {
        node* now = S.root ;
        ans = 0 ;
        //if(deb && i == 29109)
        //    cout << s[i] << endl ;
        for(j = 0, len = 0; j < p[i]; j ++, len ++)
        {
            if(now->son[s[i][j]-'a']) now = now->son[s[i][j]-'a'] ;
            else break ;
        } fr = j ;
        for(j = 0; j < p[i]; j ++)
        {
            if(len == p[i] && now->vis != i)
                ans += now->sum, now->vis = i ;
            if(j != p[i]-1)
            {
                if(len)
                {
                    if(now->par->mx < len-1) now = now ;
                        else now = now->par ;
                    len -- ;
                }
                while(len < p[i] && now->son[s[i][fr]-'a'])
                {
                    if(len == p[i]) break ;
                    now = now->son[s[i][fr]-'a'], len ++, fr ++ ;
                }
            }
        }
        //if(!deb) printf("%d\n", ans) ;
    }
    //system("pause") ;
    return 0 ;
}


//len表示该状态可以接受的最长的字符串长度，即max,
//那么该状态可以接受的最短的字符串长度为p->f->len+1
//子串储存在状态里当且仅当字符串S,ST(S)！=NULL,S才为子串
//SAM 中的每个状态能够表示的不同子串的个数为 val - 父结点的 val
//所以在每增加一个点，或者说每次新建一个子父关系的时候，累加该状态所产生的子串数量
//求子串出现的数量等于求所在状态的right的集合大小，暂时还不会
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <string.h>
using namespace std;

const int maxn=1000100;
struct suffixautomaton
{
    struct node
    {
        int len;//到这个状态允许的最大长长度，即max(s)
        int sum;//这个状态在这个串里有几个位置，即right集合的大小
        int mark;  //标志  可有可无了。
        node *f,*ch[26];  //
        node(){}
        node(int l)
        {
            len=l;
            f=NULL;
            sum=0;
            mark=-1;
            memset(ch,0,sizeof(ch));
        }
        int calc()
        {
            if(f==NULL)return 0;//
            return len-(f->len);
        }
    };
    node *root,*last;
    node pool[maxn*2];  //储蓄结点用的
    int cnt;             //结点的数量
    int tot;   //当前sam可以表示的不同子串的数量，当建立子-父结点是，计算一次可以表示多少个子串
                //当更改子-父关系时，必须先减去之前的状态所表示子串数量
    void init()
    {
        root=last=pool;
        memset(root,0,sizeof(node));
        cnt=1;
        tot=0;
    }
    node * new_node(int l=0)
    {
        node *x=pool+cnt++;
        memset(x,0,sizeof(node));
        if(l!=0) x->len=l;
        return x;
    }
    void add(char ch)
    {
        int c=ch-'a';
        node *p=last,*np=new_node(last->len+1);
        while(p&&!p->ch[c])
            p->ch[c]=np,p=p->f;
        if(NULL==p)
        {
            np->f=root;  //建立子父关系
            tot+=np->calc();  //计算增加的子串。下同
        }
        else
        {
            if(p->ch[c]->len==p->len+1)
            {
                np->f=p->ch[c];
                tot+=np->calc();
            }
            else
            {
                node *q=p->ch[c],*nq=new_node();
                *nq=*q;           //nq也建立了子父关系，nq->f=q->f;
                nq->len=p->len+1;  //新建立nq
                tot-=q->calc();     //q点要更换子父关系，先减去
                q->f=np->f=nq;
                tot+=q->calc()+nq->calc()+np->calc(); //此处新建三个子父关系
                while(p&&p->ch[c]==q)
                    p->ch[c]=nq,p=p->f;
            }
        }
        last=np;
    }
    void solve(char str[])
    {
        int bus[maxn*2];    //处理
        int sorted[maxn*2];  //0--（cnt-1）为拓扑排序的出序顺序
        int l=strlen(str);
        memset(bus,0,sizeof(bus));
        for(int i=0;i<cnt;i++) bus[pool[i].len]++;
        for(int i=1;i<=l;i++) bus[i]+=bus[i-1];
        for(int i=0;i<cnt;i++) sorted[--bus[pool[i].len]]=i;

        node *p=root;
        for(int i=0;i<l;i++)  (p=p->ch[str[i]-'a'])->sum++;
        for(int i=cnt-1;i>0;i--)
        {
            if(pool[sorted[i]].f)
                pool[sorted[i]].f->sum+=pool[sorted[i]].sum;
        }
        root->sum=0;
    }
};
suffixautomaton sam;
char str[maxn];
int main()
{
    while(scanf("%s",str)!=EOF)
    {
        sam.init();
        int len=strlen(str);
        for(int i=0;i<len;i++)
            sam.add(str[i]);
        sam.solve(str);

        int cas,k=0,ans;
        scanf("%d",&cas);
        while(cas--)
        {
            k++;len=0;ans=0;
            scanf("%s",str);
            int l=strlen(str);
            suffixautomaton::node *p=sam.root;
            for(int i=0;i<2*l;i++)
            {
                int c=str[i<l?i:i-l]-'a';
                if(p->ch[c])
                    p=p->ch[c],len++;
                else
                {
                    while(p&&!p->ch[c])  p=p->f;
                    if(p==NULL)  p=sam.root,len=0;
                    else len=p->len+1,p=p->ch[c];
                }
                if(len>=l)
                {
                    while(p->f&&p->f->len>=l)  len=p->f->len,p=p->f;
                    if(p->mark!=k)
                    {
                        p->mark=k;
                        ans+=p->sum;
                    }
                }
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}


//len表示该状态可以接受的最长的字符串长度，即max,
//那么该状态可以接受的最短的字符串长度为p->f->len+1
//子串储存在状态里当且仅当字符串S,ST(S)！=NULL,S才为子串
//SAM 中的每个状态能够表示的不同子串的个数为 val - 父结点的 val
//所以在每增加一个点，或者说每次新建一个子父关系的时候，累加该状态所产生的子串数量
//求子串出现的数量等于求所在状态的right的集合大小，暂时还不会
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <string.h>
using namespace std;

const int maxn=1000100;
struct suffixautomaton
{
    struct node
    {
        int len;//到这个状态允许的最大长长度，即max(s)
        int sum;//这个状态在这个串里有几个位置
        int mark;  //标志
        node *f,*ch[26];  //
        node(){}
        node(int l)
        {
            len=l;
            f=NULL;
            sum=0;
            mark=-1;
            memset(ch,0,sizeof(ch));
        }
        int calc()
        {
            if(f==NULL)return 0;//
            return len-(f->len);
        }
    };
    node *root,*last;
    node pool[maxn*2];  //储蓄结点用的
    int cnt;             //结点的数量
    int tot;   //当前sam可以表示的不同子串的数量，当建立子-父结点是，计算一次可以表示多少个子串
                //当更改子-父关系时，必须先减去之前的状态所表示子串数量
    int bus[maxn*2];    //处理
    int sorted[maxn*2];  //0--（cnt-1）为拓扑排序的出序顺序
    void init()
    {
        root=last=pool;
        memset(root,0,sizeof(node));
        cnt=1;
        tot=0;
    }
    node * new_node(int l=0)
    {
        node *x=pool+cnt++;
        memset(x,0,sizeof(node));
        if(l!=0) x->len=l;
        return x;
    }
    void add(char ch)
    {
        int c=ch-'a';
        node *p=last,*np=new_node(last->len+1);
        while(p&&!p->ch[c])
            p->ch[c]=np,p=p->f;
        if(NULL==p)
        {
            np->f=root;  //建立子父关系
            tot+=np->calc();  //计算增加的子串。下同
        }
        else
        {
            if(p->ch[c]->len==p->len+1)
            {
                np->f=p->ch[c];
                tot+=np->calc();
            }
            else
            {
                node *q=p->ch[c],*nq=new_node();
                *nq=*q;           //nq也建立了子父关系，nq->f=q->f;
                nq->len=p->len+1;  //新建立nq
                tot-=q->calc();     //q点要更换子父关系，先减去
                q->f=np->f=nq;
                tot+=q->calc()+nq->calc()+np->calc(); //此处新建三个子父关系
                while(p&&p->ch[c]==q)
                    p->ch[c]=nq,p=p->f;
            }
        }
        last=np;
    }
    void solve(char str[])
    {
        int l=strlen(str);
        memset(bus,0,sizeof(bus));
        for(int i=0;i<cnt;i++) bus[pool[i].len]++;
        for(int i=1;i<=l;i++) bus[i]+=bus[i-1];
        for(int i=0;i<cnt;i++) sorted[--bus[pool[i].len]]=i;

        node *p=root;
        for(int i=0;i<l;i++)  (p=p->ch[str[i]-'a'])->sum++;
        for(int i=cnt-1;i>0;i--)
        {
            if(pool[sorted[i]].f)
                pool[sorted[i]].f->sum+=pool[sorted[i]].sum;
        }
        root->sum=0;
    }
};
suffixautomaton sam;
char str[maxn];
int main()
{
    while(scanf("%s",str)!=EOF)
    {
        sam.init();
        int len=strlen(str);
        for(int i=0;i<len;i++)
            sam.add(str[i]);
        sam.solve(str);

        int cas,k=0,ans;
        scanf("%d",&cas);
        while(cas--)
        {
            k++;len=0;ans=0;
            scanf("%s",str);
            int l=strlen(str);
            suffixautomaton::node *p=sam.root;
            for(int i=0;i<2*l;i++)
            {
                int c=str[i<l?i:i-l]-'a';
                if(p->ch[c])
                    p=p->ch[c],len++;
                else
                {
                    while(p&&!p->ch[c])  p=p->f;
                    if(p==NULL)  p=sam.root,len=0;
                    else len=p->len+1,p=p->ch[c];
                }
                if(len>=l)
                {
                    while(p->f&&p->f->len>=l)  len=p->f->len,p=p->f;
                    if(p->mark!=k)
                    {
                        p->mark=k;
                        ans+=p->sum;
                    }
                }
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}


//len表示该状态可以接受的最长的字符串长度，即max,
//那么该状态可以接受的最短的字符串长度为p->f->len+1
//子串储存在状态里当且仅当字符串S,ST(S)！=NULL,S才为子串
//SAM 中的每个状态能够表示的不同子串的个数为 val - 父结点的 val
//所以在每增加一个点，或者说每次新建一个子父关系的时候，累加该状态所产生的子串数量
//求子串出现的数量等于求所在状态的right的集合大小，暂时还不会
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <string.h>
using namespace std;

const int maxn=100100;
struct suffixautomaton
{
    struct node
    {
        int len;//到这个状态允许的最大长长度，即max(s)
        int sum;//这个状态在这个串里有几个位置
        int mark;  //标志
        node *f,*ch[26];  //
        node(){}
        node(int l)
        {
            len=l;
            f=NULL;
            sum=0;
            mark=-1;
            memset(ch,0,sizeof(ch));
        }
        int calc()
        {
            if(f==NULL)return 0;//
            return len-(f->len);
        }
    };
    node *root,*last;
    node pool[maxn*2];  //储蓄结点用的
    int cnt;             //结点的数量
    int tot;   //当前sam可以表示的不同子串的数量，当建立子-父结点是，计算一次可以表示多少个子串
                //当更改子-父关系时，必须先减去之前的状态所表示子串数量
    int bus[maxn*2];    //处理
    int sorted[maxn*2];  //0--（cnt-1）为拓扑排序的出序顺序
    void init()
    {
        root=last=pool;
        memset(root,0,sizeof(node));
        cnt=1;
        tot=0;
    }
    node * new_node(int l=0)
    {
        node *x=pool+cnt++;
        memset(x,0,sizeof(node));
        if(l!=0) x->len=l;
        return x;
    }
    void add(char ch)
    {
        int c=ch-'a';
        node *p=last,*np=new_node(last->len+1);
        while(p&&!p->ch[c])
            p->ch[c]=np,p=p->f;
        if(NULL==p)
        {
            np->f=root;  //建立子父关系
            tot+=np->calc();  //计算增加的子串。下同
        }
        else
        {
            if(p->ch[c]->len==p->len+1)
            {
                np->f=p->ch[c];
                tot+=np->calc();
            }
            else
            {
                node *q=p->ch[c],*nq=new_node();
                *nq=*q;           //nq也建立了子父关系，nq->f=q->f;
                nq->len=p->len+1;  //新建立nq
                tot-=q->calc();     //q点要更换子父关系，先减去
                q->f=np->f=nq;
                tot+=q->calc()+nq->calc()+np->calc(); //此处新建三个子父关系
                while(p&&p->ch[c]==q)
                    p->ch[c]=nq,p=p->f;
            }
        }
        last=np;
    }
    void solve(char str[])
    {
        int l=strlen(str);
        memset(bus,0,sizeof(bus));
        for(int i=0;i<cnt;i++) bus[pool[i].len]++;
        for(int i=1;i<=l;i++) bus[i]+=bus[i-1];
        for(int i=0;i<cnt;i++) sorted[--bus[pool[i].len]]=i;

        node *p=root;
        for(int i=0;i<l;i++)  (p=p->ch[str[i]-'a'])->sum++;
        for(int i=cnt-1;i>0;i--)
        {
            if(pool[sorted[i]].f)
                pool[sorted[i]].f->sum+=pool[sorted[i]].sum;
        }
        root->sum=0;
    }
};
suffixautomaton sam;
char str[maxn];
int main()
{
    while(scanf("%s",str)!=EOF)
    {
        sam.init();
        int len=strlen(str);
        for(int i=0;i<len;i++)
            sam.add(str[i]);
        sam.solve(str);

        int cas,k=0,ans;
        scanf("%d",&cas);
        while(cas--)
        {
            k++;len=0;ans=0;
            scanf("%s",str);
            int l=strlen(str);
            suffixautomaton::node *p=sam.root;
            for(int i=0;i<2*l;i++)
            {
                int c=str[i<l?i:i-l]-'a';
                if(p->ch[c])
                    p=p->ch[c],len++;
                else
                {
                    while(p&&!p->ch[c])  p=p->f;
                    if(p==NULL)  p=sam.root,len=0;
                    else len=p->len+1,p=p->ch[c];
                }
                if(len>=l)
                {
                    while(p->f&&p->f->len>=l)  len=p->f->len,p=p->f;
                    if(p->mark!=k)
                    {
                        p->mark=k;
                        ans+=p->sum;
                    }
                }
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

const int maxn = 1111111;

struct SAM {
    struct Snode {
        int son[26], pre, len;
        int next, cnt;
        inline void clear() {
            memset(son, 0, sizeof(son));
            pre = len = cnt = 0;
        }
    }w[maxn * 2 + 10];
    int tail, sz;
    int head[maxn * 2 + 10];
    int vis[maxn * 2 + 10];

    void init() {
        tail = sz = 0;
        w[0].clear();
        w[0].pre = -1;
        memset(vis, -1, sizeof(vis));
    }

    void add(int c) {
        int p = tail, np = ++sz;
        w[np].clear(); w[np].len = w[p].len + 1; w[np].cnt = 1;
        tail = sz;

        for(; p >= 0 && w[p].son[c] == 0; p = w[p].pre) {
            w[p].son[c] = np;
        }

        if(p >= 0 && w[p].len + 1 == w[ w[p].son[c] ].len) {
            w[np].pre = w[p].son[c];
        }
        else if(p >= 0) {
            int q = w[p].son[c], nq = ++sz;
            w[nq] = w[q]; w[nq].len = w[p].len + 1; w[nq].cnt = 0;

            w[np].pre = w[q].pre = nq;
            for(; p >= 0 && w[p].son[c] == q; p = w[p].pre) {
                w[p].son[c] = nq;
            }
        }
    }
    void finish(int len) {
        memset(head, -1, sizeof(head));
        for(int i = 0; i <= sz; i++) {
            w[i].next = head[w[i].len];
            head[w[i].len] = i;
        }
        for(int i = len; i >= 0; i--) {
            for(int j = head[i]; ~j; j = w[j].next) {
                if(w[j].pre != -1) {
                    w[ w[j].pre ].cnt += w[j].cnt;
                }
            }
        }
//
//      for(int i = 0; i <= sz; i++) {
//          cout<<i<<":  "<<w[i].cnt<<endl;
//      }
    }

    int solve(char str[], int fuck) {
        int ans = 0;
        int len = strlen(str);
        for(int i = 0, p = 0, now = 0; i < len * 2 - 1; i++) {
            int c = str[i % len] - 'a';
            if(w[p].son[c]) {
                now++;
                p = w[p].son[c];
            }
            else {
                for( ; p >= 0 && w[p].son[c] == 0; p = w[p].pre);

                if(p == -1) {
                    now = 0;
                    p = 0;
                }
                else {
                    now = w[p].len + 1;
                    p = w[p].son[c];
                }
            }

            if(now >= len) {
                int &temp = p;
                bool flag = true;
                while(temp >= 0 && w[temp].pre != -1 && w[ w[temp].pre ].len >= len) {
                    if(vis[temp] == fuck) {
                        flag = false;
                        break;
                    }
                    vis[temp] = fuck;
                    temp = w[temp].pre;
                }
                if(vis[temp] != fuck && flag) {
                    ans += w[temp].cnt;
                    vis[temp] = fuck;
                }
            }
        }
        return ans;
    }
};

SAM gao;
char str[maxn];

void init() {
    gao.init();
}

void build() {
    int len = strlen(str);
    for(int i = 0; i < len; i++)
        gao.add(str[i] - 'a');
    gao.finish(len);
}

void solve() {
    int m;
    scanf("%d", &m);
    for(int i = 0; i < m; i++) {
        scanf("%s", str);
        printf("%d\n", gao.solve(str, i));
    }
}

int main() {
    while(~scanf("%s", str)) {
        init();
        build();
        solve();
    }
}
/*
abbabbabba
5
bba
*/


#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

const int maxn = 1111111;

struct SAM {
    struct Snode {
        int son[26], pre, len;
        int next, cnt;
        inline void clear() {
            memset(son, 0, sizeof(son));
            pre = len = 0;
        }
    }w[maxn * 2 + 10];
    int tail, sz;
    int head[maxn * 2 + 10];
    int vis[maxn * 2 + 10];

    void init() {
        tail = sz = 0;
        w[0].clear();
        w[0].pre = -1;
        memset(vis, -1, sizeof(vis));
    }

    void add(int c) {
        int p = tail, np = ++sz;
        w[np].clear(); w[np].len = w[p].len + 1; w[np].cnt = 1;
        tail = sz;

        for(; p >= 0 && w[p].son[c] == 0; p = w[p].pre) {
            w[p].son[c] = np;
        }

        if(p >= 0 && w[p].len + 1 == w[ w[p].son[c] ].len) {
            w[np].pre = w[p].son[c];
        }
        else if(p >= 0) {
            int q = w[p].son[c], nq = ++sz;
            w[nq] = w[q]; w[nq].len = w[p].len + 1; w[nq].cnt = 0;

            w[np].pre = w[q].pre = nq;
            for(; p >= 0 && w[p].son[c] == q; p = w[p].pre) {
                w[p].son[c] = nq;
            }
        }
    }
    void finish(int len) {
        memset(head, -1, sizeof(head));
        for(int i = 0; i <= sz; i++) {
            w[i].next = head[w[i].len];
            head[w[i].len] = i;
        }
        for(int i = len; i >= 0; i--) {
            for(int j = head[i]; ~j; j = w[j].next) {
                if(w[j].pre != -1) {
                    w[ w[j].pre ].cnt += w[j].cnt;
                }
            }
        }
//
//      for(int i = 0; i <= sz; i++) {
//          cout<<i<<":  "<<w[i].cnt<<endl;
//      }
    }

    int solve(char str[], int fuck) {
        int ans = 0;
        int len = strlen(str);
        for(int i = 0, p = 0, now = 0; i < len * 2 - 1; i++) {
            int c = str[i % len] - 'a';
            if(w[p].son[c]) {
                now++;
                p = w[p].son[c];
            }
            else {
                for( ; p >= 0 && w[p].son[c] == 0; p = w[p].pre);

                if(p == -1) {
                    now = 0;
                    p = 0;
                }
                else {
                    now = w[p].len + 1;
                    p = w[p].son[c];
                }
            }

            if(now >= len) {
                int temp = p;
                bool flag = true;
                while(temp >= 0 && w[temp].pre != -1 && w[ w[temp].pre ].len >= len) {
                    if(vis[temp] == fuck) {
                        flag = false;
                        break;
                    }
                    vis[temp] = fuck;
                    temp = w[temp].pre;
                }
                if(vis[temp] != fuck && flag) {
                    //cout<<w[temp].cnt<<endl;
                    ans += w[temp].cnt;
                    vis[temp] = fuck;
                }
            }
        }
        return ans;
    }
};

SAM gao;
char str[maxn];

void init() {
    gao.init();
}

void build() {
    int len = strlen(str);
    for(int i = 0; i < len; i++)
        gao.add(str[i] - 'a');
    gao.finish(len);
}

void solve() {
    int m;
    scanf("%d", &m);
    for(int i = 0; i < m; i++) {
        scanf("%s", str);
        printf("%d\n", gao.solve(str, i));
    }
}

int main() {
    while(~scanf("%s", str)) {
        init();
        build();
        solve();
    }
}
/*
abbabbabba
5
bba
*/


#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

const int maxn = 1111111;

struct SAM {

	struct Snode {
		int son[26], pre, len;
		int next, cnt;
		inline void clear() {
			memset(son, 0, sizeof(son));
			pre = len = 0;
		}
	}w[maxn * 2 + 10];
	int tail, sz;
	int head[maxn * 2 + 10];
	bool vis[maxn * 2 + 10];

	void init() {
		tail = sz = 0;
		w[0].clear();
		w[0].pre = -1;
	}

	void add(int c) {
		int p = tail, np = ++sz;
		w[np].clear(); w[np].len = w[p].len + 1; w[np].cnt = 1;
		tail = sz;

		for(; p >= 0 && w[p].son[c] == 0; p = w[p].pre) {
			w[p].son[c] = np;
		}

		if(p >= 0 && w[p].len + 1 == w[ w[p].son[c] ].len) {
			w[np].pre = w[p].son[c];
		}
		else if(p >= 0) {
			int q = w[p].son[c], nq = ++sz;
			w[nq] = w[q]; w[nq].len = w[p].len + 1; w[nq].cnt = 0;

			w[np].pre = w[q].pre = nq;
			for(; p >= 0 && w[p].son[c] == q; p = w[p].pre) {
				w[p].son[c] = nq;
			}
		}
	}
	void finish(int len) {
		memset(head, -1, sizeof(head));
		for(int i = 0; i <= sz; i++) {
			w[i].next = head[w[i].len];
			head[w[i].len] = i;
		}
		for(int i = len; i >= 0; i--) {
			for(int j = head[i]; ~j; j = w[j].next) {
				if(w[j].pre != -1) {
					w[ w[j].pre ].cnt += w[j].cnt;
				}
			}
		}
//
//		for(int i = 0; i <= sz; i++) {
//			cout<<i<<":  "<<w[i].cnt<<endl;
//		}
	}

	int solve(char str[]) {
		memset(vis, false, sizeof(vis));
		int ans = 0;
		int len = strlen(str);
		for(int i = 0, p = 0, now = 0; i < len * 2 - 1; i++) {
			int c = str[i % len] - 'a';
			if(w[p].son[c]) {
				now++;
				p = w[p].son[c];
			}
			else {
				for( ; p >= 0 && w[p].son[c] == 0; p = w[p].pre);

				if(p == -1) {
					now = 0;
					p = 0;
				}
				else {
					now = w[p].len + 1;
					p = w[p].son[c];
				}
			}

			if(now >= len) {
				int temp = p;

				while(w[temp].pre != -1 && w[ w[temp].pre ].len >= len) temp = w[temp].pre;

				if(!vis[temp]) {
					//cout<<w[temp].cnt<<endl;
					ans += w[temp].cnt;
					vis[temp] = true;
				}

			}
		}
		return ans;
	}
};

SAM gao;
char str[maxn];

void init() {
	gao.init();
}

void build() {
	int len = strlen(str);
	for(int i = 0; i < len; i++)
		gao.add(str[i] - 'a');
	gao.finish(len);
}

void solve() {
	int m;
	scanf("%d", &m);
	while(m--) {
		scanf("%s", str);
		printf("%d\n", gao.solve(str));
	}
}

int main() {
	while(~scanf("%s", str)) {
		init();
		build();
		solve();
	}
}

/*
abbabbabba
5
bba
*/


#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

const int maxn = 1111111;

struct SAM {
    struct Snode {
        int son[26], pre, len;
        int next, cnt;
        inline void clear() {
            memset(son, 0, sizeof(son));
            pre = len = 0;
        }
    }w[maxn * 2 + 10];
    int tail, sz;
    int head[maxn * 2 + 10];
    int vis[maxn * 2 + 10];

    inline void init() {
        tail = sz = 0;
        w[0].clear();
        w[0].pre = -1;
        memset(vis, -1, sizeof(vis));
    }

    inline void add(int c) {
        int p = tail, np = ++sz;
        w[np].clear(); w[np].len = w[p].len + 1; w[np].cnt = 1;
        tail = sz;

        for(; p >= 0 && w[p].son[c] == 0; p = w[p].pre) {
            w[p].son[c] = np;
        }

        if(p >= 0 && w[p].len + 1 == w[ w[p].son[c] ].len) {
            w[np].pre = w[p].son[c];
        }
        else if(p >= 0) {
            int q = w[p].son[c], nq = ++sz;
            w[nq] = w[q]; w[nq].len = w[p].len + 1; w[nq].cnt = 0;

            w[np].pre = w[q].pre = nq;
            for(; p >= 0 && w[p].son[c] == q; p = w[p].pre) {
                w[p].son[c] = nq;
            }
        }
    }
    inline void finish(int len) {
        memset(head, -1, sizeof(head));
        for(int i = 0; i <= sz; i++) {
            w[i].next = head[w[i].len];
            head[w[i].len] = i;
        }
        for(int i = len; i >= 0; i--) {
            for(int j = head[i]; ~j; j = w[j].next) {
                if(w[j].pre != -1) {
                    w[ w[j].pre ].cnt += w[j].cnt;
                }
            }
        }
//
//      for(int i = 0; i <= sz; i++) {
//          cout<<i<<":  "<<w[i].cnt<<endl;
//      }
    }
    inline int solve(char str[], int fuck) {
        int ans = 0;
        int len = strlen(str);
        for(int i = 0, p = 0, now = 0; i < len * 2 - 1; i++) {
            int c = str[i % len] - 'a';
            if(w[p].son[c]) {
                now++;
                p = w[p].son[c];
            }
            else {
                for( ; p >= 0 && w[p].son[c] == 0; p = w[p].pre);

                if(p == -1) {
                    now = 0;
                    p = 0;
                }
                else {
                    now = w[p].len + 1;
                    p = w[p].son[c];
                }
            }

            if(now >= len) {
                int &temp = p;
                bool flag = true;
                while(temp >= 0 && w[temp].pre != -1 && w[ w[temp].pre ].len >= len) {
                    if(vis[temp] == fuck) {
                        flag = false;
                        break;
                    }
                    vis[temp] = fuck;
                    temp = w[temp].pre;
                }
                if(vis[temp] != fuck && flag) {
                    ans += w[temp].cnt;
                    vis[temp] = fuck;
                }
            }
        }
        return ans;
    }
};

SAM gao;
char str[maxn];

void init() {
    gao.init();
}

void build() {
    int len = strlen(str);
    for(int i = 0; i < len; i++)
        gao.add(str[i] - 'a');
    gao.finish(len);
}

void solve() {
    int m;
    scanf("%d", &m);
    for(int i = 0; i < m; i++) {
        scanf("%s", str);
        printf("%d\n", gao.solve(str, i));
    }
}

int main() {
    while(~scanf("%s", str)) {
        init();
        build();
        solve();
    }
}
/*
abbabbabba
5
bba
*/


#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int inf=(1<<30);
const int N=3100010;
//---------------------------------------------------------

int nn,n,m;
int who[N],len[N],dis[N];
char s[1000005];
int sum[N];
int cir[100005];
int ans[100005];

int next[1000005];
void getnext(char sss[],int x) {
    int i=0,j=-1;
    next[0]=-1;
    while(i<x) {
        if(j==-1||sss[i]==sss[j]) {
            i++; j++;
            if(sss[i]==sss[j])
                next[i]=next[j];
            else
                next[i]=j;
        }
        else j=next[j];
    }
}

int r[3*N]; //3倍
int wa[N];
int wb[N];
int wc[N];
int wv[N];
int sa[3*N]; //3倍
int rank[N];
int height[N];
//dc3
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
int c0(int *r,int a,int b) {
    return r[a]==r[b]&&r[a+1]==r[b+1]&&r[a+2]==r[b+2];
}
int c12(int k,int *r,int a,int b) {
    if(k==2) return (r[a]<r[b])||(r[a]==r[b]&&c12(1,r,a+1,b+1));
    else return (r[a]<r[b])||(r[a]==r[b]&&wv[a+1]<wv[b+1]);
}
void sort(int *r,int *a,int *b,int n,int m) {
    int i;
    for(i=0;i<n;i++) wv[i]=r[a[i]];
    for(i=0;i<m;i++) wc[i]=0;
    for(i=0;i<n;i++) wc[wv[i]]++;
    for(i=1;i<m;i++) wc[i]+=wc[i-1];
    for(i=n-1;i>=0;i--) b[--wc[wv[i]]]=a[i];
}
void dc3(int *r,int *sa,int n,int m) {
    int i,j,*rn=r+n,*san=sa+n,ta=0,tb=(n+1)/3,tbc=0,p;
    r[n]=r[n+1]=0;
    for(i=0;i<n;i++) if(i%3!=0) wa[tbc++]=i;
    sort(r+2,wa,wb,tbc,m);
    sort(r+1,wb,wa,tbc,m);
    sort(r,wa,wb,tbc,m);
    for(p=1,rn[F(wb[0])]=0,i=1;i<tbc;i++)
        rn[F(wb[i])]=c0(r,wb[i-1],wb[i])?p-1:p++;
    if(p<tbc) dc3(rn,san,tbc,p);
    else for(i=0; i<tbc; i++) san[rn[i]]=i;
    for(i=0;i<tbc;i++) if(san[i]<tb) wb[ta++]=san[i]*3;
    if(n%3==1) wb[ta++]=n-1;
    sort(r,wb,wa,ta,m);
    for(i=0;i<tbc;i++) wv[wb[i]=G(san[i])]=i;
    for(i=0,j=0,p=0;i<ta&&j<tbc;p++)
        sa[p]=c12(wb[j]%3,r,wa[i],wb[j])?wa[i++]:wb[j++];
    for(;i<ta;p++) sa[p]=wa[i++];
    for(;j<tbc;p++) sa[p]=wb[j++];
}
void calheight(int r[],int sa[],int n) {
    int i,j,k=0;
    for(i=1;i<=n;i++) rank[sa[i]]=i;
    for(i=0;i<n;height[rank[i++]]=k)
        for(k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
}
int st[23][N];
int tlog2[N];
void rmq(int n) {
    for(int i=1;i<=n;i++) st[0][i]=height[i];
    for(int j=1;j<=tlog2[n];j++)
        for(int i=1;j<=tlog2[n+1-i];i++)
            st[j][i]=min(st[j-1][i],st[j-1][i+(1<<j>>1)]);
}
int get_rmq(int l,int r) {
    int tmp=tlog2[r-l+1];
    return min(st[tmp][l],st[tmp][r-(1<<tmp)+1]);
}

int main()
{
    int i,j=0,t,cas=0;
    int dd=27;
    tlog2[0]=-1;
    for(i=1;i<N;i++)
        tlog2[i]=(i&(i-1))?tlog2[i-1]:tlog2[i-1]+1;

    n=0;
    memset(who,-1,sizeof(who));

    scanf("%s",s);
    len[0]=strlen(s);
    for(i=0;i<len[0];i++) {
        r[i]=s[i]-'a'+1;
        who[i]=0;
    }
    n=len[0];

    scanf("%d",&nn);
    for(i=1;i<=nn;i++) {
        scanf("%s",s);
        len[i]=strlen(s);
        getnext(s,len[i]);
        if(len[i]%(len[i]-next[len[i]])!=0) cir[i]=1;
        else cir[i]=len[i]/(len[i]-next[len[i]]);
        r[n++]=dd++;
        for(j=0;j<len[i];j++) {
            r[n]=s[j]-'a'+1;
            who[n]=i;
            dis[n++]=len[i]*2-j;
        }
        for(j=0;j<len[i];j++) {
            r[n]=s[j]-'a'+1;
            who[n]=i;
            dis[n++]=len[i]-j;
        }
    }
    r[n]=0;

    dc3(r,sa,n+1,dd);
    calheight(r,sa,n);
    rmq(n);

    sum[0]=0;
    for(i=1;i<=n;i++) {
        sum[i]=sum[i-1];
        if(who[sa[i]]==0) sum[i]++;
    }

    memset(ans,0,sizeof(ans));
    for(i=1;i<=n;i++) {
        int idx=who[sa[i]];
        if(idx>0&&dis[sa[i]]>len[idx]) {

            int bot=0,top=i,mid;
            int lx=i;
            while(top>=bot) {
                mid=(top+bot)>>1;
                int tmp=get_rmq(mid,i);
                if(tmp>=len[idx]) top=mid-1,lx=mid-1;
                else bot=mid+1;
            }

            bot=i+1,top=n;
            int rx=i;
            while(top>=bot) {
                mid=(top+bot)>>1;
                int tmp=get_rmq(i+1,mid);
                if(tmp>=len[idx]) bot=mid+1,rx=mid;
                else top=mid-1;
            }
            ans[idx]+=sum[rx]-sum[lx-1];
        }
    }

    for(i=1;i<=nn;i++) {
        printf("%d\n",ans[i]/cir[i]);
    }

    return 0;
}

/*
abbabbabba
5
ab
bba
bbab
b
bbabb
*/


#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

const int maxn = 1111111;

struct SAM {

	struct Snode {
		int son[26], pre, len;
		int next, cnt;
		inline void clear() {
			memset(son, 0, sizeof(son));
			pre = len = 0;
		}
	}w[maxn * 2 + 10];
	int tail, sz;
	int head[maxn * 2 + 10];
	bool vis[maxn * 2 + 10];

	void init() {
		tail = sz = 0;
		w[0].clear();
		w[0].pre = -1;
	}

	void add(int c) {
		int p = tail, np = ++sz;
		w[np].clear(); w[np].len = w[p].len + 1; w[np].cnt = 1;
		tail = sz;

		for(; p >= 0 && w[p].son[c] == 0; p = w[p].pre) {
			w[p].son[c] = np;
		}

		if(p >= 0 && w[p].len + 1 == w[ w[p].son[c] ].len) {
			w[np].pre = w[p].son[c];
		}
		else if(p >= 0) {
			int q = w[p].son[c], nq = ++sz;
			w[nq] = w[q]; w[nq].len = w[p].len + 1; w[nq].cnt = 0;

			w[np].pre = w[q].pre = nq;
			for(; p >= 0 && w[p].son[c] == q; p = w[p].pre) {
				w[p].son[c] = nq;
			}
		}
	}
	void finish(int len) {
		memset(head, -1, sizeof(head));
		for(int i = 0; i <= sz; i++) {
			w[i].next = head[w[i].len];
			head[w[i].len] = i;
		}
		for(int i = len; i >= 0; i--) {
			for(int j = head[i]; ~j; j = w[j].next) {
				if(w[j].pre != -1) {
					w[ w[j].pre ].cnt += w[j].cnt;
				}
			}
		}
//
//		for(int i = 0; i <= sz; i++) {
//			cout<<i<<":  "<<w[i].cnt<<endl;
//		}
	}

	int solve(char str[]) {
		memset(vis, false, sizeof(vis));
		int ans = 0;
		int len = strlen(str);
		for(int i = 0, p = 0, now = 0; i < len * 2 - 1; i++) {
			int c = str[i % len] - 'a';
			if(w[p].son[c]) {
				now++;
				p = w[p].son[c];
			}
			else {
				for( ; p >= 0 && w[p].son[c] == 0; p = w[p].pre);

				if(p == -1) {
					now = 0;
					p = 0;
				}
				else {
					now = w[p].len + 1;
					p = w[p].son[c];
				}
			}

			if(now >= len && !vis[p]) {
				ans += w[p].cnt;
				vis[p] = true;
			}
		}
		return ans;
	}
};

SAM gao;
char str[maxn];

void init() {
	gao.init();
}

void build() {
	int len = strlen(str);
	for(int i = 0; i < len; i++)
		gao.add(str[i] - 'a');
	gao.finish(len);
}

void solve() {
	int m;
	scanf("%d", &m);
	while(m--) {
		scanf("%s", str);
		printf("%d\n", gao.solve(str));
	}
}

int main() {
	while(~scanf("%s", str)) {
		init();
		build();
		solve();
	}
}

/*
aa
3
aa
*/


#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

const int maxn = 1111111;

struct SAM {
    struct Snode {
        int son[26], pre, len;
        int next, cnt;
        inline void clear() {
            memset(son, 0, sizeof(son));
            pre = len = 0;
            cnt = 1;
        }
    }w[maxn * 2 + 10];
    int tail, sz;
    int head[maxn * 2 + 10];
    int vis[maxn * 2 + 10];

    void init() {
        tail = sz = 0;
        w[0].clear();
        w[0].pre = -1;
        memset(vis, -1, sizeof(vis));
    }

    void add(int c) {
        int p = tail, np = ++sz;
        w[np].clear(); w[np].len = w[p].len + 1;
        tail = sz;

        for(; p >= 0 && w[p].son[c] == 0; p = w[p].pre) {
            w[p].son[c] = np;
        }

        if(p >= 0 && w[p].len + 1 == w[ w[p].son[c] ].len) {
            w[np].pre = w[p].son[c];
        }
        else if(p >= 0) {
            int q = w[p].son[c], nq = ++sz;
            w[nq] = w[q]; w[nq].len = w[p].len + 1; w[nq].cnt = 0;

            w[np].pre = w[q].pre = nq;
            for(; p >= 0 && w[p].son[c] == q; p = w[p].pre) {
                w[p].son[c] = nq;
            }
        }
    }
    void finish(int len) {
        memset(head, -1, sizeof(head));
        for(int i = 0; i <= sz; i++) {
            w[i].next = head[w[i].len];
            head[w[i].len] = i;
        }
        for(int i = len; i >= 0; i--) {
            for(int j = head[i]; ~j; j = w[j].next) {
                if(w[j].pre != -1) {
                    w[ w[j].pre ].cnt += w[j].cnt;
                }
            }
        }
//
//      for(int i = 0; i <= sz; i++) {
//          cout<<i<<":  "<<w[i].cnt<<endl;
//      }
    }

    int solve(char str[], int fuck) {
        int ans = 0;
        int len = strlen(str);
        for(int i = 0, p = 0, now = 0; i < len * 2 - 1; i++) {
            int c = str[i % len] - 'a';
            if(w[p].son[c]) {
                now++;
                p = w[p].son[c];
            }
            else {
                for( ; p >= 0 && w[p].son[c] == 0; p = w[p].pre);

                if(p == -1) {
                    now = 0;
                    p = 0;
                }
                else {
                    now = w[p].len + 1;
                    p = w[p].son[c];
                }
            }

            if(now >= len) {
                int &temp = p;
                bool flag = true;
                while(temp >= 0 && w[temp].pre != -1 && w[ w[temp].pre ].len >= len) {
                    if(vis[temp] == fuck) {
                        flag = false;
                        break;
                    }
                    vis[temp] = fuck;
                    temp = w[temp].pre;
                }
                if(vis[temp] != fuck && flag) {
                    ans += w[temp].cnt;
                    vis[temp] = fuck;
                }
            }
        }
        return ans;
    }
};

SAM gao;
char str[maxn];

void init() {
    gao.init();
}

void build() {
    int len = strlen(str);
    for(int i = 0; i < len; i++)
        gao.add(str[i] - 'a');
    gao.finish(len);
}

void solve() {
    int m;
    scanf("%d", &m);
    for(int i = 0; i < m; i++) {
        scanf("%s", str);
        printf("%d\n", gao.solve(str, i));
    }
}

int main() {
    while(~scanf("%s", str)) {
        init();
        build();
        solve();
    }
}
/*
abbabbabba
5
bba
*/


#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

const int maxn = 1111111;

struct SAM {
    struct Snode {
        int son[26], pre, len;
        int next, cnt;
        inline void clear() {
            memset(son, 0, sizeof(son));
            pre = len = 0;
        }
    }w[maxn * 2 + 10];
    int tail, sz;
    int head[maxn * 2 + 10];

    void init() {
        tail = sz = 0;
        w[0].clear();
        w[0].pre = -1;
    }

    void add(int c) {
        int p = tail, np = ++sz;
        w[np].clear(); w[np].len = w[p].len + 1; w[np].cnt = 1;
        tail = sz;

        for(; p >= 0 && w[p].son[c] == 0; p = w[p].pre) {
            w[p].son[c] = np;
        }

        if(p >= 0 && w[p].len + 1 == w[ w[p].son[c] ].len) {
            w[np].pre = w[p].son[c];
        }
        else if(p >= 0) {
            int q = w[p].son[c], nq = ++sz;
            w[nq] = w[q]; w[nq].len = w[p].len + 1; w[nq].cnt = 0;

            w[np].pre = w[q].pre = nq;
            for(; p >= 0 && w[p].son[c] == q; p = w[p].pre) {
                w[p].son[c] = nq;
            }
        }
    }
    void finish(int len) {
    	len = w[tail].len;
        memset(head, -1, sizeof(head));
        for(int i = 0; i <= sz; i++) {
            w[i].next = head[w[i].len];
            head[w[i].len] = i;
        }
        for(int i = len; i >= 0; i--) {
            for(int j = head[i]; ~j; j = w[j].next) {
                if(w[j].pre != -1) {
                    w[ w[j].pre ].cnt += w[j].cnt;
                }
            }
        }
//
//      for(int i = 0; i <= sz; i++) {
//          cout<<i<<":  "<<w[i].cnt<<endl;
//      }
    }

    int solve(char str[], int tp, int len) {
        int ans = 0;
        for(int i = 0, p = 0, now = 0; i < tp; i++) {
            int c = str[i % len] - 'a';
            if(w[p].son[c]) {
                now++;
                p = w[p].son[c];
            }
            else {
                for( ; p >= 0 && w[p].son[c] == 0; p = w[p].pre);

                if(p == -1) {
                    now = 0;
                    p = 0;
                }
                else {
                    now = w[p].len + 1;
                    p = w[p].son[c];
                }
            }

            if(now >= len) {
                while(p >= 0 && w[p].pre != -1 && w[ w[p].pre ].len >= len) {
                    p = w[p].pre;
                }
                ans += w[p].cnt;
            }
        }
        return ans;
    }
};

SAM gao;
char str[maxn];
int fail[maxn];

void init() {
    gao.init();
}

void build() {
    int len = strlen(str);
    for(int i = 0; i < len; i++)
        gao.add(str[i] - 'a');
    gao.finish(len);
}

void make_fail(char s[]) {
    fail[0] = -1;
    for(int i = 1, j = -1; s[i]; i++) {
        while(j >= 0 && s[j + 1] != s[i]) j = fail[j];
        if(s[j + 1] == s[i])j++;
        fail[i] = j;
    }
}

void solve() {
    int m;
    scanf("%d", &m);
    for(int i = 0; i < m; i++) {
        scanf("%s", str);

        make_fail(str);
        int len = strlen(str);
        int tp = len - 1 - fail[len - 1];

        if(len % tp) tp = len;
        else tp = tp;

        printf("%d\n", gao.solve(str, len + tp - 1, len));
    }
}

int main() {
    while(~scanf("%s", str)) {
        init();
        build();
        solve();
    }
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

const int maxn = 1111111;

struct SAM {

    struct Snode {
        int son[26], pre, len;
        int next, cnt;
        inline void clear() {
            memset(son, 0, sizeof(son));
            pre = len = 0;
        }
    }w[maxn * 2 + 10];
    int tail, sz;
    int head[maxn * 2 + 10];
    bool vis[maxn * 2 + 10];

    void init() {
        tail = sz = 0;
        w[0].clear();
        w[0].pre = -1;
    }

    void add(int c) {
        int p = tail, np = ++sz;
        w[np].clear(); w[np].len = w[p].len + 1; w[np].cnt = 1;
        tail = sz;

        for(; p >= 0 && w[p].son[c] == 0; p = w[p].pre) {
            w[p].son[c] = np;
        }

        if(p >= 0 && w[p].len + 1 == w[ w[p].son[c] ].len) {
            w[np].pre = w[p].son[c];
        }
        else if(p >= 0) {
            int q = w[p].son[c], nq = ++sz;
            w[nq] = w[q]; w[nq].len = w[p].len + 1; w[nq].cnt = 0;

            w[np].pre = w[q].pre = nq;
            for(; p >= 0 && w[p].son[c] == q; p = w[p].pre) {
                w[p].son[c] = nq;
            }
        }
    }
    void finish(int len) {
        memset(head, -1, sizeof(head));
        for(int i = 0; i <= sz; i++) {
            w[i].next = head[w[i].len];
            head[w[i].len] = i;
        }
        for(int i = len; i >= 0; i--) {
            for(int j = head[i]; ~j; j = w[j].next) {
                if(w[j].pre != -1) {
                    w[ w[j].pre ].cnt += w[j].cnt;
                }
            }
        }
//
//      for(int i = 0; i <= sz; i++) {
//          cout<<i<<":  "<<w[i].cnt<<endl;
//      }
    }

    int solve(char str[]) {
        memset(vis, false, sizeof(vis));
        int ans = 0;
        int len = strlen(str);
        for(int i = 0, p = 0, now = 0; i < len * 2 - 1; i++) {
            int c = str[i % len] - 'a';
            if(w[p].son[c]) {
                now++;
                p = w[p].son[c];
            }
            else {
                for( ; p >= 0 && w[p].son[c] == 0; p = w[p].pre);

                if(p == -1) {
                    now = 0;
                    p = 0;
                }
                else {
                    now = w[p].len + 1;
                    p = w[p].son[c];
                }
            }

            if(now >= len) {
                int temp = p;
                bool flag = true;
                while(w[temp].pre != -1 && w[ w[temp].pre ].len >= len) {
                    if(vis[temp]) {
                        flag = false;
                        break;
                    }
                    vis[temp] = true;
                    temp = w[temp].pre;
                }
                if(!vis[temp] && flag) {
                    //cout<<w[temp].cnt<<endl;
                    ans += w[temp].cnt;
                    vis[temp] = true;
                }

            }
        }
        return ans;
    }
};

SAM gao;
char str[maxn];

void init() {
    gao.init();
}

void build() {
    int len = strlen(str);
    for(int i = 0; i < len; i++)
        gao.add(str[i] - 'a');
    gao.finish(len);
}

void solve() {
    int m;
    scanf("%d", &m);
    while(m--) {
        scanf("%s", str);
        printf("%d\n", gao.solve(str));
    }
}

int main() {
    while(~scanf("%s", str)) {
        init();
        build();
        solve();
    }
}

/*
abbabbabba
5
bba
*/


#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

const int maxn = 1111111;

struct SAM {
    struct Snode {
        int son[26], pre, len;
        int next, cnt;
        inline void clear() {
            memset(son, 0, sizeof(son));
            pre = len = 0;
        }
    }w[maxn * 2 + 10];
    int tail, sz;
    int head[maxn * 2 + 10];

    void init() {
        tail = sz = 0;
        w[0].clear();
        w[0].pre = -1;
    }

    void add(int c) {
        int p = tail, np = ++sz;
        w[np].clear(); w[np].len = w[p].len + 1; w[np].cnt = 1;
        tail = sz;

        for(; p >= 0 && w[p].son[c] == 0; p = w[p].pre) {
            w[p].son[c] = np;
        }

        if(p >= 0 && w[p].len + 1 == w[ w[p].son[c] ].len) {
            w[np].pre = w[p].son[c];
        }
        else if(p >= 0) {
            int q = w[p].son[c], nq = ++sz;
            w[nq] = w[q]; w[nq].len = w[p].len + 1; w[nq].cnt = 0;

            w[np].pre = w[q].pre = nq;
            for(; p >= 0 && w[p].son[c] == q; p = w[p].pre) {
                w[p].son[c] = nq;
            }
        }
    }
    void finish(int len) {
        memset(head, -1, sizeof(head));
        for(int i = 0; i <= sz; i++) {
            w[i].next = head[w[i].len];
            head[w[i].len] = i;
        }
        for(int i = len; i >= 0; i--) {
            for(int j = head[i]; ~j; j = w[j].next) {
                if(w[j].pre != -1) {
                    w[ w[j].pre ].cnt += w[j].cnt;
                }
            }
        }
//
//      for(int i = 0; i <= sz; i++) {
//          cout<<i<<":  "<<w[i].cnt<<endl;
//      }
    }

    int solve(char str[], int tp, int len) {
        int ans = 0;
        for(int i = 0, p = 0, now = 0; i < tp; i++) {
            int c = str[i % len] - 'a';
            if(w[p].son[c]) {
                now++;
                p = w[p].son[c];
            }
            else {
                for( ; p >= 0 && w[p].son[c] == 0; p = w[p].pre);

                if(p == -1) {
                    now = 0;
                    p = 0;
                }
                else {
                    now = w[p].len + 1;
                    p = w[p].son[c];
                }
            }

            if(now >= len) {
                while(p >= 0 && w[p].pre != -1 && w[ w[p].pre ].len >= len) {
                    p = w[p].pre;
                }
                ans += w[p].cnt;
            }
        }
        return ans;
    }
};

SAM gao;
char str[maxn];
int fail[maxn];

void init() {
    gao.init();
}

void build() {
    int len = strlen(str);
    for(int i = 0; i < len; i++)
        gao.add(str[i] - 'a');
    gao.finish(len);
}

void make_fail(char s[]) {
    fail[0] = -1;
    for(int i = 1, j = -1; s[i]; i++) {
        while(j >= 0 && s[j + 1] != s[i]) j = fail[j];
        if(s[j + 1] == s[i])j++;
        fail[i] = j;
    }
}

void solve() {
    int m;
    scanf("%d", &m);
    for(int i = 0; i < m; i++) {
        scanf("%s", str);

        make_fail(str);
        int len = strlen(str);
        int tp = len - 1 - fail[len - 1];

        if(len % tp) tp = len;
        else tp = tp;

        printf("%d\n", gao.solve(str, len + tp - 1, len));
    }
}

int main() {
    while(~scanf("%s", str)) {
        init();
        build();
        solve();
    }
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

const int maxn = 1111111;

struct SAM {
    struct Snode {
        int son[26], pre, len;
        int next, cnt;
        inline void clear() {
            memset(son, 0, sizeof(son));
            pre = len = 0;
        }
    }w[maxn * 2 + 10];
    int tail, sz;
    int head[maxn * 2 + 10];
    int vis[maxn * 2 + 10];

    void init() {
        tail = sz = 0;
        w[0].clear();
        w[0].pre = -1;
        memset(vis, -1, sizeof(vis));
    }

    void add(int c) {
        int p = tail, np = ++sz;
        w[np].clear(); w[np].len = w[p].len + 1; w[np].cnt = 1;
        tail = sz;

        for(; p >= 0 && w[p].son[c] == 0; p = w[p].pre) {
            w[p].son[c] = np;
        }

        if(p >= 0 && w[p].len + 1 == w[ w[p].son[c] ].len) {
            w[np].pre = w[p].son[c];
        }
        else if(p >= 0) {
            int q = w[p].son[c], nq = ++sz;
            w[nq] = w[q]; w[nq].len = w[p].len + 1; w[nq].cnt = 0;

            w[np].pre = w[q].pre = nq;
            for(; p >= 0 && w[p].son[c] == q; p = w[p].pre) {
                w[p].son[c] = nq;
            }
        }
    }
    void finish(int len) {
        memset(head, -1, sizeof(head));
        for(int i = 0; i <= sz; i++) {
            w[i].next = head[w[i].len];
            head[w[i].len] = i;
        }
        for(int i = len; i >= 0; i--) {
            for(int j = head[i]; ~j; j = w[j].next) {
                if(w[j].pre != -1) {
                    w[ w[j].pre ].cnt += w[j].cnt;
                }
            }
        }
//
//      for(int i = 0; i <= sz; i++) {
//          cout<<i<<":  "<<w[i].cnt<<endl;
//      }
    }

    int solve(char str[], int fuck) {
        int ans = 0;
        int len = strlen(str);
        for(int i = 0, p = 0, now = 0; i < len * 2 - 1; i++) {
            int c = str[i % len] - 'a';
            if(w[p].son[c]) {
                now++;
                p = w[p].son[c];
            }
            else {
                for( ; p >= 0 && w[p].son[c] == 0; p = w[p].pre);

                if(p == -1) {
                    now = 0;
                    p = 0;
                }
                else {
                    now = w[p].len + 1;
                    p = w[p].son[c];
                }
            }

            if(now >= len) {
                int &temp = p;
                bool flag = true;
                while(temp >= 0 && w[temp].pre != -1 && w[ w[temp].pre ].len >= len) {
                    if(vis[temp] == fuck) {
                        flag = false;
                        break;
                    }
                    vis[temp] = fuck;
                    temp = w[temp].pre;
                }
                if(vis[temp] != fuck && flag) {
                    ans += w[temp].cnt;
                    vis[temp] = fuck;
                }
            }
        }
        return ans;
    }
};

SAM gao;
char str[maxn];

void init() {
    gao.init();
}

void build() {
    int len = strlen(str);
    for(int i = 0; i < len; i++)
        gao.add(str[i] - 'a');
    gao.finish(len);
}

void solve() {
    int m;
    scanf("%d", &m);
    for(int i = 0; i < m; i++) {
        scanf("%s", str);
        printf("%d\n", gao.solve(str, i));
    }
}

int main() {
    while(~scanf("%s", str)) {
        init();
        build();
        solve();
    }
}
/*
abbabbabba
5
bba
*/


#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

const int maxn = 1111111;

struct SAM {
    struct Snode {
        int son[26], pre, len;
        int next, cnt;
        inline void clear() {
            memset(son, 0, sizeof(son));
            pre = len = 0;
        }
    }w[maxn * 2 + 10];
    int tail, sz;
    int head[maxn * 2 + 10];
    bool vis[maxn * 2 + 10];

    void init() {
        tail = sz = 0;
        w[0].clear();
        w[0].pre = -1;
    }

    void add(int c) {
        int p = tail, np = ++sz;
        w[np].clear(); w[np].len = w[p].len + 1; w[np].cnt = 1;
        tail = sz;

        for(; p >= 0 && w[p].son[c] == 0; p = w[p].pre) {
            w[p].son[c] = np;
        }

        if(p >= 0 && w[p].len + 1 == w[ w[p].son[c] ].len) {
            w[np].pre = w[p].son[c];
        }
        else if(p >= 0) {
            int q = w[p].son[c], nq = ++sz;
            w[nq] = w[q]; w[nq].len = w[p].len + 1; w[nq].cnt = 0;

            w[np].pre = w[q].pre = nq;
            for(; p >= 0 && w[p].son[c] == q; p = w[p].pre) {
                w[p].son[c] = nq;
            }
        }
    }
    void finish(int len) {
        memset(head, -1, sizeof(head));
        for(int i = 0; i <= sz; i++) {
            w[i].next = head[w[i].len];
            head[w[i].len] = i;
        }
        for(int i = len; i >= 0; i--) {
            for(int j = head[i]; ~j; j = w[j].next) {
                if(w[j].pre != -1) {
                    w[ w[j].pre ].cnt += w[j].cnt;
                }
            }
        }
//
//      for(int i = 0; i <= sz; i++) {
//          cout<<i<<":  "<<w[i].cnt<<endl;
//      }
    }

    int solve(char str[]) {
        memset(vis, false, sizeof(vis));
        int ans = 0;
        int len = strlen(str);
        for(int i = 0, p = 0, now = 0; i < len * 2 - 1; i++) {
            int c = str[i % len] - 'a';
            if(w[p].son[c]) {
                now++;
                p = w[p].son[c];
            }
            else {
                for( ; p >= 0 && w[p].son[c] == 0; p = w[p].pre);

                if(p == -1) {
                    now = 0;
                    p = 0;
                }
                else {
                    now = w[p].len + 1;
                    p = w[p].son[c];
                }
            }

            if(now >= len) {
                int &temp = p;
                bool flag = true;
                while(temp >= 0 && w[temp].pre != -1 && w[ w[temp].pre ].len >= len) {
                    if(vis[temp]) {
                        flag = false;
                        break;
                    }
                    vis[temp] = true;
                    temp = w[temp].pre;
                }
                if(!vis[temp] && flag) {
                    //cout<<w[temp].cnt<<endl;
                    ans += w[temp].cnt;
                    vis[temp] = true;
                }
            }
        }
        return ans;
    }
};

SAM gao;
char str[maxn];

void init() {
    gao.init();
}

void build() {
    int len = strlen(str);
    for(int i = 0; i < len; i++)
        gao.add(str[i] - 'a');
    gao.finish(len);
}

void solve() {
    int m;
    scanf("%d", &m);
    while(m--) {
        scanf("%s", str);
        printf("%d\n", gao.solve(str));
    }
}

int main() {
    while(~scanf("%s", str)) {
        init();
        build();
        solve();
    }
}
/*
abbabbabba
5
bba
*/


// hpfdf
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <iomanip>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<(n);++i)
#define fab(i,a,b) for(int i=(a);i<=(b);++i)
#define fba(i,b,a) for(int i=(b);i>=(a);--i)
#define clr(a) memset(a,0,sizeof(a))
#define fec(i,a) for(__typeof((a).end())i=(a).begin();i!=(a).end();++i)
#define fpc(i,j,v) for(int i=a[v],j;j=to[i],i;i=nx[i])

#define N 1111111

char s[N], t[N];

struct sanode {
    sanode *f, *ch[26];
    int l, nl, ml;
    ll ct;
}pool[N*2 + 18], *init = pool, *tail = pool, *p[N*2 + 18];

int n, tot, l;

void add(int c)
{
    sanode *q = pool + (++tot), *p = tail;
    memset(q, 0, sizeof(sanode));
    q->l = q->ml = p->l + 1; q->nl = 0; q->ct = -1;
    tail = q;
    while (p && !p->ch[c]) p->ch[c] = q, p = p->f;
    if (!p) q->f = init;
    else
        if (p->ch[c]->l == p->l + 1) q->f = p->ch[c];
        else {
            sanode *r = pool + (++tot), *u = p->ch[c];
            memcpy(r->ch, u->ch, sizeof(size_t)*26); r->nl = 0; r->ct = -1;
            r->l = r->ml = p->l + 1, r->f = u->f;
            u->f = q->f = r;
            while (p && p->ch[c] == u) p->ch[c] = r, p = p->f;
        }
}

void dfs(sanode * v)
{
    if (v->ct >= 0) return;
    if (v->ct == -1)
        v->ct = 0;
    else
        v->ct = 1;
    for (int i=0; i < 26; ++i)
        if (v->ch[i])
        {
            dfs(v->ch[i]);
            v->ct += v->ch[i]->ct;
        }
}

int main()
{
    scanf("%s", s);

    l = strlen(s);

    init->ct = -1;

    rep(i,l) add(s[i] - 'a');

    sanode *pp = tail;
    while (pp)
    {
        pp->ct = -2;
        pp = pp->f;
    }

    dfs(init);

    scanf("%d", &n);

    fab(ii,1,n)
    {
        scanf("%s", t);
        int m = strlen(t);

        sanode *a = init;
        int ret = 0;
        for (int i = 0, nl = 0; i < m*2; ++i)
        {
            int c = t[i<m?i:i-m] - 'a';
            if (a->ch[c])
            {
                a = a->ch[c], ++nl;
            }
            else
            {
                while (a && !a->ch[c]) a = a->f;
                if (a)
                {
                    nl = a->l + 1, a = a->ch[c];
                }
                else
                    a = init, nl = 0;
            }
            if (nl >= m)
            {
                if (a->f)
                while (a->f->l >= m)
                    a = a->f, nl = a->l;

                if (a->nl != ii)
                {
                    a->nl = ii;
                    ret += a->ct;
                }
            }
        }

        printf("%d\n", ret);
    }

    return 0;
}


/*
Codeforces: Cyclical Quest
http://codeforces.com/contest/235/problem/C
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.

Sample test(s)
input
baabaabaaa
5
a
ba
baa
aabaa
aaba
output
7
5
7
3
5
input
aabbaa
3
aa
aabb
abba
output
2
3
3
*/

//#pragma comment(linker, "/STACK:60000000")

#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <set>
#include <cmath>
#include <cstring>

#include <stdio.h>
#include <string.h>
#include <sstream>
#include <stdlib.h>
#include <vector>
#include <iomanip>
#include <ctime>
#include <assert.h>

using namespace std;

//Type Definition
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi>vvi;
typedef vector<string> vs;
typedef map<string,int> msi;
typedef map<int,int>mii;

#define ERR 1e-9
#define PI 3.141592653589793

#define REP(i,n) for (i=0;i<n;i++)
#define FOR(i,p,k) for (i=p; i<k;i++)
#define FOREACH(it,x) for(__typeof((x).begin()) it=(x.begin()); it!=(x).end(); ++it)

#define Sort(x) sort(x.begin(),x.end())
#define Reverse(x) reverse(x.begin(),x.end())
#define MP(a,b) make_pair(a,b)
#define Clear(x,with) memset(x,with,sizeof(x))
#define SZ(x) (int)x.size()
#define pb push_back
#define popcount(i) __builtin_popcount(i)
#define gcd(a,b)    __gcd(a,b)
#define lcm(a,b) ((a)*((b)/gcd(a,b)))
#define two(X) (1<<(X))
#define twoL(X) (((ll)(1))<<(X))
#define setBit(mask,i) (mask|two(i))
#define contain(S,X) (((S)&two(X))!=0)
#define fs first
#define sc second
#define CS c_str()
#define EQ(a,b) (fabs(a-b)<ERR)
#define Unique(store) store.resize(unique(store.begin(),store.end())-store.begin());
#define Find(x,y) ((int)x.find(y))

//For debugging
#define debug_array(a,n) for(ll i=0;i<n;i++) cerr<<a[i]<<" "; cerr<<endl;
#define debug_matrix(mat,row,col) for(ll i=0;i<row;i++) {for(ll j=0;j<col;j++) cerr<<mat[i][j]<<" ";cerr<<endl;}
#define debug(args...) {dbg,args; cerr<<endl;}
struct debugger{template<typename T> debugger& operator , (const T& v){cerr<<v<<"\t"; return *this; }}dbg;

//Important Functions
template<class T> void setmax(T &a, T b) { if(a < b) a = b; }
template<class T> void setmin(T &a, T b) { if(b < a) a = b; }
template<class T> T Abs(T x) {return x > 0 ? x : -x;}
template<class T> inline T sqr(T x){return x*x;}
template<class T> inline T Mod(T n,T m) {return (n%m+m)%m;} //For Positive Negative No.
template<class T> string toString(T n){ostringstream oss;oss<<n;oss.flush();return oss.str();}
ll toInt(string s){ll r=0;istringstream sin(s);sin>>r;return r;}
bool isVowel(char ch){ch=tolower(ch);if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')return true;return false;}
bool isUpper(char c){return c>='A' && c<='Z';}
bool isLower(char c){return c>='a' && c<='z';}
ll Pow(ll B,ll P){  ll R=1; while(P>0)  {if(P%2==1) R=(R*B);P/=2;B=(B*B);}return R;}
ll BigMod(ll B,ll P,ll M){  ll R=1; while(P>0)  {if(P%2==1){R=(R*B)%M;}P/=2;B=(B*B)%M;} return R;}
void binprint(ll mask,ll n){ll i;string s="";do{s+=(mask%2+'0');mask/=2;}while(mask);Reverse(s);s=string(max(n-SZ(s),0LL),'0')+s;for(i=SZ(s)-n;i<SZ(s);i++) printf("%c",s[i]);printf("\n");}
void ASCII_Chart(){ll i,j,k;printf("ASCII Chart:(30-129)\n");FOR(i,30,50){REP(j,5){k=i+j*20;printf("%3d---> '%c'   ",k,k);}printf("\n");}}

//int month[]={-1,31,28,31,30,31,30,31,31,30,31,30,31};  //Not Leap Year

//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction
//int dx[]={-1,-1,+0,+1,+1,+0};int dy[]={-1,+1,+2,+1,-1,-2}; //Hexagonal Direction

//#include<conio.h> //for using getch();

//struct edge{ int cost,node; edge(int _cost=0,int _node=0){cost=_cost;node=_node;}bool operator<(const edge &b)const {return cost>b.cost;}}; // Min Priority Queue
//bool comp(edge a,edge b){ return a.cost < b.cost;} //Asc Sort by cost

#define INF (1<<28)
#define SIZE 1000100

#define STATE_SIZE 2*SIZE

struct state
{
    int len,link,visit,cnt;
    map<char,int>child;
};

state T[STATE_SIZE];
char temp[SIZE];
int sz,last;
string str;

void Initialize(int N)
{
    int i;
    REP(i,2*N)
    {
        T[i].len=T[i].visit=0;
        T[i].link=-1;
        T[i].child.clear();
    }
    sz=1;
    last=0;
}

void add_char(char ch)
{
    int cur=sz++;
    T[cur].len=T[last].len+1;
    T[cur].cnt=1;

    int p;
    for(p=last; p!=-1 && T[p].child[ch]==0;p=T[p].link)
        T[p].child[ch]=cur;
    if(p==-1)
        T[cur].link=0;
    else
    {
        int q=T[p].child[ch];
        if(T[p].len+1==T[q].len)
            T[cur].link=q;
        else
        {
            int clone=sz++;
            T[clone].link=T[q].link;
            T[clone].len=T[p].len+1;
            T[clone].child=T[q].child;
            for(p=p;p!=-1 && T[p].child[ch]==q;p=T[p].link)
                T[p].child[ch]=clone;
            T[cur].link=T[q].link=clone;
        }
    }
    last=cur;
}

vector<int>GraphByLen[SIZE];

void DP_Processing()
{
    int i,j,u;

    REP(i,SZ(str)+1) GraphByLen[i].clear();
    REP(i,sz) GraphByLen[T[i].len].pb(i);

    for(i=SZ(str);i>=1;i--)
    {
        REP(j,SZ(GraphByLen[i]))
        {
            u=GraphByLen[i][j];
            T[T[u].link].cnt+=T[u].cnt;
        }
    }
}

int solve(string s,int VisitNo)
{
    int L,R,cur,len=SZ(s),ans;

    s=s+s;
    L=R=cur=ans=0;
    while(R<SZ(s) && L<=R)
    {
        while(R<SZ(s) && R-L<len)
        {
            if(T[cur].child[s[R]]==0) break;
            cur=T[cur].child[s[R]];
            R++;
        }
        if(R-L==len)
        {
            if(T[cur].visit!=VisitNo)
            {
                T[cur].visit=VisitNo;
                ans+=T[cur].cnt;
            }
        }
        if(cur!=0 && T[T[cur].link].len==(R-L-1))
            cur=T[cur].link;
        L++;
    }
    return ans;
}

string s;

int main()
{
    //freopen("A.in","r",stdin);
    //freopen("A.out","w",stdout);

    int i,j,test,Case=1,Q,ans;

    while(scanf("%s",temp)==1)
    {
        str=temp;
        Initialize(SZ(str));
        REP(i,SZ(str)) add_char(str[i]);
        DP_Processing();
        scanf("%d",&Q);
        while(Q--)
        {
            scanf("%s",temp);
            s=temp;
            ans=solve(s,Case++);
            printf("%d\n",ans);
        }
    }

    return 0;
}

/*
bbbbbbbbbbbbbbb
5
ba
bbb
bbbbbb
bbb
b
*/