#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<vector>
#define maxn 6000005
#define re register
#define LL long long
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
std::map<int,int> son[maxn];
struct E{int v,nxt;}e[maxn];
int n,lst=1,cnt=1,num,m;
int len[maxn],head[maxn],sz[maxn],deep[maxn],vis[maxn],fa[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[1000005],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(std::map<int,int>::iterator it=son[x].begin();it!=son[x].end();it++) son[y].insert(*it);
    while(f&&son[f][c]==x) son[f][c]=y,f=fa[f];
}
inline int find(int x,int li) {while(len[fa[x]]>=li) x=fa[x];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=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 INPUT_LEN 1000
#define NN 2000100
#define MM 26

using namespace std;
inline void print(int x){if(x<0){putchar('-');return print(-x);}if(x<10){putchar('0'+x);return;}print(x/10);putchar(x%10+'0');}
template<class T>inline void println(T x){print(x);putchar('\n');}

char s[NN];
int sz, ed;
int cnt[NN], ord[NN<<1], vst[NN<<1], runs;

struct node {
	int deep, fail, cnt, nxt[MM], val;
	void clear() {
		memset(nxt, -1, sizeof nxt);
		deep=fail=cnt=val=0;
	}
} mem[NN<<1];

struct automaton {
	void init() {
		for(int i=0; i<26; i++) mem[0].nxt[i]=1;
		mem[0].deep=-1;
		mem[1].clear(); mem[1].cnt=1;
		sz=2, ed=1;
	}

	inline void calc() {
		scanf("%s", s);
		int N=strlen(s);
		for(int i=0; i<N; i++) s[N+i]=s[i];

		runs++;
		int u=1, len=0, ans=0;
		for(int i=0, mx=N-1<<1; i<=mx; i++) {
			int c=s[i]-'a';

			if(~mem[u].nxt[c]) {
				u=mem[u].nxt[c];
				len++;
				if(len==N) {
					if(vst[u]!=runs) {
						vst[u]=runs;
						ans+=mem[u].val;
						int pre=mem[u].fail;
						if(mem[pre].deep==N-1) u=pre;
						len--;
					}
				}
			}
			else {
				while(mem[u].nxt[c]==-1) u=mem[u].fail;
				len=mem[u].deep+1;
				u=mem[u].nxt[c];
			}
		}
		println(ans);
	}
	void insert(int c) {
		int p=sz++;
		mem[p].clear();
		mem[p].deep=mem[ed].deep+1;
		mem[p].val=1;

		while(mem[ed].nxt[c]==-1) {
			mem[ed].nxt[c]=p;
			mem[p].cnt+=mem[ed].cnt;
			ed=mem[ed].fail;
		}

		int p1=mem[ed].nxt[c];
		if(mem[p1].deep==mem[ed].deep+1) mem[p].fail=p1;
		else {
			int p2=sz++;
			mem[p2]=mem[p1];
			mem[p2].deep=mem[ed].deep+1;
			mem[p2].cnt=0;
			mem[p2].val=0;

			mem[p].fail=mem[p1].fail=p2;

			while(mem[ed].nxt[c]==p1) {
				mem[ed].nxt[c]=p2;

				mem[p1].cnt-=mem[ed].cnt;
				mem[p2].cnt+=mem[ed].cnt;

				ed=mem[ed].fail;
			}
		}
		ed=p;
	}

	void build(int n) {
		memset(cnt, 0, sizeof cnt);
		for(int i=1; i<sz; i++) cnt[mem[i].deep]++;
		for(int i=1; i<NN; i++) cnt[i]+=cnt[i-1];
		for(int i=1; i<sz; i++) ord[--cnt[mem[i].deep]]=i;

		for(int i=sz-1; i--; ) {
			int cur=ord[i], prv=mem[cur].fail;
			mem[prv].val+=mem[cur].val;
		}
	}
} sam;

int main() {
	sam.init();
	scanf("%s", s);
	int n=strlen(s);
	for(int i=0; i<n; i++) sam.insert(s[i]-'a');
	sam.build(n);

	scanf("%d", &n);
	while(n--) sam.calc();
}


#include<bits/stdc++.h>
#define INPUT_LEN 1000
#define NN 2000100
#define MM 26

using namespace std;
inline void print(int x){if(x<0){putchar('-');return print(-x);}if(x<10){putchar('0'+x);return;}print(x/10);putchar(x%10+'0');}

char s[NN];
int sz, ed;
int cnt[NN], ord[NN<<1], vst[NN<<1], runs;

struct node {
	int deep, fail, nxt[MM], val;
	void clear() {
		memset(nxt, -1, sizeof nxt);
		deep=fail=val=0;
	}
} mem[NN<<1];

struct automaton {
	inline void init() {
		for(int i=0; i<26; i++) mem[0].nxt[i]=1;
		mem[0].deep=-1;
		mem[1].clear();
		sz=2, ed=1;
	}

	inline void calc() {
		scanf("%s", s);
		int N=strlen(s);
		for(int i=0; i<N; i++) s[N+i]=s[i];

		runs++;
		int u=1, len=0, ans=0;
		for(int i=0, mx=N-1<<1, c; i<=mx; i++) {
			c=s[i]-'a';

			if(~mem[u].nxt[c]) {
				u=mem[u].nxt[c];
				len++;
				if(len==N) {
					if(vst[u]!=runs) {
						vst[u]=runs;
						ans+=mem[u].val;
						int pre=mem[u].fail;
						if(mem[pre].deep==N-1) u=pre;
						len--;
					}
				}
			}
			else {
				while(mem[u].nxt[c]==-1) u=mem[u].fail;
				len=mem[u].deep+1;
				u=mem[u].nxt[c];
			}
		}
		print(ans); putchar('\n');
	}
	inline void insert(int c) {
		int p=sz++;
		mem[p].clear();
		mem[p].deep=mem[ed].deep+1;
		mem[p].val=1;

		while(mem[ed].nxt[c]==-1) {
			mem[ed].nxt[c]=p;
			ed=mem[ed].fail;
		}

		int p1=mem[ed].nxt[c];
		if(mem[p1].deep==mem[ed].deep+1) mem[p].fail=p1;
		else {
			int p2=sz++;
			mem[p2]=mem[p1];
			mem[p2].deep=mem[ed].deep+1;
			mem[p2].val=0;

			mem[p].fail=mem[p1].fail=p2;

			while(mem[ed].nxt[c]==p1) {
				mem[ed].nxt[c]=p2;
				ed=mem[ed].fail;
			}
		}
		ed=p;
	}

	inline void build(int n) {
		for(int i=1; i<sz; i++) cnt[mem[i].deep]++;
		for(int i=1; i<NN; i++) cnt[i]+=cnt[i-1];
		for(int i=1; i<sz; i++) ord[--cnt[mem[i].deep]]=i;

		for(int i=sz-1, cur, prv; i--; ) {
			cur=ord[i], prv=mem[cur].fail;
			mem[prv].val+=mem[cur].val;
		}
	}
} sam;

int main() {
	sam.init();
	scanf("%s", s);
	int n=strlen(s);
	for(int i=0; i<n; i++) sam.insert(s[i]-'a');
	sam.build(n);

	scanf("%d", &n);
	while(n--) sam.calc();
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define pb push_back
#define y second
#define x first

using namespace std;
typedef long long INT;
typedef pair<int, int> pii;
template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}
typedef vector<int> VI;

#define NN 2000100
#define MM 26

char s[NN];

int sz, ed;
int cnt[NN], ord[NN<<1], vst[NN<<1], runs;

struct node {
	int deep, fail, cnt, nxt[MM], val;
	void clear() {
		memset(nxt, -1, sizeof nxt);
		deep=fail=cnt=val=0;
	}
} mem[NN<<1];

struct automaton {
	void init() {
		for(int i=0; i<26; i++) mem[0].nxt[i]=1;
		mem[0].deep=-1;
		mem[1].clear(); mem[1].cnt=1;
		sz=2, ed=1;
	}

	inline void calc() {
		scanf("%s", s);
		int N=strlen(s);
		for(int i=0; i<N; i++) s[N+i]=s[i];

		runs++;
		int u=1, len=0, ans=0;
		for(int i=0, mx=N-1<<1; i<=mx; i++) {
			int c=s[i]-'a';

			if(~mem[u].nxt[c]) {
				u=mem[u].nxt[c];
				len++;
				if(len==N) {
					if(vst[u]!=runs) {
						vst[u]=runs;
						ans+=mem[u].val;
						int pre=mem[u].fail;
						if(mem[pre].deep==N-1) u=pre;
						len--;
					}
				}
			}
			else {
				while(mem[u].nxt[c]==-1) u=mem[u].fail;
				len=mem[u].deep+1;
				u=mem[u].nxt[c];
			}
		}
		printf("%d\n", ans);
	}

	void insert(int c) {
		int p=sz++;
		mem[p].clear();
		mem[p].deep=mem[ed].deep+1;
		mem[p].val=1;

		while(mem[ed].nxt[c]==-1) {
			mem[ed].nxt[c]=p;
			mem[p].cnt+=mem[ed].cnt;
			ed=mem[ed].fail;
		}

		int p1=mem[ed].nxt[c];
		if(mem[p1].deep==mem[ed].deep+1) mem[p].fail=p1;
		else {
			int p2=sz++;
			mem[p2]=mem[p1];
			mem[p2].deep=mem[ed].deep+1;
			mem[p2].cnt=0;
			mem[p2].val=0;

			mem[p].fail=mem[p1].fail=p2;

			while(mem[ed].nxt[c]==p1) {
				mem[ed].nxt[c]=p2;

				mem[p1].cnt-=mem[ed].cnt;
				mem[p2].cnt+=mem[ed].cnt;

				ed=mem[ed].fail;
			}
		}
		ed=p;
	}

	void build(int n) {
		memset(cnt, 0, sizeof cnt);
		for(int i=1; i<sz; i++) cnt[mem[i].deep]++;
		for(int i=1; i<NN; i++) cnt[i]+=cnt[i-1];
		for(int i=1; i<sz; i++) ord[--cnt[mem[i].deep]]=i;

		for(int i=sz-1; i--; ) {
			int cur=ord[i], prv=mem[cur].fail;
			mem[prv].val+=mem[cur].val;
		}
	}
} sam;

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

	sam.init();
	scanf("%s", s);
	int n=strlen(s);
	for(int i=0; i<n; i++) sam.insert(s[i]-'a');
	sam.build(n);

	scanf("%d", &n);
	while(n--) sam.calc();
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back

using namespace std;

typedef long long INT;
typedef vector<int> VI;
typedef pair<int, int> pii;

template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}

template <class T> inline void gn(T &x) {char c, sg=0; while(c=getchar(), (c>'9' || c<'0') && c!='-'); for((c=='-' ? sg=1, c=getchar() : 0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0'; if(sg) x=-x;}
template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) {gn(x1), gn(x2);}
int power(int a, int b, int m, int ans=1){for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m; return ans;}

#define NN 1000100
char s[NN];

struct SuffixAutomaton {
	#define CC 27
	struct Node {
		int fail, deep, nxt[CC], val;
		void clear() {
			memset(nxt, 0xff, sizeof(nxt));
			deep=fail=0;
		}
	};
	Node mem[2*NN];
	int sz, end;

	void init() {
		for (int i=0; i<CC; i++) mem[0].nxt[i]=1;
		mem[0].deep=-1; mem[1].clear();
		sz=2, end=1;
	}

	void insert(int c) {
		int p=sz++;
		mem[p].clear();
		mem[p].deep=mem[end].deep+1;
		mem[p].val=1;
		while (mem[end].nxt[c]==-1) {
			mem[end].nxt[c]=p;
			end=mem[end].fail;
		}
		int p1=mem[end].nxt[c];
		if (mem[p1].deep==mem[end].deep+1) mem[p].fail=p1;
		else {
			int p2=sz++;
			mem[p2]=mem[p1];
			mem[p2].deep=mem[end].deep+1;
			mem[p1].fail=mem[p].fail=p2;
			mem[p2].val=0;
			while (mem[end].nxt[c]==p1) {
				mem[end].nxt[c]=p2;
				end=mem[end].fail;
			}
		}
		end=p;
	}

	int cnt[NN<<1], ord[NN<<1];
	void build() {
		for(int i=1; i<sz; i++) cnt[mem[i].deep]++;
		for(int i=1; i<NN; i++) cnt[i]+=cnt[i-1];
		for(int i=1; i<sz; i++) ord[--cnt[mem[i].deep]]=i;

		for(int i=sz-1; i--; ) {
			int cur=ord[i], prv=mem[cur].fail;
			mem[prv].val+=mem[cur].val;
		}
	}

	int vst[NN<<1], runs;
	int calc(char *t, int n){
		runs++;
		int p=1, len=0;
		int ans=0;

		for(int i=0; t[i]; i++){
			int c=t[i]-'a';
			while(mem[p].nxt[c]==-1){
				p=mem[p].fail;
				len=mem[p].deep;
			}
			p=mem[p].nxt[c];
			len++;
			if(len==n){
				if(vst[p]!=runs) ans+=mem[p].val;
				vst[p]=runs;
				if(mem[mem[p].fail].deep==n-1) p=mem[p].fail;
				len--;
			}
		}
		return ans;
	}
} sam;

char t[NN<<1];

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

	scanf("%s", s);

	sam.init();
	for(int i=0; s[i]; i++) sam.insert(s[i]-'a');
	sam.build();

	int n;
	for(cin >> n; n--; ){
		scanf("%s", t);
		int m=strlen(t);
		for(int i=0; i<m; i++) t[i+m]=t[i];
		t[m<<1]=0;
		printf("%d\n", sam.calc(t, m));
	}

	return 0;
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define pb push_back
#define y second
#define x first

using namespace std;
typedef long long INT;
typedef pair<int, int> pii;
template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}
typedef vector<int> VI;

#define NN 200010
#define MM 26

char s[NN];

int sz, ed;
int cnt[NN], ord[NN<<1], vst[NN<<1], runs;

struct node {
	int deep, fail, cnt, nxt[MM], val;
	void clear() {
		memset(nxt, -1, sizeof nxt);
		deep=fail=cnt=val=0;
	}
} mem[NN<<1];

struct automaton {
	void init() {
		for(int i=0; i<26; i++) mem[0].nxt[i]=1;
		mem[0].deep=-1;
		mem[1].clear(); mem[1].cnt=1;
		sz=2, ed=1;
	}

	inline void calc() {
		scanf("%s", s);
		int N=strlen(s);
		for(int i=0; i<N; i++) s[N+i]=s[i];

		runs++;
		int u=1, len=0, ans=0;
		for(int i=0, mx=N-1<<1; i<=mx; i++) {
			int c=s[i]-'a';

			if(~mem[u].nxt[c]) {
				u=mem[u].nxt[c];
				len++;
				if(len==N) {
					if(vst[u]!=runs) {
						vst[u]=runs;
						ans+=mem[u].val;
						int pre=mem[u].fail;
						if(mem[pre].deep==N-1) u=pre;
						len--;
					}
				}
			}
			else {
				while(mem[u].nxt[c]==-1) u=mem[u].fail;
				len=mem[u].deep+1;
				u=mem[u].nxt[c];
			}
		}
		printf("%d\n", ans);
	}

	void insert(int c) {
		int p=sz++;
		mem[p].clear();
		mem[p].deep=mem[ed].deep+1;
		mem[p].val=1;

		while(mem[ed].nxt[c]==-1) {
			mem[ed].nxt[c]=p;
			mem[p].cnt+=mem[ed].cnt;
			ed=mem[ed].fail;
		}

		int p1=mem[ed].nxt[c];
		if(mem[p1].deep==mem[ed].deep+1) mem[p].fail=p1;
		else {
			int p2=sz++;
			mem[p2]=mem[p1];
			mem[p2].deep=mem[ed].deep+1;
			mem[p2].cnt=0;
			mem[p2].val=0;

			mem[p].fail=mem[p1].fail=p2;

			while(mem[ed].nxt[c]==p1) {
				mem[ed].nxt[c]=p2;

				mem[p1].cnt-=mem[ed].cnt;
				mem[p2].cnt+=mem[ed].cnt;

				ed=mem[ed].fail;
			}
		}
		ed=p;
	}

	void build(int n) {
		memset(cnt, 0, sizeof cnt);
		for(int i=1; i<sz; i++) cnt[mem[i].deep]++;
		for(int i=1; i<NN; i++) cnt[i]+=cnt[i-1];
		for(int i=1; i<sz; i++) ord[--cnt[mem[i].deep]]=i;

		for(int i=sz-1; i--; ) {
			int cur=ord[i], prv=mem[cur].fail;
			mem[prv].val+=mem[cur].val;
		}
	}
} sam;

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

	sam.init();
	scanf("%s", s);
	int n=strlen(s);
	for(int i=0; i<n; i++) sam.insert(s[i]-'a');
	sam.build(n);

	scanf("%d", &n);
	while(n--) sam.calc();
}


#include<bits/stdc++.h>
#define N 200005
using namespace std;
struct Node {
	int ch[26], len, fa;
	void clear() {
		for(int i = 0; i < 26; i ++) ch[i] = 0;
		len = fa = 0;
	}
} a[N << 1];
int tot, lst;
void add(int c) {
	int p = lst, np = ++ tot; lst = np;
	a[np].len = a[p].len + 1;
	for(; p && !a[p].ch[c]; p = a[p].fa) a[p].ch[c] = np;
	if(!p) a[np].fa = 1;
	else {
		int v = a[p].ch[c];
		if(a[v].len == a[p].len + 1) a[np].fa = v;
		else {
			int nv = ++ tot; a[nv] = a[v];
			a[nv].len = a[p].len + 1;
			for(; p && a[p].ch[c] == v; p = a[p].fa) a[p].ch[c] = nv;
			a[np].fa = a[v].fa = nv;
		}
	}
}
char st[N], stt[N];
int n, slen[N], vis[N], m;
vector<int> g[N];
int size[N];
void dfs(int u) {
	for(int i = 0; i < g[u].size(); i ++) {
		int v = g[u][i];
		dfs(v);
		size[u] += size[v];
	}
}
void calc() {
	scanf("%s", st + 1);
	n = strlen(st + 1);
	int p = 1, s = 0;
	for(int i = 1; i <= n; i ++) {
		int c = st[i] - 'a';
		if(!a[p].ch[c]) {
			while(p != 1 && !a[p].ch[c]) p = a[p].fa;
			s = a[p].len;
		}
		if(a[p].ch[c]) {
			p = a[p].ch[c];
			s ++;
		}
	}
//	printf("%d  %d   ", p, s);
	long long ans = 0;
	for(int i = 1; i <= n; i ++) {// printf("%d %d\n", p, s);
		int c = st[i] - 'a';
		if(s == n) {
			if(!vis[p]) ans += size[p];//, printf("*");
			vis[p] = 1;
			if(-- s == a[a[p].fa].len) p = a[p].fa;
		}
		if(!a[p].ch[c]) {
			while(p != 1 && !a[p].ch[c]) p = a[p].fa;
			s = a[p].len;
		}

		if(a[p].ch[c]) {
			p = a[p].ch[c];
			s ++;
		}
	}
	printf("%lld\n", ans);
	for(int i = 1; i <= tot; i ++) vis[i] = 0;
}
int main() {
	scanf("%s", stt + 1);
	lst = tot = 1;
	int len = strlen(stt + 1);
	for(int i = 1; i <= len; i ++) add(stt[i] - 'a');
	int p  = 1;
	for(int i = 1; i <= len; i ++) {
		p = a[p].ch[stt[i] - 'a'];
		size[p] ++;
	}
	for(int i = 1; i <= tot; i ++) g[a[i].fa].push_back(i);//, printf("%d ---> %d\n", a[i].fa, i);
	dfs(1);
	//for(int i = 1; i <= tot; i ++) printf("%d ", size[i]); printf("\n");
	scanf("%d", &m);
	while(m --) calc();
	return 0;
}


#include<bits/stdc++.h>
#define N 2000005
using namespace std;
struct Node {
	int ch[26], len, fa;
	void clear() {
		for(int i = 0; i < 26; i ++) ch[i] = 0;
		len = fa = 0;
	}
} a[N << 1];
int tot, lst;
void add(int c) {
	int p = lst, np = ++ tot; lst = np;
	a[np].len = a[p].len + 1;
	for(; p && !a[p].ch[c]; p = a[p].fa) a[p].ch[c] = np;
	if(!p) a[np].fa = 1;
	else {
		int v = a[p].ch[c];
		if(a[v].len == a[p].len + 1) a[np].fa = v;
		else {
			int nv = ++ tot; a[nv] = a[v];
			a[nv].len = a[p].len + 1;
			for(; p && a[p].ch[c] == v; p = a[p].fa) a[p].ch[c] = nv;
			a[np].fa = a[v].fa = nv;
		}
	}
}
char st[N], stt[N];
int n, slen[N], vis[N], m;
vector<int> g[N];
int size[N];
void dfs(int u) {
	for(int i = 0; i < g[u].size(); i ++) {
		int v = g[u][i];
		dfs(v);
		size[u] += size[v];
	}
}
void calc() {
	scanf("%s", st + 1);
	n = strlen(st + 1);
	int p = 1, s = 0;
	for(int i = 1; i <= n; i ++) {
		int c = st[i] - 'a';
		if(!a[p].ch[c]) {
			while(p != 1 && !a[p].ch[c]) p = a[p].fa;
			s = a[p].len;
		}
		if(a[p].ch[c]) {
			p = a[p].ch[c];
			s ++;
		}
	}
//	printf("%d  %d   ", p, s);
	long long ans = 0;
	for(int i = 1; i <= n; i ++) {// printf("%d %d\n", p, s);
		int c = st[i] - 'a';
		if(s == n) {
			if(!vis[p]) ans += size[p];//, printf("*");
			vis[p] = 1;
			if(-- s == a[a[p].fa].len) p = a[p].fa;
		}
		if(!a[p].ch[c]) {
			while(p != 1 && !a[p].ch[c]) p = a[p].fa;
			s = a[p].len;
		}

		if(a[p].ch[c]) {
			p = a[p].ch[c];
			s ++;
		}
	}
	printf("%lld\n", ans);
	for(int i = 1; i <= tot; i ++) vis[i] = 0;
}
int main() {
	scanf("%s", stt + 1);
	lst = tot = 1;
	int len = strlen(stt + 1);
	for(int i = 1; i <= len; i ++) add(stt[i] - 'a');
	int p  = 1;
	for(int i = 1; i <= len; i ++) {
		p = a[p].ch[stt[i] - 'a'];
		size[p] ++;
	}
	for(int i = 1; i <= tot; i ++) g[a[i].fa].push_back(i);//, printf("%d ---> %d\n", a[i].fa, i);
	dfs(1);
	scanf("%d", &m);
	while(m --) calc();
	return 0;
}


#include<bits/stdc++.h>
#define N 2000005
using namespace std;
struct Node {
	int ch[26], len, fa;
	void clear() {
		for(int i = 0; i < 26; i ++) ch[i] = 0;
		len = fa = 0;
	}
} a[N << 1];
int tot, lst;
void add(int c) {
	int p = lst, np = ++ tot; lst = np;
	a[np].len = a[p].len + 1;
	for(; p && !a[p].ch[c]; p = a[p].fa) a[p].ch[c] = np;
	if(!p) a[np].fa = 1;
	else {
		int v = a[p].ch[c];
		if(a[v].len == a[p].len + 1) a[np].fa = v;
		else {
			int nv = ++ tot; a[nv] = a[v];
			a[nv].len = a[p].len + 1;
			for(; p && a[p].ch[c] == v; p = a[p].fa) a[p].ch[c] = nv;
			a[np].fa = a[v].fa = nv;
		}
	}
}
char st[N], stt[N];
int n, slen[N], vis[N], m;
vector<int> g[N];
int size[N];
void dfs(int u) {
	for(int i = 0; i < g[u].size(); i ++) {
		int v = g[u][i];
		dfs(v);
		size[u] += size[v];
	}
}
void calc() {
	scanf("%s", st + 1);
	n = strlen(st + 1);
	int p = 1, s = 0;
	for(int i = 1; i <= n; i ++) {
		int c = st[i] - 'a';
		if(!a[p].ch[c]) {
			while(p != 1 && !a[p].ch[c]) p = a[p].fa;
			s = a[p].len;
		}
		if(a[p].ch[c]) {
			p = a[p].ch[c];
			s ++;
		}
	}
//	printf("%d  %d   ", p, s);
	long long ans = 0;
	for(int i = 1; i <= n; i ++) {// printf("%d %d\n", p, s);
		int c = st[i] - 'a';
		if(s == n) {
			if(!vis[p]) ans += size[p];//, printf("*");
			vis[p] = 1;
			if(-- s == a[a[p].fa].len) p = a[p].fa;
		}
		if(!a[p].ch[c]) {
			while(p != 1 && !a[p].ch[c]) p = a[p].fa;
			s = a[p].len;
		}

		if(a[p].ch[c]) {
			p = a[p].ch[c];
			s ++;
		}
	}
	printf("%lld\n", ans);
	for(int i = 1; i <= tot; i ++) vis[i] = 0;
}
int main() {
	scanf("%s", stt + 1);
	lst = tot = 1;
	int len = strlen(stt + 1);
	for(int i = 1; i <= len; i ++) add(stt[i] - 'a');
	int p  = 1;
	for(int i = 1; i <= len; i ++) {
		p = a[p].ch[stt[i] - 'a'];
		size[p] ++;
	}
	for(int i = 1; i <= tot; i ++) g[a[i].fa].push_back(i);//, printf("%d ---> %d\n", a[i].fa, i);
	dfs(1);
	//for(int i = 1; i <= tot; i ++) printf("%d ", size[i]); printf("\n");
	scanf("%d", &m);
	while(m --) calc();
	return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#pragma GCC diagnostic error "-std=c++11"
#pragma GCC optimize(3)
#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
/*???????
??20???578?
?????
????????????????
??
???????
????????20???3700?
?????20???500?
????
?????
??????
??????
????
?????????
?????
???????????
???????
?????????
??????
?????K??J
????????
?????
?????
??
???K??
????2??
????
??
???K????
????????
??????
??????
???
???2?
????????
???????????
??????????
???????
????
??
????????
?????
??????????????
???
??????????*/
/*????????
????
??????
???????????????
??,????
??,???!*/
#include<bits/stdc++.h>
#define N 2000005
using namespace std;
struct Node {
	int ch[26], len, fa;
	void clear() {
		for(int i = 0; i < 26; i ++) ch[i] = 0;
		len = fa = 0;
	}
} a[N << 1];
int tot, lst;
void add(int c) {
	int p = lst, np = ++ tot; lst = np;
	a[np].len = a[p].len + 1;
	for(; p && !a[p].ch[c]; p = a[p].fa) a[p].ch[c] = np;
	if(!p) a[np].fa = 1;
	else {
		int v = a[p].ch[c];
		if(a[v].len == a[p].len + 1) a[np].fa = v;
		else {
			int nv = ++ tot; a[nv] = a[v];
			a[nv].len = a[p].len + 1;
			for(; p && a[p].ch[c] == v; p = a[p].fa) a[p].ch[c] = nv;
			a[np].fa = a[v].fa = nv;
		}
	}
}
char st[N], stt[N];
int n, slen[N], vis[N], m;
vector<int> g[N];
int size[N];
void dfs(int u) {
	for(int i = 0; i < g[u].size(); i ++) {
		int v = g[u][i];
		dfs(v);
		size[u] += size[v];
	}
}
void calc() {
	scanf("%s", st + 1);
	n = strlen(st + 1);
	int p = 1, s = 0;
	for(int i = 1; i <= n; i ++) {
		int c = st[i] - 'a';
		if(!a[p].ch[c]) {
			while(p != 1 && !a[p].ch[c]) p = a[p].fa;
			s = a[p].len;
		}
		if(a[p].ch[c]) {
			p = a[p].ch[c];
			s ++;
		}
	}
//	printf("%d  %d   ", p, s);
	long long ans = 0;
	for(int i = 1; i <= n; i ++) {// printf("%d %d\n", p, s);
		int c = st[i] - 'a';
		if(s == n) {
			if(!vis[p]) ans += size[p];//, printf("*");
			vis[p] = 1;
			if(-- s == a[a[p].fa].len) p = a[p].fa;
		}
		if(!a[p].ch[c]) {
			while(p != 1 && !a[p].ch[c]) p = a[p].fa;
			s = a[p].len;
		}

		if(a[p].ch[c]) {
			p = a[p].ch[c];
			s ++;
		}
	}
	printf("%lld\n", ans);
	for(int i = 1; i <= tot; i ++) vis[i] = 0;
}
int main() {
	scanf("%s", stt + 1);
	lst = tot = 1;
	int len = strlen(stt + 1);
	for(int i = 1; i <= len; i ++) add(stt[i] - 'a');
	int p  = 1;
	for(int i = 1; i <= len; i ++) {
		p = a[p].ch[stt[i] - 'a'];
		size[p] ++;
	}
	for(int i = 1; i <= tot; i ++) g[a[i].fa].push_back(i);//, printf("%d ---> %d\n", a[i].fa, i);
	dfs(1);
	//for(int i = 1; i <= tot; i ++) printf("%d ", size[i]); printf("\n");
	scanf("%d", &m);
	while(m --) calc();
	return 0;
}


#include<bits/stdc++.h>
#define N 2000005
using namespace std;
struct Node {
	int ch[26], len, fa;
	void clear() {
		for(int i = 0; i < 26; i ++) ch[i] = 0;
		len = fa = 0;
	}
} a[N << 1];
int tot, lst;
void add(int c) {
	int p = lst, np = ++ tot; lst = np;
	a[np].len = a[p].len + 1;
	for(; p && !a[p].ch[c]; p = a[p].fa) a[p].ch[c] = np;
	if(!p) a[np].fa = 1;
	else {
		int v = a[p].ch[c];
		if(a[v].len == a[p].len + 1) a[np].fa = v;
		else {
			int nv = ++ tot; a[nv] = a[v];
			a[nv].len = a[p].len + 1;
			for(; p && a[p].ch[c] == v; p = a[p].fa) a[p].ch[c] = nv;
			a[np].fa = a[v].fa = nv;
		}
	}
}
char st[N], stt[N];
int n, slen[N], vis[N], m;
vector<int> g[N];
int size[N];
void dfs(int u) {
	for(int i = 0; i < g[u].size(); i ++) {
		int v = g[u][i];
		dfs(v);
		size[u] += size[v];
	}
}
void calc() {
	scanf("%s", st + 1);
	n = strlen(st + 1);
	int p = 1, s = 0;
	for(int i = 1; i <= n; i ++) {
		int c = st[i] - 'a';
		if(!a[p].ch[c]) {
			while(p != 1 && !a[p].ch[c]) p = a[p].fa;
			s = a[p].len;
		}
		if(a[p].ch[c]) {
			p = a[p].ch[c];
			s ++;
		}
	}
//	printf("%d  %d   ", p, s);
	long long ans = 0;
	for(int i = 1; i <= n; i ++) {// printf("%d %d\n", p, s);
		int c = st[i] - 'a';
		if(s == n) {
			if(vis[p] != m + 1) ans += size[p];//, printf("*");
			vis[p] = m + 1;
			if(-- s == a[a[p].fa].len) p = a[p].fa;
		}
		if(!a[p].ch[c]) {
			while(p != 1 && !a[p].ch[c]) p = a[p].fa;
			s = a[p].len;
		}

		if(a[p].ch[c]) {
			p = a[p].ch[c];
			s ++;
		}
	}
	printf("%lld\n", ans);
}
int main() {
	scanf("%s", stt + 1);
	lst = tot = 1;
	int len = strlen(stt + 1);
	for(int i = 1; i <= len; i ++) add(stt[i] - 'a');
	int p  = 1;
	for(int i = 1; i <= len; i ++) {
		p = a[p].ch[stt[i] - 'a'];
		size[p] ++;
	}
	for(int i = 1; i <= tot; i ++) g[a[i].fa].push_back(i);//, printf("%d ---> %d\n", a[i].fa, i);
	dfs(1);
	//for(int i = 1; i <= tot; i ++) printf("%d ", size[i]); printf("\n");
	scanf("%d", &m);
	while(m --) calc();
	return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#pragma GCC diagnostic error "-std=c++11"
#pragma GCC optimize(3)
#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
/*???????
??20???578?
?????
????????????????
??
???????
????????20???3700?
?????20???500?
????
?????
??????
??????
????
?????????
?????
???????????
???????
?????????
??????
?????K??J
????????
?????
?????
??
???K??
????2??
????
??
???K????
????????
??????
??????
???
???2?
????????
???????????
??????????
???????
????
??
????????
?????
??????????????
???
??????????*/
/*????????
????
??????
???????????????
??,????
??,???!*/
#include<bits/stdc++.h>
#define N 200005
using namespace std;
struct Node {
	int ch[26], len, fa;
	void clear() {
		for(int i = 0; i < 26; i ++) ch[i] = 0;
		len = fa = 0;
	}
} a[N << 1];
int tot, lst;
void add(int c) {
	int p = lst, np = ++ tot; lst = np;
	a[np].len = a[p].len + 1;
	for(; p && !a[p].ch[c]; p = a[p].fa) a[p].ch[c] = np;
	if(!p) a[np].fa = 1;
	else {
		int v = a[p].ch[c];
		if(a[v].len == a[p].len + 1) a[np].fa = v;
		else {
			int nv = ++ tot; a[nv] = a[v];
			a[nv].len = a[p].len + 1;
			for(; p && a[p].ch[c] == v; p = a[p].fa) a[p].ch[c] = nv;
			a[np].fa = a[v].fa = nv;
		}
	}
}
char st[N], stt[N];
int n, slen[N], vis[N], m;
vector<int> g[N];
int size[N];
void dfs(int u) {
	for(int i = 0; i < g[u].size(); i ++) {
		int v = g[u][i];
		dfs(v);
		size[u] += size[v];
	}
}
void calc() {
	scanf("%s", st + 1);
	n = strlen(st + 1);
	int p = 1, s = 0;
	for(int i = 1; i <= n; i ++) {
		int c = st[i] - 'a';
		if(!a[p].ch[c]) {
			while(p != 1 && !a[p].ch[c]) p = a[p].fa;
			s = a[p].len;
		}
		if(a[p].ch[c]) {
			p = a[p].ch[c];
			s ++;
		}
	}
//	printf("%d  %d   ", p, s);
	long long ans = 0;
	for(int i = 1; i <= n; i ++) {// printf("%d %d\n", p, s);
		int c = st[i] - 'a';
		if(s == n) {
			if(!vis[p]) ans += size[p];//, printf("*");
			vis[p] = 1;
			if(-- s == a[a[p].fa].len) p = a[p].fa;
		}
		if(!a[p].ch[c]) {
			while(p != 1 && !a[p].ch[c]) p = a[p].fa;
			s = a[p].len;
		}

		if(a[p].ch[c]) {
			p = a[p].ch[c];
			s ++;
		}
	}
	printf("%lld\n", ans);
	for(int i = 1; i <= tot; i ++) vis[i] = 0;
}
int main() {
	scanf("%s", stt + 1);
	lst = tot = 1;
	int len = strlen(stt + 1);
	for(int i = 1; i <= len; i ++) add(stt[i] - 'a');
	int p  = 1;
	for(int i = 1; i <= len; i ++) {
		p = a[p].ch[stt[i] - 'a'];
		size[p] ++;
	}
	for(int i = 1; i <= tot; i ++) g[a[i].fa].push_back(i);//, printf("%d ---> %d\n", a[i].fa, i);
	dfs(1);
	//for(int i = 1; i <= tot; i ++) printf("%d ", size[i]); printf("\n");
	scanf("%d", &m);
	while(m --) calc();
	return 0;
}


#include <iostream>
#include <string>
#include <map>
#include <set>
using namespace std;
string a, b;
struct state {
    int len, link, cnt;
    map<char,int>next;
};

const int MAXLEN = 2000000;
state st[MAXLEN*2];
int sz, last;

void sa_init() {
    sz = last = 0;
    st[0].len = 0;
    st[0].link = -1;
    ++sz;
}
set<pair<int,int> > base ;

void sa_extend (char c) {
    int cur = sz++;
    st[cur].len = st[last].len + 1;
    st[cur].cnt = 1;
    base.insert(make_pair(st[cur].len, cur));

    int p;
    for (p=last; p!=-1 && !st[p].next.count(c); p=st[p].link)
        st[p].next[c] = cur;
    if (p == -1)
        st[cur].link = 0;
    else {
        int q = st[p].next[c];
        if (st[p].len + 1 == st[q].len)
            st[cur].link = q;
        else {
            int clone = sz++;
            st[clone].len = st[p].len + 1;
            st[clone].next = st[q].next;
            st[clone].link = st[q].link;
            st[clone].cnt = 0;
            base.insert(make_pair(st[clone].len,clone));

            for (; p!=-1 && st[p].next[c]==q; p=st[p].link)
                st[p].next[c] = clone;
            st[q].link = st[cur].link = clone;
        }
    }
    last = cur;
}

void pre() {
  set< pair < int , int > > ::reverse_iterator it;
  for(  it=base.rbegin(); it!=base.rend(); it++ ) {
       st[ st[ it -> second ].link ].cnt += st[ it->second ].cnt;
  }
}
int ans = 0;
int fl[3000042];

string cc;
void dfs ( int i ) {
    cout << cc;
    if ( cc.size() ) {
        cout << " : " << st[ st[i].link ].len;
    }
    cout << "\n";
    for ( auto it = st[i].next.begin() ; it != st[i].next.end() ; it ++ ) {
        if ( it->second ) {
            cc += it->first;
            dfs( it->second );
            cc.pop_back();
        }
    }
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    sa_init();
    cin >> a;
    for ( int i = 0 ; i < a.size() ; i ++ ) {
        sa_extend( a[i] );
    }
    pre();
    int n;

//    dfs(0);
  //  return 0;

    cin >> n;
    for ( int i = 0 ; i < n ; i ++ ) {
        ans = 0;

        cin >> b;
        int bLen = b.size();
        b += b;
        b.pop_back();

        for ( int i = 0 ; i < b.size()+1 ; i ++ ) { fl[i] = 0; }
        fl[0] = -1;
        for ( int i = 0 ; i < b.size() ; i ++ ) {
            int j = fl[i];
            for ( ; j != -1 && b[i] != b[j] ; j = fl[j] ) {}
            fl[i+1] = j+1;
        }

        int saI = 0, bb = 0;
        for ( int i = 0 ; i < b.size() ; i ++ ) {
            for ( ; saI != -1 && st[saI].next.count( b[i] ) == 0 ;  ) {
                saI = st[saI].link;
                bb = st[saI].len;
//                cout << "fl\n";
            }
//            cout << saI << " " << st[saI].link << " " << st[ st[ saI ].link ].len << "\n";
//            cout << st[ saI ].len << "\n";

            if ( saI != -1 ) {
                saI = st[saI].next[b[i]];
                bb ++;

                for ( ; saI != -1 && st[saI].link != -1 && st[ st[ saI ].link ].len >= bLen ; ) {
                    saI = st[saI].link;
                    bb = st[saI].len;
                    //cout << "fl\n";
                }
                if ( bb >= bLen && fl[i+1] < bLen ) {
                    ans += st[saI].cnt;
  //                  cout << "to " << i << " : " << st[saI].cnt << " ) " << bb << "\n";
                }
            }else {
                bb = 0;
                saI = 0;
            }
        }
        cout << ans << "\n";
    }
    return 0;
}


#include <iostream>
#include <string>
#include <map>
#include <set>
using namespace std;
string a, b;
struct state {
    int len, link, cnt;
    map<char,int>next;
};

const int MAXLEN = 100000;
state st[MAXLEN*2];
int sz, last;

void sa_init() {
    sz = last = 0;
    st[0].len = 0;
    st[0].link = -1;
    ++sz;
}
set<pair<int,int> > base ;

void sa_extend (char c) {
    int cur = sz++;
    st[cur].len = st[last].len + 1;
    st[cur].cnt = 1;
    base.insert(make_pair(st[cur].len, cur));

    int p;
    for (p=last; p!=-1 && !st[p].next.count(c); p=st[p].link)
        st[p].next[c] = cur;
    if (p == -1)
        st[cur].link = 0;
    else {
        int q = st[p].next[c];
        if (st[p].len + 1 == st[q].len)
            st[cur].link = q;
        else {
            int clone = sz++;
            st[clone].len = st[p].len + 1;
            st[clone].next = st[q].next;
            st[clone].link = st[q].link;
            st[clone].cnt = 0;
            base.insert(make_pair(st[clone].len,clone));

            for (; p!=-1 && st[p].next[c]==q; p=st[p].link)
                st[p].next[c] = clone;
            st[q].link = st[cur].link = clone;
        }
    }
    last = cur;
}

void pre() {
  set< pair < int , int > > ::reverse_iterator it;
  for(  it=base.rbegin(); it!=base.rend(); it++ ) {
       st[ st[ it -> second ].link ].cnt += st[ it->second ].cnt;
  }
}
int ans = 0;
int fl[10000042];

string cc;
void dfs ( int i ) {
    cout << cc << " : " << st[i].cnt << "\n";
    for ( auto it = st[i].next.begin() ; it != st[i].next.end() ; it ++ ) {
        if ( it->second ) {
            cc += it->first;
            dfs( it->second );
            cc.pop_back();
        }
    }
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    sa_init();
    cin >> a;
    for ( int i = 0 ; i < a.size() ; i ++ ) {
        sa_extend( a[i] );
    }
    pre();
    int n;

 //   dfs(0);

    cin >> n;
    for ( int i = 0 ; i < n ; i ++ ) {
        ans = 0;

        cin >> b;
        int bLen = b.size();
        b += b;
        b.pop_back();

        for ( int i = 0 ; i < b.size()+1 ; i ++ ) { fl[i] = 0; }
        fl[0] = -1;
        for ( int i = 0 ; i < b.size() ; i ++ ) {
            int j = fl[i];
            for ( ; j != -1 && b[i] != b[j] ; j = fl[j] ) {}
            fl[i+1] = j+1;
        }

        int saI = 0, bb = 0;
        for ( int i = 0 ; i < b.size() ; i ++ ) {
            for ( ; saI != -1 && st[saI].next.count( b[i] ) == 0 ;  ) {
                saI = st[saI].link;
                bb = st[saI].len;
            }

            if ( saI != -1 ) {
                saI = st[saI].next[b[i]];
                bb ++;
                if ( bb >= bLen && fl[i+1] < bLen ) {
                    ans += st[saI].cnt;
               //     cout << "to " << i << " : " << st[saI].cnt << "\n";
                }
            }else {
                bb = 0;
                saI = 0;
            }
        }
        cout << ans << "\n";
    }
    return 0;
}


#include <iostream>
#include <string>
#include <map>
#include <set>
using namespace std;
string a, b;
struct state {
    int len, link, cnt;
    map<char,int>next;
};

const int MAXLEN = 100000;
state st[MAXLEN*2];
int sz, last;

void sa_init() {
    sz = last = 0;
    st[0].len = 0;
    st[0].link = -1;
    ++sz;
}
set<pair<int,int> > base ;

void sa_extend (char c) {
    int cur = sz++;
    st[cur].len = st[last].len + 1;
    st[cur].cnt = 1;
    base.insert(make_pair(st[cur].len, cur));

    int p;
    for (p=last; p!=-1 && !st[p].next.count(c); p=st[p].link)
        st[p].next[c] = cur;
    if (p == -1)
        st[cur].link = 0;
    else {
        int q = st[p].next[c];
        if (st[p].len + 1 == st[q].len)
            st[cur].link = q;
        else {
            int clone = sz++;
            st[clone].len = st[p].len + 1;
            st[clone].next = st[q].next;
            st[clone].link = st[q].link;
            st[clone].cnt = 0;
            base.insert(make_pair(st[clone].len,clone));

            for (; p!=-1 && st[p].next[c]==q; p=st[p].link)
                st[p].next[c] = clone;
            st[q].link = st[cur].link = clone;
        }
    }
    last = cur;
}

void pre() {
  set< pair < int , int > > ::reverse_iterator it;
  for(  it=base.rbegin(); it!=base.rend(); it++ ) {
       st[ st[ it -> second ].link ].cnt += st[ it->second ].cnt;
  }
}
int ans = 0;
int fl[10000042];

string cc;
void dfs ( int i ) {
    cout << cc << " : " << st[i].cnt << "\n";
    for ( auto it = st[i].next.begin() ; it != st[i].next.end() ; it ++ ) {
        if ( it->second ) {
            cc += it->first;
            dfs( it->second );
            cc.pop_back();
        }
    }
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    sa_init();
    cin >> a;
    for ( int i = 0 ; i < a.size() ; i ++ ) {
        sa_extend( a[i] );
    }
    pre();
    int n;

 //   dfs(0);

    cin >> n;
    for ( int i = 0 ; i < n ; i ++ ) {
        ans = 0;

        cin >> b;
        int bLen = b.size();
        b += b;
        b.pop_back();

        for ( int i = 0 ; i < b.size()+1 ; i ++ ) { fl[i] = 0; }
        fl[0] = -1;
        for ( int i = 0 ; i < b.size() ; i ++ ) {
            int j = fl[i];
            for ( ; j != -1 && b[i] != b[j] ; j = fl[j] ) {}
            fl[i+1] = j+1;
        }

        int saI = 0, bb = 0;
        for ( int i = 0 ; i < b.size() ; i ++ ) {
            for ( ; saI != -1 && st[saI].next.count( b[i] ) == 0 ;  ) {
                saI = st[saI].link;
                bb = st[saI].len;
            }

            if ( saI != -1 ) {
                saI = st[saI].next[b[i]];
                bb ++;
                if ( bb >= bLen ) {
                    ans += st[saI].cnt;
               //     cout << "to " << i << " : " << st[saI].cnt << "\n";
                }
            }else {
                bb = 0;
                saI = 0;
            }
        }
        cout << ans << "\n";
    }
    return 0;
}


#include <iostream>
#include <string>
#include <map>
#include <set>
using namespace std;
string a, b;
struct state {
    int len, link, cnt;
    map<char,int>next;
};

const int MAXLEN = 100000;
state st[MAXLEN*2];
int sz, last;

void sa_init() {
    sz = last = 0;
    st[0].len = 0;
    st[0].link = -1;
    ++sz;
}
set<pair<int,int> > base ;

void sa_extend (char c) {
    int cur = sz++;
    st[cur].len = st[last].len + 1;
    st[cur].cnt = 1;
    base.insert(make_pair(st[cur].len, cur));

    int p;
    for (p=last; p!=-1 && !st[p].next.count(c); p=st[p].link)
        st[p].next[c] = cur;
    if (p == -1)
        st[cur].link = 0;
    else {
        int q = st[p].next[c];
        if (st[p].len + 1 == st[q].len)
            st[cur].link = q;
        else {
            int clone = sz++;
            st[clone].len = st[p].len + 1;
            st[clone].next = st[q].next;
            st[clone].link = st[q].link;
            st[clone].cnt = 0;
            base.insert(make_pair(st[clone].len,clone));

            for (; p!=-1 && st[p].next[c]==q; p=st[p].link)
                st[p].next[c] = clone;
            st[q].link = st[cur].link = clone;
        }
    }
    last = cur;
}

void pre() {
  set< pair < int , int > > ::reverse_iterator it;
  for(  it=base.rbegin(); it!=base.rend(); it++ ) {
       st[ st[ it -> second ].link ].cnt += st[ it->second ].cnt;
  }
}
int ans = 0;
int fl[10000042];

string cc;
void dfs ( int i ) {
    cout << cc;
    if ( cc.size() ) {
        cout << " : " << st[ st[i].link ].len;
    }
    cout << "\n";
    for ( auto it = st[i].next.begin() ; it != st[i].next.end() ; it ++ ) {
        if ( it->second ) {
            cc += it->first;
            dfs( it->second );
            cc.pop_back();
        }
    }
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    sa_init();
    cin >> a;
    for ( int i = 0 ; i < a.size() ; i ++ ) {
        sa_extend( a[i] );
    }
    pre();
    int n;

//    dfs(0);
  //  return 0;

    cin >> n;
    for ( int i = 0 ; i < n ; i ++ ) {
        ans = 0;

        cin >> b;
        int bLen = b.size();
        b += b;
        b.pop_back();

        for ( int i = 0 ; i < b.size()+1 ; i ++ ) { fl[i] = 0; }
        fl[0] = -1;
        for ( int i = 0 ; i < b.size() ; i ++ ) {
            int j = fl[i];
            for ( ; j != -1 && b[i] != b[j] ; j = fl[j] ) {}
            fl[i+1] = j+1;
        }

        int saI = 0, bb = 0;
        for ( int i = 0 ; i < b.size() ; i ++ ) {
            for ( ; saI != -1 && st[saI].next.count( b[i] ) == 0 ;  ) {
                saI = st[saI].link;
                bb = st[saI].len;
//                cout << "fl\n";
            }
//            cout << saI << " " << st[saI].link << " " << st[ st[ saI ].link ].len << "\n";
//            cout << st[ saI ].len << "\n";

            if ( saI != -1 ) {
                saI = st[saI].next[b[i]];
                bb ++;

                for ( ; saI != -1 && st[saI].link != -1 && st[ st[ saI ].link ].len >= bLen ; ) {
                    saI = st[saI].link;
                    bb = st[saI].len;
                    //cout << "fl\n";
                }
                if ( bb >= bLen && fl[i+1] < bLen ) {
                    ans += st[saI].cnt;
  //                  cout << "to " << i << " : " << st[saI].cnt << " ) " << bb << "\n";
                }
            }else {
                bb = 0;
                saI = 0;
            }
        }
        cout << ans << "\n";
    }
    return 0;
}


#include <iostream>
#include <string>
#include <map>
using namespace std;
string a, b;

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

const int MAXLEN = 100000;
state st[MAXLEN*2];
int sz, last;

void sa_init() {
    sz = last = 0;
    st[0].len = 0;
    st[0].link = -1;
    ++sz;
}

void sa_extend (char c) {
    int crr = sz; sz ++;

    st[crr].cnt = 1;
    st[crr].len = st[last].len+1;

    int p = last;
    for ( ; p != -1 && st[p].next.count(c) == 0 ; p = st[p].link ) {
        st[p].next[c] = crr;
    }
    if ( p == -1 ) {
        st[crr].link = 0;
    }else {
        int q = st[p].next[c];
        if ( st[q].len == st[p].len+1 ) {
            st[crr].link = q;
        }else {
            int clone = sz; sz ++;
            st[clone].link = st[q].link;
            st[clone].next = st[q].next;
            st[clone].len = st[p].len+1;

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

            st[q].link = clone;
            st[crr].link = clone;
        }
    }
    last = crr;

}
void pre() {
    for ( int i = sz-1 ; i > 0 ; i -- ) {
        st[ st[i].link ].cnt += st[i].cnt;
    }
}
int ans = 0;
int fl[10000042];
int main () {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    sa_init();
    cin >> a;
    for ( int i = 0 ; i < a.size() ; i ++ ) {
        sa_extend( a[i] );
    }
    pre();
    int n;
    cin >> n;
    for ( int i = 0 ; i < n ; i ++ ) {
        ans = 0;

        cin >> b;
        int bLen = b.size();
        b += b;
        b.pop_back();

        for ( int i = 0 ; i < b.size()+1 ; i ++ ) { fl[i] = 0; }
        fl[0] = -1;
        for ( int i = 0 ; i < b.size() ; i ++ ) {
            int j = fl[i];
            for ( ; j != -1 && b[i] != b[j] ; j = fl[j] ) {}
            fl[i+1] = j+1;
        }

        int saI = 0, bb = 0;
        for ( int i = 0 ; i < b.size() ; i ++ ) {
            for ( ; saI != -1 && st[saI].next.count( b[i] ) == 0 ;  ) {
                saI = st[saI].link;
                bb = st[saI].len;
            }

            if ( saI != -1 ) {
                saI = st[saI].next[b[i]];
                bb ++;
                if ( bb >= bLen && fl[i+1] < bLen ) {
                    ans += st[saI].cnt;
                    //cout << "to " << i << " : " << st[saI].cnt << "\n";
                }
            }else {
                bb = 0;
                saI = 0;
            }
        }
        cout << ans << "\n";
    }
    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;
			if(--l < 0) break;
			if(l <= len[fa[wz]]) wz = fa[wz];

			while(!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 <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(wz != 1 && 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<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]; char s[N]; bool vis[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(); while (Q--) {
			int ans = 0,x = 1,k = 0,c; memset(vis,0,sizeof(vis)); scanf("%s",s+1); n = strlen(s+1); m = n<<1; rep(i,1,n) s[i+n] = s[i];
			rep(i,1,m-1) {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]) {ans += siz[x]; vis[x] = 1;}}
			printf("%d\n",ans);
		}
	}
}
int main() {qiqi::main(); return 0;}


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

using namespace std;

const int N = 2e6 + 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(wz != 1 && 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 <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;
}


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

const int MAXN = 4e5 + 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++;
			//cout<<cur<<' '<<l<<' '<<siz[cur]<<endl;
			while(mx[fa[cur]] >= len / 2)
				cur = fa[cur], l = mx[cur];
			//cout<<cur<<' '<<l<<' '<<siz[cur]<<endl;
			if(l >= len / 2 && vis[cur] != i)
				ans += siz[cur], vis[cur] = i;
			//cout<<ans<<endl;
		}
		cout<<ans<<endl;
	}
	return 0;
}


#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-1) {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 <bits/stdc++.h>
using namespace std ;
const int N = 1e6+10,M = N<<1 ;
long long ans ;
namespace SAM{
    int ch[M][26],len[M],link[M],size[M],tot,last ;
    inline void init(void){ tot = last = 1 ; }
    inline void extend(int c){
        int cur = ++tot ;
        len[cur] = len[last]+1 ; size[cur] = 1 ;
        int p = last ;
        while(p && !ch[p][c]){
            ch[p][c] = cur ;
            p = link[p] ;
        }
        if(!p){
            link[cur] = 1 ;
        }else{
            int q = ch[p][c] ;
            if(len[q] == len[p]+1){
                link[cur] = q ;
            }else{
                int clone = ++tot ;
                len[clone] = len[p]+1 ;
                memcpy(ch[clone],ch[q],sizeof ch[q]) ;
                link[clone] = link[q] ;
                while(p && ch[p][c] == q){
                    ch[p][c] = clone ;
                    p = link[p] ;
                }
                link[q] = link[cur] = clone ;
            }
        }
        last = cur ;
    }
}
int head[M],ver[M],Next[M],tot,len,T,p,vis[M],m,n ;
inline void add(int x,int y){
    ver[++tot] = y ; Next[tot] = head[x] ; head[x] = tot ;
}
inline void dfs(int x){
    for(int i = head[x] ; i ; i = Next[i]){
        int y = ver[i] ;
        dfs(y) ;
        SAM::size[x] += SAM::size[y] ;
    }
}
inline void add(int c){
    while(p && !SAM::ch[p][c]) p = SAM::link[p],len = SAM::len[p] ;// len = SAM::len[p] ;
    if(SAM::ch[p][c]) len++ , p = SAM::ch[p][c] ;
}
inline void sub(void){ if(len > m && --len == SAM::len[SAM::link[p]]) p = SAM::link[p] ; }
inline void calc(void){ if(len == m && vis[p] < T) ans += SAM::size[p] , vis[p] = T ; }
char s[N] ; int Q ;
int main(){
    //freopen("in.txt","r",stdin)  ;
    SAM::init() ; scanf("%s",s+1) ; n = strlen(s+1) ;
    for(int i = 1 ; i <= n ; i++) SAM::extend(s[i]-'a') ;
    for(int i = 2 ; i <= SAM::tot ; i++) add(SAM::link[i],i) ;
    dfs(1) ;
    cin >> Q ;
    for(T = 1 ; T <= Q ; T++){
        scanf("%s",s+1) ;
        m = strlen(s+1) ;
        p = 1 ; ans = len = 0 ;
        for(int i = 1 ; i <= m ; i++) add(s[i]-'a') ;
        calc() ;
        for(int i = 1 ; i < m ; i++){ add(s[i]-'a') ; sub() ; calc() ; }
        printf("%lld\n",ans);
    }
    return 0 ;
}


#pragma GCC diagnostic error "-fwhole-program"
#pragma GCC diagnostic error "-fcse-skip-blocks"
#pragma GCC diagnostic error "-funsafe-loop-optimizations"
#pragma GCC diagnostic error "-std=c++11"
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")

#define getcha() (S == T && (T = (S = BB) + fread(BB, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
char BB[1 << 15], *S = BB, *T = BB;

#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;
int fa[MAXN],num[MAXN],ch[MAXN][26];
int len[MAXN],v[MAXN],q[MAXN],flag[MAXN];
long long tot;char str[MAXN];
int n,l,last=1,cnt=1;
vector<int>ans;

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;
}


#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;
        memset(vis,63,sizeof vis);
        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 <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;
const int N = 2e6+5;
int last = 1, tot = 1, size[N], len[N], fa[N], ch[N][26], n;
int tax[N], c[N], q;
char s[N];
void samAdd(int val)
{
    int cur = ++tot, pre = last; last = cur;
    size[cur] = 1; len[cur] = len[pre]+1;
    while(pre&&!ch[pre][val]) ch[pre][val] = cur, pre = fa[pre];
    if(!pre) return void(fa[cur] = 1);
    int x = ch[pre][val];
    if(len[pre]+1 == len[x]) return void(fa[cur] = x);
    int y = ++tot; fa[y] = fa[x]; fa[x] = fa[cur] = y; len[y] = len[pre]+1;
    memcpy(ch[y], ch[x], sizeof(ch[x]));
    while(pre&&ch[pre][val] == x) ch[pre][val] = y, pre = fa[pre];
}
int rec[N], p[N];
int query(char *s, int d)
{
    int x = 1, n = strlen(s), lcs = 0, ret = 0;
    for(int i = n; i < n<<1; ++i) s[i] = s[i-n];
    for(int i = 0; i < n<<1; ++i)
    {
        int c = s[i]-'a';
        while(x&&!ch[x][c]) x = fa[x], lcs = len[x];
        if(!x) x = 1, lcs = 0;
        else
        {
            x = ch[x][c]; ++lcs;
            while(x&&len[fa[x]] >= n) x = fa[x], lcs = len[x];
            if(lcs >= n&&rec[x] != d) ret += size[x], rec[x] = d;
        }
    }
    return ret;
}

int main()
{
    scanf("%s", s); n = strlen(s);
    for(int i = 0; i < n; ++i) samAdd(s[i]-'a');
    for(int i = 1; i <= tot; ++i) ++tax[len[i]];
    for(int i = 1; i <= tot; ++i) tax[i] += tax[i-1];
    for(int i = 1; i <= tot; ++i) c[tax[len[i]]--] = i;
    for(int i = tot; i; --i) size[fa[c[i]]] += size[c[i]];
    scanf("%d", &q);
    while(q--) scanf("%s", s), printf("%d\n", query(s, q+1));
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=1000086;
struct Suffix_Automation
{
	struct node
	{
		int ch[27];
		int len,fa,siz;
	}t[maxn<<1];
	int vis[maxn<<1];
	int las,tot;
	void ins(int c)
	{
		int p=las,np=++tot;
		t[np].len=t[p].len+1;
		for(;p&&!t[p].ch[c];p=t[p].fa)
			t[p].ch[c]=np;
		if(!p) t[np].fa=1;
		else
		{
			int q=t[p].ch[c];
			if(t[p].len+1==t[q].len)
				t[np].fa=q;
			else
			{
				int nq=++tot;
				t[nq].fa=t[q].fa;
				t[q].fa=t[np].fa=nq;
				t[nq].len=t[p].len+1;
				memcpy(t[nq].ch,t[q].ch,sizeof(t[q].ch));
				for(;t[p].ch[c]==q;p=t[p].fa)
					t[p].ch[c]=nq;
			}
		}
		las=np,t[np].siz=1;
	}
	int tong[maxn<<1],arr[maxn<<1];
	void build(char *s)
	{
		las=tot=1;
		int len=strlen(s+1);
		for(int i=1;i<=len;++i)
			ins(s[i]^96);
		for(int i=1;i<=tot;++i)
			tong[t[i].len]++;
		for(int i=1;i<=len;++i)
			tong[i]+=tong[i-1];
		for(int i=1;i<=tot;++i)
			arr[tong[t[i].len]--]=i;
		long long ans=1ll*(len+1)*len*(len-1)/2;
		for(int i=tot;i;--i)
		{
			int u=arr[i],f=t[u].fa;
			t[f].siz += t[u].siz;
		}
	}
	void work(const string &s,int id)
	{
		int num=0,u=1,tar=(s.length()+1)/2;
		for(int i=0,j=0;i<s.length();++i)
		{
			int c=s[i]^96;
			while(u!=1&& !t[u].ch[c])
				u=t[u].fa,j=t[u].len;
			if(t[u].ch[c])
				u=t[u].ch[c],++j;
			if(j==tar)
			{
				if(vis[u]!=id)
					num+=t[u].siz,vis[u]=id;
				if(t[t[u].fa].len==j-1)
					u=t[u].fa;
				--j;
			}
		}
		printf("%d\n",num);
	}
}sam;
char s[maxn];
signed main()
{
	scanf("%s",s+1);
	sam.build(s);
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;++i)
	{
		string ss;
		cin>>ss;
		int len=ss.length();
		for(unsigned j=0;j<len-1;++j)
			ss+=ss[j];
		sam.work(ss,i);
	}
	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]];

	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){
				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||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<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<vector>
#define maxn 6000005
#define re register
#define LL long long
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
std::map<int,int> son[maxn];
struct E{int v,nxt;}e[maxn];
int n,lst=1,cnt=1,num,m;
int len[maxn],head[maxn],sz[maxn],deep[maxn],vis[maxn],fa[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[1000005],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(std::map<int,int>::iterator it=son[x].begin();it!=son[x].end();it++) son[y].insert(*it);
    while(f&&son[f][c]==x) son[f][c]=y,f=fa[f];
}
inline int find(int x,int li) {while(len[fa[x]]>=li) x=fa[x];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=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>
using namespace std;

#define Int register int
#define MAXN 2000005

template <typename T> inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;}
template <typename T,typename ... Args> inline void read (T &t,Args&... args){read (t);read (args...);}
template <typename T> inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');}

int n,node = 1,lst = 1;
int ch[MAXN][26],fa[MAXN],len[MAXN];

int cnt[MAXN],las[MAXN];

char s[MAXN];

struct edge{
	int v,nxt;
}e[MAXN];

int top = 1;
int head[MAXN];

void Add_Edge (int u,int v){
	e[++ top] = edge {v,head[u]};
	head[u] = top;
}

void Insert (int c){
	int f = lst,q = lst = ++ node;len[q] = len[f] + 1;cnt[q] ++;
	while (f && !ch[f][c]) ch[f][c] = q,f = fa[f];
	if (f == 0) fa[q] = 1;
	else{
		int x = ch[f][c];
		if (len[x] == len[f] + 1) fa[q] = x;
		else{
			int p = ++ node;len[p] = len[f] + 1;
			fa[p] = fa[x];fa[x] = fa[q] = p;
			memcpy (ch[p],ch[x],sizeof (ch[x]));
			while (f && ch[f][c] == x) ch[f][c] = p,f = fa[f];
		}
	}
}

void dfs (int u){
	for (Int i = head[u];i;i = e[i].nxt){
		int v = e[i].v;
		dfs (v);
		cnt[u] += cnt[v];
	}
}

void solve (){
	for (Int i = 2;i <= node;++ i) Add_Edge (fa[i],i);
	dfs (1);
}

int x,l,m,ans,tim;

void calc (){
	if (l == m && las[x] < tim){
		ans += cnt[x];
		las[x] = tim;
	}
}

void del(){
	if (l > m && -- l == len[fa[x]]) x = fa[x];
}

void Extend (int c){
	while (x && !ch[x][c]) l = len[x = fa[x]];
	if (ch[x][c]){
		++ l;
		x = ch[x][c];
	}
}

signed main(){
	int minlen = 0x7f7f7f7f;
	scanf ("%s",s + 1);
	for (Int i = 1;s[i];++ i) Insert (s[i] - 'a');
	solve ();
	read (n);
	for (tim = 1;tim <= n;++ tim){
		scanf ("%s",s + 1);
		x = 1,l = 0,ans = 0,m = strlen (s + 1);
		for (Int i = 1;i <= m;++ i) Extend (s[i] - 'a');
		calc ();
		for (Int i = 1;i < m;++ i) Extend (s[i] - 'a'),del (),calc ();
		write (ans),putchar ('\n');
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;
char ss[4000010];
int len,tt,dt;
long long ans;
unsigned long long has[4000100],powr[4000100],base=13331;
int head[2000100],nxt[4001000],vis[2000100],tot,tim,mod=1000007;unsigned long long val[4000100];
struct SAM
{
	SAM *son[27],*pre;
	int len,siz,rd;
}sa[3000100],*root,*last,*sd[3000100];
bool exist(unsigned long long x)
{
	int u=x%mod;
	if(vis[u]!=tim)vis[u]=tim,head[u]=0;
	for(int i=head[u];i;i=nxt[i])
	{
		if(val[i]==x)
		return true;
	}
	tot++;nxt[tot]=head[u],val[tot]=x,head[u]=tot;
	return false;
}
void insert(char c,int len)
{
	SAM *np=&sa[++tt],*p=last;last=np;
	np->len=len;np->siz=1;
	while(p&&!p->son[c])p->son[c]=np,p=p->pre;
	if(!p){np->pre=root;return;}
	SAM *q=p->son[c];
	if(q->len==p->len+1)
	np->pre=q;
	else
	{
		SAM *nq=&sa[++tt];*nq=*q;              // �½��ڵ��ʱ��һ��ҪС��,�ĵ��ǽڵ㱾�������ָ��
		nq->len=p->len+1;nq->siz=0;
		np->pre=q->pre=nq;
		while(p&&p->son[c]==q)p->son[c]=nq,p=p->pre;
	}
}
void deal()
{
	for(int i=1;i<=tt;i++)if(!sa[i].rd)sd[++dt]=&sa[i];
	while(dt)
	{
		SAM *qr=sd[dt--];
		if(qr==root)return;
		qr->pre->siz+=qr->siz;
		qr->pre->rd--;
		if(!qr->pre->rd)sd[++dt]=qr->pre;
	}
}
long long solve()
{
	int lenr=(len+1)/2;
	SAM *now=root;int lennow=0;
	long long ans=0;
	tim++;tot=0;
	for(int i=1;i<=len;i++)
	{
		has[i]=has[i-1]*base+ss[i];
//		cout<<" "<<lennow<<endl;
		while(now&&!now->son[ss[i]-'a'])now=now->pre,lennow=min(lennow,now->len);
		if(!now)now=root;
		else now=now->son[ss[i]-'a'],lennow++;
//		cout<<i<<" "<<lennow<<endl;
		if(lennow==lenr)
		{
			SAM *t=now;
//			while(t)cout<<"B "<<t->len<<" ",t=t->pre;cout<<endl;
			while(now->pre->len>=lennow)now=now->pre;
//			cout<<now->siz<<endl;
			if(!exist(has[i]-has[i-lenr]*powr[lenr]))
			ans+=now->siz;
			lennow=lenr-1;
			if(now->pre->len==lennow)now=now->pre;
		}
	}
	return ans;
}
int main()
{
	scanf("%s",ss+1);
	len=strlen(ss+1);powr[0]=1;
	for(int i=1;i<=1000000;i++)powr[i]=powr[i-1]*base;
	root=last=&sa[++tt];
	for(int i=1;i<=len;i++)
	insert(ss[i]-'a',i);
	for(int i=2;i<=tt;i++)sa[i].pre->rd++;
	deal();
	int n;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%s",ss+1);
		len=strlen(ss+1);
		for(int i=len+1;i<=2*len-1;i++)ss[i]=ss[i-len];
		len=2*len-1;ss[len+1]=0;
		long long ans=solve();
		printf("%I64d\n",ans);
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;
char ss[4000010];
int len,tt,dt;
long long ans;
unsigned long long has[4000100],powr[4000100],base=13331;
int head[2000100],nxt[4001000],vis[2000100],tot,tim,mod=1000007;unsigned long long val[4000100];
struct SAM
{
	SAM *son[27],*pre;
	int len,siz,rd,pan;
}sa[3000100],*root,*last,*sd[3000100];
bool exist(unsigned long long x)
{
	int u=x%mod;
	if(vis[u]!=tim)vis[u]=tim,head[u]=0;
	for(int i=head[u];i;i=nxt[i])
	{
		if(val[i]==x)
		return true;
	}
	tot++;nxt[tot]=head[u],val[tot]=x,head[u]=tot;
	return false;
}
void insert(char c,int len)
{
	SAM *np=&sa[++tt],*p=last;last=np;
	np->len=len;np->siz=1;
	while(p&&!p->son[c])p->son[c]=np,p=p->pre;
	if(!p){np->pre=root;return;}
	SAM *q=p->son[c];
	if(q->len==p->len+1)
	np->pre=q;
	else
	{
		SAM *nq=&sa[++tt];*nq=*q;              // �½��ڵ��ʱ��һ��ҪС��,�ĵ��ǽڵ㱾�������ָ��
		nq->len=p->len+1;nq->siz=0;
		np->pre=q->pre=nq;
		while(p&&p->son[c]==q)p->son[c]=nq,p=p->pre;
	}
}
void deal()
{
	for(int i=1;i<=tt;i++)if(!sa[i].rd)sd[++dt]=&sa[i];
	while(dt)
	{
		SAM *qr=sd[dt--];
		if(qr==root)return;
		qr->pre->siz+=qr->siz;
		qr->pre->rd--;
		if(!qr->pre->rd)sd[++dt]=qr->pre;
	}
}
long long solve()
{
	int lenr=(len+1)/2;
	SAM *now=root;int lennow=0;
	long long ans=0;
	tim++;tot=0;
	for(int i=1;i<=len;i++)
	{
		has[i]=has[i-1]*base+ss[i];
		while(now&&!now->son[ss[i]-'a'])
		{
			now=now->pre;
			if(now)
			lennow=min(lennow,now->len);
		}
		if(!now)now=root,lennow=0;
		else now=now->son[ss[i]-'a'],lennow++;
		if(lennow==lenr)
		{
			while(now->pre->len>=lennow)now=now->pre;
			if(now->pan!=tim)now->pan=tim,ans+=now->siz;
			lennow=lenr-1;
			if(now->pre->len==lennow)now=now->pre;
		}
	}
	return ans;
}
int main()
{
	scanf("%s",ss+1);
	len=strlen(ss+1);powr[0]=1;
	for(int i=1;i<=2000000;i++)powr[i]=powr[i-1]*base;
	root=last=&sa[++tt];
	for(int i=1;i<=len;i++)
	insert(ss[i]-'a',i);
	for(int i=2;i<=tt;i++)sa[i].pre->rd++;
	deal();
	int n;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%s",ss+1);
		len=strlen(ss+1);
		for(int i=len+1;i<=2*len-1;i++)ss[i]=ss[i-len];
		len=2*len-1;ss[len+1]=0;
		long long ans=solve();
		printf("%I64d\n",ans);
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;
char ss[4000010];
int len,tt,dt;
long long ans;
unsigned long long has[4000100],powr[4000100],base=13331;
int head[2000100],nxt[4001000],vis[2000100],tot,tim,mod=1000007;unsigned long long val[4000100];
struct SAM
{
	SAM *son[27],*pre;
	int len,siz,rd,pan;
}sa[3000100],*root,*last,*sd[3000100];
bool exist(unsigned long long x)
{
	int u=x%mod;
	if(vis[u]!=tim)vis[u]=tim,head[u]=0;
	for(int i=head[u];i;i=nxt[i])
	{
		if(val[i]==x)
		return true;
	}
	tot++;nxt[tot]=head[u],val[tot]=x,head[u]=tot;
	return false;
}
void insert(char c,int len)
{
	SAM *np=&sa[++tt],*p=last;last=np;
	np->len=len;np->siz=1;
	while(p&&!p->son[c])p->son[c]=np,p=p->pre;
	if(!p){np->pre=root;return;}
	SAM *q=p->son[c];
	if(q->len==p->len+1)
	np->pre=q;
	else
	{
		SAM *nq=&sa[++tt];*nq=*q;              // �½��ڵ��ʱ��һ��ҪС��,�ĵ��ǽڵ㱾�������ָ��
		nq->len=p->len+1;nq->siz=0;
		np->pre=q->pre=nq;
		while(p&&p->son[c]==q)p->son[c]=nq,p=p->pre;
	}
}
void deal()
{
	for(int i=1;i<=tt;i++)if(!sa[i].rd)sd[++dt]=&sa[i];
	while(dt)
	{
		SAM *qr=sd[dt--];
		if(qr==root)return;
		qr->pre->siz+=qr->siz;
		qr->pre->rd--;
		if(!qr->pre->rd)sd[++dt]=qr->pre;
	}
}
long long solve()
{
	int lenr=(len+1)/2;
	SAM *now=root;int lennow=0;
	long long ans=0;
	tim++;tot=0;
	for(int i=1;i<=len;i++)
	{
		has[i]=has[i-1]*base+ss[i];
		while(now&&!now->son[ss[i]-'a'])
		{
			now=now->pre;
			if(now)
			lennow=min(lennow,now->len);
		}
		if(!now)now=root,lennow=0;
		else now=now->son[ss[i]-'a'],lennow++;
		if(lennow>=lenr)
		{
			while(now->pre->len>=lennow)now=now->pre;
			if(now->pan!=tim)now->pan=tim,ans+=now->siz;
			lennow=min(lennow,now->len);
		}
	}
	return ans;
}
int main()
{
	scanf("%s",ss+1);
	len=strlen(ss+1);powr[0]=1;
	for(int i=1;i<=2000000;i++)powr[i]=powr[i-1]*base;
	root=last=&sa[++tt];
	for(int i=1;i<=len;i++)
	insert(ss[i]-'a',i);
	for(int i=2;i<=tt;i++)sa[i].pre->rd++;
	deal();
	int n;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%s",ss+1);
		len=strlen(ss+1);
		for(int i=len+1;i<=2*len-1;i++)ss[i]=ss[i-len];
		len=2*len-1;ss[len+1]=0;
		long long ans=solve();
		printf("%I64d\n",ans);
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;
char ss[2000010];
int len,tt,dt;
long long ans;
unsigned long long hash[2000100],powr[2000100],base=13331;
int head[1000100],nxt[2001000],vis[1000100],tot,tim,mod=1000007;unsigned long long val[2000100];
struct SAM
{
	SAM *son[27],*pre;
	int len,siz,rd;
}sa[2000100],*root,*last,*sd[2000100];
bool exist(unsigned long long x)
{
	int u=x%mod;
	if(vis[u]!=tim)vis[u]=tim,head[u]=0;
	for(int i=head[u];i;i=nxt[i])
	{
		if(val[i]==x)
		return true;
	}
	tot++;nxt[tot]=head[u],val[tot]=x,head[u]=tot;
	return false;
}
void insert(char c,int len)
{
	SAM *np=&sa[++tt],*p=last;last=np;
	np->len=len;np->siz=1;
	while(p&&!p->son[c])p->son[c]=np,p=p->pre;
	if(!p){np->pre=root;return;}
	SAM *q=p->son[c];
	if(q->len==p->len+1)
	np->pre=q;
	else
	{
		SAM *nq=&sa[++tt];*nq=*q;              // �½��ڵ��ʱ��һ��ҪС��,�ĵ��ǽڵ㱾�������ָ��
		nq->len=p->len+1;nq->siz=0;
		np->pre=q->pre=nq;
		while(p&&p->son[c]==q)p->son[c]=nq,p=p->pre;
	}
}
void deal()
{
	for(int i=1;i<=tt;i++)if(!sa[i].rd)sd[++dt]=&sa[i];
	while(dt)
	{
		SAM *qr=sd[dt--];
		if(qr==root)return;
		qr->pre->siz+=qr->siz;
		qr->pre->rd--;
		if(!qr->pre->rd)sd[++dt]=qr->pre;
	}
}
long long solve()
{
	int lenr=(len+1)/2;
	SAM *now=root;int lennow=0;
	long long ans=0;
	tim++;tot=0;
	for(int i=1;i<=len;i++)
	{
		hash[i]=hash[i-1]*base+ss[i];
//		cout<<" "<<lennow<<endl;
		while(now&&!now->son[ss[i]-'a'])now=now->pre,lennow=min(lennow,now->len);
		if(!now)now=root;
		else now=now->son[ss[i]-'a'],lennow++;
//		cout<<i<<" "<<lennow<<endl;
		if(lennow==lenr)
		{
			SAM *t=now;
//			while(t)cout<<"B "<<t->len<<" ",t=t->pre;cout<<endl;
			while(now->pre->len>=lennow)now=now->pre;
//			cout<<now->siz<<endl;
			if(!exist(hash[i]-hash[i-lenr]*powr[lenr]))
			ans+=now->siz;
			lennow=lenr-1;
			if(now->pre->len==lennow)now=now->pre;
		}
	}
	return ans;
}
int main()
{
	scanf("%s",ss+1);
	len=strlen(ss+1);powr[0]=1;
	for(int i=1;i<=1000000;i++)powr[i]=powr[i-1]*base;
	root=last=&sa[++tt];
	for(int i=1;i<=len;i++)
	insert(ss[i]-'a',i);
	for(int i=2;i<=tt;i++)sa[i].pre->rd++;
	deal();
	int n;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%s",ss+1);
		len=strlen(ss+1);
		for(int i=len+1;i<=2*len-1;i++)ss[i]=ss[i-len];
		len=2*len-1;ss[len+1]=0;
		long long ans=solve();
		printf("%I64d\n",ans);
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;
char ss[4000010];
int len,tt,dt;
long long ans;
unsigned long long has[4000100],powr[4000100],base=13331;
int head[2000100],nxt[4001000],vis[2000100],tot,tim,mod=1000007;unsigned long long val[4000100];
struct SAM
{
	SAM *son[27],*pre;
	int len,siz,rd,pan;
}sa[3000100],*root,*last,*sd[3000100];
bool exist(unsigned long long x)
{
	int u=x%mod;
	if(vis[u]!=tim)vis[u]=tim,head[u]=0;
	for(int i=head[u];i;i=nxt[i])
	{
		if(val[i]==x)
		return true;
	}
	tot++;nxt[tot]=head[u],val[tot]=x,head[u]=tot;
	return false;
}
void insert(char c,int len)
{
	SAM *np=&sa[++tt],*p=last;last=np;
	np->len=len;np->siz=1;
	while(p&&!p->son[c])p->son[c]=np,p=p->pre;
	if(!p){np->pre=root;return;}
	SAM *q=p->son[c];
	if(q->len==p->len+1)
	np->pre=q;
	else
	{
		SAM *nq=&sa[++tt];*nq=*q;              // �½��ڵ��ʱ��һ��ҪС��,�ĵ��ǽڵ㱾�������ָ��
		nq->len=p->len+1;nq->siz=0;
		np->pre=q->pre=nq;
		while(p&&p->son[c]==q)p->son[c]=nq,p=p->pre;
	}
}
void deal()
{
	for(int i=1;i<=tt;i++)if(!sa[i].rd)sd[++dt]=&sa[i];
	while(dt)
	{
		SAM *qr=sd[dt--];
		if(qr==root)return;
		qr->pre->siz+=qr->siz;
		qr->pre->rd--;
		if(!qr->pre->rd)sd[++dt]=qr->pre;
	}
}
long long solve()
{
	int lenr=(len+1)/2;
	SAM *now=root;int lennow=0;
	long long ans=0;
	tim++;tot=0;
	for(int i=1;i<=len;i++)
	{
		has[i]=has[i-1]*base+ss[i];
		while(now&&!now->son[ss[i]-'a'])
		{
			now=now->pre;
			if(now)
			lennow=min(lennow,now->len);
		}
		if(!now)now=root,lennow=0;
		else now=now->son[ss[i]-'a'],lennow++;
		if(lennow>=lenr)
		{
			while(now->pre->len>=lenr)now=now->pre;
			if(now->pan!=tim)now->pan=tim,ans+=now->siz;
			lennow=min(lennow,now->len);
		}
	}
	return ans;
}
int main()
{
	scanf("%s",ss+1);
	len=strlen(ss+1);powr[0]=1;
	for(int i=1;i<=2000000;i++)powr[i]=powr[i-1]*base;
	root=last=&sa[++tt];
	for(int i=1;i<=len;i++)
	insert(ss[i]-'a',i);
	for(int i=2;i<=tt;i++)sa[i].pre->rd++;
	deal();
	int n;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%s",ss+1);
		len=strlen(ss+1);
		for(int i=len+1;i<=2*len-1;i++)ss[i]=ss[i-len];
		len=2*len-1;ss[len+1]=0;
		long long ans=solve();
		printf("%I64d\n",ans);
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;
char ss[4000010];
int len,tt,dt;
long long ans;
unsigned long long has[4000100],powr[4000100],base=13331;
int head[2000100],nxt[4001000],vis[2000100],tot,tim,mod=1000007;unsigned long long val[4000100];
struct SAM
{
	SAM *son[27],*pre;
	int len,siz,rd;
}sa[4000100],*root,*last,*sd[4000100];
bool exist(unsigned long long x)
{
	int u=x%mod;
	if(vis[u]!=tim)vis[u]=tim,head[u]=0;
	for(int i=head[u];i;i=nxt[i])
	{
		if(val[i]==x)
		return true;
	}
	tot++;nxt[tot]=head[u],val[tot]=x,head[u]=tot;
	return false;
}
void insert(char c,int len)
{
	SAM *np=&sa[++tt],*p=last;last=np;
	np->len=len;np->siz=1;
	while(p&&!p->son[c])p->son[c]=np,p=p->pre;
	if(!p){np->pre=root;return;}
	SAM *q=p->son[c];
	if(q->len==p->len+1)
	np->pre=q;
	else
	{
		SAM *nq=&sa[++tt];*nq=*q;              // �½��ڵ��ʱ��һ��ҪС��,�ĵ��ǽڵ㱾�������ָ��
		nq->len=p->len+1;nq->siz=0;
		np->pre=q->pre=nq;
		while(p&&p->son[c]==q)p->son[c]=nq,p=p->pre;
	}
}
void deal()
{
	for(int i=1;i<=tt;i++)if(!sa[i].rd)sd[++dt]=&sa[i];
	while(dt)
	{
		SAM *qr=sd[dt--];
		if(qr==root)return;
		qr->pre->siz+=qr->siz;
		qr->pre->rd--;
		if(!qr->pre->rd)sd[++dt]=qr->pre;
	}
}
long long solve()
{
	int lenr=(len+1)/2;
	SAM *now=root;int lennow=0;
	long long ans=0;
	tim++;tot=0;
	for(int i=1;i<=len;i++)
	{
		has[i]=has[i-1]*base+ss[i];
//		cout<<" "<<lennow<<endl;
		while(now&&!now->son[ss[i]-'a'])now=now->pre,lennow=min(lennow,now->len);
		if(!now)now=root;
		else now=now->son[ss[i]-'a'],lennow++;
//		cout<<i<<" "<<lennow<<endl;
		if(lennow==lenr)
		{
			SAM *t=now;
//			while(t)cout<<"B "<<t->len<<" ",t=t->pre;cout<<endl;
			while(now->pre->len>=lennow)now=now->pre;
//			cout<<now->siz<<endl;
			if(!exist(has[i]-has[i-lenr]*powr[lenr]))
			ans+=now->siz;
			lennow=lenr-1;
			if(now->pre->len==lennow)now=now->pre;
		}
	}
	return ans;
}
int main()
{
	scanf("%s",ss+1);
	len=strlen(ss+1);powr[0]=1;
	for(int i=1;i<=1000000;i++)powr[i]=powr[i-1]*base;
	root=last=&sa[++tt];
	for(int i=1;i<=len;i++)
	insert(ss[i]-'a',i);
	for(int i=2;i<=tt;i++)sa[i].pre->rd++;
	deal();
	int n;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%s",ss+1);
		len=strlen(ss+1);
		for(int i=len+1;i<=2*len-1;i++)ss[i]=ss[i-len];
		len=2*len-1;ss[len+1]=0;
		long long ans=solve();
		printf("%I64d\n",ans);
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;
char ss[2000010];
int len,tt,dt;
long long ans;
unsigned long long has[2000100],powr[2000100],base=13331;
int head[1000100],nxt[2001000],vis[1000100],tot,tim,mod=1000007;unsigned long long val[2000100];
struct SAM
{
	SAM *son[27],*pre;
	int len,siz,rd;
}sa[2000100],*root,*last,*sd[2000100];
bool exist(unsigned long long x)
{
	int u=x%mod;
	if(vis[u]!=tim)vis[u]=tim,head[u]=0;
	for(int i=head[u];i;i=nxt[i])
	{
		if(val[i]==x)
		return true;
	}
	tot++;nxt[tot]=head[u],val[tot]=x,head[u]=tot;
	return false;
}
void insert(char c,int len)
{
	SAM *np=&sa[++tt],*p=last;last=np;
	np->len=len;np->siz=1;
	while(p&&!p->son[c])p->son[c]=np,p=p->pre;
	if(!p){np->pre=root;return;}
	SAM *q=p->son[c];
	if(q->len==p->len+1)
	np->pre=q;
	else
	{
		SAM *nq=&sa[++tt];*nq=*q;              // �½��ڵ��ʱ��һ��ҪС��,�ĵ��ǽڵ㱾�������ָ��
		nq->len=p->len+1;nq->siz=0;
		np->pre=q->pre=nq;
		while(p&&p->son[c]==q)p->son[c]=nq,p=p->pre;
	}
}
void deal()
{
	for(int i=1;i<=tt;i++)if(!sa[i].rd)sd[++dt]=&sa[i];
	while(dt)
	{
		SAM *qr=sd[dt--];
		if(qr==root)return;
		qr->pre->siz+=qr->siz;
		qr->pre->rd--;
		if(!qr->pre->rd)sd[++dt]=qr->pre;
	}
}
long long solve()
{
	int lenr=(len+1)/2;
	SAM *now=root;int lennow=0;
	long long ans=0;
	tim++;tot=0;
	for(int i=1;i<=len;i++)
	{
		has[i]=has[i-1]*base+ss[i];
//		cout<<" "<<lennow<<endl;
		while(now&&!now->son[ss[i]-'a'])now=now->pre,lennow=min(lennow,now->len);
		if(!now)now=root;
		else now=now->son[ss[i]-'a'],lennow++;
//		cout<<i<<" "<<lennow<<endl;
		if(lennow==lenr)
		{
			SAM *t=now;
//			while(t)cout<<"B "<<t->len<<" ",t=t->pre;cout<<endl;
			while(now->pre->len>=lennow)now=now->pre;
//			cout<<now->siz<<endl;
			if(!exist(has[i]-has[i-lenr]*powr[lenr]))
			ans+=now->siz;
			lennow=lenr-1;
			if(now->pre->len==lennow)now=now->pre;
		}
	}
	return ans;
}
int main()
{
	scanf("%s",ss+1);
	len=strlen(ss+1);powr[0]=1;
	for(int i=1;i<=1000000;i++)powr[i]=powr[i-1]*base;
	root=last=&sa[++tt];
	for(int i=1;i<=len;i++)
	insert(ss[i]-'a',i);
	for(int i=2;i<=tt;i++)sa[i].pre->rd++;
	deal();
	int n;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%s",ss+1);
		len=strlen(ss+1);
		for(int i=len+1;i<=2*len-1;i++)ss[i]=ss[i-len];
		len=2*len-1;ss[len+1]=0;
		long long ans=solve();
		printf("%I64d\n",ans);
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;
char ss[4000010];
int len,tt,dt;
long long ans;
unsigned long long has[4000100],powr[4000100],base=13331;
int head[2000100],nxt[4001000],vis[2000100],tot,tim,mod=1000007;unsigned long long val[4000100];
struct SAM
{
	SAM *son[27],*pre;
	int len,siz,rd;
}sa[3900100],*root,*last,*sd[4000100];
bool exist(unsigned long long x)
{
	int u=x%mod;
	if(vis[u]!=tim)vis[u]=tim,head[u]=0;
	for(int i=head[u];i;i=nxt[i])
	{
		if(val[i]==x)
		return true;
	}
	tot++;nxt[tot]=head[u],val[tot]=x,head[u]=tot;
	return false;
}
void insert(char c,int len)
{
	SAM *np=&sa[++tt],*p=last;last=np;
	np->len=len;np->siz=1;
	while(p&&!p->son[c])p->son[c]=np,p=p->pre;
	if(!p){np->pre=root;return;}
	SAM *q=p->son[c];
	if(q->len==p->len+1)
	np->pre=q;
	else
	{
		SAM *nq=&sa[++tt];*nq=*q;              // �½��ڵ��ʱ��һ��ҪС��,�ĵ��ǽڵ㱾�������ָ��
		nq->len=p->len+1;nq->siz=0;
		np->pre=q->pre=nq;
		while(p&&p->son[c]==q)p->son[c]=nq,p=p->pre;
	}
}
void deal()
{
	for(int i=1;i<=tt;i++)if(!sa[i].rd)sd[++dt]=&sa[i];
	while(dt)
	{
		SAM *qr=sd[dt--];
		if(qr==root)return;
		qr->pre->siz+=qr->siz;
		qr->pre->rd--;
		if(!qr->pre->rd)sd[++dt]=qr->pre;
	}
}
long long solve()
{
	int lenr=(len+1)/2;
	SAM *now=root;int lennow=0;
	long long ans=0;
	tim++;tot=0;
	for(int i=1;i<=len;i++)
	{
		has[i]=has[i-1]*base+ss[i];
//		cout<<" "<<lennow<<endl;
		while(now&&!now->son[ss[i]-'a'])now=now->pre,lennow=min(lennow,now->len);
		if(!now)now=root;
		else now=now->son[ss[i]-'a'],lennow++;
//		cout<<i<<" "<<lennow<<endl;
		if(lennow==lenr)
		{
			SAM *t=now;
//			while(t)cout<<"B "<<t->len<<" ",t=t->pre;cout<<endl;
			while(now->pre->len>=lennow)now=now->pre;
//			cout<<now->siz<<endl;
			if(!exist(has[i]-has[i-lenr]*powr[lenr]))
			ans+=now->siz;
			lennow=lenr-1;
			if(now->pre->len==lennow)now=now->pre;
		}
	}
	return ans;
}
int main()
{
	scanf("%s",ss+1);
	len=strlen(ss+1);powr[0]=1;
	for(int i=1;i<=1000000;i++)powr[i]=powr[i-1]*base;
	root=last=&sa[++tt];
	for(int i=1;i<=len;i++)
	insert(ss[i]-'a',i);
	for(int i=2;i<=tt;i++)sa[i].pre->rd++;
	deal();
	int n;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%s",ss+1);
		len=strlen(ss+1);
		for(int i=len+1;i<=2*len-1;i++)ss[i]=ss[i-len];
		len=2*len-1;ss[len+1]=0;
		long long ans=solve();
		printf("%I64d\n",ans);
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;
char ss[4000010];
int len,tt,dt;
long long ans;
unsigned long long has[4000100],powr[4000100],base=13331;
int head[2000100],nxt[4001000],vis[2000100],tot,tim,mod=1000007;unsigned long long val[4000100];
struct SAM
{
	SAM *son[27],*pre;
	int len,siz,rd;
}sa[3000100],*root,*last,*sd[3000100];
bool exist(unsigned long long x)
{
	int u=x%mod;
	if(vis[u]!=tim)vis[u]=tim,head[u]=0;
	for(int i=head[u];i;i=nxt[i])
	{
		if(val[i]==x)
		return true;
	}
	tot++;nxt[tot]=head[u],val[tot]=x,head[u]=tot;
	return false;
}
void insert(char c,int len)
{
	SAM *np=&sa[++tt],*p=last;last=np;
	np->len=len;np->siz=1;
	while(p&&!p->son[c])p->son[c]=np,p=p->pre;
	if(!p){np->pre=root;return;}
	SAM *q=p->son[c];
	if(q->len==p->len+1)
	np->pre=q;
	else
	{
		SAM *nq=&sa[++tt];*nq=*q;              // �½��ڵ��ʱ��һ��ҪС��,�ĵ��ǽڵ㱾�������ָ��
		nq->len=p->len+1;nq->siz=0;
		np->pre=q->pre=nq;
		while(p&&p->son[c]==q)p->son[c]=nq,p=p->pre;
	}
}
void deal()
{
	for(int i=1;i<=tt;i++)if(!sa[i].rd)sd[++dt]=&sa[i];
	while(dt)
	{
		SAM *qr=sd[dt--];
		if(qr==root)return;
		qr->pre->siz+=qr->siz;
		qr->pre->rd--;
		if(!qr->pre->rd)sd[++dt]=qr->pre;
	}
}
long long solve()
{
	int lenr=(len+1)/2;
	SAM *now=root;int lennow=0;
	long long ans=0;
	tim++;tot=0;
	for(int i=1;i<=len;i++)
	{
		has[i]=has[i-1]*base+ss[i];
		while(now&&!now->son[ss[i]-'a'])
		{
			now=now->pre;
			if(now)
			lennow=min(lennow,now->len);
		}
		if(!now)now=root,lennow=0;
		else now=now->son[ss[i]-'a'],lennow++;
//		cout<<i<<" "<<lennow<<endl;
		if(lennow==lenr)
		{
//			cout<<"A"<<endl;
			while(now->pre->len>=lennow)now=now->pre;
			if(!exist(has[i]-has[i-lenr]*powr[lenr]))
			ans+=now->siz;
			lennow=lenr-1;
			if(now->pre->len==lennow)now=now->pre;
		}
	}
	return ans;
}
int main()
{
	scanf("%s",ss+1);
	len=strlen(ss+1);powr[0]=1;
	for(int i=1;i<=2000000;i++)powr[i]=powr[i-1]*base;
	root=last=&sa[++tt];
	for(int i=1;i<=len;i++)
	insert(ss[i]-'a',i);
	for(int i=2;i<=tt;i++)sa[i].pre->rd++;
	deal();
	int n;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%s",ss+1);
		len=strlen(ss+1);
		for(int i=len+1;i<=2*len-1;i++)ss[i]=ss[i-len];
		len=2*len-1;ss[len+1]=0;
		long long ans=solve();
		printf("%I64d\n",ans);
	}
	return 0;
}


// sync
#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));
}