#include <algorithm>
#include <cstdio>
#include <cstring>

const int N = 62;

struct graph {
	struct edge {
		int to, next;
	} e[N];
	int head[N], cnt;
	void operator()(int x, int y) { e[++cnt] = (edge){y, x[head]}, x[head] = cnt; }
};
struct SAM {
	int len[N], tr[N][26], link[N], cnt[N], nc, last;
	void ins(int c) {
		int cur = ++nc;
		cur[len] = last[len] + 1;
		int p = last, q, cl;
		for (; p && !(q = p[tr][c]); p = p[link]) p[tr][c] = cur;
		if (!p)
			cur[link] = 1;
		else if (p[len] + 1 == q[len])
			cur[link] = q;
		else {
			cl = ++nc;
			cl[len] = p[len] + 1, cl[link] = q[link];
			memcpy(cl[tr], q[tr], sizeof *tr);
			for (; p && p[tr][c] == q; p = p[link]) p[tr][c] = cl;
			q[link] = cur[link] = cl;
		}
		cur[cnt] = 1, last = cur;
	}
	void operator+=(char c) { ins(c - 97); }
	void operator+=(char *s) {
		while (*s) ins(*s++ - 97);
	}
	SAM() { nc = last = 1; }

	graph g;
	void dfs(int x) {
		for (int i = x[g.head], nx; i; i = g.e[i].next)
			dfs(nx = g.e[i].to), x[cnt] += nx[cnt];
	}
	void build() {
		for (int i = 2; i <= nc; i++) g(i[link], i);
		dfs(1);
	}

	int vis[N];
	int calc(char *s) {
		static int tm = 1;
		int n = strlen(s), x = 1, l = 0, ans = 0;
		tm++;
		for (int i = 0; i < n; i++) {
			int nx = s[i] - 'a';
			while (x && !x[tr][nx]) l = len[x = x[link]];
			if (x[tr][nx]) l++, x = x[tr][nx];
		}
		if (l == n && x[vis] < tm) x[vis] = tm, ans += x[cnt];
		for (int i = 0; i < n - 1; i++) {
			int nx = s[i] - 'a';
			while (x && !x[tr][nx]) l = len[x = x[link]];
			if (x[tr][nx]) l++, x = x[tr][nx];

			if (l > n && --l == x[link][len]) x = x[link];
			if (l >= n && x[vis] < tm) x[vis] = tm, ans += x[cnt];
		}
		return ans;
	}
} sam;

char s[N];
int main() {
	scanf("%s", s);
	sam += s;
	sam.build();
	for (scanf("%*d"); ~scanf("%s", s);) printf("%d\n", sam.calc(s));
}


#include <algorithm>
#include <cstdio>
#include <cstring>

const int N = 2e6 + 62;

struct graph {
	struct edge {
		int to, next;
	} e[N];
	int head[N], cnt;
	void operator()(int x, int y) { e[++cnt] = (edge){y, x[head]}, x[head] = cnt; }
};
struct SAM {
	int len[N], tr[N][26], link[N], cnt[N], nc, last;
	void ins(int c) {
		int cur = ++nc;
		cur[len] = last[len] + 1;
		int p = last, q, cl;
		for (; p && !(q = p[tr][c]); p = p[link]) p[tr][c] = cur;
		if (!p)
			cur[link] = 1;
		else if (p[len] + 1 == q[len])
			cur[link] = q;
		else {
			cl = ++nc;
			cl[len] = p[len] + 1, cl[link] = q[link];
			memcpy(cl[tr], q[tr], sizeof *tr);
			for (; p && p[tr][c] == q; p = p[link]) p[tr][c] = cl;
			q[link] = cur[link] = cl;
		}
		cur[cnt] = 1, last = cur;
	}
	void operator+=(char c) { ins(c - 97); }
	void operator+=(char *s) {
		while (*s) ins(*s++ - 97);
	}
	SAM() { nc = last = 1; }

	graph g;
	void dfs(int x) {
		for (int i = x[g.head], nx; i; i = g.e[i].next)
			dfs(nx = g.e[i].to), x[cnt] += nx[cnt];
	}
	void build() {
		for (int i = 2; i <= nc; i++) g(i[link], i);
		dfs(1);
	}

	int vis[N];
	int calc(char *s) {
		static int tm = 1;
		int n = strlen(s), x = 1, l = 0, ans = 0;
		tm++;
		for (int i = 0; i < n; i++) {
			int nx = s[i] - 'a';
			while (x && !x[tr][nx]) l = len[x = x[link]];
			if (x[tr][nx]) l++, x = x[tr][nx];
		}
		if (l == n && x[vis] < tm) x[vis] = tm, ans += x[cnt];
		for (int i = 0; i < n - 1; i++) {
			int nx = s[i] - 'a';
			while (x && !x[tr][nx]) l = len[x = x[link]];
			if (x[tr][nx]) l++, x = x[tr][nx];

			if (l > n && --l == x[link][len]) x = x[link];
			if (l >= n && x[vis] < tm) x[vis] = tm, ans += x[cnt];
		}
		return ans;
	}
} sam;

char s[N];
int main() {
	scanf("%s", s);
	sam += s;
	sam.build();
	for (scanf("%*d"); ~scanf("%s", s);) printf("%d\n", sam.calc(s));
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#define maxn 200000
#define maxc 26
using namespace std;
struct SAM{
#define len(x) (t[x].len)
#define link(x) (t[x].link)
	struct node{
		int ch[maxc];
		int link;
		int len;
		int cnt;
		int vis;
	}t[maxn+5];
	vector<int>E[maxn+5];
	const int root=1;
	int ptr=1;
	int last=root;
	void extend(int c){
		int p=last,cur=++ptr;
		len(cur)=len(p)+1;
		t[cur].cnt=1;
		while(p&&t[p].ch[c]==0){
			t[p].ch[c]=cur;
			p=link(p);
		}
		if(p==0) link(cur)=root;
		else{
			int q=t[p].ch[c];
			if(len(p)+1==len(q)) link(cur)=q;
			else{
				int clo=++ptr;
				link(clo)=link(q);
				memcpy(t[clo].ch,t[q].ch,sizeof(t[q].ch));
				len(clo)=len(p)+1;
				link(cur)=link(q)=clo;
				while(p&&t[p].ch[c]==q){
					t[p].ch[c]=clo;
					p=link(p);
				}
			}
		}
		last=cur;
	}
	void dfs(int x){
		for(int i=0;i<(int)E[x].size();i++){
			int y=E[x][i];
			dfs(y);
			t[x].cnt+=t[y].cnt;
		}
	}
	void build(char *s){
		int len=strlen(s+1);
		for(int i=1;i<=len;i++) extend(s[i]-'a');
		for(int i=2;i<=ptr;i++) E[link(i)].push_back(i);
		dfs(root);
	}
	int query(char *s,int qid){
		int ans=0;
		int m=strlen(s+1);
		int x=1,len=0;
		for(int i=1;i<=m;i++){//在自动机上匹配
			int c=s[i]-'a';
			while(x&&t[x].ch[c]==0){
				x=link(x);
				len=len(x);
			}
			if(t[x].ch[c]){
				len++;
				x=t[x].ch[c];
			}
		}
		if(len==m&&t[x].vis<qid){//防止一个询问中相同子串被重复计算
			ans+=t[x].cnt;
			t[x].vis=qid;
		}
		for(int i=1;i<=m;i++){//进行循环移位
			int c=s[i]-'a';
			while(x&&t[x].ch[c]==0){
				x=link(x);
				len=len(x);
			}
			if(t[x].ch[c]){
				len++;
				x=t[x].ch[c];
			}
			if(len>m){ //完整匹配时移到下一个,跳link,删除头部字符
				len--;//
				if(len==len(link(x))) x=link(x); //如果不满足len-1=len(link(x))的条件,会在添加尾部字符时转移
			}
			if(len==m&&t[x].vis<qid){//防止一个询问中相同子串被重复计算
				ans+=t[x].cnt;
				t[x].vis=qid;
			}
		}
		return ans;
	}
};
SAM T;
char s[maxn+5],t[maxn+5];
int q;
int main(){
	scanf("%s",s+1);
	scanf("%d",&q);
	T.build(s);
	for(int i=1;i<=q;i++){
		scanf("%s",t+1);
		printf("%d\n",T.query(t,i));
	}
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#define maxn 1000000
#define maxc 26
using namespace std;
struct SAM{
#define len(x) (t[x].len)
#define link(x) (t[x].link)
	struct node{
		int ch[maxc];
		int link;
		int len;
		int cnt;
		int vis;
	}t[maxn+5];
	vector<int>E[maxn+5];
	const int root=1;
	int ptr=1;
	int last=root;
	void extend(int c){
		int p=last,cur=++ptr;
		len(cur)=len(p)+1;
		t[cur].cnt=1;
		while(p&&t[p].ch[c]==0){
			t[p].ch[c]=cur;
			p=link(p);
		}
		if(p==0) link(cur)=root;
		else{
			int q=t[p].ch[c];
			if(len(p)+1==len(q)) link(cur)=q;
			else{
				int clo=++ptr;
				link(clo)=link(q);
				memcpy(t[clo].ch,t[q].ch,sizeof(t[q].ch));
				len(clo)=len(p)+1;
				link(cur)=link(q)=clo;
				while(p&&t[p].ch[c]==q){
					t[p].ch[c]=clo;
					p=link(p);
				}
			}
		}
		last=cur;
	}
	void dfs(int x){
		for(int i=0;i<(int)E[x].size();i++){
			int y=E[x][i];
			dfs(y);
			t[x].cnt+=t[y].cnt;
		}
	}
	void build(char *s){
		int len=strlen(s+1);
		for(int i=1;i<=len;i++) extend(s[i]-'a');
		for(int i=2;i<=ptr;i++) E[link(i)].push_back(i);
		dfs(root);
	}
	int query(char *s,int qid){
		int ans=0;
		int m=strlen(s+1);
		int x=1,len=0;
		for(int i=1;i<=m;i++){//在自动机上匹配
			int c=s[i]-'a';
			while(x&&t[x].ch[c]==0){
				x=link(x);
				len=len(x);
			}
			if(t[x].ch[c]){
				len++;
				x=t[x].ch[c];
			}
		}
		if(len==m&&t[x].vis<qid){//防止一个询问中相同子串被重复计算
			ans+=t[x].cnt;
			t[x].vis=qid;
		}
		for(int i=1;i<=m;i++){//进行循环移位
			int c=s[i]-'a';
			while(x&&t[x].ch[c]==0){
				x=link(x);
				len=len(x);
			}
			if(t[x].ch[c]){
				len++;
				x=t[x].ch[c];
			}
			if(len>m){ //完整匹配时移到下一个,跳link,删除头部字符
				len--;//
				if(len==len(link(x))) x=link(x); //如果不满足len-1=len(link(x))的条件,会在添加尾部字符时转移
			}
			if(len==m&&t[x].vis<qid){//防止一个询问中相同子串被重复计算
				ans+=t[x].cnt;
				t[x].vis=qid;
			}
		}
		return ans;
	}
};
SAM T;
char s[maxn+5],t[maxn+5];
int q;
int main(){
	scanf("%s",s+1);
	scanf("%d",&q);
	T.build(s);
	for(int i=1;i<=q;i++){
		scanf("%s",t+1);
		printf("%d\n",T.query(t,i));
	}
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#define maxn 400000
#define maxc 26
using namespace std;
struct SAM{
#define len(x) (t[x].len)
#define link(x) (t[x].link)
	struct node{
		int ch[maxc];
		int link;
		int len;
		int cnt;
		int vis;
	}t[maxn+5];
	vector<int>E[maxn+5];
	const int root=1;
	int ptr=1;
	int last=root;
	void extend(int c){
		int p=last,cur=++ptr;
		len(cur)=len(p)+1;
		t[cur].cnt=1;
		while(p&&t[p].ch[c]==0){
			t[p].ch[c]=cur;
			p=link(p);
		}
		if(p==0) link(cur)=root;
		else{
			int q=t[p].ch[c];
			if(len(p)+1==len(q)) link(cur)=q;
			else{
				int clo=++ptr;
				link(clo)=link(q);
				memcpy(t[clo].ch,t[q].ch,sizeof(t[q].ch));
				len(clo)=len(p)+1;
				link(cur)=link(q)=clo;
				while(p&&t[p].ch[c]==q){
					t[p].ch[c]=clo;
					p=link(p);
				}
			}
		}
		last=cur;
	}
	void dfs(int x){
		for(int i=0;i<(int)E[x].size();i++){
			int y=E[x][i];
			dfs(y);
			t[x].cnt+=t[y].cnt;
		}
	}
	void build(char *s){
		int len=strlen(s+1);
		for(int i=1;i<=len;i++) extend(s[i]-'a');
		for(int i=2;i<=ptr;i++) E[link(i)].push_back(i);
		dfs(root);
	}
	int query(char *s,int qid){
		int ans=0;
		int m=strlen(s+1);
		int x=1,len=0;
		for(int i=1;i<=m;i++){//在自动机上匹配
			int c=s[i]-'a';
			while(x&&t[x].ch[c]==0){
				x=link(x);
				len=len(x);
			}
			if(t[x].ch[c]){
				len++;
				x=t[x].ch[c];
			}
		}
		if(len==m&&t[x].vis<qid){//防止一个询问中相同子串被重复计算
			ans+=t[x].cnt;
			t[x].vis=qid;
		}
		for(int i=1;i<=m;i++){//进行循环移位
			int c=s[i]-'a';
			while(x&&t[x].ch[c]==0){
				x=link(x);
				len=len(x);
			}
			if(t[x].ch[c]){
				len++;
				x=t[x].ch[c];
			}
			if(len>m){ //完整匹配时移到下一个,跳link,删除头部字符
				len--;//
				if(len==len(link(x))) x=link(x); //如果不满足len-1=len(link(x))的条件,会在添加尾部字符时转移
			}
			if(len==m&&t[x].vis<qid){//防止一个询问中相同子串被重复计算
				ans+=t[x].cnt;
				t[x].vis=qid;
			}
		}
		return ans;
	}
};
SAM T;
char s[maxn+5],t[maxn+5];
int q;
int main(){
	scanf("%s",s+1);
	scanf("%d",&q);
	T.build(s);
	for(int i=1;i<=q;i++){
		scanf("%s",t+1);
		printf("%d\n",T.query(t,i));
	}
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#define maxn 200000
#define maxc 26
using namespace std;
struct SAM{
#define len(x) (t[x].len)
#define link(x) (t[x].link)
	struct node{
		int ch[maxc];
		int link;
		int len;
		int cnt;
		int vis;
	}t[maxn+5];
	vector<int>E[maxn+5];
	const int root=1;
	int ptr=1;
	int last=root;
	void extend(int c){
		int p=last,cur=++ptr;
		len(cur)=len(p)+1;
		t[cur].cnt=1;
		while(p&&t[p].ch[c]==0){
			t[p].ch[c]=cur;
			p=link(p);
		}
		if(p==0) link(cur)=root;
		else{
			int q=t[p].ch[c];
			if(len(p)+1==len(q)) link(cur)=q;
			else{
				int clo=++ptr;
				link(clo)=link(q);
				memcpy(t[clo].ch,t[q].ch,sizeof(t[q].ch));
				len(clo)=len(p)+1;
				link(cur)=link(q)=clo;
				while(p&&t[p].ch[c]==q){
					t[p].ch[c]=clo;
					p=link(p);
				}
			}
		}
		last=cur;
	}
	void dfs(int x){
		for(int i=0;i<(int)E[x].size();i++){
			int y=E[x][i];
			dfs(y);
			t[x].cnt+=t[y].cnt;
		}
	}
	void build(char *s){
		int len=strlen(s+1);
		for(int i=1;i<=len;i++) extend(s[i]-'a');
		for(int i=2;i<=ptr;i++) E[link(i)].push_back(i);
		dfs(root);
	}
	int query(char *s,int qid){
		int ans=0;
		int m=strlen(s+1);
		int x=1,len=0;
		for(int i=1;i<=m;i++){//在自动机上匹配
			int c=s[i]-'a';
			while(x&&t[x].ch[c]==0){
				x=link(x);
				len=len(x);
			}
			if(t[x].ch[c]){
				len++;
				x=t[x].ch[c];
			}
		}
		if(len==m&&t[x].vis<qid){//防止一个询问中相同子串被重复计算
			ans+=t[x].cnt;
			t[x].vis=qid;
		}
		for(int i=1;i<=m;i++){//进行循环移位
			int c=s[i]-'a';
			while(x&&t[x].ch[c]==0){
				x=link(x);
				len=len(x);
			}
			if(t[x].ch[c]){
				len++;
				x=t[x].ch[c];
			}
			if(len>m&&len-1==len(link(x))){ //完整匹配时移到下一个,跳link,删除头部字符
				len--;
				x=link(x); //如果不满足len-1=len(link(x))的条件,会在添加尾部字符时转移
			}
			if(len==m&&t[x].vis<qid){//防止一个询问中相同子串被重复计算
				ans+=t[x].cnt;
				t[x].vis=qid;
			}
		}
		return ans;
	}
};
SAM T;
char s[maxn+5],t[maxn+5];
int q;
int main(){
	scanf("%s",s+1);
	scanf("%d",&q);
	T.build(s);
	for(int i=1;i<=q;i++){
		scanf("%s",t+1);
		printf("%d\n",T.query(t,i));
	}
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#define maxn 2000000
#define maxc 26
using namespace std;
struct SAM{
#define len(x) (t[x].len)
#define link(x) (t[x].link)
	struct node{
		int ch[maxc];
		int link;
		int len;
		int cnt;
		int vis;
	}t[maxn+5];
	vector<int>E[maxn+5];
	const int root=1;
	int ptr=1;
	int last=root;
	void extend(int c){
		int p=last,cur=++ptr;
		len(cur)=len(p)+1;
		t[cur].cnt=1;
		while(p&&t[p].ch[c]==0){
			t[p].ch[c]=cur;
			p=link(p);
		}
		if(p==0) link(cur)=root;
		else{
			int q=t[p].ch[c];
			if(len(p)+1==len(q)) link(cur)=q;
			else{
				int clo=++ptr;
				link(clo)=link(q);
				memcpy(t[clo].ch,t[q].ch,sizeof(t[q].ch));
				len(clo)=len(p)+1;
				link(cur)=link(q)=clo;
				while(p&&t[p].ch[c]==q){
					t[p].ch[c]=clo;
					p=link(p);
				}
			}
		}
		last=cur;
	}
	void dfs(int x){
		for(int i=0;i<(int)E[x].size();i++){
			int y=E[x][i];
			dfs(y);
			t[x].cnt+=t[y].cnt;
		}
	}
	void build(char *s){
		int len=strlen(s+1);
		for(int i=1;i<=len;i++) extend(s[i]-'a');
		for(int i=2;i<=ptr;i++) E[link(i)].push_back(i);
		dfs(root);
	}
	int query(char *s,int qid){
		int ans=0;
		int m=strlen(s+1);
		int x=1,len=0;
		for(int i=1;i<=m;i++){//在自动机上匹配
			int c=s[i]-'a';
			while(x&&t[x].ch[c]==0){
				x=link(x);
				len=len(x);
			}
			if(t[x].ch[c]){
				len++;
				x=t[x].ch[c];
			}
		}
		if(len==m&&t[x].vis<qid){//防止一个询问中相同子串被重复计算
			ans+=t[x].cnt;
			t[x].vis=qid;
		}
		for(int i=1;i<=m;i++){//进行循环移位
			int c=s[i]-'a';
			while(x&&t[x].ch[c]==0){
				x=link(x);
				len=len(x);
			}
			if(t[x].ch[c]){
				len++;
				x=t[x].ch[c];
			}
			if(len>m){ //完整匹配时移到下一个,跳link,删除头部字符
				len--;//
				if(len==len(link(x))) x=link(x); //如果不满足len-1=len(link(x))的条件,会在添加尾部字符时转移
			}
			if(len==m&&t[x].vis<qid){//防止一个询问中相同子串被重复计算
				ans+=t[x].cnt;
				t[x].vis=qid;
			}
		}
		return ans;
	}
};
SAM T;
char s[maxn+5],t[maxn+5];
int q;
int main(){
	scanf("%s",s+1);
	scanf("%d",&q);
	T.build(s);
	for(int i=1;i<=q;i++){
		scanf("%s",t+1);
		printf("%d\n",T.query(t,i));
	}
}


#include<bits/stdc++.h>
using namespace std;
#define MAXNODE 2000005
#define SIGMA 26
struct node{
    node *fail,*ch[SIGMA];
    int len,cnt,vis;
    node() {fail=0;len=cnt=vis=0;memset(ch,0,sizeof ch);}
}mem[MAXNODE], *root, *last;

int str_cnt;

struct SuffixAutomaton{
    int size;
    inline int idx(char c){return c-'a';}
    inline void init(){
        size = 0;
        root = last = &mem[0];
    }
    // main SAM
    inline void add(char c){
        c = idx(c);
        node *pre = last;
        node *now = &mem[++size];
        now->len = pre->len+1;
        while(pre && !pre->ch[c]){
            pre->ch[c] = now;
            pre = pre->fail;
        }
        if(!pre) now->fail = root;
        else{
            node *q = pre->ch[c];
            if(q->len == pre->len+1) now->fail = q;
            else
            {
                node *nq = &mem[++size];
                nq->len = pre->len+1;
                memcpy(nq->ch, q->ch, sizeof(q->ch));
                nq->fail = q->fail;
                q->fail = now->fail = nq;
                while(pre && pre->ch[c] == q){
                    pre->ch[c] = nq;
                    pre=pre->fail;
                }
            }
        }
        last = now;
    }
    // solve problem
    int C[MAXNODE], ord[MAXNODE];
    void build_cnt(char* s,int n){
        for(int i = 0; i <= size; i++) C[mem[i].len]++;
        for(int i = 1; i <= size; i++) C[i] += C[i-1];
        for(int i = 0; i <= size; i++) ord[--C[mem[i].len]] = i;
        node* now = root;
        for(int i = 0; i < n; i++){
            int c = idx(s[i]);
            now = now->ch[c];
            now->cnt = 1;
        }
        for(int i = size; i >= 0; i--){
            now = &mem[ord[i]];
            if(now->fail) now->fail->cnt += now->cnt;
        }
    }

    int solve(char* s){
        str_cnt++;
        int n = strlen(s), cnt = 0, ans = 0;
        node* now = root;
        for(int i = 0; i < n-1; i++){
            int c = idx(s[i]);
            if(now->ch[c]) cnt++, now = now->ch[c];
            else{
                while(now && !now->ch[c]) now = now->fail;
                if(!now) cnt=0, now=root;
                else cnt = now->len+1, now = now->ch[c];
            }
            while(cnt >= n/2 && now->fail && now->fail->len >= n/2){
                cnt = now->fail->len;
                now = now->fail;
            }
            if(cnt >= n/2 && now->vis != str_cnt){
                ans += now->cnt;
                now->vis = str_cnt;
            }
        }
        return ans;
    }
}SAM;

char s[MAXNODE];
int main(){
    SAM.init();
    scanf("%s",s);
    int n=strlen(s);
    for(int i=0;i<n;i++){
        SAM.add(s[i]);
    }
    SAM.build_cnt(s,n);
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%s",s);
        n=strlen(s);
        for(int i=n;i<n*2;i++) s[i]=s[i-n];
        s[n*2]=0;
        printf("%d\n",SAM.solve(s));
    }
}

// ntuj - 94uza37nhf0owh31htrxg


// default code for competitive programming
// ver 3.1415 {{{

// Includes
#include <bits/stdc++.h>

// Defines
#define NAME(x) #x
#define SZ(c) (int)(c).size()
#define ALL(c) (c).begin(), (c).end()
#define FOR(i, e) for( int i = 0 ; i < (e) ; i++ )
#define ITER(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define REP(i, s, e) for(int i = (s); i <= (e); i++)
#define REPD(i, s, e) for(int i = (s); i >= (e); i--)
#define IOS ios_base::sync_with_stdio( 0 )
#define DEBUG 1
#define fst first
#define snd second
#define PB push_back
#ifdef ONLINE_JUDGE
#define FILE( fn ) \
    freopen(fn".in","r",stdin); \
freopen(fn".out","w",stdout);
#else
#define FILE( fn )
#endif

#ifdef AKAI
#define debug( ... ) fprintf( stderr , __VA_ARGS__ )
#else
#define debug( ... )
#endif

using namespace std;

// Typedefs
typedef double real;
typedef long long ll;
typedef pair<ll, int> pli;
typedef tuple<ll, int> tli;
typedef pair<int, int> pii;
typedef tuple<int, int> tii;
typedef unsigned long long ull;

// Some common const.
const double EPS = 1e-8;
const double Pi = acos(-1);

// Equal for double
bool inline equ(double a, double b)
{return fabs(a - b) < EPS;}

int _R( int& x ) { return scanf( "%d" , &x ); }
int _R( ll& x ) { return scanf( "%" PRId64 , &x ); }
int _R( double& x ) { return scanf( "%lf" , &x ); }
int _R( char* s ) { return scanf( "%s" , s ); }

int R() { return 0; }

  template< typename T1 , typename... T2 >
int R( T1& x , T2&... tail )
{
  int tmp = _R( x );
  return tmp + R( tail... );
}

  template< typename Iter , typename F >
inline void out( Iter s , Iter e , F of )
{
  bool flag = 0;
  for( Iter it = s ; it != e ; it++ )
  {
    if( flag ) printf( " " );
    else flag = 1;
    of( *it );
  }
  puts( "" );
}

// }}}
// start ~~QAQ~~

const int MAXM = 2000010;
struct SAM{
  int tot, root, lst, mom[MAXM], mx[MAXM];
  int acc[MAXM], nxt[MAXM][33];
  int newNode(){
    int res = ++tot;
    fill(nxt[res], nxt[res]+33, 0);
    mom[res] = mx[res] = acc[res] = 0;
    return res;
  }
  void init(){
    tot = 0;
    root = newNode();
    mom[root] = 0, mx[root] = 0;
    lst = root;
  }
  void push(int c){
    int p = lst;
    int np = newNode();
    mx[np] = mx[p]+1;
    for(; p && nxt[p][c] == 0; p = mom[p])
      nxt[p][c] = np;
    if(p == 0) mom[np] = root;
    else{
      int q = nxt[p][c];
      if(mx[p]+1 == mx[q]) mom[np] = q;
      else{
        int nq = newNode();
        mx[nq] = mx[p]+1;
        for(int i = 0; i < 33; i++)
          nxt[nq][i] = nxt[q][i];
        mom[nq] = mom[q];
        mom[q] = nq;
        mom[np] = nq;
        for(; p && nxt[p][c] == q; p = mom[p])
          nxt[p][c] = nq;
      }
    }
    lst = np;
  }
  void print(){
    REP(i, 1, tot){
      printf("node %d :\n", i);
      printf("mx %d, mom %d\n", mx[i], mom[i]);
      REP(j, 1, 26) if(nxt[i][j])
        printf("nxt %c %d\n", 'a'+j-1, nxt[i][j]);
      puts("---------------------------");
    }
  }
  void push(char *str){
    for(int i = 0; str[i]; i++)
      push(str[i]-'a'+1);
  }
  bool end[ MAXM ];
  void dfs( int u ) {
    if( end[ u ] ) {
      acc[ u ]++;
    }
    REP( c , 1 , 26 ) if( nxt[ u ][ c ] ) {
      if( !acc[ nxt[ u ][ c ] ] )
        dfs( nxt[ u ][ c ] );
      acc[ u ] += acc[ nxt[ u ][ c ] ];
    }
  }
  void yutruli() {
    int now = lst;
    while( now ) {
      end[ now ] = 1;
      now = mom[ now ];
    }
    dfs( root );
  }
} sam;

const int MAXL = 1e6+10;

char ip[ MAXL ] , qry[ MAXL ];

inline void calc() {
  R( qry );
  int len = strlen( qry );
  int np = sam.root;
  int cl = 0;
  FOR( i , len ) {
    int c = qry[ i ]-'a'+1;
    while( np && !sam.nxt[ np ][ c ] ) {
      np = sam.mom[ np ];
      cl = min( cl , sam.mx[ np ] );
    }
    if( np ) np = sam.nxt[ np ][ c ] , cl++;
    else np = sam.root , cl = 0;
  }
  int ans = 0;
  set<int> vis;
  FOR( i , len ) {
    if( cl >= len ) {
      if( !vis.count( np ) ) {
        //printf( "i %d np %d acc %d\n" , i , np , sam.acc[ np ] );
        ans += sam.acc[ np ];
        vis.insert( np );
      }
    }
    int c = qry[ i ]-'a'+1;
    while( np && !sam.nxt[ np ][ c ] ) {
      np = sam.mom[ np ];
      cl = min( cl , sam.mx[ np ] );
    }
    while( np && sam.mx[ sam.mom[ np ] ] >= len-1 ) {
      np = sam.mom[ np ];
      cl = min( cl , sam.mx[ np ] );
    }
    if( np ) np = sam.nxt[ np ][ c ] , cl++;
    else np = sam.root , cl = 0;
  }
  printf( "%d\n" , ans );
}

int main() {
  R( ip );
  sam.init();
  sam.push( ip );
  sam.yutruli();
  int q; R( q );
  while( q-- ) {
    calc();
  }
}

// ntuj - f5rrg04k4ebr5ib14phbu


#include <cstdio>
#include <cstring>

constexpr int MAXL = 1e6 + 10;

constexpr int CSIZ = 26;

struct Node {
    Node *p, *nxt[CSIZ];
    int maxl, ts, cnt;
    void clear() {
        p = NULL;
        for (int i = 0; i < CSIZ; ++i) nxt[i] = NULL;
    }
} _bfr[MAXL*2];

Node *newNode() {
    static int i = 0;
    _bfr[i].ts = -1, _bfr[i].cnt = 0;
    return &_bfr[i++];
}

Node *root = newNode(), *tail = root;

void add_char(int c) {
    Node *new_tail = newNode();
    new_tail->clear();
    new_tail->maxl = tail->maxl + 1;

    Node *n = tail;
    for (; n != NULL and n->nxt[c] == NULL; n = n->p)
        n->nxt[c] = new_tail;

    if (n == NULL) {
        new_tail->p = root;
    } else {
        Node *q = n->nxt[c];
        if (q->maxl == n->maxl + 1) new_tail->p = q;
        else {
            Node *nq = newNode();
            nq->maxl = n->maxl + 1, nq->p = q->p;
            for (int i = 0; i < CSIZ; ++i) nq->nxt[i] = q->nxt[i];

            q->p = new_tail->p = nq;
            for (; n != NULL and n->nxt[c] == q; n = n->p) n->nxt[c] = nq;
        }
    }

    tail = new_tail;
}

int dfs(Node *n) {
    if (n->ts != -1) return n->cnt;
    for (auto c : n->nxt)
        if (c)
            n->cnt += dfs(c);
    n->ts = 0;
    return n->cnt;
}

char str[MAXL*2];
int query() {
    static int T = 0;
    ++T;
    scanf("%s", str);
    const int len = strlen(str);

    for (int i = 0; i < len; ++i) str[i+len] = str[i];
    str[len*2-1] = '\0';

    Node *c = root;
    int ans = 0, cnt = 0;
    for (int i = 0; str[i]; ) {
        Node *n = c->nxt[str[i]-'a'];
        if (cnt < len and n)
            c = n, ++cnt, ++i;
        else if (c == root)
            return 0;
        else {
            --cnt;
            if (c->p->maxl == cnt)
                c = c->p;
        }

        if (cnt == len and c->ts != T) {
            c->ts = T;
            ans += c->cnt;
        }
    }

    return ans;
}

int main() {
    scanf("%s", str);
    for (char *c = str; *c; ++c) add_char(*c-'a');
    for (Node *n = tail; n != root; n = n->p) n->cnt = 1;
    dfs(root);

    int q; scanf("%d", &q);
    while (q --> 0) printf("%d\n", query());

    return 0;
}

// ntuj - dzaqwixi7jj5cetx5n027


//bcw0x1bd2 {{{
#include<bits/stdc++.h>
#include<unistd.h>
using namespace std;
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define SZ(x) ((int)((x).size()))
#define ALL(x) begin(x),end(x)
#define REP(i,x) for (int i=0; i<(x); i++)
#define REP1(i,a,b) for (int i=(a); i<=(b); i++)

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;

#ifdef DARKHH
#define FILEIO(name)
#else
#define FILEIO(name) \
  freopen(name".in", "r", stdin); \
  freopen(name".out", "w", stdout);
#endif

#ifdef DARKHH
template<typename T>
void _dump( const char* s, T&& head ) { cerr<<s<<"="<<head<<endl; }

template<typename T, typename... Args>
void _dump( const char* s, T&& head, Args&&... tail ) {
  int c=0;
  while ( *s!=',' || c!=0 ) {
    if ( *s=='(' || *s=='[' || *s=='{' ) c++;
    if ( *s==')' || *s==']' || *s=='}' ) c--;
    cerr<<*s++;
  }
  cerr<<"="<<head<<", ";
  _dump(s+1,tail...);
}

#define dump(...) do { \
  fprintf(stderr, "%s:%d - ", __PRETTY_FUNCTION__, __LINE__); \
  _dump(#__VA_ARGS__, __VA_ARGS__); \
} while (0)

template<typename Iter>
ostream& _out( ostream &s, Iter b, Iter e ) {
  s<<"[";
  for ( auto it=b; it!=e; it++ ) s<<(it==b?"":" ")<<*it;
  s<<"]";
  return s;
}

template<typename A, typename B>
ostream& operator <<( ostream &s, const pair<A,B> &p ) { return s<<"("<<p.first<<","<<p.second<<")"; }
template<typename T>
ostream& operator <<( ostream &s, const vector<T> &c ) { return _out(s,ALL(c)); }
template<typename T, size_t N>
ostream& operator <<( ostream &s, const array<T,N> &c ) { return _out(s,ALL(c)); }
template<typename T>
ostream& operator <<( ostream &s, const set<T> &c ) { return _out(s,ALL(c)); }
template<typename A, typename B>
ostream& operator <<( ostream &s, const map<A,B> &c ) { return _out(s,ALL(c)); }
#else
#define dump(...)
#endif
// }}}
// Let's Fight! ~OAO~~

// par : fail link
// val : a topological order ( useful for DP )
// go[x] : automata edge ( x is integer in [0,26) )

struct SAM{
	struct State{
		int par, go[26], val, cnt, vst;
		State () : par(0), val(0), cnt(0), vst(0) { memset(go,0,sizeof(go)); }
		State (int _val) : par(0), val(_val), cnt(0), vst(0) {	memset(go,0,sizeof(go)); }
	};
	vector<State> vec;
	int root, tail;

	void init(){
		vec.resize(2);
		vec[0] = vec[1] = State(0);
		root = tail = 1;
	}
	void extend(int w){
		int p = tail, np = vec.size();
		vec.PB(State(vec[p].val+1));
		for ( ; p && vec[p].go[w]==0; p=vec[p].par)
			vec[p].go[w] = np;
		if (p == 0){
			vec[np].par = root;
		} else {
			if (vec[vec[p].go[w]].val == vec[p].val+1){
				vec[np].par = vec[p].go[w];
			} else {
				int q = vec[p].go[w], r = vec.size();
				vec.PB(vec[q]);
				vec[r].val = vec[p].val+1;
				vec[q].par = vec[np].par = r;
				for ( ; p && vec[p].go[w] == q; p=vec[p].par)
					vec[p].go[w] = r;
			}
		}
		tail = np;
	}
  void build(string s) {
    init();
    for (auto c:s) extend(c-'a');
    vector<int> vvv;
    REP1(i,1,SZ(vec)-1) vvv.PB(i);
    sort(ALL(vvv), [&](int a, int b) { return vec[a].val > vec[b].val; });

    int v = root;
    for (auto c:s) {
      v = vec[v].go[c-'a'];
      vec[v].cnt = 1;
    }
    for (auto i:vvv) {
      vec[vec[i].par].cnt += vec[i].cnt;
    }
  }
  int solve(string s) {
    static int tag = 0;
    tag++;
    int res=0;
    int len = 0;
    int n = SZ(s);
    int v = root;
    s += s;
    REP(i,2*n-1) {
      int c=s[i]-'a';
      if(vec[v].go[c]) {
        len++;
        v = vec[v].go[c];
      } else {
        while (v and !vec[v].go[c]) v = vec[v].par;
        if (!v) {
          v = root;
          len = 0;
        } else {
          len = vec[v].val + 1;
          v = vec[v].go[c];
        }
      }
      while (len >= n and vec[v].par and vec[vec[v].par].val >= n) {
        v = vec[v].par;
        len = vec[v].val;
      }
      if (len >= n and vec[v].vst != tag) {
        res += vec[v].cnt;
        vec[v].vst = tag;
      }
    }
    return res;
  }
}sam;

int main() {
  IOS;
  string s;
  cin >> s;
  sam.build(s);
  int Q;
  cin >> Q;
  REP(i,Q) {
    cin >> s;
    int ans = sam.solve(s);
    cout << ans << "\n";
  }

  return 0;
}

// ntuj - ghkjbdtpucjh1jyrbpi7r


#include <cstdio>
#include <cstring>
#include <set>

std::set<int>set;
int son[2000001][26],fa[2000001],size[2000001],len[2000001],cnt=1,last=1,pre[2000001][22],n,Q,tax[2000001],pos[2000001],P=998244353;
int hash[2000001];
char str[2000001];
void add(int x){
	int p=last;
	size[last=++cnt]=1;
	len[last]=n;
	for(;p&&!son[p][x];p=fa[p])son[p][x]=last;
	if(!p)return (void)(fa[last]=1);
	int q=son[p][x];
	if(len[q]==len[p]+1)return (void)(fa[last]=q);
	len[++cnt]=len[p]+1;
	fa[cnt]=fa[q];
	fa[q]=fa[last]=cnt;
	for(int i=0;i<26;i++)son[cnt][i]=son[q][i];
	for(;son[p][x]==q;p=fa[p])son[p][x]=cnt;
}
int min(int a,int b){return a<b?a:b;}
bool allsame(){
	for(int i=2;i<=n;i++)if(str[i]!=str[i-1])return 0;
	return 1;
}
typedef long long ll;
int qsm(int a,int b){
	int ans=1;
	while(b){
		if(b&1)ans=(ll)ans*a%P;
		a=(ll)a*a%P;
		b>>=1;
	}
	return ans;
}
int main(){
	scanf("%s",str+1);
	for(n=1;str[n];n++)add(str[n]-'a');

	for(int i=1;i<=cnt;i++)tax[len[i]]++;
	for(int i=1;i<=n;i++)tax[i]+=tax[i-1];
	for(int i=cnt;i;i--)pos[tax[len[i]]--]=i;
	for(int i=cnt;i;i--)size[fa[pos[i]]]+=size[pos[i]];

	for(int i=1;i<=cnt;i++)pre[i][0]=fa[i];
	for(int j=1;j<=21;j++)
		for(int i=1;i<=cnt;i++)
			pre[i][j]=pre[pre[i][j-1]][j-1];

//	for(int i=1;i<=cnt;i++){
//		printf("%d : %d : -> %d : ",i,len[i],fa[i]);
//		for(int j=0;j<26;j++)if(son[i][j])printf("(%c %d)",j+'a',son[i][j]);
//		putchar('\n');
//	}

	scanf("%d",&Q);
	for(int i=1;i<=Q;i++){
		scanf("%s",str+1);
		n=strlen(str+1);
		for(int i=1;i<=n;i++)str[i+n]=str[i];
		int p=1,length=0,pow=qsm(6666,n);
		long long ans=0;
		for(int i=1;i<n<<1;i++){
			hash[i]=((ll)hash[i-1]*6666+(ll)233*str[i])%P;
			if(i>n)hash[i]=((ll)-pow*hash[i-n]%P+hash[i]+P)%P;
			while(p&&!son[p][str[i]-'a'])p=fa[p];
			if(!p)p=1,length=0;
			else length=min(length,len[p])+1,p=son[p][str[i]-'a'];
			//printf("%d %d %d\n",i,p,length);
			if(i<n||set.count(hash[i]))continue;
			set.insert(hash[i]);
			int now=p;
			for(int j=21;~j;j--)if(len[pre[now][j]]>=n)now=pre[now][j];
			if(length>=n)ans+=size[now];
		}
		set.clear();
		printf("%lld\n",ans);
	}
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
#include<algorithm>

using namespace std;

#define LL long long
#define fgx cerr<<"------------------"<<endl;
#define dgx cerr<<"=================="<<endl;
#define prt(x) cerr<<#x<<": "<<x<<endl;

inline int read(){
	int x=0,f=1;char c=getchar();
	for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
	for(;isdigit(c);c=getchar()) x=x*10+c-'0';
	return x*f;
}
const int MAXN = 6000010;
const int MAXM = 1000010;

map<int,int> ch[MAXN+1];
int dis[MAXN+1],slink[MAXN+1],sz[MAXN+1],L[MAXM+1];
int N,Q,tot,Last,RT; char str[MAXM+1]; bool vis[MAXN+1];
vector<int> vec[MAXM+1],clr;

inline int Nod(int d){ dis[tot]=d,ch[tot].clear(),slink[tot]=0; return tot++; }
inline void extend(int c,int id,int opr){
	int p=Last,np=Nod(dis[p]+1);
	if(!id) sz[np]=1; if(id&&opr) vec[id].push_back(np);
	while(!ch[p][c]) ch[p][c]=np,p=slink[p];
	if(p==RT&&ch[p][c]==np) slink[np]=RT;
	else{ int q=ch[p][c];
		if(dis[q]==dis[p]+1) slink[np]=q;
		else{ int nq=Nod(dis[p]+1);
			slink[nq]=slink[q],slink[q]=slink[np]=nq;
			ch[nq]=ch[q]; while(ch[p][c]==q) ch[p][c]=nq,p=slink[p];
		}
	} Last=np;
} int Node[MAXN+1],Next[MAXN+1],Root[MAXN+1],cnt; //int sz[MAXN+1];
inline void insert(int u,int v){
	Node[++cnt]=v; Next[cnt]=Root[u]; Root[u]=cnt; return ;
} inline void DFS(int k){ for(int x=Root[k];x;x=Next[x]) DFS(Node[x]),sz[k]+=sz[Node[x]]; }
inline int Find(int k,int lim){ while(dis[slink[k]]>=lim) k=slink[k]; return k; }

int main(){
	scanf("%s",str+1); Q=read(); N=strlen(str+1); Last=RT=Nod(0);
	for(int i=1;i<=N;i++) extend(str[i]-'a',0,0);
	for(int i=1;i<=Q;i++){
		scanf("%s",str+1); L[i]=strlen(str+1),Last=0;
		for(int j=1;j<L[i];j++) extend(str[j]-'a',i,0); extend(str[L[i]]-'a',i,1);
		for(int j=1;j<L[i];j++) extend(str[j]-'a',i,1); //prt(Last);
	} for(int i=1;i<tot;i++) insert(slink[i],i); DFS(0);
	for(int i=1;i<=Q;i++){
		int ans=0; clr.clear();
		for(auto gg:vec[i]){
			int tmp=Find(gg,L[i]); if(vis[tmp]) continue;
			clr.push_back(tmp),vis[tmp]=1,ans+=sz[tmp];
		} printf("%d\n",ans); for(auto gg:clr) vis[gg]=0;
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<vector>

using namespace std;

struct SAM
{
	struct nd
	{
		int ch[26],fail,len,sz;
	}t[2000000];int used,last;
	SAM():used(1),last(1){}
	void ins(int val)
	{
		int p=last,cur=++used;t[cur].len=t[p].len+1,t[cur].sz=1;
		while(p&&!t[p].ch[val]){t[p].ch[val]=cur;p=t[p].fail;}
		if(!p)t[cur].fail=1;
		else
		{
			int q=t[p].ch[val];
			if(t[q].len==t[p].len+1)t[cur].fail=q;
			else
			{
				int clone=++used;t[clone].len=t[p].len+1,t[clone].sz=0;
				for(int i=0;i<26;i++)t[clone].ch[i]=t[q].ch[i];
				t[clone].fail=t[q].fail;
				while(t[p].ch[val]==q)t[p].ch[val]=clone,p=t[p].fail;
				t[q].fail=t[cur].fail=clone;
			}
		}
		last=cur;
	}
	vector<int> ed[2000000];
	void add_fail(){for(int i=2;i<=used;i++) if(t[i].fail) ed[t[i].fail].push_back(i);}
	void dfs(int u=1){
		for(int i=0;i<ed[u].size();i++){
			dfs(ed[u][i]);
			t[u].sz+=t[ed[u][i]].sz;
		}
	}
	void output()
	{
		for(int i=1;i<=used;i++)
		{
			printf("%d: %d %d %d\n",i,t[i].fail,t[i].len,t[i].sz);
			for(int j=0;j<26;j++)if(t[i].ch[j])printf("%d %d\n",j,t[i].ch[j]);
		}
	}
}T;

char s[2000000],t[2000000];

int nxt[2000000];

int main()
{
	scanf("%s",s+1);int m=strlen(s+1);for(int i=1;i<=m;i++)T.ins(s[i]-'a');
	T.add_fail();T.dfs();//T.output();
	int n=0;scanf("%d",&n);
	while(n--)
	{
		scanf("%s",t+1);int q=strlen(t+1);
		nxt[1]=0;
		for(int i=2,j=0;i<=q;i++)
		{
			while(j&&t[i]!=t[j+1])j=nxt[j];
			if(t[i]==t[j+1])j++;
			nxt[i]=j;
		}
		//for(int i=1;i<=q;i++)printf("%d ",nxt[i]);puts("");
		int pr=0,p=q;
		while(q%(q-nxt[p]))p=nxt[p];pr=q-nxt[p];
		int pos=1,len=0;
		for(int i=1;i<=q;i++)
		{
			int val=t[i]-'a';
			while(pos!=1&&!T.t[pos].ch[val])pos=T.t[pos].fail,len=T.t[pos].len;
			if(T.t[pos].ch[val]){pos=T.t[pos].ch[val],len++;}
		}
		long long ans=0;if(len==q)ans+=T.t[pos].sz;
		for(int i=2;i<=pr;i++)
		{
			if(len==q){len--;if(T.t[T.t[pos].fail].len==len)pos=T.t[pos].fail;}
			int val=t[i-1]-'a';
			while(pos!=1&&!T.t[pos].ch[val])pos=T.t[pos].fail,len=T.t[pos].len;
			if(T.t[pos].ch[val]){pos=T.t[pos].ch[val],len++;}
			if(len==q)ans+=T.t[pos].sz;
		}
		printf("%lld\n",ans);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N=2e6+50;
struct SAM
{
	int top,last,mx[N],fa[N],ch[N][26];
	int siz[N],t[N],rk[N],vis[N],tim;
	void init() {top=last=1;}
	void add(int c)
	{
		int p=last,np=last=++top;
		mx[np]=mx[p]+1;
		siz[np]=1;
		for (;p&&!ch[p][c];p=fa[p]) ch[p][c]=np;
		if (!p) fa[np]=1;
		else
		{
			int q=ch[p][c];
			if (mx[q]==mx[p]+1) fa[np]=q;
			else
			{
				int nq=++top;
				mx[nq]=mx[p]+1;
				fa[nq]=fa[q];
				fa[q]=fa[np]=nq;
				memcpy(ch[nq],ch[q],sizeof(ch[nq]));
				for (;ch[p][c]==q;p=fa[p]) ch[p][c]=nq;
			}
		}
	}

	void work()
	{
		for (int i=1;i<=top;i++) ++t[mx[i]];
		for (int i=1;i<=top;i++) t[i]+=t[i-1];
		for (int i=1;i<=top;i++) rk[t[mx[i]]--]=i;
		for (int u,i=top;i>1;i--)
		{
			u=rk[i];
			siz[fa[u]]+=siz[u];
		}
	}

	int query(char s[])
	{
		int len=0,n=strlen(s+1),ret=0;
		++tim;
		for (int i=1;i<n;i++)
			s[i+n]=s[i];
		for (int u=1,c,i=1;i<=2*n-1;i++)
		{
			c=s[i]-'a';
			if (ch[u][c])
			{
				u=ch[u][c];
				++len;
			}
			else
			{
				while (!ch[u][c]&&fa[u])
				{
					u=fa[u];
					len=mx[u];
				}
				if (ch[u][c])
				{
					u=ch[u][c];
					++len;
				}
			}
			while (mx[fa[u]]>=n)
			{
				u=fa[u];
				len=mx[u];
			}
			if (len>=n&&vis[u]!=tim)
			{
				vis[u]=tim;
				ret+=siz[u];
			}
		}
		return ret;
	}
}sam;

char s[N];
int m;
int main()
{
	scanf("%s",s);
	sam.init();
	for (int len=strlen(s),i=0;i<len;i++)
		sam.add(s[i]-'a');
	sam.work();
	scanf("%d",&m);
	while (m--)
	{
		scanf("%s",s+1);
		printf("%d\n",sam.query(s));
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

inline int read()
{
    int x=0; int f=1; char ch=getchar();
    while(!isdigit(ch)) {if(ch=='-')f=-1; ch=getchar();}
    while(isdigit(ch)) {x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}

const int MAXN = 2000005;
char str[MAXN];
int fa[MAXN],num[MAXN],ch[MAXN][26];
int len[MAXN],v[MAXN],q[MAXN],flag[MAXN];
vector<int>ans;
int n,l,last=1,cnt=1;
long long tot;

inline void extend(int c)
{
	int p=last,np=last=++cnt;
    num[np]=1; len[np]=len[p]+1;
    while(p&&!ch[p][c])
    	ch[p][c]=np,p=fa[p];
    if(!p)fa[np]=1;
    else
    {
        int q=ch[p][c];
        if(len[q]==len[p]+1) fa[np]=q;
        else
        {
            int nq=++cnt;len[nq]=len[p]+1;
            memcpy(ch[nq],ch[q],sizeof(ch[q]));
            fa[nq]=fa[q]; fa[np]=fa[q]=nq;
            while(ch[p][c]==q)
            	ch[p][c]=nq,p=fa[p];
        }
    }
}

inline void ini()
{
    for(int i=1;i<=cnt;i++) v[len[i]]++;
    for(int i=1;i<=cnt;i++) v[i]+=v[i-1];
    for(int i=cnt;i;i--) q[v[len[i]]--]=i;
    for(int i=cnt;i;i--)
    {
        int t=q[i];
        num[fa[t]]+=num[t];
    }
}

inline void solve()
{
    int p=1,tmp=0;
    for(int i=1;i<=2*l-1;i++)
    {
        int c=str[i]-'a';
        while(p&&!ch[p][c])p=fa[p];
        if(!p) p=1,tmp=0;
        else
        {
            tmp=min(len[p],tmp)+1;
            p=ch[p][c];
        }
        while(len[fa[p]]>=l)
        	p=fa[p],tmp=len[p];
        if(!flag[p]&&tmp>=l)
        	ans.push_back(p),flag[p]=1;
    }
    for(int i=0;i<ans.size();i++)
    	flag[ans[i]]=0;
    for(int i=0;i<ans.size();i++)
    	tot+=num[ans[i]];
    ans.clear();
}

int main(int argc, char const *argv[])
{
	scanf("%s",str+1);
    l=strlen(str+1);
    for(int i=1;i<=l;i++)
        extend(str[i]-'a');
    ini(); n=read();
    for(int i=1;i<=n;i++)
    {
        scanf("%s",str+1);
        l=strlen(str+1);
        for(int i=1;i<l;i++)
            str[i+l]=str[i];
        tot=0;solve();
        printf("%I64d\n",tot);
    }
	return 0;
}


#pragma GCC optimize(3)
#include <cstdio>
#include <cstring>

int son[2000001][26],fa[2000001],size[2000001],len[2000001],cnt=1,last=1,n,Q,tax[2000001],pos[2000001],P=998244353,queue[2000001],t;
bool vis[2000001];
char str[2000001];
void add(int x){
    int p=last;
    size[last=++cnt]=1;
    len[last]=n;
    for(;p&&!son[p][x];p=fa[p])son[p][x]=last;
    if(!p)return (void)(fa[last]=1);
    int q=son[p][x];
    if(len[q]==len[p]+1)return (void)(fa[last]=q);
    len[++cnt]=len[p]+1;
    fa[cnt]=fa[q];
    fa[q]=fa[last]=cnt;
    memcpy(son[cnt],son[q],sizeof 26<<2);
    for(;son[p][x]==q;p=fa[p])son[p][x]=cnt;
}
inline int min(const int &a,const int &b){return a<b?a:b;}
int main(){
    scanf("%s",str+1);
    for(n=1;str[n];++n)add(str[n]-'a');

    for(int i=1;i<=cnt;++i)tax[len[i]]++;
    for(int i=1;i<=n;++i)tax[i]+=tax[i-1];
    for(int i=cnt;i;--i)pos[tax[len[i]]--]=i;
    for(int i=cnt;i;--i)size[fa[pos[i]]]+=size[pos[i]];

    scanf("%d",&Q);
    for(int i=1;i<=Q;++i){
        scanf("%s",str+1);
        n=strlen(str+1);
        for(int i=1;i<=n;++i)str[i+n]=str[i]=str[i]-'a';
        int p=1,length=0;
        long long ans=0;
        for(int i=1;i<n<<1;++i){
            while(p&&!son[p][str[i]])p=fa[p];
            if(!p)p=1,length=0;
            else length=min(length,len[p])+1,p=son[p][str[i]];
            while(len[fa[p]]>=n)p=fa[p];
            length=min(length,len[p]);
            if(length>=n){
            	if(vis[p])break;
				ans+=size[p],vis[p]=1,queue[++t]=p;
			}
        }
        for(int i=1;i<=t;++i)vis[queue[i]]=0;t=0;
        printf("%lld\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;
const int N=2e6+100;
typedef long long ll;

struct sam{
    char s[N];
    int ch[N][26],len[N],fa[N],a[N],c[N];
    int last=1,tot=1;
    ll endpos_size[N];
    void add(int c) {
        int p=last,np=last=++tot;
        len[np]=len[p]+1; endpos_size[np]=1;
        for(;p&&!ch[p][c];p=fa[p]) ch[p][c]=np;
        if(!p) fa[np]=1;
        else {
            int q=ch[p][c];
            if(len[q]==len[p]+1) fa[np]=q;
            else {
                int nq=++tot;len[nq]=len[p]+1;
                memcpy(ch[nq],ch[q],sizeof(ch[q]));
                fa[nq]=fa[q];fa[q]=fa[np]=nq;
                for(;p&&ch[p][c]==q;p=fa[p]) ch[p][c]=nq;
            }
        }
    }
    void get_endpos_size() {
        for(int i=1;i<=tot;i++) c[len[i]]++;
        for(int i=1;i<=tot;i++) c[i]+=c[i-1];
        for(int i=1;i<=tot;i++) a[c[len[i]]--]=i;
        for(int i=tot;i;i--) {
            int p=a[i];
            endpos_size[fa[p]]+=endpos_size[p];
        }

    }
    int vis[N];
    void solve() {
        scanf("%s",s+1);
        for(int i=1;s[i];i++) add(s[i]-'a');
        get_endpos_size();
        int q;cin>>q;
        while(q--) {
            ll ans=0;
            scanf("%s",s+1);
            int n=strlen(s+1);
            for(int i=1;i<=n;i++) s[i+n]=s[i];
            int pos=1,l=0;
            for(int i=1;i<=n+n;i++) {
                while(pos&&!ch[pos][s[i]-'a']) pos=fa[pos],l=len[pos];
                if(!pos) pos=1,l=0;
                else pos=ch[pos][s[i]-'a'],l++;
                //printf("pos=%d endpos_size=%lld len[pos]=%d \n",pos,endpos_size[pos],len[pos]);
                while(fa[pos]&&len[fa[pos]]>=n) pos=fa[pos];
                //printf("pos=%d endpos_size=%lld len[pos]=%d len[fa[pos]]+1=%d  vis[pos]=%d \n\n",pos,endpos_size[pos],len[pos],len[fa[pos]]+1,vis[pos]);
                if(i>n)
                if(vis[pos]!=q&&l>=n) ans+=endpos_size[pos],vis[pos]=q;
            }
            printf("%lld\n",ans);
        }
    }
}sam;

int main() {
    sam.solve();
}


#include<stdio.h>
const int N=1000001,M=N<<1;
int n,m,Q,q,u,l,ans;char c[N],s[N];
struct SAM{
	int tn,pn,en,head[M];
	struct T{int f,s[26],len,v,t;}t[M];
	struct E{int v,nxt;}e[M];
	SAM(){tn=pn=1;}
	void add(int u,int v){e[++en].v=v,e[en].nxt=head[u],head[u]=en;}
	void dfs(int u){for(int i=head[u];i;i=e[i].nxt)dfs(e[i].v),t[u].v+=t[e[i].v].v;}
	void Make(){for(int i=2;i<=pn;++i)add(t[i].f,i);dfs(1);}
	void Insert(int c){
		int p=tn,np=++pn;tn=pn,t[np].len=t[p].len+1,t[np].v=1;
		for(;p&&!t[p].s[c];p=t[p].f)t[p].s[c]=np;
		if(!p){t[np].f=1;return;}
		int q=t[p].s[c];
		if(t[q].len==t[p].len+1){t[np].f=q;return;}
		int nq=++pn;t[nq]=t[q],t[nq].len=t[p].len+1,t[nq].v=0,t[q].f=t[np].f=nq;
		for(;p&&t[p].s[c]==q;p=t[p].f)t[p].s[c]=nq;
	}
	void Add(int c){
		while(u&&!t[u].s[c])l=t[u=t[u].f].len;
		if(t[u].s[c])++l,u=t[u].s[c];
	}
	void Del(){if(l>m&&--l==t[t[u].f].len)u=t[u].f;}
	void Calc(){if(l==m&&t[u].t<q)t[u].t=q,ans+=t[u].v;}
}sam;
int main(){
	scanf("%s%d",c,&Q);
	for(;c[n]!='\0';++n)sam.Insert(c[n]-'a');
	sam.Make();
	for(q=1;q<=Q;++q){
		scanf("%s",s),m=ans=l=0,u=1;
		for(;s[m]!='\0';++m)s[m]-='a',sam.Add(s[m]);
		sam.Calc();
		for(int i=0;i<m;++i)sam.Add(s[i]),sam.Del(),sam.Calc();
		printf("%d\n",ans);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int N = 2e6 + 10;
const int ALP = 26;
char s[N], t[N];
int q, vis[N];

struct SAM {
	//空间开两倍，节点个数是两倍字符串长度
	//maxlen 表示节点i表示的最大后缀长度, nx[j]表示节点i加一个字符j所表示的字符串对应的节点
	//minlen 表示节点i表示的最小后缀长度，其等于t[t[i].fa].maxlen + 1
	//fa 表示节点i的后缀链接 cnt 表示节点i的enspos集合大小
	//c[]表示拓扑排序辅助数组,　rk[]表示拓扑序，rk[i],i小的节点所表示的后缀长度也小
	struct node {
		int maxlen, cnt, fa, nx[ALP];
		node() { maxlen = cnt = fa = 0; memset(nx, 0, sizeof nx); }
	}t[N << 1];
	int tot, lst, c[N << 1], rk[N << 1];
	inline int newnode() {
		++tot;
		t[tot] = node();
		return tot;
	}
	inline void init() {
		tot = 0;
		lst = newnode();
	}
	inline void extend(int id) {
		int cur = newnode(), p;
		t[cur].cnt = 1;
		t[cur].maxlen = t[lst].maxlen + 1;
		for (p = lst; p && !t[p].nx[id]; p = t[p].fa) t[p].nx[id] = cur;
		if (!p) {
			t[cur].fa = 1;
		} else {
			int q = t[p].nx[id];
			if (t[q].maxlen == t[p].maxlen + 1) {
				t[cur].fa = q;
			} else {
				int clone = newnode();
			  	t[clone] = t[q]; t[clone].cnt = 0;
				t[clone].maxlen = t[p].maxlen + 1;
				for (; p && t[p].nx[id] == q; p = t[p].fa) t[p].nx[id] = clone;
				t[cur].fa = t[q].fa = clone;
			}
		}
		lst = cur;
	}
	//字符串从0开始
	void build(char *s) {
		init();
		for (int i = 0; s[i]; ++i) {
			extend(s[i] - 'a');
		}
		memset(c, 0, sizeof c);
		for (int i = 1; i <= tot; ++i) c[t[i].maxlen]++;
		for (int i = 1; i <= tot; ++i) c[i] += c[i - 1];
		for (int i = 1; i <= tot; ++i) rk[c[t[i].maxlen]--] = i;
		for (int i = tot; i; --i) t[t[rk[i]].fa].cnt += t[rk[i]].cnt;
	}
	void gao(int _q, char *s) {
		int len = strlen(s);
		for (int i = len; i < 2 * len; ++i) s[i] = s[i - len];
		s[2 * len] = 0;
		int cur = 1, curlen = 0;
		ll res = 0;
		for (int i = 0; s[i]; ++i) {
			int c = s[i] - 'a';
			while (cur > 1 && t[t[cur].fa].maxlen >= len - 1) cur = t[cur].fa, curlen = t[cur].maxlen;
			while (cur && !t[cur].nx[c]) cur = t[cur].fa, curlen = t[cur].maxlen;
			if (!cur) cur = 1, curlen = 0;
			else cur = t[cur].nx[c], ++curlen;
			if (curlen >= len && vis[cur] != _q) {
				res += t[cur].cnt;
				vis[cur] = _q;
			}
		}
		printf("%lld\n", res);
	}
}sam;

int main() {
	while (scanf("%s", s) != EOF) {
		sam.build(s);
	    memset(vis, 0, sizeof vis);
		scanf("%d", &q);
		for (int _q = 1; _q <= q; ++_q) {
			scanf("%s", t);
			sam.gao(_q, t);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define de() cout<<"test"<<"\n"
using namespace std;

const int maxn=2e6+10;

char c[maxn];
int q,n;
int ch[maxn][26],fa[maxn],len[maxn],la=1,tot=1,rt=1;
ll siz[maxn],ans;
void ins(int x)
{
	int now=++tot,pre=la;la=now;
	len[now]=len[pre]+1;siz[now]=1;
	for(;pre&&!ch[pre][x];pre=fa[pre]) ch[pre][x]=now;
	if(!pre) fa[now]=rt;
	else {
		int q=ch[pre][x];
		if(len[q]==len[pre]+1) fa[now]=q;
		else {
			int nq=++tot;len[nq]=len[pre]+1;
			memcpy(ch[nq], ch[q], sizeof(ch[q]));
			fa[nq]=fa[q];fa[q]=fa[now]=nq;
			for(;pre&&ch[pre][x]==q;pre=fa[pre]) ch[pre][x]=nq;
		}
	}
}
int nt[maxn],qwq;
char s[maxn];
vector<int> ve[maxn];
void dfs(int x)
{
	for(int i=0;i<ve[x].size();++i)
		dfs(ve[x][i]),siz[x]+=siz[ve[x][i]];
}
int L=0;
void find(int x) {
	while(!ch[qwq][x]) {
		if(qwq==rt) break;
		//cout<<qwq<<"++"<<"\n";
		qwq=fa[qwq];L=len[qwq];
	}
	if(ch[qwq][x]) {
		qwq=ch[qwq][x];L++;
	}
}
int main()
{
	scanf("%s",c+1);
	n=strlen(c+1);
	for(int i=1;i<=n;++i) {
		ins(c[i]-'a');
	}
	for(int i=2;i<=tot;++i) ve[fa[i]].push_back(i);
	dfs(rt);
	scanf("%d",&q);
	while(q--) {
		int p=0;
		scanf("%s",s+1);
		int Len=strlen(s+1);
		for(int i=1;i<=Len;++i) nt[i]=0;
		for(int i=2;i<=Len;++i) {
			while(p&&s[p+1]!=s[i]) p=nt[p];
			if(s[p+1]==s[i]) {
				p++;
			}
			nt[i]=p;
		}
		int l2=Len-nt[Len];ans=0;
		qwq=rt; L=0;
		for(int i=1;i<=Len;++i)
		find(s[i]-'a');
		if(L==Len) {
			ans+=siz[qwq];
		}
		//cout<<l2<<"*\n";
		if((Len%l2)) l2=Len-1;
		else l2=l2-1;
		//cout<<l2<<"*\n";
		for(int i=1;i<=l2;++i) {
			//cout<<Len<<"++"<<L<<"\n";
			if(Len==L) {
				if(L-1==len[fa[qwq]])
				qwq=fa[qwq];L--;
			}
			find(s[i]-'a');
			if(L==Len) {
				ans+=siz[qwq];
			}
		}
		cout<<ans<<"\n";
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <set>

std::set<int>set;
int son[2000001][26],fa[2000001],size[2000001],len[2000001],cnt=1,last=1,pre[2000001][22],n,Q,tax[2000001],pos[2000001],P=998244353;
long long hash[2000001];
char str[2000001];
void add(int x){
	int p=last;
	size[last=++cnt]=1;
	len[last]=n;
	for(;p&&!son[p][x];p=fa[p])son[p][x]=last;
	if(!p)return (void)(fa[last]=1);
	int q=son[p][x];
	if(len[q]==len[p]+1)return (void)(fa[last]=q);
	len[++cnt]=len[p]+1;
	fa[cnt]=fa[q];
	fa[q]=fa[last]=cnt;
	for(int i=0;i<26;i++)son[cnt][i]=son[q][i];
	for(;son[p][x]==q;p=fa[p])son[p][x]=cnt;
}
int min(int a,int b){return a<b?a:b;}
bool allsame(){
	for(int i=2;i<=n;i++)if(str[i]!=str[i-1])return 0;
	return 1;
}
typedef long long ll;
int qsm(int a,int b){
	int ans=1;
	while(b){
		if(b&1)ans=(ll)ans*a%P;
		a=(ll)a*a%P;
		b>>=1;
	}
	return ans;
}
int main(){
	scanf("%s",str+1);
	for(n=1;str[n];n++)add(str[n]-'a');

	for(int i=1;i<=cnt;i++)tax[len[i]]++;
	for(int i=1;i<=n;i++)tax[i]+=tax[i-1];
	for(int i=cnt;i;i--)pos[tax[len[i]]--]=i;
	for(int i=cnt;i;i--)size[fa[pos[i]]]+=size[pos[i]];

	scanf("%d",&Q);
	for(int i=1;i<=Q;i++){
		scanf("%s",str+1);
		n=strlen(str+1);
		for(int i=1;i<=n;i++)str[i+n]=str[i];
		int p=1,length=0,pow=qsm(6666,n);
		long long ans=0;
		for(int i=1;i<n<<1;i++){
			hash[i]=((ll)hash[i-1]*6666+(ll)233*str[i])%P;
			if(i>n){
				if(i==n+1)hash[i]=(hash[i]-((ll)pow*hash[i-n]%P)+P)%P;
				else hash[i]=(hash[i]-((ll)pow*233*str[i-n]%P)+P)%P;
			}
			while(p&&!son[p][str[i]-'a'])p=fa[p];
			if(!p)p=1,length=0;
			else length=min(length,len[p])+1,p=son[p][str[i]-'a'];
			//printf("%d %d %d\n",i,p,length);
			if(i<n)continue;
			if(set.count(hash[i]))break;
			set.insert(hash[i]);
			while(len[fa[p]]>=n)p=fa[p],length=len[p];
			if(length>=n)ans+=size[p];
		}
		set.clear();
		printf("%lld\n",ans);
	}
}


#include <cstdio>
#include <cstring>

int son[2000001][26],fa[2000001],size[2000001],len[2000001],cnt=1,last=1,n,Q,tax[2000001],pos[2000001],P=998244353,queue[2000001],t;
bool vis[2000001];
char str[2000001];
void add(int x){
    int p=last;
    size[last=++cnt]=1;
    len[last]=n;
    for(;p&&!son[p][x];p=fa[p])son[p][x]=last;
    if(!p)return (void)(fa[last]=1);
    int q=son[p][x];
    if(len[q]==len[p]+1)return (void)(fa[last]=q);
    len[++cnt]=len[p]+1;
    fa[cnt]=fa[q];
    fa[q]=fa[last]=cnt;
    for(int i=0;i<26;i++)son[cnt][i]=son[q][i];
    for(;son[p][x]==q;p=fa[p])son[p][x]=cnt;
}
int min(int a,int b){return a<b?a:b;}
bool allsame(){
    for(int i=2;i<=n;i++)if(str[i]!=str[i-1])return 0;
    return 1;
}
typedef long long ll;
int qsm(int a,int b){
    int ans=1;
    while(b){
        if(b&1)ans=(ll)ans*a%P;
        a=(ll)a*a%P;
        b>>=1;
    }
    return ans;
}
int main(){
    scanf("%s",str+1);
    for(n=1;str[n];n++)add(str[n]-'a');

    for(int i=1;i<=cnt;i++)tax[len[i]]++;
    for(int i=1;i<=n;i++)tax[i]+=tax[i-1];
    for(int i=cnt;i;i--)pos[tax[len[i]]--]=i;
    for(int i=cnt;i;i--)size[fa[pos[i]]]+=size[pos[i]];

    scanf("%d",&Q);
    for(int i=1;i<=Q;i++){
        scanf("%s",str+1);
        n=strlen(str+1);
        for(int i=1;i<=n;i++)str[i+n]=str[i]=str[i]-'a';
        int p=1,length=0;
        long long ans=0;
        for(int i=1;i<n<<1;i++){
            while(p&&!son[p][str[i]])p=fa[p];
            if(!p)p=1,length=0;
            else length=min(length,len[p])+1,p=son[p][str[i]];
            if(i<n)continue;
            while(len[fa[p]]>=n)p=fa[p];
            length=min(length,len[p]);
            if(length>=n){
            	if(vis[p])break;
				ans+=size[p],vis[p]=1,queue[++t]=p;
			}
        }
        for(int i=1;i<=t;i++)vis[queue[i]]=0;t=0;
        printf("%lld\n",ans);
    }
}


#include <bits/stdc++.h>
using namespace std;

const int MAXN = 2e6 + 5;

string str;
int n, pre = 1, cnt = 1, ans = 0;
int mx[MAXN], fa[MAXN], ch[MAXN][26], siz[MAXN];
int b[MAXN], r[MAXN], vis[MAXN];

void extend(int x)
{
    int p = pre, np = pre = ++cnt;
    mx[np] = mx[p] + 1; siz[np] = 1;
    while(p && !ch[p][x]) ch[p][x] = np, p = fa[p];
    if(!p) {fa[np] = 1; return ;}
    int q = ch[p][x];
    if(mx[q] == mx[p] + 1) {fa[np] = q; return ;}
    int nq = ++cnt;
    fa[nq] = fa[q]; fa[q] = fa[np] = nq;
    mx[nq] = mx[p] + 1;
    for(int i = 0; i < 26; i++)
        ch[nq][i] = ch[q][i];
    while(p && ch[p][x] == q) ch[p][x] = nq, p = fa[p];
}

void radix_sort()
{
    for(int i = 1; i <= cnt; i++) b[mx[i]]++;
    for(int i = 1; i <= cnt; i++) b[i] += b[i - 1];
    for(int i = cnt; i; i--) r[b[mx[i]]--] = i;
    for(int i = cnt; i; i--) siz[fa[r[i]]] += siz[r[i]];
}

int main()
{
	cin>>str>>n; int len = str.size();
	for(int i = 0; i < len; i++)
		extend(str[i] - 'a');
	radix_sort();
	for(int i = 1; i <= n; i++)
	{
		cin>>str;
		str += str; len = str.size();
		int cur = 1, l = 0; ans = 0;
		for(int j = 0; j < len; j++)
		{
			int x = str[j] - 'a';
			while(cur && !ch[cur][x]) cur = fa[cur], l = mx[cur];
			if(!cur) cur = 1, l = 0;
			else cur = ch[cur][x], l++;
			while(mx[fa[cur]] >= len / 2)
				cur = fa[cur], l = mx[cur];
			if(l >= len / 2 && vis[cur] != i)
				ans += siz[cur], vis[cur] = i;
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <set>

std::set<int>set;
int son[2000001][26],fa[2000001],size[2000001],len[2000001],cnt=1,last=1,pre[2000001][22],n,Q,tax[2000001],pos[2000001],P=998244353,queue[2000001],t;
bool vis[2000001];
long long hash[2000001];
char str[2000001];
void add(int x){
    int p=last;
    size[last=++cnt]=1;
    len[last]=n;
    for(;p&&!son[p][x];p=fa[p])son[p][x]=last;
    if(!p)return (void)(fa[last]=1);
    int q=son[p][x];
    if(len[q]==len[p]+1)return (void)(fa[last]=q);
    len[++cnt]=len[p]+1;
    fa[cnt]=fa[q];
    fa[q]=fa[last]=cnt;
    for(int i=0;i<26;i++)son[cnt][i]=son[q][i];
    for(;son[p][x]==q;p=fa[p])son[p][x]=cnt;
}
int min(int a,int b){return a<b?a:b;}
bool allsame(){
    for(int i=2;i<=n;i++)if(str[i]!=str[i-1])return 0;
    return 1;
}
typedef long long ll;
int qsm(int a,int b){
    int ans=1;
    while(b){
        if(b&1)ans=(ll)ans*a%P;
        a=(ll)a*a%P;
        b>>=1;
    }
    return ans;
}
int main(){
    scanf("%s",str+1);
    for(n=1;str[n];n++)add(str[n]-'a');

    for(int i=1;i<=cnt;i++)tax[len[i]]++;
    for(int i=1;i<=n;i++)tax[i]+=tax[i-1];
    for(int i=cnt;i;i--)pos[tax[len[i]]--]=i;
    for(int i=cnt;i;i--)size[fa[pos[i]]]+=size[pos[i]];

    scanf("%d",&Q);
    for(int i=1;i<=Q;i++){
        scanf("%s",str+1);
        n=strlen(str+1);
        for(int i=1;i<=n;i++)str[i+n]=str[i];
        int p=1,length=0;
        long long ans=0;
        for(int i=1;i<n<<1;i++){
            while(p&&!son[p][str[i]-'a'])p=fa[p];
            if(!p)p=1,length=0;
            else length=min(length,len[p])+1,p=son[p][str[i]-'a'];
            if(i<n)continue;
            while(len[fa[p]]>=n)p=fa[p];
            length=min(length,len[p]);
            if(vis[p])break;
            if(length>=n)ans+=size[p],vis[p]=1,queue[++t]=p;
        }
        for(int i=1;i<=t;i++)vis[queue[i]]=0;t=0;
        printf("%lld\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define ll long long
#define cs const
#define fr first
#define se second
//#define ls (now<<1)
#define rs (now<<1|1)
#define mid ((l+r)>>1)
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define low(i) (i&(-i))
#define par pair<int,int>
#define cn(x) memset(x, 0, sizeof(x))
#define rep(i, x, y) for(int i=x; i<=y; ++i)
#define sep(i, x, y) for(int i=x; i>=y; --i)
#define fore(i, x) for(int i=fir[x]; i; i=nex[i])

cs int G = 3;
cs int ff = 2e6 + 11;
cs ll inf = 1e17 + 1;
cs int M = 1e9 + 7;
cs double eps = 1e-7;

void Add(ll &x, ll y) { x = ((x + y % M) % M + M) % M; }
void Del(ll &x, ll y) { x = ((x - y % M) % M + M) % M; }
void Mul(ll &x, ll y) { x = ((x * y % M) % M + M) % M; }

ll siz[ff][4];
int rt[ff], cnt, n;
struct Node
{
	int tot, las;
	struct node { int fa, ch[26]; int len; } tr[600011];
	void clear(int x) { cn(tr[x].ch); tr[x].len = tr[x].fa = 0; }
	void add(int x, int pos)
	{
		int p = las, np = ++tot;
		las = tot; siz[tot][pos] = 1;

		tr[np].len = tr[p].len + 1;
		while(p && !tr[p].ch[x]) tr[p].ch[x] = np, p = tr[p].fa;

		if(p == 0) return (void) (tr[np].fa = 1);

		int q = tr[p].ch[x];
		if(tr[q].len == tr[p].len + 1) return (void)(tr[np].fa = q);

		int nq = ++tot; tr[nq] = tr[q];
		tr[nq].len = tr[p].len + 1;
		tr[q].fa = tr[np].fa = nq;
		while(p && tr[p].ch[x] == q) tr[p].ch[x] = nq, p = tr[p].fa;

	}
}Sam;

int fir[ff], nex[ff], to[ff], totq;
void ade(int x, int y)
{
	nex[++totq] = fir[x];
	fir[x] = totq;
	to[totq] = y;
}

ll A[ff];
ll Ans = 0;
void dfs(int now, int las)
{
 	fore(i, now)
 		if(to[i] != las)
			dfs(to[i], now), siz[now][1] += siz[to[i]][1];
}

int tag[ff];
char s[ff];

#define mnlen (Sam.tr[Sam.tr[now].fa].len + 1)
#define mxlen (Sam.tr[now].len)
#define son(x, y) (Sam.tr[x].ch[y])
#define f(x) (Sam.tr[x].fa)
void init()
{

//	rep(i, 1, n) rt[i] = ++cnt;
	Sam.las = Sam.tot = 1;
	cin >> (s + 1); int len = strlen(s + 1);
	rep(i, 1, len) Sam.add(s[i] - 'a', 1);
	rep(i, 2, Sam.tot) ade(Sam.tr[i].fa, i), ade(i, Sam.tr[i].fa);

	dfs(1, 0);

	int T; cin >> T;
	while(T --)
	{
		cin >> (s + 1); len = strlen(s + 1); n = len;
		rep(i, 1, len - 1) s[i + len] = s[i];
		len = len + len - 1;
		int now = 1, lq = 0;
		Ans = 0;
		queue<int> qs;
		rep(i, 1, len)
		{
			int x = s[i] - 'a';
			while(now != 1 && !son(now, x)) now = f(now), lq = mxlen;
			if(son(now, x)) lq ++, now = son(now, x);

		//	cout << now << " " << lq << " " << n << " " << siz[now][1] << " " << Ans << "\n";
			if(lq == n)
			{
				Ans += siz[now][1], siz[now][2] = max(siz[now][2], siz[now][1]), siz[now][1] = 0;
				qs.push(now), lq --;
				if(mnlen == n) now = f(now);
			}

		}
		int u;
	//	cout << "\n";
		while(!qs.empty()) u = qs.front(), siz[u][1] = siz[u][2], qs.pop();
	//	cout << "\n";
		cout << Ans << "\n";
	}
}
signed main()
{
//	freopen("1.in", "r", stdin);
	int Ts = 1;
	while(Ts--)
		init();
}


#include <iostream>
#include <map>
#include <string>

using namespace std;
const int N = 1e5 + 5;
const int L = 1e6 + 5;

namespace SAM {
	const int T = L << 1;
	struct Node {
		map<char, int> ch;
		int link, len, size;
	} t[T];

	int last;
	int total;

	void extend_char(char c) {
		int p = last, np = last = ++total;
		t[np].len = t[p].len + 1;

		for (; p && !t[p].ch[c]; p = t[p].link)
			t[p].ch[c] = np;

		if (!p) {
			t[np].link = 1;
		} else {
			int q = t[p].ch[c];
			if (t[p].len + 1 == t[q].len) {
				t[np].link = q;
			} else {
				int nq = ++total;
				t[nq] = t[q], t[nq].len = t[p].len + 1, t[nq].size = 0;
				t[np].link = t[q].link = nq;
				for (; p && t[p].ch[c] == q; p = t[p].link)
					t[p].ch[c] = nq;
			}
		}
		t[np].size = 1;
	}

	int b[T], c[T];
	void init_data(string& s) {
		last = total = 1;
		for (string::iterator it = s.begin(); it != s.end(); it++)
			extend_char(*it);

		for (register int i = 1; i <= total; i++) ++c[t[i].len];
		for (register int i = 1; i <= total; i++) c[i] += c[i - 1];
		for (register int i = 1; i <= total; i++) b[c[t[i].len]--] = i;

		for (register int i = total; i; i--) {
			int x = b[i], f = t[x].link;
			t[f].size += t[x].size;
		}
	}

	int vis[T];
	long long count(string& str, int time);
};

long long SAM::count(string& str, int time) {
	long long ret = 0ll;
	int x = 1, len = 0;

	str = str + str;
	for (register int i = 0; i < str.size(); i++) {
		char c = str[i];
		while (x && !t[x].ch[c]) x = t[x].link, len = t[x].len;
		if (!x) x = 1, len = 0;
		else x = t[x].ch[c], len++;

		while (x && t[t[x].link].len >= str.size() / 2)
			x = t[x].link, len = t[x].len;
		if (len == str.size() / 2 && vis[x] != time)
			ret += t[x].size, vis[x] = time;
	}
	return ret;
}

signed main() {
	ios::sync_with_stdio(false);

	string str;
	cin >> str;
	SAM::init_data(str);

	int m;
	cin >> m;
	for (register int time = 1; time <= m; time++) {
		cin >> str;
		cout << SAM::count(str, time) << endl;
	}

	return 0;
}


#pragma GCC optimize(3)
#include <cstdio>
#include <cstring>
#include <set>

std::set<int>set;
int son[2000001][26],fa[2000001],size[2000001],len[2000001],cnt=1,last=1,pre[2000001][22],n,Q,tax[2000001],pos[2000001],P=998244353,queue[2000001],t;
bool vis[2000001];
long long hash[2000001];
char str[2000001];
void add(int x){
    int p=last;
    size[last=++cnt]=1;
    len[last]=n;
    for(;p&&!son[p][x];p=fa[p])son[p][x]=last;
    if(!p)return (void)(fa[last]=1);
    int q=son[p][x];
    if(len[q]==len[p]+1)return (void)(fa[last]=q);
    len[++cnt]=len[p]+1;
    fa[cnt]=fa[q];
    fa[q]=fa[last]=cnt;
    for(int i=0;i<26;i++)son[cnt][i]=son[q][i];
    for(;son[p][x]==q;p=fa[p])son[p][x]=cnt;
}
int min(int a,int b){return a<b?a:b;}
bool allsame(){
    for(int i=2;i<=n;i++)if(str[i]!=str[i-1])return 0;
    return 1;
}
typedef long long ll;
int qsm(int a,int b){
    int ans=1;
    while(b){
        if(b&1)ans=(ll)ans*a%P;
        a=(ll)a*a%P;
        b>>=1;
    }
    return ans;
}
int main(){
    scanf("%s",str+1);
    for(n=1;str[n];n++)add(str[n]-'a');

    for(int i=1;i<=cnt;i++)tax[len[i]]++;
    for(int i=1;i<=n;i++)tax[i]+=tax[i-1];
    for(int i=cnt;i;i--)pos[tax[len[i]]--]=i;
    for(int i=cnt;i;i--)size[fa[pos[i]]]+=size[pos[i]];

    scanf("%d",&Q);
    for(int i=1;i<=Q;i++){
        scanf("%s",str+1);
        n=strlen(str+1);
        for(int i=1;i<=n;i++)str[i+n]=str[i];
        int p=1,length=0;
        long long ans=0;
        for(int i=1;i<n<<1;i++){
            while(p&&!son[p][str[i]-'a'])p=fa[p];
            if(!p)p=1,length=0;
            else length=min(length,len[p])+1,p=son[p][str[i]-'a'];
            if(i<n)continue;
            while(len[fa[p]]>=n)p=fa[p];
            length=min(length,len[p]);
            if(length>=n){
            	if(vis[p])break;
                ans+=size[p],vis[p]=1,queue[++t]=p;
            }
        }
        for(int i=1;i<=t;i++)vis[queue[i]]=0;t=0;
        printf("%lld\n",ans);
    }
}


#include<cstdio>
#define re register
#define fr(i,n) for(re int i=1; i<=n; ++i)
#define rf(i,n) for(re int i=n; i>=1; --i)
#define fro(i,a,b) for(re int i=a; i<=b; ++i)
#define rfo(i,a,b) for(re int i=a; i>=b; --i)
#define gc pa==pb&&(pb=(pa=buf)+fread(buf,1,100000,stdin),pa==pb)?EOF:*pa++
static char buf[100000],*pa(buf),*pb(buf);
inline int read() {
	re int x(0); re char c(gc);
	while((c<'0'||c>'9')&&c!='-') c=gc;
	re int f(c=='-'?c=gc,-1:1);
	while(c>='0'&&c<='9') x=x*10+c-48,c=gc;
	return x*f;
}
const int maxn=2000100;
int n,m;
int len[maxn],ch[maxn][27],fa[maxn],siz[maxn];
char s[maxn];
int tp[maxn],st[maxn];
int val[maxn],vis[maxn];
int tot=1,last=1;
inline void check() {
	fr(i,tot) printf("%d %d %d %d\n",i,fa[i],siz[i],len[i]);
	puts("=================================");
	return;
}
inline void insert(int k) {
	re int p=last,np=++tot;
	last=tot;
	len[np]=len[p]+1; siz[np]=1;
	while(p&&!ch[p][k]) {
		ch[p][k]=np;
		p=fa[p];
	}
	if(!p) {
		fa[np]=1;
		return;
	}
	re int q=ch[p][k];
	if(len[q]==len[p]+1) {
		fa[np]=q;
		return;
	}
	re int nq=++tot;
	fr(i,26) ch[nq][i]=ch[q][i];
	len[nq]=len[p]+1;
	fa[nq]=fa[q];
	fa[np]=nq; fa[q]=nq;
	while(p&&ch[p][k]==q) {
		ch[p][k]=nq;
		p=fa[p];
	}
	return;
}
int main() {
//	freopen("test.in" ,"r", stdin);
//	freopen("test.out","w",stdout);
	scanf("%s",s+1);
	while(s[n+1]) {
		++n;
		insert(s[n]-'a'+1);
	}
	fr(i,tot) ++tp[len[i]];
	fr(i,tot) tp[i]+=tp[i-1];
	rf(i,tot) st[tp[len[i]]--]=i;
	rf(i,tot) siz[fa[st[i]]]+=siz[st[i]];
//	check();
	scanf("%d",&m);
//	m=read();
	fr(t,m) {
		scanf("%s",s+1);
		n=0;
		while(s[n+1]) {
			++n;
			val[n]=s[n]-'a'+1;
		}
		fr(i,n) val[i+n]=val[i];
		re int ln=0,now=1;
		re int ans=0;
		fr(i,(n<<1)) {
			re int k=val[i];
			while(now&&!ch[now][k]) {
				now=fa[now];
				ln=len[now];
			}
			if(!now) now=1,ln=0;
            else now=ch[now][k],++ln;
            while(now&&len[fa[now]]>=n) now=fa[now],ln=len[now];
            if(ln>=n&&vis[now]!=t) ans+=siz[now],vis[now]=t;
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 2e5 + 10;

char s[N];

int n, L, las = 1, tot = 1;
int fa[N], len[N], cnt[N], ch[N][26], times[N];

vector <int > v[N];

inline int read();

void Insert(int c)
{
	int p = las, new_p = las = ++tot;
	len[new_p] = len[p] + 1, times[new_p]++;
	for(; p && !ch[p][c]; p = fa[p])
		ch[p][c] = new_p;

	if(!p) fa[new_p] = 1;
	else
	{
		int q = ch[p][c];

		if(len[q] == len[p] + 1) fa[new_p] = q;
		else
		{
			int new_q = ++tot;
			memcpy(ch[new_q], ch[q], sizeof(ch[q]));
			fa[new_q] = fa[q], fa[q] = fa[new_p] = new_q;
			len[new_q] = len[p] + 1;

			for(; p && ch[p][c] == q; p = fa[p])
				ch[p][c] = new_q;
		}
	}
}

long long ans;

signed main()
{
	scanf("%s", s), n = strlen(s);
	for(register int k = 0; k < n; k++)
		Insert(s[k] - 'a');
	for(register int k = 2; k <= tot; k++)
		v[len[k]].push_back(k);
	for(register int k = n; k >= 1; k--)
		for(int i = 0; i < (int )v[k].size(); i++)
			times[fa[v[k][i]]] += times[v[k][i]];

	n = read();
	for(register int k = 1; k <= n; k++)
	{
		memset(cnt, 0, sizeof(cnt));
		scanf("%s", s), L = strlen(s), ans = 0;
		int wz = 1, l = 0;

		for(int i = 0; i < L; i++)
		{
			while(wz != 1 && !ch[wz][s[i] - 'a'])
				wz = fa[wz], l = len[wz];
			if(ch[wz][s[i] - 'a'])
				wz = ch[wz][s[i] - 'a'], l++;
		}
		for(int i = 0; i < L; i++)
		{
			if(l == L && !cnt[wz]) ans += times[wz], cnt[wz] = 1, l--;
			if(l <= len[fa[wz]]) wz = fa[wz];

			while(wz != 1 && !ch[wz][s[i] - 'a'])
				wz = fa[wz], l = len[wz];
			if(ch[wz][s[i] - 'a'])
				wz = ch[wz][s[i] - 'a'], l++;
		}
		printf("%lld\n", ans);
	}

	return 0;
}

inline int read()
{
	int fh = 1, x = 0, ch = '~';

	while(!(ch >= '0' && ch <= '9')) fh = ch == '-' ? -1 : 1, ch = getchar();
	while(ch >= '0' && ch <= '9') x = (x << 3) + (x << 1), x += ch - '0', ch = getchar();

	return x * fh;
}


#include <cstdio>
#include <cstring>

int son[2000001][26],fa[2000001],size[2000001],len[2000001],cnt=1,last=1,n,Q,tax[2000001],pos[2000001],P=998244353,t;
bool *queue[2000001];
bool vis[2000001];
char str[2000001];
void add(int x){
    int p=last;
    size[last=++cnt]=1;
    len[last]=n;
    for(;p&&!son[p][x];p=fa[p])son[p][x]=last;
    if(!p)return (void)(fa[last]=1);
    int q=son[p][x];
    if(len[q]==len[p]+1)return (void)(fa[last]=q);
    len[++cnt]=len[p]+1;
    fa[cnt]=fa[q];
    fa[q]=fa[last]=cnt;
    for(int i=0;i<26;i++)son[cnt][i]=son[q][i];
    for(;son[p][x]==q;p=fa[p])son[p][x]=cnt;
}
int min(int a,int b){return a<b?a:b;}
bool allsame(){
    for(int i=2;i<=n;i++)if(str[i]!=str[i-1])return 0;
    return 1;
}
typedef long long ll;
int qsm(int a,int b){
    int ans=1;
    while(b){
        if(b&1)ans=(ll)ans*a%P;
        a=(ll)a*a%P;
        b>>=1;
    }
    return ans;
}
int main(){
    scanf("%s",str+1);
    for(n=1;str[n];n++)add(str[n]-'a');

    for(int i=1;i<=cnt;i++)tax[len[i]]++;
    for(int i=1;i<=n;i++)tax[i]+=tax[i-1];
    for(int i=cnt;i;i--)pos[tax[len[i]]--]=i;
    for(int i=cnt;i;i--)size[fa[pos[i]]]+=size[pos[i]];

    scanf("%d",&Q);
    for(int i=1;i<=Q;++i){
        scanf("%s",str+1);
        n=strlen(str+1);
        for(int i=1;i<=n;++i)str[i+n]=str[i]=str[i]-'a';
        int p=1,length=0;
        long long ans=0;
        for(int i=1;i<n<<1;++i){
            while(p&&!son[p][str[i]])p=fa[p];
            if(!p)p=1,length=0;
            else length=min(length,len[p])+1,p=son[p][str[i]];
            while(len[fa[p]]>=n)p=fa[p];
            length=min(length,len[p]);
            if(length>=n){
            	if(vis[p])break;
				ans+=size[p],vis[p]=1,queue[++t]=&vis[p];
			}
        }
        for(int i=1;i<=t;++i)*queue[i]=0;t=0;
        printf("%lld\n",ans);
    }
}


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

using namespace std;
const int N=2000005;
typedef long long LL;

int n,len,a[N],c[N],stk[N],top,f[N][22];
LL ans;
char s[N],t[N];
bool vis[N];

struct SAM{
	int fa[N],ch[N][28],l[N],siz[N],cnt,lst;
	void Insert(int c){
		int p=lst,np=++cnt; l[np]=l[p]+1; lst=cnt; siz[np]=1;
		for(;p && !ch[p][c];p=fa[p]) ch[p][c]=np;
		if(!p) fa[np]=1;
		else {
			int q=ch[p][c];
			if(l[q]==l[p]+1) fa[np]=q;
			else {
				int nq=++cnt; l[nq]=l[p]+1;
				memcpy(ch[nq],ch[q],sizeof(ch[nq]));
				fa[nq]=fa[q]; fa[q]=fa[np]=nq;
				for(;ch[p][c]==q;p=fa[p]) ch[p][c]=nq;
			}
		}
	}
	void solve(){
		for(int i=1;i<=cnt;i++) c[l[i]]++;
		for(int i=1;i<=cnt;i++) c[i]+=c[i-1];
		for(int i=1;i<=cnt;i++) a[c[l[i]]--]=i;
		for(int i=cnt;i;i--) siz[fa[a[i]]]+=siz[a[i]];
		for(int i=2;i<=cnt;i++){
			int p=a[i]; f[p][0]=fa[p];
			for(int j=1;j<=20;j++)
				f[p][j]=f[f[p][j-1]][j-1];
		}
	}
}sam;

int main(){
	scanf("%s%d",s+1,&n); sam.cnt=sam.lst=1; len=strlen(s+1);
	for(int i=1;i<=len;i++) sam.Insert(s[i]-'a'+1);
	sam.solve(); int now,tmp;
	for(int i=1;i<=n;i++){
		scanf("%s",t+1); len=strlen(t+1);
		for(int j=1;j<=len;j++) t[j+len]=t[j];
		now=1; ans=0; tmp=0; top=0;
		for(int j=1;j<=len*2;j++){
			if(sam.ch[now][t[j]-'a'+1]) now=sam.ch[now][t[j]-'a'+1],tmp++;
			else {
				for(;now && !sam.ch[now][t[j]-'a'+1];now=sam.fa[now]);
				if(!now) now=1,tmp=0;
				else tmp=sam.l[now]+1,now=sam.ch[now][t[j]-'a'+1];
			}
			if(tmp>=len) {
				int zz=now;
				for(int k=20;k>=0;k--)
					if(f[zz][k] && sam.l[f[zz][k]]>=len) zz=f[zz][k];
				if(vis[zz]) continue; vis[zz]=1; stk[++top]=zz;
				ans+=sam.siz[zz];
			}
		}
		while(top) vis[stk[top--]]=0;
		printf("%lld\n",ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define ll long long
#define cs const
#define fr first
#define se second
//#define ls (now<<1)
#define rs (now<<1|1)
#define mid ((l+r)>>1)
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define low(i) (i&(-i))
#define par pair<int,int>
#define cn(x) memset(x, 0, sizeof(x))
#define rep(i, x, y) for(int i=x; i<=y; ++i)
#define sep(i, x, y) for(int i=x; i>=y; --i)
#define fore(i, x) for(int i=fir[x]; i; i=nex[i])

cs int G = 3;
cs int ff = 2e6 + 11;
cs ll inf = 1e17 + 1;
cs int M = 1e9 + 7;
cs double eps = 1e-7;

void Add(ll &x, ll y) { x = ((x + y % M) % M + M) % M; }
void Del(ll &x, ll y) { x = ((x - y % M) % M + M) % M; }
void Mul(ll &x, ll y) { x = ((x * y % M) % M + M) % M; }

ll siz[ff][4];
int rt[ff], cnt, n;
struct Node
{
	int tot, las;
	struct node { int fa, ch[26]; int len; } tr[600011];
	void clear(int x) { cn(tr[x].ch); tr[x].len = tr[x].fa = 0; }
	void add(int x, int pos)
	{
		int p = las, np = ++tot;
		las = tot; siz[tot][pos] = 1;

		tr[np].len = tr[p].len + 1;
		while(p && !tr[p].ch[x]) tr[p].ch[x] = np, p = tr[p].fa;

		if(p == 0) return (void) (tr[np].fa = 1);

		int q = tr[p].ch[x];
		if(tr[q].len == tr[p].len + 1) return (void)(tr[np].fa = q);

		int nq = ++tot; tr[nq] = tr[q];
		tr[nq].len = tr[p].len + 1;
		tr[q].fa = tr[np].fa = nq;
		while(p && tr[p].ch[x] == q) tr[p].ch[x] = nq, p = tr[p].fa;

	}
}Sam;

int fir[ff], nex[ff], to[ff], totq;
void ade(int x, int y)
{
	nex[++totq] = fir[x];
	fir[x] = totq;
	to[totq] = y;
}

ll A[ff];
ll Ans = 0;
void dfs(int now, int las)
{
 	fore(i, now)
 		if(to[i] != las)
			dfs(to[i], now), siz[now][1] += siz[to[i]][1];
}

int tag[ff];
char s[ff];

#define mnlen (Sam.tr[Sam.tr[now].fa].len + 1)
#define mxlen (Sam.tr[now].len)
#define son(x, y) (Sam.tr[x].ch[y])
#define f(x) (Sam.tr[x].fa)
void init()
{

//	rep(i, 1, n) rt[i] = ++cnt;
	Sam.las = Sam.tot = 1;
	cin >> (s + 1); int len = strlen(s + 1);
	rep(i, 1, len) Sam.add(s[i] - 'a', 1);
	rep(i, 2, Sam.tot) ade(Sam.tr[i].fa, i), ade(i, Sam.tr[i].fa);

	dfs(1, 0);

	int T; cin >> T;
	while(T --)
	{
		cin >> (s + 1); len = strlen(s + 1); n = len;
		rep(i, 1, len - 1) s[i + len] = s[i];
		len = len + len - 1;
		int now = 1, lq = 0;
		Ans = 0;
		queue<int> qs;
	//	cout << " ffffefefef " << siz[3][1] << "\n";
		rep(i, 1, len)
		{
			int x = s[i] - 'a';
			while(now != 1 && !son(now, x)) now = f(now), lq = mxlen;
			if(son(now, x)) lq ++, now = son(now, x);
	//		cout << now << " ";
			if(lq == n)
			{
				Ans += siz[now][1], siz[now][2] = siz[now][1], siz[now][1] = 0,
				qs.push(now), lq --;
				if(mnlen == n) now = f(now);
			}
		//	cout << now << " " << i << " " << lq << " " << siz[now][1] << " " << siz[now][2] << " " << Ans << "\n";
		}
		int u;
		while(!qs.empty()) u = qs.front(), siz[u][1] = siz[u][2], qs.pop();//, cout << u << " " << siz[u][1] << "\n";
		cout << Ans << "\n";
	}
}
signed main()
{
//	freopen("1.in", "r", stdin);
	int Ts = 1;
	while(Ts--)
		init();
}


#include<bits/stdc++.h>
#define rep(i,a,b) for (int i = (a); i <= (b); ++i)
#define drep(i,a,b) for (int i = (a); i >= (b); --i)
#define grep(i,u) for (int i = head[u],v = e[i].v; i; v = e[i = e[i].nxt].v)
#define il inline
#define LL long long
using namespace std;
il int read() {
	int res = 0,f = 1; char ch = getchar(); while (!isdigit(ch)) {if (ch == '-') f = -f; ch = getchar();}
	while (isdigit(ch)) {res = (res<<3)+(res<<1)+ch-'0'; ch = getchar();} return res*f;
}
namespace qiqi {
	const int N = 2e6+5; int n,m,Q,now = 1,cnt = 1,a[N],c[N],ch[N][26],siz[N],len[N],link[N],vis[N]; char s[N];
	il void ins(int c) {
		int p = now; len[now = ++cnt] = len[p]+1; siz[now] = 1; while (p && !ch[p][c]) {ch[p][c] = now; p = link[p];}
		if (!p) {link[now] = 1; return;} int q = ch[p][c]; if (len[p]+1 == len[q]) {link[now] = q; return;}
		link[++cnt] = link[q]; len[link[now] = link[q] = cnt] = len[p]+1; memcpy(ch[cnt],ch[q],sizeof(ch[q]));
		while (p && ch[p][c] == q) {ch[p][c] = cnt; p = link[p];}
	}
	void main() {
		scanf("%s",s+1); n = strlen(s+1); rep(i,1,n) ins(s[i]-'a'); rep(i,1,cnt) ++c[len[i]]; rep(i,1,n) c[i] += c[i-1]; rep(i,1,cnt) a[c[len[i]]--] = i; drep(i,cnt,1) siz[link[a[i]]] += siz[a[i]];
		Q = read(); rep(T,1,Q) {
			int ans = 0,x = 1,k = 0,c; scanf("%s",s+1); n = strlen(s+1); m = n<<1; rep(i,1,n) s[i+n] = s[i];
			rep(i,1,m) {c = s[i]-'a'; while (x && !ch[x][c]) k = len[x = link[x]]; if (x) {x = ch[x][c]; ++k;} else x = 1; if (k > n && --k == len[link[x]]) x = link[x]; if (k == n && vis[x] != T) {ans += siz[x]; vis[x] = T;}}
			printf("%d\n",ans);
		}
	}
}
int main() {qiqi::main(); return 0;}


#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#define maxn 4000005
#define re register
#define LL long long
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
struct E{int v,nxt;}e[maxn];
int n,lst=1,cnt=1,num,m;
int len[maxn],head[maxn],sz[maxn],son[maxn][26],f[maxn][22],log_2[maxn],deep[maxn],fa[maxn],vis[maxn];
inline void add(int x,int y) {e[++num].v=y;e[num].nxt=head[x];head[x]=num;}
void dfs(int x) {for(re int i=head[x];i;i=e[i].nxt) deep[e[i].v]=deep[x]+1,dfs(e[i].v),sz[x]+=sz[e[i].v];}
int st[500005],top;
std::vector<int> a[100005];
int L[100005];
char S[1000005];
inline void ins(int c,int o,int t)
{
	int f=lst,p=++cnt; lst=p;
	len[p]=len[f]+1; if(!o) sz[p]=1;
	if(o&&t) a[o].push_back(p);
	while(f&&!son[f][c]) son[f][c]=p,f=fa[f];
	if(!f) {fa[p]=1;return;}
	int x=son[f][c];
	if(len[f]+1==len[x]) {fa[p]=x;return;}
	int y=++cnt;
	len[y]=len[f]+1,fa[y]=fa[x],fa[x]=fa[p]=y;
	for(re int i=0;i<26;i++) son[y][i]=son[x][i];
	while(f&&son[f][c]==x) son[f][c]=y,f=fa[f];
}
inline int find(int x,int li)
{
	for(re int i=log_2[deep[x]];i>=0;--i)
		if(f[x][i]&&len[f[x][i]]>=li) x=f[x][i];
	return x;
}
int main()
{
	scanf("%s",S+1),n=strlen(S+1);scanf("%d",&m);
	for(re int i=1;i<=n;i++) ins(S[i]-'a',0,0);
	for(re int i=1;i<=m;i++)
	{
		scanf("%s",S+1);L[i]=strlen(S+1);lst=1;
		for(re int j=1;j<L[i];j++) ins(S[j]-'a',i,0);ins(S[L[i]]-'a',i,1);
		for(re int j=1;j<L[i];j++) ins(S[j]-'a',i,1);
	}
	for(re int i=2;i<=cnt;i++) add(fa[i],i);deep[1]=1;dfs(1);
	for(re int i=2;i<=cnt;i++) f[i][0]=fa[i],log_2[i]=log_2[i>>1]+1;
	for(re int j=1;j<=log_2[cnt];j++)
		for(re int i=2;i<=cnt;i++) f[i][j]=f[f[i][j-1]][j-1];
	for(re int i=1;i<=m;i++)
	{
		int ans=0;top=0;
		for(re int j=0;j<a[i].size();j++)
		{
			int t=find(a[i][j],L[i]);
			if(vis[t]) continue;
			st[++top]=t;ans+=sz[t];vis[t]=1;
		}
		printf("%d\n",ans);
		for(re int j=1;j<=top;j++) vis[st[j]]=0;
	}
	return 0;
}


#include<bits/stdc++.h>
#define de() cout<<"test"<<"\n"
using namespace std;

const int maxn=2e5+10;

char c[maxn];
int q,n;
int ch[maxn][26],siz[maxn],fa[maxn],len[maxn],la=1,tot=1,rt=1;
void ins(int x)
{
	int now=++tot,pre=la;la=now;
	len[now]=len[pre]+1;siz[now]=1;
	for(;pre&&!ch[pre][x];pre=fa[pre]) ch[pre][x]=now;
	if(!pre) fa[now]=rt;
	else {
		int q=ch[pre][x];
		if(len[q]==len[pre]+1) fa[now]=q;
		else {
			int nq=++tot;len[nq]=len[pre]+1;
			memcpy(ch[nq], ch[q], sizeof(ch[q]));
			fa[nq]=fa[q];fa[q]=fa[now]=nq;
			for(;pre&&ch[pre][x]==q;pre=fa[pre]) ch[pre][x]=nq;
		}
	}
}
int nt[maxn],ans,qwq;
int dp[maxn];
char s[maxn];
vector<int> ve[maxn];
void dfs(int x)
{
	for(int i=0;i<ve[x].size();++i)
		dfs(ve[x][i]),siz[x]+=siz[ve[x][i]];
}
int L=0;
void find(int x) {
	while(!ch[qwq][x]) {
		if(qwq==rt) break;
		//cout<<qwq<<"++"<<"\n";
		qwq=fa[qwq];L=len[qwq];
	}
	if(ch[qwq][x]) {
		qwq=ch[qwq][x];L++;
	}
}
int main()
{
	scanf("%s",c+1);
	n=strlen(c+1);
	for(int i=1;i<=n;++i) {
		ins(c[i]-'a');
	}
	for(int i=2;i<=tot;++i) ve[fa[i]].push_back(i);
	dfs(rt);
	scanf("%d",&q);
	while(q--) {
		int p=0;
		scanf("%s",s+1);
		int Len=strlen(s+1);
		for(int i=1;i<=Len;++i) nt[i]=0;
		for(int i=2;i<=Len;++i) {
			while(p&&s[p+1]!=s[i]) p=nt[p];
			if(s[p+1]==s[i]) {
				p++;
			}
			nt[i]=p;
		}
		int l2=Len-nt[Len];ans=0;
		qwq=rt; L=0;
		for(int i=1;i<=Len;++i)
		find(s[i]-'a');
		if(L==Len) {
			ans+=siz[qwq];
		}

		if((Len%l2)) l2++; //cout<<l2<<"*\n";
		for(int i=1;i<l2;++i) {
			//cout<<Len<<"++"<<L<<"\n";
			if(Len==L) {
				if(L-1==len[fa[qwq]])
				qwq=fa[qwq];L--;
			}
			find(s[i]-'a');
			if(L==Len) {
				ans+=siz[qwq];
			}
		}
		cout<<ans<<"\n";
	}
	return 0;
}


# include<iostream>
# include<cstring>
# include<cstdio>
# include<algorithm>
using namespace std;
const int MAX=2e6+5;
int n,Q,lim;
int use[MAX];
char a[MAX],b[MAX];
struct SAM{
	int l,r,L;
	SAM() {l=r=1;}
	int fa[MAX],len[MAX],siz[MAX],tax[MAX],id[MAX];
	int son[MAX][26],f[22][MAX];
	void ins(int x)
	{
		int tt=r;
		len[r=++l]=++L,siz[r]=1;
		for(;tt&&!son[tt][x];tt=fa[tt])
		  son[tt][x]=r;
		if(!tt) return void(fa[r]=1);
		int qwq=son[tt][x];
		if(len[qwq]==len[tt]+1) return void(fa[r]=qwq);
		len[++l]=len[tt]+1,fa[l]=fa[qwq],fa[qwq]=fa[r]=l;
		for(int i=0;i<26;++i)
		  son[l][i]=son[qwq][i];
		for(int i=tt;son[i][x]==qwq;i=fa[i])
		  son[i][x]=l;
	}
	void Init()
	{
		for(int i=1;i<=l;++i)
		  f[0][i]=fa[i],++tax[len[i]];
		for(int i=1;(1<<i)<=l;lim=i++)
		  for(int j=1;j<=l;++j)
		    f[i][j]=f[i-1][f[i-1][j]];
		for(int i=1;i<=n;++i)
		  tax[i]+=tax[i-1];
		for(int i=l;i>=1;--i)
		  id[tax[len[i]]--]=i;
		for(int i=l;i>=1;--i)
		  if(fa[id[i]]) siz[fa[id[i]]]+=siz[id[i]];
	}
	int ask(int x,int L,int id)
	{
		for(int i=lim;i>=0;--i)
		  if(len[f[i][x]]>L) x=f[i][x];
		if(len[fa[x]]>=L) x=fa[x];
		return use[x]!=id?(use[x]=id,siz[x]):0;
	}
	int Solve(int _n,int id)
	{
		int ans=0;
		for(int i=1,x=1,L=0;i<_n<<1;++i)
		  {
		  	if(son[x][b[i]-'a']) x=son[x][b[i]-'a'],++L;
		  	else
		  	{
		  		while(x&&!son[x][b[i]-'a']) x=fa[x];
		  		if(!x) x=1,L=0;
		  		else L=len[x]+1,x=son[x][b[i]-'a'];
			}
			if(L>=_n) ans+=ask(x,_n,id);
		  }
		return ans;
	}
}_S;
int main()
{
	scanf("%s%d",a+1,&Q),n=strlen(a+1);
	for(int i=1;i<=n;++i)
	  _S.ins(a[i]-'a');
	_S.Init();
	for(int i=1,_n;i<=Q;++i)
	  {
	  	scanf("%s",b+1),_n=strlen(b+1);
	  	for(int j=1;j<=_n;++j)
	  	  b[j+_n]=b[j];
	  	printf("%d\n",_S.Solve(_n,i));
	  }
	return 0;
}
/*
baabaabaaa
4
ba
baa
aabaa
aaba
*/


# include<iostream>
# include<cstring>
# include<cstdio>
# include<algorithm>
# define LL long long
using namespace std;
const int MAX=2e6+5;
int n,Q,lim;
int use[MAX];
char a[MAX],b[MAX];
struct SAM{
	int l,r,L;
	SAM() {l=r=1;}
	int fa[MAX],len[MAX],siz[MAX],tax[MAX],id[MAX];
	int son[MAX][26],f[22][MAX];
	void ins(int x)
	{
		int tt=r;
		len[r=++l]=++L,siz[r]=1;
		for(;tt&&!son[tt][x];tt=fa[tt])
		  son[tt][x]=r;
		if(!tt) return void(fa[r]=1);
		int qwq=son[tt][x];
		if(len[qwq]==len[tt]+1) return void(fa[r]=qwq);
		len[++l]=len[tt]+1,fa[l]=fa[qwq],fa[qwq]=fa[r]=l;
		for(int i=0;i<26;++i)
		  son[l][i]=son[qwq][i];
		for(int i=tt;son[i][x]==qwq;i=fa[i])
		  son[i][x]=l;
	}
	void Init()
	{
		for(int i=1;i<=l;++i)
		  f[0][i]=fa[i],++tax[len[i]];
		for(int i=1;(1<<i)<=l;lim=i++)
		  for(int j=1;j<=l;++j)
		    f[i][j]=f[i-1][f[i-1][j]];
		for(int i=1;i<=n;++i)
		  tax[i]+=tax[i-1];
		for(int i=l;i>=1;--i)
		  id[tax[len[i]]--]=i;
		for(int i=l;i>=1;--i)
		  if(fa[id[i]]) siz[fa[id[i]]]+=siz[id[i]];
	}
	int ask(int x,int L,int id)
	{
		for(int i=lim;i>=0;--i)
		  if(len[f[i][x]]>L) x=f[i][x];
		if(len[fa[x]]>=L) x=fa[x];
		return use[x]!=id?(use[x]=id,siz[x]):0;
	}
	LL Solve(int _n,int id)
	{
		LL ans=0;
		for(int i=1,x=1,L=0;i<_n<<1;++i)
		  {
		  	if(son[x][b[i]-'a']) x=son[x][b[i]-'a'],++L;
		  	else
		  	{
		  		while(!son[x][b[i]-'a']) x=fa[x];
		  		if(!x) x=1,L=0;
		  		else L=len[x]+1,x=son[x][b[i]-'a'];
			}
			if(L>=_n) ans+=ask(x,_n,id);
		  }
		return ans;
	}
}_S;
int main()
{
	scanf("%s%d",a+1,&Q),n=strlen(a+1);
	for(int i=1;i<=n;++i)
	  _S.ins(a[i]-'a');
	_S.Init();
	for(int i=1,_n;i<=Q;++i)
	  {
	  	scanf("%s",b+1),_n=strlen(b+1);
	  	for(int j=1;j<=_n;++j)
	  	  b[j+_n]=b[j];
	  	printf("%lld\n",_S.Solve(_n,i));
	  }
	return 0;
}
/*
baabaabaaa
4
ba
baa
aabaa
aaba
*/


# include<iostream>
# include<cstring>
# include<cstdio>
# include<algorithm>
using namespace std;
const int MAX=2e6+5;
int n,Q,lim;
int use[MAX];
char a[MAX],b[MAX];
struct SAM{
	int l,r,L;
	SAM() {l=r=1;}
	int fa[MAX],len[MAX],siz[MAX],tax[MAX],id[MAX];
	int son[MAX][26];
	void ins(int x)
	{
		int tt=r;
		len[r=++l]=++L,siz[r]=1;
		for(;tt&&!son[tt][x];tt=fa[tt])
		  son[tt][x]=r;
		if(!tt) return void(fa[r]=1);
		int qwq=son[tt][x];
		if(len[qwq]==len[tt]+1) return void(fa[r]=qwq);
		len[++l]=len[tt]+1,fa[l]=fa[qwq],fa[qwq]=fa[r]=l;
		for(int i=0;i<26;++i)
		  son[l][i]=son[qwq][i];
		for(int i=tt;son[i][x]==qwq;i=fa[i])
		  son[i][x]=l;
	}
	void Init()
	{
		for(int i=1;i<=l;++i)
		  ++tax[len[i]];
		for(int i=1;i<=n;++i)
		  tax[i]+=tax[i-1];
		for(int i=l;i>=1;--i)
		  id[tax[len[i]]--]=i;
		for(int i=l;i>=1;--i)
		  if(fa[id[i]]) siz[fa[id[i]]]+=siz[id[i]];
	}
	int Solve(int _n,int id)
	{
		int ans=0;
		for(int i=1,x=1,L=0;i<_n<<1;++i)
		  {
		  	if(son[x][b[i]-'a']) x=son[x][b[i]-'a'],++L;
		  	else
		  	{
		  		while(!son[x][b[i]-'a']) x=fa[x];
		  		if(!x) x=1,L=0;
		  		else L=len[x]+1,x=son[x][b[i]-'a'];
			}
			while(x&&len[fa[x]]>=_n) L=len[x=fa[x]];
			if(L>=_n&&use[x]!=id) use[x]=id,ans+=siz[x];
		  }
		return ans;
	}
}_S;
int main()
{
	scanf("%s%d",a+1,&Q),n=strlen(a+1);
	for(int i=1;i<=n;++i)
	  _S.ins(a[i]-'a');
	_S.Init();
	for(int i=1,_n;i<=Q;++i)
	  {
	  	scanf("%s",b+1),_n=strlen(b+1);
	  	for(int j=1;j<=_n;++j)
	  	  b[j+_n]=b[j];
	  	printf("%d\n",_S.Solve(_n,i));
	  }
	return 0;
}
/*
baabaabaaa
4
ba
baa
aabaa
aaba
*/


# include<iostream>
# include<cstring>
# include<cstdio>
# include<algorithm>
using namespace std;
const int MAX=2e6+5;
int n,Q;
char a[MAX];
struct SAM{
	int l,r,L;
	int len[MAX],fa[MAX],use[MAX],id[MAX],tax[MAX],siz[MAX];
	int son[MAX][26];
	SAM() {l=r=1;}
	void ins(int x)
	{
		int tt=r;
		len[r=++l]=++L,siz[r]=1;
		for(;tt&&!son[tt][x];tt=fa[tt])
		  son[tt][x]=r;
		if(!tt) return void(fa[r]=1);
		int qwq=son[tt][x];
		if(len[qwq]==len[tt]+1) return void(fa[r]=qwq);
		len[++l]=len[tt]+1,fa[l]=fa[qwq],fa[qwq]=fa[r]=l;
		for(int i=0;i<26;++i)
		  son[l][i]=son[qwq][i];
		for(int i=tt;son[i][x]==qwq;i=fa[i])
		  son[i][x]=l;
	}
	void Init()
	{
		for(int i=1;i<=l;++i)
		  ++tax[len[i]];
		for(int i=1;i<=n;++i)
		  tax[i]+=tax[i-1];
		for(int i=l;i>=1;--i)
		  id[tax[len[i]]--]=i;
		for(int i=l;i>=1;--i)
		  siz[fa[id[i]]]+=siz[id[i]];
	}
	int Solve(int _n,int id)
	{
		int ans=0;
		for(int i=1,x=1,_L=0;i<=2*_n;++i)
		  {
		  	if(son[x][a[i]-'a']) x=son[x][a[i]-'a'],++_L;
		  	else
		  	{
		  		while(x&&!son[x][a[i]-'a']) x=fa[x];
		  		if(!x) x=1,_L=0;
		  		else _L=len[x]+1,x=son[x][a[i]-'a'];
			}
		  	while(x&&len[fa[x]]>=_n) _L=len[x=fa[x]];
		  	if(_L>=_n&&use[x]!=id) use[x]=id,ans+=siz[x];
		  }
		return ans;
	}
}_S;
int main()
{
	scanf("%s%d",a+1,&Q),n=strlen(a+1);
	for(int i=1;i<=n;++i)
	  _S.ins(a[i]-'a');
	_S.Init();
	for(int i=1,_n;i<=Q;++i)
	  {
	  	scanf("%s",a+1),_n=strlen(a+1);
	  	for(int j=1;j<=_n;++j)
	  	  a[j+_n]=a[j];
	  	printf("%d\n",_S.Solve(_n,i));
	  }
	return 0;
}


#include <bits/stdc++.h>

#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foreach(it, v) for(__typeof((v).begin()) it=(v).begin(); it != (v).end(); ++it)
#define meta __FUNCTION__,__LINE__

#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);

using namespace std;

template<typename S, typename T>
ostream& operator<<(ostream& out,pair<S,T> const& p){out<<'('<<p.fi<<", "<<p.se<<')';return out;}

template<typename T>
ostream& operator<<(ostream& out,vector<T> const& v){
int l=v.size();for(int i=0;i<l-1;i++)out<<v[i]<<' ';if(l>0)out<<v[l-1];return out;}

void tr(){cout << endl;}
template<typename S, typename ... Strings>
void tr(S x, const Strings&... rest){cout<<x<<' ';tr(rest...);}

typedef long long ll;
typedef pair<int,int> pii;

const int N = 1001000;
const int M = 26;

struct suffixAutomaton{
	struct state{
		int len, link;
		int next[M];
		ll cnt;

		bool terminal;	// by default, empty string is a suffix

		state(){
			len = 0, link = -1, cnt = 0;
			terminal = false;
			for(int i = 0; i < M; i++) next[i] = -1;
		}
	};

	state st[N*2];
	int sz, last;
	char offset = 'a';	// Careful!

	suffixAutomaton(string &s){
		int l = s.length();

		for(int i = 0; i < 2*l; i++) st[i] = state();

		sz = 1, last = 0;
		st[0].len = 0;
		st[0].link = -1;

		for(int i = 0; i < l; i++)
			addChar(s[i] - offset);

		for(int i = last; i != -1; i = st[i].link) st[i].terminal = true;
	}

	void addChar(int c){
		int cur = sz++; assert(cur < N*2);
		st[cur].len = st[last].len + 1;
		st[cur].cnt = 1;

		int p = last;
		while(p != -1 and st[p].next[c] == -1){
			st[p].next[c] = cur;
			p = st[p].link;
		}

		last = cur;

		if(p == -1){
			st[cur].link = 0; return;
		}

		int q = st[p].next[c];

		if(st[q].len == st[p].len + 1){
			st[cur].link = q; return;
		}

		int clone = sz++;

		for(int i = 0; i < M; i++) st[clone].next[i] = st[q].next[i];
		st[clone].link = st[q].link;
		st[clone].len = st[p].len + 1;
		st[clone].cnt = 0;

		while(p != -1 and st[p].next[c] == q){
			st[p].next[c] = clone;
			p = st[p].link;
		}

		st[q].link = st[cur].link = clone;

		return;
	}

	bool contains(string &t){
		int cur = 0;
		for(int i = 0; i < t.length(); i++){
			cur = st[cur].next[t[i] - offset];
			if(cur == -1) return false;
		}
		return true;
	}

	// alternatively, compute the number of paths in a DAG
	// since each substring corresponds to one unique path in SA
	ll numberOfSubstrings(){
		ll res = 0;
		for(int i = 1; i < sz; i++) res += st[i].len - st[st[i].link].len;
		return res;
	}

	ll h[2*N];
	ll base = 31;
	ll pr = 999998727899999ll;
	set<ll> done;
	ll p[2*N];

	void numberOfOccPreprocess(){
		vector<pii> v;
		for(int i = 1; i < sz; i++) v.pb(mp(st[i].len, i));

		sort(v.begin(), v.end(), greater<pii>());

		for(int i = 0; i < sz-1; i++){
			int suf = st[v[i].se].link;
			st[suf].cnt += st[v[i].se].cnt;
		}

		p[0] = 1;
		for(int i = 1; i < 2*N; i++){
			p[i] = (p[i-1] * base) % pr;
		}
	}

	ll numberOfOcc(string &t){
		ll res = 0;
		int cur = 0;
		int l = 0, m = t.length();
		done.clear();

		string tmp = t+t;
		h[0] = 0;
		for(int i = 1; i <= 2*m; i++){
			h[i] = ((h[i-1] * base) + (tmp[i-1] - offset + 1)) % pr;
		}

		for(int i = 0; i < m; i++){
			int nxt = st[cur].next[t[i] - offset];
			if(nxt == -1) nxt = 0, l = 0;
			else l++;
			cur = nxt;
		}

		if(l == m){
			res += st[cur].cnt;
			done.insert(h[m]);
		}

		for(int i = 0; i < m-1; i++){
			if(l == m){
				if(l-1 == st[st[cur].link].len)
					cur = st[cur].link;
				l--;
			}

			int nxt = st[cur].next[t[i] - offset];
			if(nxt == -1) nxt = 0, l = 0;
			else l++;

			cur = nxt;
			ll v1 = h[m+i+1];
			ll v2 = (h[i+1] * p[m]) % pr;
			v1 = (v1 - v2 + pr) % pr;

			if(l == m and !done.count(v1)){
				res += st[cur].cnt;
				done.insert(v1);
			}

		}

		return res;
	}

};

string s;
string t;

int main(){ _
	cin >> s;
	suffixAutomaton *suf = new suffixAutomaton(s);
	suf->numberOfOccPreprocess();

	int n;
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> t;
		cout << suf->numberOfOcc(t) << '\n';
	}

	return 0;
}


#include <bits/stdc++.h>

#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foreach(it, v) for(__typeof((v).begin()) it=(v).begin(); it != (v).end(); ++it)
#define meta __FUNCTION__,__LINE__

#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);

using namespace std;

template<typename S, typename T>
ostream& operator<<(ostream& out,pair<S,T> const& p){out<<'('<<p.fi<<", "<<p.se<<')';return out;}

template<typename T>
ostream& operator<<(ostream& out,vector<T> const& v){
int l=v.size();for(int i=0;i<l-1;i++)out<<v[i]<<' ';if(l>0)out<<v[l-1];return out;}

void tr(){cout << endl;}
template<typename S, typename ... Strings>
void tr(S x, const Strings&... rest){cout<<x<<' ';tr(rest...);}

typedef long long ll;
typedef pair<int,int> pii;

const int N = 1001000;
const int M = 26;

struct suffixAutomaton{
	struct state{
		int len, link;
		int next[M];
		ll cnt;

		bool terminal;	// by default, empty string is a suffix

		state(){
			len = 0, link = -1, cnt = 0;
			terminal = false;
			for(int i = 0; i < M; i++) next[i] = -1;
		}
	};

	state st[N*2];
	int sz, last;
	char offset = 'a';	// Careful!

	suffixAutomaton(string &s){
		int l = s.length();

		for(int i = 0; i < 2*l; i++) st[i] = state();

		sz = 1, last = 0;
		st[0].len = 0;
		st[0].link = -1;

		for(int i = 0; i < l; i++)
			addChar(s[i] - offset);

		for(int i = last; i != -1; i = st[i].link) st[i].terminal = true;
	}

	void addChar(int c){
		int cur = sz++; assert(cur < N*2);
		st[cur].len = st[last].len + 1;
		st[cur].cnt = 1;

		int p = last;
		while(p != -1 and st[p].next[c] == -1){
			st[p].next[c] = cur;
			p = st[p].link;
		}

		last = cur;

		if(p == -1){
			st[cur].link = 0; return;
		}

		int q = st[p].next[c];

		if(st[q].len == st[p].len + 1){
			st[cur].link = q; return;
		}

		int clone = sz++;

		for(int i = 0; i < M; i++) st[clone].next[i] = st[q].next[i];
		st[clone].link = st[q].link;
		st[clone].len = st[p].len + 1;
		st[clone].cnt = 0;

		while(p != -1 and st[p].next[c] == q){
			st[p].next[c] = clone;
			p = st[p].link;
		}

		st[q].link = st[cur].link = clone;

		return;
	}

	ll h[2*N], p[2*N];
	ll base = 29, pr = 1e9 + 7;
	unordered_set<ll> done;

	void numberOfOccPreprocess(){
		vector<pii> v;
		for(int i = 1; i < sz; i++) v.pb(mp(st[i].len, i));

		sort(v.begin(), v.end(), greater<pii>());

		for(int i = 0; i < sz-1; i++){
			int suf = st[v[i].se].link;
			st[suf].cnt += st[v[i].se].cnt;
		}

		p[0] = 1;
		for(int i = 1; i < 2*N; i++)
			p[i] = (p[i-1] * base) % pr;
	}

	ll numberOfOcc(string &t){
		ll res = 0;
		int l = 0, m = t.length();
		done.clear();

		string tmp = t+t;
		h[0] = 0;
		for(int i = 1; i <= 2*m; i++)
			h[i] = ((h[i-1] * base) + (tmp[i-1] - offset + 1)) % pr;

		int cur = 0, nxt;
		for(int i = 0; i < 2*m - 1; i++){
			while(cur != 0 and st[cur].next[tmp[i] - offset] == -1){
				cur = st[cur].link;
				l = st[cur].len;
			}

			nxt = st[cur].next[tmp[i] - offset];
			if(nxt == -1) nxt = 0, l = 0;
			else l++;

			cur = nxt;

			if(l > m){
				if(l-1 == st[st[cur].link].len) cur = st[cur].link;
				l--;
			}

			if(l == m){
				ll v1 = h[i+1];
				ll v2 = (h[i+1-m] * p[m]) % pr;
				v1 = (v1 - v2 + pr) % pr;
				if(!done.count(v1)){
					res += st[cur].cnt;
					done.insert(v1);
				}
			}
		}

		return res;
	}

};

string s;
string t;

int main(){ _
	cin >> s;
	suffixAutomaton *suf = new suffixAutomaton(s);
	suf->numberOfOccPreprocess();

	int n;
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> t;
		cout << suf->numberOfOcc(t) << '\n';
	}

	return 0;
}


#include <bits/stdc++.h>

#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foreach(it, v) for(__typeof((v).begin()) it=(v).begin(); it != (v).end(); ++it)
#define meta __FUNCTION__,__LINE__

#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);

using namespace std;

template<typename S, typename T>
ostream& operator<<(ostream& out,pair<S,T> const& p){out<<'('<<p.fi<<", "<<p.se<<')';return out;}

template<typename T>
ostream& operator<<(ostream& out,vector<T> const& v){
int l=v.size();for(int i=0;i<l-1;i++)out<<v[i]<<' ';if(l>0)out<<v[l-1];return out;}

void tr(){cout << endl;}
template<typename S, typename ... Strings>
void tr(S x, const Strings&... rest){cout<<x<<' ';tr(rest...);}

typedef long long ll;
typedef pair<int,int> pii;

const int N = 1001000;
const int M = 26;

struct suffixAutomaton{
	struct state{
		int len, link;
		int next[M];
		ll cnt;

		bool terminal;	// by default, empty string is a suffix

		state(){
			len = 0, link = -1, cnt = 0;
			terminal = false;
			for(int i = 0; i < M; i++) next[i] = -1;
		}
	};

	state st[N*2];
	int sz, last;
	char offset = 'a';	// Careful!

	suffixAutomaton(string &s){
		int l = s.length();

		for(int i = 0; i < 2*l; i++) st[i] = state();

		sz = 1, last = 0;
		st[0].len = 0;
		st[0].link = -1;

		for(int i = 0; i < l; i++)
			addChar(s[i] - offset);

		for(int i = last; i != -1; i = st[i].link) st[i].terminal = true;
	}

	void addChar(int c){
		int cur = sz++; assert(cur < N*2);
		st[cur].len = st[last].len + 1;
		st[cur].cnt = 1;

		int p = last;
		while(p != -1 and st[p].next[c] == -1){
			st[p].next[c] = cur;
			p = st[p].link;
		}

		last = cur;

		if(p == -1){
			st[cur].link = 0; return;
		}

		int q = st[p].next[c];

		if(st[q].len == st[p].len + 1){
			st[cur].link = q; return;
		}

		int clone = sz++;

		for(int i = 0; i < M; i++) st[clone].next[i] = st[q].next[i];
		st[clone].link = st[q].link;
		st[clone].len = st[p].len + 1;
		st[clone].cnt = 0;

		while(p != -1 and st[p].next[c] == q){
			st[p].next[c] = clone;
			p = st[p].link;
		}

		st[q].link = st[cur].link = clone;

		return;
	}

	bool contains(string &t){
		int cur = 0;
		for(int i = 0; i < t.length(); i++){
			cur = st[cur].next[t[i] - offset];
			if(cur == -1) return false;
		}
		return true;
	}

	// alternatively, compute the number of paths in a DAG
	// since each substring corresponds to one unique path in SA
	ll numberOfSubstrings(){
		ll res = 0;
		for(int i = 1; i < sz; i++) res += st[i].len - st[st[i].link].len;
		return res;
	}

	ll h[2*N];
	ll base = 31;
	ll pr = 999998727899999ll;
	set<ll> done;
	ll p[2*N];

	void numberOfOccPreprocess(){
		vector<pii> v;
		for(int i = 1; i < sz; i++) v.pb(mp(st[i].len, i));

		sort(v.begin(), v.end(), greater<pii>());

		for(int i = 0; i < sz-1; i++){
			int suf = st[v[i].se].link;
			st[suf].cnt += st[v[i].se].cnt;
		}

		p[0] = 1;
		for(int i = 1; i < 2*N; i++){
			p[i] = (p[i-1] * base) % pr;
		}
	}

	ll numberOfOcc(string &t){
		ll res = 0;
		int cur = 0;
		int l = 0, m = t.length();
		done.clear();

		string tmp = t+t;
		h[0] = 0;
		for(int i = 1; i <= 2*m; i++){
			h[i] = ((h[i-1] * base) + (tmp[i-1] - offset + 1)) % pr;
		}

		for(int i = 0; i < m; i++){
			while(cur != 0 and st[cur].next[t[i] - offset] == -1){
				cur = st[cur].link;
				l = st[cur].len;
			}
			int nxt = st[cur].next[t[i] - offset];

			if(nxt == -1) nxt = 0, l = 0;
			else l++;
			cur = nxt;
		}

		if(l == m){
			res += st[cur].cnt;
			done.insert(h[m]);
		}

		for(int i = 0; i < m-1; i++){
			while(cur != 0 and st[cur].next[t[i] - offset] == -1){
				cur = st[cur].link;
				l = st[cur].len;
			}
			int nxt = st[cur].next[t[i] - offset];
			if(nxt == -1) nxt = 0, l = 0;
			else l++;
			cur = nxt;

			if(l > m){
				if(l-1 == st[st[cur].link].len)
					cur = st[cur].link;
				l--;
			}

			ll v1 = h[m+i+1];
			ll v2 = (h[i+1] * p[m]) % pr;
			v1 = (v1 - v2 + pr) % pr;

			if(l == m and !done.count(v1)){
				res += st[cur].cnt;
				done.insert(v1);
			}

		}

		return res;
	}

};

string s;
string t;

int main(){ _
	cin >> s;
	suffixAutomaton *suf = new suffixAutomaton(s);
	suf->numberOfOccPreprocess();

	int n;
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> t;
		cout << suf->numberOfOcc(t) << '\n';
	}

	return 0;
}