#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> p2;
ll a[5005],b[5005];
vector<ll> func(string s){
    ll n=s.length();
    vector<ll> z(n,0);
    for(ll i=1,l=0,r=0;i<n;i++){
        if(i<=r)
        z[i]=min(z[i-l],r-i+1);
        while(i+z[i]<n&&s[z[i]]==s[i+z[i]])
        z[i]++;
        if(i+z[i]-1>r){
            l=i;
            r=i+z[i]-1;
        }
    }
    return z;
}
void func(string s,ll a[]){
    ll n=s.length();
    for(ll i=0;i<n;i++){
        vector<ll> z=func(s.substr(i,n-i));
        for(ll j=i+1;j<n;j++){
            a[i]=max(a[i],z[j-i]);
            a[j]=max(a[j],z[j-i]);
        }
    }
}
int main(){
    string s1,s2;
    cin>>s1>>s2;
    ll n=s1.length(),m=s2.length();
    func(s1,a);
    func(s2,b);
    ll res=5005;
    for(ll i=0;i<n;i++){
        vector<ll> z=func(s1.substr(i,n-i)+"$"+s2);
        for(ll j=0;j<m;j++)
        if(z[n-i+j+1]>max(a[i],b[j]))
        res=min(res,max(a[i],b[j])+1);
    }
    cout<<(res==5005?-1:res);
}


#include <bits/stdc++.h>

#define inf 0x3f3f3f3f

#define INF 0x3f3f3f3f3f3f3f3fLL

#define mod 1000000007

#define pb push_back

#define sq(x) ((x)*(x))

#define sz(a) ((int)a.size())

#define x first

#define y second

#define eps 1e-8

#define bpt(x) (__builtin_popcount(x))

#define bptl(x) (__builtin_popcountll(x))

#define bit(x, y) (((x)>>(y))&1)

#define bclz(x) (__builtin_clz(x))

#define bclzl(x) (__builtin_clzll(x))

#define bctz(x) (__builtin_ctz(x))

#define bctzl(x) (__builtin_ctzll(x))

#define rands(n) (rand()%n+1)

#define rand0(n) (rands(n)-1)

#define Rands(n) ((INT)rand()*rand()%n+1)

#define Rand0(n) (Rands(n)-1)

#define PQ priority_queue<pii, vector<pii>, greater<pii> >

#define rep(i, a, b) for(int i=a; i<b; i++)

using namespace std;

typedef long long INT;

typedef vector<int> VI;

typedef pair<int, int> pii;

typedef pair<INT, INT> PII;

typedef pair<pii, int> ppi;

typedef double DO;

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

template<typename T, typename U> inline void smax(T &a, 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 T>inline void print(T x) {if (x<0) {putchar('-');return print(-x);}if (x<10) {putchar('0'+x);return;} print(x/10);putchar(x%10+'0');}

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;

}

#if ( ( _WIN32 || __WIN32__ ) && __cplusplus < 201103L)

    #define lld "%I64d"

#else

    #define lld "%lld"

#endif

#define NN 1000011

int sz, ed;

int runs;

int vst[NN*2];

int cnt[NN], ord[NN*2];

struct node{

	int deep, fail, cnt, nxt[27], ter, val;

	void clear() {

		memset(nxt, -1, sizeof nxt);

		fail=0; deep=0; cnt=0; ter=0;

	}

} mem[NN*2];

struct automaton{

	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;

		return ;

	}

	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].val = 0;

			mem[p2].deep=mem[ed].deep+1;

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

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

				mem[ed].nxt[c]=p2;

				ed=mem[ed].fail;

			}

		}

		ed=p;

		return ;

	}

	void build() {

		for(int i=0; i<NN; i++) cnt[i]=0;

		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-2; i >= 0; i--) {

			int cur=ord[i];

			int prv=mem[cur].fail;

			if(prv == -1) continue;

			mem[prv].val += mem[cur].val;

		}

		return ;

	}

	int solve(char *s) {

		++runs;

		int ans = 0;

		int n = strlen(s), p = 1, dp=0;

		int f;

		for(int i=0; i<(n<<1)-1; i++) {

			if(i>=n) s[i] = s[i-n];

			if(mem[p].nxt[s[i]-'a'] == -1) {

				while(mem[p].nxt[s[i]-'a'] == -1) {

					p = mem[p].fail;

				}

				dp = mem[p].deep + 1;

				p = mem[p].nxt[s[i]-'a'];

			}

			else {

				p = mem[p].nxt[s[i]-'a'];

				dp ++ ;

				if(dp == n) {

					if(vst[p] != runs) vst[p] = runs, ans += mem[p].val;

					f = mem[p].fail;

					if(mem[f].deep == n-1) p = f;

					dp --;

				}

			}

		}

		return ans;

	}

} sam;

char s[NN*2];

int main() {

#ifndef ONLINE_JUDGE

	freopen("in.in", "r", stdin);

	freopen("out.out", "w", stdout);

	int _time=clock();

#endif

	scanf("%s", s);

	sam.init();

	int n = strlen(s);

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

	sam.build();

	cin >> n;

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

		scanf("%s", s);

		printf("%d\n", sam.solve(s));

	}

#ifndef ONLINE_JUDGE

	cerr<<(int)(clock()-_time)<<endl;

#endif

	return 0;

}


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

#define nn 2000010
using namespace std;

char str[nn],s[nn];
int N,par[nn],go[nn][26],Max[nn],lst,b[nn],n,sz[nn],mark[nn];

inline void Ins(int x)
{
	int np=++N,p=lst;
	Max[np]=Max[lst]+1;
	while (!go[p][x]&&p) go[p][x]=np,p=par[p];

	if (!p) par[np]=1;
	else{
		int q=go[p][x];
		if (Max[q]==Max[p]+1) par[np]=q;
		else{
			int nq=++N;
			par[nq]=par[q];
			Max[nq]=Max[p]+1;
			par[q]=par[np]=nq;

			for (int i=0;i<26;i++) go[nq][i]=go[q][i];
			while (p&&go[p][x]==q) go[p][x]=nq,p=par[p];
		}
	}
	sz[lst=np]+=1;
}

inline void Kmp(int len)
{
	int j=0;
	for (int i=1;i<=len;i++) b[i]=0;
	for (int i=2;i<=len;i++)
	{
		while (j&&s[j+1]!=s[i]) j=b[j];
		if (s[j+1]==s[i]) j++;b[i]=j;
	}
}

int h[nn],T;
bool cmp(int a,int b) {return(Max[a]>Max[b]);}
int main()
{

	scanf("%s",str);
	int lstr=strlen(str);N=lst=1;
	for (int i=0;i<lstr;i++) Ins(str[i]-'a');
	for (int i=2;i<=N;i++) h[i]=i;

	sort(h+2,h+1+N,cmp);
	for (int i=2;i<=N;i++) sz[par[h[i]]]+=sz[h[i]];
	sz[1]=0;

	scanf("%d",&n);
	for (int t=1;t<=n;t++)
	{
		//printf("%d ",t);
		if (t==830)
		int deb=0;

		scanf("%s",s+1);T++;
		int lens=strlen(s+1),cir=lens;
		for (int i=1;i<=lens;i++) s[i+lens]=s[i];lens*=2;
		//Kmp(lens);

		int p=1,l=0;long long ans=0;
		for (int i=1;i<=lens;i++)
		{
			int x=s[i]-'a';
			if (go[p][x]) p=go[p][x],l++;
			else{
				while (p&&!go[p][x]) p=par[p];
				if (p>1) l=Max[p]+1,p=go[p][x];
				else p=1,l=0;
			}
			if (l>=cir){
				while (par[p]>1&&Max[par[p]]>=cir)
				p=par[p],l=Max[p];
				if (mark[p]!=T) ans+=sz[p],mark[p]=T;
			}
		}

		printf("%I64d\n",ans);
	}
}


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

#define nn 2000010
using namespace std;

char str[nn],s[nn];
int N,par[nn],go[nn][26],Max[nn],lst,b[nn],n,sz[nn],mark[nn];

inline void Ins(int x)
{
	int np=++N,p=lst;
	Max[np]=Max[lst]+1;
	while (!go[p][x]&&p) go[p][x]=np,p=par[p];

	if (!p) par[np]=1;
	else{
		int q=go[p][x];
		if (Max[q]==Max[p]+1) par[np]=q;
		else{
			int nq=++N;
			par[nq]=par[q];
			Max[nq]=Max[p]+1;
			par[q]=par[np]=nq;

			for (int i=0;i<26;i++) go[nq][i]=go[q][i];
			while (p&&go[p][x]==q) go[p][x]=nq,p=par[p];
		}
	}
	sz[lst=np]+=1;
}

int h[nn],T;
bool cmp(int a,int b) {return(Max[a]>Max[b]);}
int main()
{

	scanf("%s",str);
	int lstr=strlen(str);N=lst=1;
	for (int i=0;i<lstr;i++) Ins(str[i]-'a');
	for (int i=2;i<=N;i++) h[i]=i;

	sort(h+2,h+1+N,cmp);
	for (int i=2;i<=N;i++) sz[par[h[i]]]+=sz[h[i]];
	sz[1]=0;

	scanf("%d",&n);
	for (int t=1;t<=n;t++)
	{
		//printf("%d ",t);
		if (t==830)
		int deb=0;

		scanf("%s",s+1);T++;
		int lens=strlen(s+1),cir=lens;

		if (!lens) {puts("1");continue;}

		for (int i=1;i<=lens;i++) s[i+lens]=s[i];lens*=2;
		//Kmp(lens);

		int p=1,l=0;long long ans=0;
		for (int i=1;i<=lens;i++)
		{
			int x=s[i]-'a';
			if (go[p][x]) p=go[p][x],l++;
			else{
				while (p&&!go[p][x]) p=par[p];
				if (p>1) l=Max[p]+1,p=go[p][x];
				else p=1,l=0;
			}
			if (l>=cir){
				while (par[p]>1&&Max[par[p]]>=cir)
				p=par[p],l=Max[p];
				if (mark[p]!=T) ans+=sz[p],mark[p]=T;
			}
		}

		printf("%I64d\n",ans);
	}
}


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

#define nn 2000010
using namespace std;

char str[nn],s[nn];
int N,par[nn],go[nn][26],Max[nn],lst,b[nn],n,sz[nn],mark[nn];

inline void Ins(int x)
{
	int np=++N,p=lst;
	Max[np]=Max[lst]+1;
	while (!go[p][x]&&p) go[p][x]=np,p=par[p];

	if (!p) par[np]=1;
	else{
		int q=go[p][x];
		if (Max[q]==Max[p]+1) par[np]=q;
		else{
			int nq=++N;
			par[nq]=par[q];
			Max[nq]=Max[p]+1;
			par[q]=par[np]=nq;

			for (int i=0;i<26;i++) go[nq][i]=go[q][i];
			while (p&&go[p][x]==q) go[p][x]=nq,p=par[p];
		}
	}
	sz[lst=np]+=1;
}

int h[nn],T;
bool cmp(int a,int b) {return(Max[a]>Max[b]);}
int main()
{

	scanf("%s",str);
	int lstr=strlen(str);N=lst=1;
	for (int i=0;i<lstr;i++) Ins(str[i]-'a');
	for (int i=2;i<=N;i++) h[i]=i;

	sort(h+2,h+1+N,cmp);
	for (int i=2;i<=N;i++) sz[par[h[i]]]+=sz[h[i]];
	sz[1]=0;

	scanf("%d",&n);
	for (int t=1;t<=n;t++)
	{
		scanf("%s",s+1);T++;
		int lens=strlen(s+1),cir=lens;

		if (!lens) {puts("1");continue;}
		for (int i=1;i<=lens;i++) s[i+lens]=s[i];lens*=2;

		int p=1,l=0;long long ans=0;
		for (int i=1;i<=lens;i++)
		{
			int x=s[i]-'a';
			if (go[p][x]) p=go[p][x],l++;
			else{
				while (p&&!go[p][x]) p=par[p];
				if (p>1) l=Max[p]+1,p=go[p][x];
				else p=1,l=0;
			}
			if (l>=cir){
				while (par[p]>1&&Max[par[p]]>=cir)
				p=par[p],l=Max[p];
				if (mark[p]!=T) ans+=sz[p],mark[p]=T;
			}
		}

		for (int i=1;i<=lens;i++) s[i]=0;
		printf("%I64d\n",ans);
	}
}


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

#define nn 2000010
using namespace std;

char str[nn],s[nn];
int N,par[nn],go[nn][26],Max[nn],lst,b[nn],n,sz[nn];

inline void Ins(int x)
{
	int np=++N,p=lst;
	Max[np]=Max[lst]+1;
	while (!go[p][x]&&p) go[p][x]=np,p=par[p];

	if (!p) par[np]=1;
	else{
		int q=go[p][x];
		if (Max[q]==Max[p]+1) par[np]=q;
		else{
			int nq=++N;
			par[nq]=par[q];
			Max[nq]=Max[p]+1;
			par[q]=par[np]=nq;

			for (int i=0;i<26;i++) go[nq][i]=go[q][i];
			while (p&&go[p][x]==q) go[p][x]=nq,p=par[p];
		}
	}
	sz[lst=np]+=1;
}

inline void Kmp(int len)
{
	int j=0;
	for (int i=1;i<=len;i++) b[i]=0;
	for (int i=2;i<=len;i++)
	{
		while (j&&s[j+1]!=s[i]) j=b[j];
		if (s[j+1]==s[i]) j++;b[i]=j;
	}
}

int h[nn];
bool cmp(int a,int b) {return(Max[a]>Max[b]);}
int main()
{
	freopen("1.in","r",stdin);
	freopen("1.out","w",stdout);

	scanf("%s",str);
	int lstr=strlen(str);N=lst=1;
	for (int i=0;i<lstr;i++) Ins(str[i]-'a');
	for (int i=2;i<=N;i++) h[i]=i;

	sort(h+2,h+1+N,cmp);
	for (int i=2;i<=N;i++) sz[par[h[i]]]+=sz[h[i]];
	sz[1]=0;

	scanf("%d",&n);
	while (n--)
	{
		scanf("%s",s+1);
		int lens=strlen(s+1),cir=lens;
		for (int i=1;i<=lens;i++) s[i+lens]=s[i];lens*=2;
		Kmp(lens);

		int p=1,l=0;long long ans=0;
		for (int i=1;i<=lens&&b[i]<cir;i++)
		{
			int x=s[i]-'a';
			if (go[p][x]) p=go[p][x],l++;
			else{
				while (p&&!go[p][x]) p=par[p];
				if (p>1) l=Max[p]+1,p=go[p][x];
				else p=1,l=0;
			}
			if (l>=cir){
				while (par[p]>1&&Max[par[p]]>=cir)
				p=par[p],l=Max[p];
				ans+=sz[p];
			}
		}

		printf("%I64d\n",ans);
	}
}


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

#define nn 1000010
using namespace std;

char str[nn],s[nn];
int N,par[nn],go[nn][26],Max[nn],lst,b[nn],n,sz[nn];

inline void Ins(int x)
{
	int np=++N,p=lst;
	Max[np]=Max[lst]+1;
	while (!go[p][x]&&p) go[p][x]=np,p=par[p];

	if (!p) par[np]=1;
	else{
		int q=go[p][x];
		if (Max[q]==Max[p]+1) par[np]=q;
		else{
			int nq=++N;
			par[nq]=par[q];
			Max[nq]=Max[p]+1;
			par[q]=par[np]=nq;

			for (int i=0;i<26;i++) go[nq][i]=go[q][i];
			while (p&&go[p][x]==q) go[p][x]=nq,p=par[p];
		}
	}
	sz[lst=np]+=1;
}

inline void Kmp(int len)
{
	int j=0;
	for (int i=1;i<=len;i++) b[i]=0;
	for (int i=2;i<=len;i++)
	{
		while (j&&s[j+1]!=s[i]) j=b[j];
		if (s[j+1]==s[i]) j++;b[i]=j;
	}
}

int h[nn];
bool cmp(int a,int b) {return(Max[a]>Max[b]);}
int main()
{
	scanf("%s",str);
	int lstr=strlen(str);N=lst=1;
	for (int i=0;i<lstr;i++) Ins(str[i]-'a');
	for (int i=2;i<=N;i++) h[i]=i;

	sort(h+2,h+1+N,cmp);
	for (int i=2;i<=N;i++) sz[par[h[i]]]+=sz[h[i]];

	scanf("%d",&n);
	while (n--)
	{
		scanf("%s",s+1);
		int lens=strlen(s+1),cir=lens;
		for (int i=1;i<=lens;i++) s[i+lens]=s[i];lens*=2;
		Kmp(lens);

		int p=1,l=0;long long ans=0;
		for (int i=1;i<=lens&&b[i]<cir;i++)
		{
			int x=s[i]-'a';
			if (go[p][x]) p=go[p][x],l++;
			else{

				while (p&&!go[p][x]) p=par[p];
				if (p) l=Max[p]+1,p=go[p][x];
				else p=1,l=0;
			}
			if (l>=cir) ans+=sz[p];
		}

		printf("%I64d\n",ans);
	}
}


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

#define nn 2000010
using namespace std;

char str[nn],s[nn];
int N,par[nn],go[nn][26],Max[nn],lst,b[nn],n,sz[nn];

inline void Ins(int x)
{
	int np=++N,p=lst;
	Max[np]=Max[lst]+1;
	while (!go[p][x]&&p) go[p][x]=np,p=par[p];

	if (!p) par[np]=1;
	else{
		int q=go[p][x];
		if (Max[q]==Max[p]+1) par[np]=q;
		else{
			int nq=++N;
			par[nq]=par[q];
			Max[nq]=Max[p]+1;
			par[q]=par[np]=nq;

			for (int i=0;i<26;i++) go[nq][i]=go[q][i];
			while (p&&go[p][x]==q) go[p][x]=nq,p=par[p];
		}
	}
	sz[lst=np]+=1;
}

inline void Kmp(int len)
{
	int j=0;
	for (int i=1;i<=len;i++) b[i]=0;
	for (int i=2;i<=len;i++)
	{
		while (j&&s[j+1]!=s[i]) j=b[j];
		if (s[j+1]==s[i]) j++;b[i]=j;
	}
}

int h[nn];
bool cmp(int a,int b) {return(Max[a]>Max[b]);}
int main()
{
	scanf("%s",str);
	int lstr=strlen(str);N=lst=1;
	for (int i=0;i<lstr;i++) Ins(str[i]-'a');
	for (int i=2;i<=N;i++) h[i]=i;

	sort(h+2,h+1+N,cmp);
	for (int i=2;i<=N;i++) sz[par[h[i]]]+=sz[h[i]];

	scanf("%d",&n);
	while (n--)
	{
		scanf("%s",s+1);
		int lens=strlen(s+1),cir=lens;
		for (int i=1;i<=lens;i++) s[i+lens]=s[i];lens*=2;
		Kmp(lens);

		int p=1,l=0;long long ans=0;
		for (int i=1;i<=lens&&b[i]<cir;i++)
		{
			int x=s[i]-'a';
			if (go[p][x]) p=go[p][x],l++;
			else{

				while (p&&!go[p][x]) p=par[p];
				if (p>1) l=Max[p]+1,p=go[p][x];
				else p=1,l=0;
			}
			if (l>=cir){
				while (Max[par[p]]>=cir) p=par[p];
				ans+=sz[p];
			}
		}

		printf("%I64d\n",ans);
	}
}


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

#define nn 2000010
using namespace std;

char str[nn],s[nn];
int N,par[nn],go[nn][26],Max[nn],lst,b[nn],n,sz[nn];

inline void Ins(int x)
{
	int np=++N,p=lst;
	Max[np]=Max[lst]+1;
	while (!go[p][x]&&p) go[p][x]=np,p=par[p];

	if (!p) par[np]=1;
	else{
		int q=go[p][x];
		if (Max[q]==Max[p]+1) par[np]=q;
		else{
			int nq=++N;
			par[nq]=par[q];
			Max[nq]=Max[p]+1;
			par[q]=par[np]=nq;

			for (int i=0;i<26;i++) go[nq][i]=go[q][i];
			while (p&&go[p][x]==q) go[p][x]=nq,p=par[p];
		}
	}
	sz[lst=np]+=1;
}

inline void Kmp(int len)
{
	int j=0;
	for (int i=1;i<=len;i++) b[i]=0;
	for (int i=2;i<=len;i++)
	{
		while (j&&s[j+1]!=s[i]) j=b[j];
		if (s[j+1]==s[i]) j++;b[i]=j;
	}
}

int h[nn];
bool cmp(int a,int b) {return(Max[a]>Max[b]);}
int main()
{

	scanf("%s",str);
	int lstr=strlen(str);N=lst=1;
	for (int i=0;i<lstr;i++) Ins(str[i]-'a');
	for (int i=2;i<=N;i++) h[i]=i;

	sort(h+2,h+1+N,cmp);
	for (int i=2;i<=N;i++) sz[par[h[i]]]+=sz[h[i]];
	sz[1]=0;

	scanf("%d",&n);
	while (n--)
	{
		scanf("%s",s+1);
		int lens=strlen(s+1),cir=lens;
		for (int i=1;i<=lens;i++) s[i+lens]=s[i];lens*=2;
		Kmp(lens);

		int p=1,l=0;long long ans=0;
		for (int i=1;i<=lens&&b[i]<cir;i++)
		{
			int x=s[i]-'a';
			if (go[p][x]) p=go[p][x],l++;
			else{
				while (p&&!go[p][x]) p=par[p];
				if (p>1) l=Max[p]+1,p=go[p][x];
				else p=1,l=0;
			}
			if (l>=cir){
				while (par[p]>1&&Max[par[p]]>=cir)
				p=par[p],l=Max[p];
				ans+=sz[p];
			}
		}

		printf("%I64d\n",ans);
	}
}


#include <iostream>
#include <string.h>
#include <stdio.h>
#define maxn 2000100
#define Smaxn 26
using namespace std;
struct node
{
    node *par,*go[Smaxn];
    int flag;
    int num;
    int val;
}*root,*tail,que[maxn],*top[maxn];
int tot;
char str[maxn];
void add(int c,int l)
{
    node *p=tail,*np=&que[tot++];
    np->val=l;
    while(p&&p->go[c]==NULL)
    p->go[c]=np,p=p->par;
    if(p==NULL) np->par=root;
    else
    {
        node *q=p->go[c];
        if(p->val+1==q->val) np->par=q;
        else
        {
            node *nq=&que[tot++];
            *nq=*q;
            nq->val=p->val+1;
            np->par=q->par=nq;
            while(p&&p->go[c]==q) p->go[c]=nq,p=p->par;
        }
    }
    tail=np;
}
int c[maxn],len;
void init()
{
    len=1;
    tot=0;
    memset(que,0,sizeof(que));
    root=tail=&que[tot++];
}
char st[2000100];
void solve(int n)
{
    int i,j;
    memset(c,0,sizeof(c));
    for(i=0;i<tot;i++)
    c[que[i].val]++;
    for(i=1;i<len;i++)
    c[i]+=c[i-1];
    for(i=0;i<tot;i++)
    top[--c[que[i].val]]=&que[i];
    for(node *p=root;;p=p->go[str[p->val]-'a'])
    {
        p->num=1;
        if (p->val==len-1)break;
    }
    for(i=tot-1;i>=0;i--)
    {
        node *p=top[i];
        if(p->par)
        {
            p->par->num+=p->num;
        }
    }
    int tmp=0;
    node *p=root;
    for(i=1;i<=n;i++)
    {
        long long ans=0;
        scanf("%s",st);
        int l=strlen(st);
        memcpy(st+l,st,l);
        int ll=2*l;
        st[ll-1]='\0';
        for(j=0;j<ll-1;j++)
        {
            int x=st[j]-'a';
            if(p->go[x])
            {
                tmp++;
                p=p->go[x];
            }
            else
            {
                while(p&&p->go[x]==NULL)
                p=p->par;
                if(p)
                {
                    tmp=p->val+1;
                    p=p->go[x];
                }
                else
                {
                    tmp=0;
                    p=root;
                }
            }
            if(j>=l-1&&tmp>=l)
            {
                node *q=p;
                while(1)
                {
                    if(l>=q->par->val+1&&l<=q->val)
                    break;
                    q=q->par;
                }
                if(q->flag!=i)
                {
                    ans+=q->num;
                    q->flag=i;
                }
            }
        }
        printf("%I64d\n",ans);
    }
}
int main()
{
    freopen("dd.txt","r",stdin);
    scanf("%s",str);
    init();
    int i,l=strlen(str);
    for(i=0;i<l;i++)
    {
        add(str[i]-'a',len++);
    }
    int n;
    scanf("%d",&n);
    solve(n);
    return 0;
}


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

#define nn 10010
using namespace std;

char str[nn],s[nn];
int N,par[nn],go[nn][26],Max[nn],lst,b[nn],n,sz[nn];

inline void Ins(int x)
{
	int np=++N,p=lst;
	Max[np]=Max[lst]+1;
	while (!go[p][x]&&p) go[p][x]=np,p=par[p];

	if (!p) par[np]=1;
	else{
		int q=go[p][x];
		if (Max[q]==Max[p]+1) par[np]=q;
		else{
			int nq=++N;
			par[nq]=par[q];
			Max[nq]=Max[p]+1;
			par[q]=par[np]=nq;

			for (int i=0;i<26;i++) go[nq][i]=go[q][i];
			while (p&&go[p][x]==q) go[p][x]=nq,p=par[p];
		}
	}
	sz[lst=np]+=1;
}

inline void Kmp(int len)
{
	int j=0;
	for (int i=1;i<=len;i++) b[i]=0;
	for (int i=2;i<=len;i++)
	{
		while (j&&s[j+1]!=s[i]) j=b[j];
		if (s[j+1]==s[i]) j++;b[i]=j;
	}
}

int h[nn];
bool cmp(int a,int b) {return(Max[a]>Max[b]);}
int main()
{
	scanf("%s",str);
	int lstr=strlen(str);N=lst=1;
	for (int i=0;i<lstr;i++) Ins(str[i]-'a');
	for (int i=2;i<=N;i++) h[i]=i;

	sort(h+2,h+1+N,cmp);
	for (int i=2;i<=N;i++) sz[par[h[i]]]+=sz[h[i]];

	scanf("%d",&n);
	while (n--)
	{
		scanf("%s",s+1);
		int lens=strlen(s+1),cir=lens;
		for (int i=1;i<=lens;i++) s[i+lens]=s[i];lens*=2;
		Kmp(lens);

		int p=1,l=0;long long ans=0;
		for (int i=1;i<=lens&&b[i]<cir;i++)
		{
			int x=s[i]-'a';
			if (go[p][x]) p=go[p][x],l++;
			else{

				while (p&&!go[p][x]) p=par[p];
				if (p>1) l=Max[p]+1,p=go[p][x];
				else p=1,l=0;
			}
			if (l>=cir){
				while (Max[par[p]]>=cir) p=par[p];
				ans+=sz[p];
			}
		}

		printf("%I64d\n",ans);
	}
}


#include <iostream>
#include <string.h>
#include <stdio.h>
#define maxn 2000100
#define Smaxn 26
using namespace std;
struct node
{
    node *par,*go[Smaxn];
    int flag;
    int num;
    int val;
}*root,*tail,que[maxn],*top[maxn];
int tot;
char str[maxn];
void add(int c,int l)
{
    node *p=tail,*np=&que[tot++];
    np->val=l;
    while(p&&p->go[c]==NULL)
    p->go[c]=np,p=p->par;
    if(p==NULL) np->par=root;
    else
    {
        node *q=p->go[c];
        if(p->val+1==q->val) np->par=q;
        else
        {
            node *nq=&que[tot++];
            *nq=*q;
            nq->val=p->val+1;
            np->par=q->par=nq;
            while(p&&p->go[c]==q) p->go[c]=nq,p=p->par;
        }
    }
    tail=np;
}
int c[maxn],len;
void init()
{
    len=1;
    tot=0;
    memset(que,0,sizeof(que));
    root=tail=&que[tot++];
}
char st[2000100];
void solve(int n)
{
    int i,j;
    memset(c,0,sizeof(c));
    for(i=0;i<tot;i++)
    c[que[i].val]++;
    for(i=1;i<len;i++)
    c[i]+=c[i-1];
    for(i=0;i<tot;i++)
    top[--c[que[i].val]]=&que[i];
    for(node *p=root;;p=p->go[str[p->val]-'a'])
    {
        p->num=1;
        if (p->val==len-1)break;
    }
    for(i=tot-1;i>=0;i--)
    {
        node *p=top[i];
        if(p->par)
        {
            p->par->num+=p->num;
        }
    }
    int tmp=0;
    node *p=root;
    for(i=1;i<=n;i++)
    {
        long long ans=0;
        scanf("%s",st);
        int l=strlen(st);
        memcpy(st+l,st,l);
        int ll=2*l;
        st[ll-1]='\0';
        for(j=0;j<ll-1;j++)
        {
            int x=st[j]-'a';
            if(p->go[x])
            {
                tmp++;
                p=p->go[x];
            }
            else
            {
                while(p&&p->go[x]==NULL)
                p=p->par;
                if(p)
                {
                    tmp=p->val+1;
                    p=p->go[x];
                }
                else
                {
                    tmp=0;
                    p=root;
                }
            }
            if(j>=l-1&&tmp>=l)
            {
                node *q=p;
                while(1)
                {
                    if(l>=q->par->val+1&&l<=q->val)
                    break;
                    q=q->par;
                }
                if(q->flag!=i)
                {
                    ans+=q->num;
                    q->flag=i;
                }
            }
        }
        printf("%I64d\n",ans);
    }
}
int main()
{
    scanf("%s",str);
    init();
    int i,l=strlen(str);
    for(i=0;i<l;i++)
    {
        add(str[i]-'a',len++);
    }
    int n;
    scanf("%d",&n);
    solve(n);
    return 0;
}


#include<cstdio>
#include<cstring>
using namespace std;
const int NN=2001111;
int son[NN][26],pre[NN],val[NN],sum[NN],b[NN],f[NN],next[NN];
int m,TTT,last;
char s[NN];
void samadd(int x)
{
	int p=last,end=++TTT;
	last=end;
	val[end]=val[p]+1;
	for (;p!=-1&&!son[p][x];p=pre[p]) son[p][x]=end;
	if (p==-1) {pre[end]=0;return;}
	int q=son[p][x];
	if (val[q]==val[p]+1) {pre[end]=q;return;}
	int nq=++TTT;
	val[nq]=val[p]+1;
	pre[nq]=pre[q];
	pre[q]=pre[end]=nq;
	memcpy(son[nq],son[q],sizeof(son[q]));
	for (;p!=-1&&son[p][x]==q;p=pre[p]) son[p][x]=nq;
}
int main()
{
	scanf("%s\n",s+1);
	int len=strlen(s+1);
	pre[0]=-1;
	int i,j;
	for (i=1;i<=len;i++) samadd(s[i]-'a');
	for (i=1;i<=TTT;i++) sum[val[i]]++;
	for (i=2;i<=len;i++) sum[i]+=sum[i-1];
	for (i=TTT;i;i--) b[sum[val[i]]--]=i;
	int x=son[0][s[1]-'a'];
	for (i=1;i<=len;i++,x=son[x][s[i]-'a']) f[x]=1;
	for (i=TTT;i;i--) f[pre[b[i]]]+=f[b[i]];
	for (scanf("%d",&m);m;m--)
	{
		scanf("%s\n",s+1);
		len=strlen(s+1);
		next[1]=0,j=0;
		for (i=2;i<=len;i++)
		{
			while (s[j+1]!=s[i]&&j) j=next[j];
			if (s[j+1]==s[i]) j++;
			next[i]=j;
		}
		int t=len-next[len];
		if (len%t==0) t=len-next[len];
		else t=len;
		for (i=1;i<t;i++) s[i+len]=s[i];
		int ans=0,x=0,now=0;
		for (i=1;i<=len+t-1;i++)
		{
			int t=s[i]-'a';
			while (pre[x]!=-1&&!son[x][t])
				x=pre[x],now=val[x];
			if (son[x][t]) now++,x=son[x][t];
			if (now>len)
			{
				now--;
				if (val[pre[x]]>=now) x=pre[x];
			}
			if (now==len) ans+=f[x];
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include<cstdio>
#include<cstring>
using namespace std;
const int NN=2001111;
int son[NN][26],pre[NN],val[NN],sum[NN],b[NN],f[NN],next[NN];
int m,TTT,last;
char s[NN];
void samadd(int x)
{
	int p=last,end=++TTT;
	last=end;
	val[end]=val[p]+1;
	for (;p!=-1&&!son[p][x];p=pre[p]) son[p][x]=end;
	if (p==-1) {pre[end]=0;return;}
	int q=son[p][x];
	if (val[q]==val[p]+1) {pre[end]=q;return;}
	int nq=++TTT;
	val[nq]=val[p]+1;
	pre[nq]=pre[q];
	pre[q]=pre[end]=nq;
	memcpy(son[nq],son[q],sizeof(son[q]));
	for (;p!=-1&&son[p][x]==q;p=pre[p]) son[p][x]=nq;
}
int main()
{
	scanf("%s\n",s+1);
	int len=strlen(s+1);
	pre[0]=-1;
	int i,j;
	for (i=1;i<=len;i++) samadd(s[i]-'a');
	for (i=1;i<=TTT;i++) sum[val[i]]++;
	for (i=2;i<=len;i++) sum[i]+=sum[i-1];
	for (i=TTT;i;i--) b[sum[val[i]]--]=i;
	int x=son[0][s[1]-'a'];
	for (i=1;i<=len;i++,x=son[x][s[i]-'a']) f[x]=1;
	for (i=TTT;i;i--) f[pre[b[i]]]+=f[b[i]];
	for (scanf("%d",&m);m;m--)
	{
		scanf("%s\n",s+1);
		len=strlen(s+1);
		next[1]=0,j=0;
		for (i=2;i<=len;i++)
		{
			while (s[j+1]!=s[i]&&j) j=next[j];
			if (s[j+1]==s[i]) j++;
			next[i]=j;
		}
		int t=len-next[len];
		if (len%t==0) t=len-next[len];
		else t=len;
		for (i=1;i<t;i++) s[i+len]=s[i];
		int ans=0,x=0,now=0;
		for (i=1;i<=len+t-1;i++)
		{
			int t=s[i]-'a';
			if (son[x][t]) now++,x=son[x][t];
			else
			{
				while (x!=-1&&!son[x][t]) x=pre[x];
				if (x==-1) now=x=0;
				else now=val[x]+1,x=son[x][t];
			}
			if (now>len)
			{
				while (pre[x]!=-1&&val[pre[x]]>=len) x=pre[x];
				now=len;
			}
			if (now==len) ans+=f[x];
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include<cstdio>
#include<cstring>
using namespace std;
const int NN=2001111;
int son[NN][26],pre[NN],val[NN],sum[NN],b[NN],f[NN],next[NN];
int m,TTT,last;
char s[NN];
void samadd(int x)
{
	int p=last,end=++TTT;last=end;
	val[end]=val[p]+1;
	for (;p!=-1&&!son[p][x];p=pre[p]) son[p][x]=end;
	if (p==-1) {pre[end]=0;return;}
	int q=son[p][x];
	if (val[q]==val[p]+1) {pre[end]=q;return;}
	int nq=++TTT;
	val[nq]=val[p]+1;
	pre[nq]=pre[q];
	pre[q]=pre[end]=nq;
	memcpy(son[nq],son[q],sizeof(son[q]));
	for (;p!=-1&&son[p][x]==q;p=pre[p]) son[p][x]=nq;
}
int main()
{
	//freopen("1.in","r",stdin);freopen("1.out","w",stdout);
	scanf("%s\n",s+1);
	int len=strlen(s+1);
	pre[0]=-1;
	int i,j;
	for (i=1;i<=len;i++) samadd(s[i]-'a');
	for (i=1;i<=TTT;i++) sum[val[i]]++;
	for (i=2;i<=len;i++) sum[i]+=sum[i-1];
	for (i=TTT;i;i--) b[sum[val[i]]--]=i;
	int x=son[0][s[1]-'a'];
	for (i=1;i<=len;i++,x=son[x][s[i]-'a']) f[x]=1;
	for (i=TTT;i;i--) f[pre[b[i]]]+=f[b[i]];
	for (scanf("%d",&m);m;m--)
	{
		scanf("%s\n",s+1);
		len=strlen(s+1);
		next[1]=0,j=0;
		for (i=2;i<=len;i++)
		{
			while (s[j+1]!=s[i]&&j) j=next[j];
			if (s[j+1]==s[i]) j++;
			next[i]=j;
		}
		int t=len-next[len];
		if (len%t==0) t=len-next[len];
		else t=len;
		for (i=1;i<t;i++) s[i+len]=s[i];
		int ans=0,x=0,now=0;
		for (i=1;i<=len+t-1;i++)
		{
			int t=s[i]-'a';
			if (son[x][t]) now++,x=son[x][t];
			else
			{
				while (x!=-1&&!son[x][t]) x=pre[x];
				if (x==-1) x=0,now=0;
				else now=val[x]+1,x=son[x][t];
			}
			if (now>=len) ans+=f[x];
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include<cstdio>
#include<cstring>
using namespace std;
#define ln printf("\n")
const int NN=2001111;
int son[NN][26],pre[NN],val[NN],sum[NN],b[NN],f[NN],next[NN];
int m,TTT,last;
char s[NN];
void samadd(int x)
{
	int p=last,end=++TTT;
	last=end;
	val[end]=val[p]+1;
	for (;p!=-1&&!son[p][x];p=pre[p]) son[p][x]=end;
	if (p==-1) {pre[end]=0;return;}
	int q=son[p][x];
	if (val[q]==val[p]+1) {pre[end]=q;return;}
	int nq=++TTT;
	val[nq]=val[p]+1;
	pre[nq]=pre[q];
	pre[q]=pre[end]=nq;
	memcpy(son[nq],son[q],sizeof(son[q]));
	for (;p!=-1&&son[p][x]==q;p=pre[p]) son[p][x]=nq;
}
int main()
{
	//freopen("1.in","r",stdin);freopen("1.out","w",stdout);
	scanf("%s\n",s+1);
	int len=strlen(s+1);
	pre[0]=-1;
	int i,j;
	for (i=1;i<=len;i++) samadd(s[i]-'a');
	for (i=1;i<=TTT;i++) sum[val[i]]++;
	for (i=2;i<=len;i++) sum[i]+=sum[i-1];
	for (i=TTT;i;i--) b[sum[val[i]]--]=i;
	int x=son[0][s[1]-'a'];
	for (i=1;i<=len;i++,x=son[x][s[i]-'a']) f[x]=1;
	for (i=TTT;i;i--) f[pre[b[i]]]+=f[b[i]];
	for (scanf("%d",&m);m;m--)
	{
		scanf("%s\n",s+1);
		len=strlen(s+1);
		//printf("%s\n",s+1);
		next[1]=0,j=0;
		for (i=2;i<=len;i++)
		{
			while (s[j+1]!=s[i]&&j) j=next[j];
			if (s[j+1]==s[i]) j++;
			next[i]=j;
		}
		int t=len-next[len];
		if (len%t==0) t=len-next[len];
		else t=len;
		for (i=1;i<t;i++) s[i+len]=s[i];
		//for (i=1;i<=len+t-1;i++) printf("%c",s[i]);ln;
		int ans=0,x=0,now=0;
		for (i=1;i<=len+t-1;i++)
		{
			int t=s[i]-'a';
			if (son[x][t]) now++,x=son[x][t];
			else
			{
				while (x!=-1&&!son[x][t]) x=pre[x];
				if (x==-1) now=x=0;
				else now=val[x]+1,x=son[x][t];
			}
			if (now>len)
			{
				while (pre[x]!=-1&&val[pre[x]]>=len) x=pre[x];
				now=len;
			}
			if (now==len) ans+=f[x];
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include<iostream>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<cassert>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<iomanip>
#include<utility>
#include<functional>
using namespace std;

#define mp make_pair
#define pb push_back
#define pf push_front
#define X first
#define Y second
#define rg register
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef double dbl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

inline int qmin(const int a,const int b) {return a<b?a:b;}
inline int qmax(const int a,const int b) {return a>b?a:b;}

inline void fileio(string s){
	if(s.empty()) return ;
	freopen((s+".in").c_str(),"r",stdin);
	freopen((s+".out").c_str(),"w",stdout);
}

const int inf=1e9+7;
const ll linf=1e17+7;

const int N=2e6+7,logN=23;
int c[N],r1[N],r2[N],sa[N],rank[N],mn[N][logN],len[N],n,q,m;
int f[N];
char s[N],t[N];

inline void build(int m){
	rg int i,j,k,p,*x=r1,*y=r2,*t;
	for(i=0;i<n;++i) ++c[x[i]=s[i]];
	for(i=1;i<=m;++i) c[i]+=c[i-1];
	for(i=n-1;i>=0;--i) sa[--c[x[i]]]=i;
	for(k=1;k<=n;k<<=1){
		p=0;
		for(i=n-k;i<n;++i) y[p++]=i;
		for(i=0;i<n;++i) if(sa[i]>=k) y[p++]=sa[i]-k;

		memset(c,0,sizeof(int)*(m+1));
		for(i=0;i<n;++i) ++c[x[i]];
		for(i=1;i<=m;++i) c[i]+=c[i-1];
		for(i=n-1;i>=0;--i) sa[--c[x[y[i]]]]=y[i];

		t=x,x=y,y=t;
		p=x[sa[0]]=1;
		for(i=1;i<n;++i){
			x[sa[i]]=(y[sa[i]]==y[sa[i-1]]&&y[sa[i]+k]==y[sa[i-1]+k]?p:++p);
		}
		if(p>=n) break;
		m=p;
	}
	for(i=0;i<n;++i) rank[sa[i]]=i;
	for(i=0,k=0;i<n;++i){
		if(!rank[i]) continue;
		j=sa[rank[i]-1];
		while(s[i+k]==s[j+k]) ++k;
		mn[rank[i]][0]=k;
		if(k) --k;
	}
	for(i=1;(1<<i)<=n;++i){
		for(j=0;j+(1<<i)<=n;++j) mn[j][i]=qmin(mn[j][i-1],mn[j+(1<<i-1)][i-1]);
	}
}

inline int lcp(int x,int y){
	rg int l=len[y-x+1];
	return qmin(mn[x][l],mn[y-(1<<l)+1][l]);
}

inline pii ins(pii &match,int len,char ch){
	rg int st,ed,ul=match.X-1,ur=match.Y+1;
	while(ur-ul>1){
		int mid=ul+ur>>1;
		if(sa[mid]+len<=n&&s[sa[mid]+len]>=ch) ur=mid;else ul=mid;
	}
	st=ur;
	ul=match.X-1,ur=match.Y+1;
	while(ur-ul>1){
		int mid=ul+ur>>1;
		if(sa[mid]+len>n||s[sa[mid]+len]<=ch) ul=mid;else ur=mid;
	}
	ed=ul;
	return mp(st,ed);
}

inline pii del(pii &match,int len){
	if(len==1) return mp(0,n-1);
	rg int st,ed,ul,ur,pos=rank[sa[match.X]+1];
	ul=-1,ur=pos;
	while(ur-ul>1){
		int mid=ul+ur>>1;
		if(lcp(mid+1,pos)>=len-1) ur=mid;else ul=mid;
	}
	st=ur;
	ul=pos,ur=n;
	while(ur-ul>1){
		int mid=ul+ur>>1;
		if(lcp(pos+1,mid)>=len-1) ul=mid;else ur=mid;
	}
	ed=ul;
	return mp(st,ed);
}

int main(){
	len[0]=len[1]=0;
	for(rg int i=2;i<N;++i) len[i]=len[i>>1]+1;
	scanf("%s",s);
	n=strlen(s);
	build(128);
	scanf("%d",&q);
	while(q--){
		scanf("%s",t);
		m=strlen(t);
		for(rg int i=0;i<m;++i) t[m+i]=t[i];
		t[m<<1]=0;
		//getfail
		f[0]=-1;
		for(rg int j,i=0;i<m<<1;++i){
			j=f[i];
			while(j!=-1&&t[i]!=t[j]) j=f[j];
			f[i+1]=j+1;
		}
		//get start position
		rg int maxst=m<<1;
		while(f[maxst]>m) maxst=f[maxst];
		maxst-=m;
		//two pointers
		rg int st=0,ed=0,ans=0;
		pii match(0,n-1);
		while(st<maxst){
			bool flag=false;
			int len=ed-st;
			if(ed-st==m){
				ans+=match.Y-match.X+1;
			}else{
				pii nmatch=ins(match,len,t[ed]);
				if(nmatch.X<=nmatch.Y){
					++ed;
					match=nmatch;
					flag=true;
				}
			}
			if(!flag){
				++st;
				match=del(match,len);
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cctype>
#include <cassert>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <utility>
#include <functional>

using namespace std;

#define mp make_pair
#define pb push_back
#define pf push_front
#define X first
#define Y second
#define rg register
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef double dbl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

template<typename T> inline T qmin(const T a,const T b) {return a<b?a:b;}
template<typename T> inline T qmax(const T a,const T b) {return a>b?a:b;}
template<typename T> inline void getmin(T &a,const T b) {if(a>b) a=b;}
template<typename T> inline void getmax(T &a,const T b) {if(a<b) a=b;}

inline void fileio(string s){
	freopen((s+".in").c_str(),"r",stdin);
	freopen((s+".out").c_str(),"w",stdout);
}

const int inf=1e9+7;
const ll linf=1e17+7;

const int N=1e6+7,Node=N<<1,logN=23,Sigma=26;

int val[Node],times[Node],par[Node],ch[Node][26],node=1,root=1,last=1;
int head[Node],v[Node],nxt[Node],used[Node],edge;

int newnode(int v){
	val[++node]=v;
	return node;
}

void addedge(int x,int y){
	v[++edge]=y,nxt[edge]=head[x],head[x]=edge;
}

void dfs(int u){
	for(int e=head[u];e;e=nxt[e]){
		dfs(v[e]);
		times[u]+=times[v[e]];
	}
}

void ins(int c){
	int np=newnode(val[last]+1),p=last;
	times[np]=1;
	while(p&&!ch[p][c]) ch[p][c]=np,p=par[p];
	if(!p){
		par[np]=root;
	}else{
		int q=ch[p][c];
		if(val[q]==val[p]+1){
			par[np]=q;
		}else{
			int nq=newnode(val[p]+1);
			par[nq]=par[q];
			par[q]=par[np]=nq;
			memcpy(ch[nq],ch[q],sizeof(int)*Sigma);
			while(p&&ch[p][c]==q) ch[p][c]=nq,p=par[p];
		}
	}
	last=np;
}

char s[N],t[N];
bool rep[N<<1];
int f[N<<1],n,q,m,cnt;

int main(){
	scanf("%s",s);
	n=strlen(s);
	for(int i=0;i<n;++i) ins(s[i]-'a');
//	cerr<<"node="<<node<<endl;
	for(int i=2;i<=node;++i) addedge(par[i],i);
	dfs(1);
//	cerr<<times[5]<<endl;
	scanf("%d",&q);
	while(q--){
		scanf("%s",t);
		m=strlen(t);
		++cnt;
		int ans=0;
		for(int i=0,p=root,l=0;i<2*m-1;++i){
			int c=t[i>=m?i-m:i]-'a';
			if(ch[p][c]){
				p=ch[p][c],++l;
			}else{
				while(p&&!ch[p][c]) p=par[p];
				if(!p) p=root,l=0;
				else l=val[p]+1,p=ch[p][c];
			}
			if(l>=m){
				int u=p;
				while(m<=val[par[u]]) u=par[u];
				if(used[u]!=cnt){
					used[u]=cnt;
					ans+=times[u];
				}
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include<iostream>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<cassert>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<iomanip>
#include<utility>
#include<functional>
using namespace std;

#define mp make_pair
#define pb push_back
#define pf push_front
#define X first
#define Y second
#define rg register
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef double dbl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

template<typename T> inline T qmin(const T a,const T b) {return a<b?a:b;}
template<typename T> inline T qmax(const T a,const T b) {return a>b?a:b;}
template<typename T> inline void getmin(T &a,const T b) {if(a>b) a=b;}
template<typename T> inline void getmax(T &a,const T b) {if(a<b) a=b;}

inline void fileio(string s){
	if(s.empty()) return ;
	freopen((s+".in").c_str(),"r",stdin);
	freopen((s+".out").c_str(),"w",stdout);
}

const int inf=1e9+7;
const ll linf=1e17+7;

const int N=2e6+7,logN=23;
int c[N],r1[N],r2[N],sa[N],rank[N],mn[N][logN],len[N],n,q,m;
int f[N];
char s[N],t[N];

inline void init(){
	len[0]=len[1]=0;
	for(rg int i=2;i<N;++i) len[i]=len[i>>1]+1;
}

inline void build(int m){
	rg int i,j,k,p,*x=r1,*y=r2,*t;
	memset(c,0,sizeof(int)*(m+1));
	for(i=0;i<n;++i) ++c[x[i]=s[i]];
	for(i=1;i<=m;++i) c[i]+=c[i-1];
	for(i=n-1;i>=0;--i) sa[--c[x[i]]]=i;
	for(k=1;k<=n;k<<=1){
		p=0;
		for(i=n-k;i<n;++i) y[p++]=i;
		for(i=0;i<n;++i) if(sa[i]>=k) y[p++]=sa[i]-k;

		memset(c,0,sizeof(int)*(m+1));
		for(i=0;i<n;++i) ++c[x[i]];
		for(i=1;i<=m;++i) c[i]+=c[i-1];
		for(i=n-1;i>=0;--i) sa[--c[x[y[i]]]]=y[i];

		t=x,x=y,y=t;
		p=x[sa[0]]=1;
		for(i=1;i<n;++i){
			x[sa[i]]=(y[sa[i]]==y[sa[i-1]]&&y[sa[i]+k]==y[sa[i-1]+k]?p:++p);
		}
		if(p>=n) break;
		m=p;
	}
	for(i=0;i<n;++i) rank[sa[i]]=i;
	for(i=0,k=0;i<n;++i){
		if(!rank[i]) continue;
		j=sa[rank[i]-1];
		while(s[i+k]==s[j+k]) ++k;
		mn[rank[i]][0]=k;
		if(k) --k;
	}
	for(i=1;(1<<i)<=n;++i){
		for(j=0;j+(1<<i)<=n;++j) mn[j][i]=qmin(mn[j][i-1],mn[j+(1<<i-1)][i-1]);
	}
}

inline int lcp(int x,int y){
	int l=len[y-x+1];
	return qmin(mn[x][l],mn[y-(1<<l)+1][l]);
}

inline pii ins(pii &match,int len,char ch){
	rg int st,ed,ul=match.X-1,ur=match.Y+1;
	while(ur-ul>1){
		int mid=ul+ur>>1;
		if(sa[mid]+len<=n&&s[sa[mid]+len]>=ch) ur=mid;else ul=mid;
	}
	st=ur;
	ul=match.X-1,ur=match.Y+1;
	while(ur-ul>1){
		int mid=ul+ur>>1;
		if(sa[mid]+len>n||s[sa[mid]+len]<=ch) ul=mid;else ur=mid;
	}
	ed=ul;
	return mp(st,ed);
}

inline pii del(pii &match,int len){
	if(len==1) return mp(0,n-1);
	rg int st,ed,ul,ur,pos=rank[sa[match.X]+1];
	ul=-1,ur=pos;
	while(ur-ul>1){
		int mid=ul+ur>>1;
		if(lcp(mid+1,pos)>=len-1) ur=mid;else ul=mid;
	}
	st=ur;
	ul=pos,ur=n;
	while(ur-ul>1){
		int mid=ul+ur>>1;
		if(lcp(pos+1,mid)>=len-1) ul=mid;else ur=mid;
	}
	ed=ul;
	return mp(st,ed);
}

int main(){
	init();
	scanf("%s",s);
	n=strlen(s);
	build(128);
	scanf("%d",&q);
	while(q--){
		scanf("%s",t);
		m=strlen(t);
		for(int i=0;i<m;++i) t[m+i]=t[i];
		t[m<<1]=0;
		//getfail
		f[0]=-1;
		for(rg int j,i=0;i<m<<1;++i){
			j=f[i];
			while(j>=0&&t[i]!=t[j]) j=f[j];
			f[i+1]=j+1;
		}
		//get start position
		int maxst=m<<1;
		while(f[maxst]>m) maxst=f[maxst];
		maxst-=m;
		//two pointers
		int st=0,ed=0,ans=0;
		pii match(0,n-1);
		while(st<maxst){
			bool flag=false;
			int len=ed-st;
			if(ed-st==m){
				ans+=match.Y-match.X+1;
			}else{
				pii nmatch=ins(match,len,t[ed]);
				if(nmatch.X<=nmatch.Y){
					++ed;
					match=nmatch;
					flag=true;
				}
			}
			if(!flag){
				++st;
				match=del(match,len);
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<bitset>
using namespace std;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#define inf 0x3f3f3f3f
#define eps 1e-9
#define FOR(i,s,t) for(int i = s; i < t; ++i )
#define REP(i,s,t) for( int i = s; i <= t; ++i )
#define pii pair<int,int>
#define MP make_pair
#define lson id << 1 , l , mid
#define rson id << 1 | 1 , mid + 1 , r
#define LL long long
#define N ( 2000000 +100)
#define M (200000 + 10)
#define mod 258280327
struct state {
	state *par, *son[26];
	int val, vis, num;
	void init() {
		par = NULL;
		memset(son, 0, sizeof son);
		val = 0;
		vis = num = 0;
	}
}*root, *last, que[N];
int tot;

void extend( int w ){
	state *p = last;
	que[tot].init();
	state *np = &que[tot++];
	np->val = p->val+1;
	while( p&& p -> son[w] == 0 )
		p->son[w] = np, p = p->par;
	if( p == 0 )
		np->par = root;
	else {
		state *q = p->son[w];
		if( q->val == p->val+1 )
			np->par = q;
		else {
			state *nq = &que[tot++];
			*nq = *q;
			nq->val = p->val+1;
			q->par = np->par = nq;
			while( p && p ->son[w] == q ) {
				p->son[w] = nq, p = p->par;
			}
		}
	}
	last = np;
}

char s[N];
int rk[N],cnt[N];

int main () {
	scanf("%s", s);
	tot = 0;
	que[0].init();
	root = last = &que[tot++];
	int n = strlen(s);
	for(int i = 0; i < n; ++i)
		extend(s[i]-'a');
	state *cur = root;
	for(int i = 0; i < n; ++i)
		cur = cur->son[s[i]-'a'], cur->num++;
	for(int i = 0; i < tot; ++i)
		++cnt[que[i].val];
	for(int i = 1; i <= n; ++i)
		cnt[i] += cnt[i-1];
	for(int i = 0; i < tot; ++i)
		rk[--cnt[que[i].val]] = i;
	for(int i = tot-1; i >= 0; --i){
		int v = rk[i];
		if(que[v].par) que[v].par->num += que[v].num;
	}

	int t;
	scanf("%d", &t);
	while(t--){
		scanf("%s", s);
		int len = strlen(s);
		for(int i = len; i < len*2; ++i) s[i] = s[i-len];
		cur = root;
		int l = 0;
		int ans = 0;
		for(int i = 0; i < len*2; ++i) {
			int c = s[i] - 'a';
			if(cur->son[c]) {
				++l;
				cur = cur->son[c];
			}
			else {
				while( cur && cur->son[c] == 0 ) cur = cur->par;
				if(cur==NULL) cur = root, l = 0;
				else l = cur->val+1, cur = cur->son[c];
			}
			if(l >= len) {
				while( cur->par && cur->par->val >= len) cur = cur->par;
				if(cur->vis != t + 1 ) cur->vis = t+1, ans += cur->num;
			}
		}
		printf("%d\n", ans );
	}
}


#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<bitset>
using namespace std;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#define inf 0x3f3f3f3f
#define eps 1e-9
#define FOR(i,s,t) for(int i = s; i < t; ++i )
#define REP(i,s,t) for( int i = s; i <= t; ++i )
#define pii pair<int,int>
#define MP make_pair
#define lson id << 1 , l , mid
#define rson id << 1 | 1 , mid + 1 , r
#define LL long long
#define N ( 2000000 +100)
#define M (200000 + 10)
#define mod 258280327

struct state {
	state *par, *son[26];
	int val, num;
	bool vis;
	void init() {
		par = NULL;
		memset(son, 0, sizeof son);
		val = 0;
		num = 0;
		vis = 0;
	}
}*root, *last, que[N];
int tot;

void extend( int w ){
	state *p = last;
	que[tot].init();
	state *np = &que[tot++];
	np->val = p->val+1;
	while( p&& p -> son[w] == 0 )
		p->son[w] = np, p = p->par;
	if( p == 0 )
		np->par = root;
	else {
		state *q = p->son[w];
		if( q->val == p->val+1 )
			np->par = q;
		else {
			state *nq = &que[tot++];
			*nq = *q;
			nq->val = p->val+1;
			q->par = np->par = nq;
			while( p && p ->son[w] == q ) {
				p->son[w] = nq, p = p->par;
			}
		}
	}
	last = np;
}

int n;
char s[N];
bool vis[N];
int cnt[N], rk[N];
int main () {
	int P;
	while( ~scanf("%s", s ) ) {
		tot = 0;
		que[tot].init();
		root = last = &que[tot++];
		int n = strlen(s);
		for(int i = 0; i < n; ++i)
			extend(s[i]-'a');
		state *cur = root;
		for(int i = 0; i < n; ++i) cur = cur->son[s[i]-'a'], cur->num++;
		for(int i = 0; i <= n; ++i) cnt[i] = 0;
		for(int i = 0; i < tot; ++i) cnt[que[i].val]++;
		for(int i = 1; i <= n; ++i) cnt[i] += cnt[i-1];
		for(int i = 0; i < tot; ++i) rk[--cnt[que[i].val]] = i;
		for(int i = tot-1; i >= 0; --i) {
			int v = rk[i];
			if(que[v].par!=NULL) que[v].par->num += que[v].num;
		}
		int t;
		scanf("%d", &t);
		while( t-- ) {
			memset( vis, 0, sizeof vis);
			scanf("%s", s);
			int len = strlen(s);
			for(int i = len; i < len*2-1; ++i) s[i] = s[i-len];
			state *cur = root, *tmp;
			int l = 0, ans = 0;
			for(int i = 0; i < len*2-1; ++i)
			{
				int c = s[i] - 'a';
				if( cur->son[c] ) {
					l++;
					cur = cur->son[c];
				}
				else {
					while(cur && cur->son[c] == 0) cur = cur->par;
					if(cur == root) l = 0;
					else l = cur->val+1, cur = cur->son[c];
				}
				if( l >= len ) {
				   while( cur->par && cur->par->val >= len ) cur = cur->par;
				   if( !cur->vis ) ans += cur->num;
				   cur->vis = 1;
				}
			}
			printf("%d\n", ans );
			for(int i = 0; i < tot; ++i ) que[i].vis = 0;
		}
	}
}


#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<bitset>
using namespace std;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#define inf 0x3f3f3f3f
#define eps 1e-9
#define FOR(i,s,t) for(int i = s; i < t; ++i )
#define REP(i,s,t) for( int i = s; i <= t; ++i )
#define pii pair<int,int>
#define MP make_pair
#define lson id << 1 , l , mid
#define rson id << 1 | 1 , mid + 1 , r
#define LL long long
#define N ( 2000000 +100)
#define M (200000 + 10)
#define mod 258280327

struct state {
	state *par, *son[26];
	int val, num;
	bool vis;
	void init() {
		par = NULL;
		memset(son, 0, sizeof son);
		val = 0;
		num = 0;
		vis = 0;
	}
}*root, *last, que[N];
int tot;

void extend( int w ){
	state *p = last;
	que[tot].init();
	state *np = &que[tot++];
	np->val = p->val+1;
	while( p&& p -> son[w] == 0 )
		p->son[w] = np, p = p->par;
	if( p == 0 )
		np->par = root;
	else {
		state *q = p->son[w];
		if( q->val == p->val+1 )
			np->par = q;
		else {
			state *nq = &que[tot++];
			*nq = *q;
			nq->val = p->val+1;
			q->par = np->par = nq;
			while( p && p ->son[w] == q ) {
				p->son[w] = nq, p = p->par;
			}
		}
	}
	last = np;
}

int n;
char s[N];
bool vis[N];
int cnt[N], rk[N];
int main () {
	int P;
	while( ~scanf("%s", s ) ) {
		tot = 0;
		que[tot].init();
		root = last = &que[tot++];
		int n = strlen(s);
		for(int i = 0; i < n; ++i)
			extend(s[i]-'a');
		state *cur = root;
		for(int i = 0; i < n; ++i) cur = cur->son[s[i]-'a'], cur->num++;
		for(int i = 0; i <= n; ++i) cnt[i] = 0;
		for(int i = 0; i < tot; ++i) cnt[que[i].val]++;
		for(int i = 1; i <= n; ++i) cnt[i] += cnt[i-1];
		for(int i = 0; i < tot; ++i) rk[--cnt[que[i].val]] = i;
		for(int i = tot-1; i >= 0; --i) {
			int v = rk[i];
			if(que[v].par!=NULL) que[v].par->num += que[v].num;
		}
		int t;
		scanf("%d", &t);
		while( t-- ) {
			memset( vis, 0, sizeof vis);
			scanf("%s", s);
			int len = strlen(s);
			for(int i = len; i < len*2-1; ++i) s[i] = s[i-len];
			state *cur = root;
			int l = 0, ans = 0;
			for(int i = 0; i < len*2-1; ++i)
			{
				int c = s[i] - 'a';
				if( cur->son[c] ) {
					l++;
					cur = cur->son[c];
				}
				else {
					while(cur && cur->son[c] == 0) cur = cur->par;
					if(cur == NULL) l = 0;
					else l = cur->val+1, cur = cur->son[c];
				}
				if( l >= len ) {
				   while( cur->par && cur->par->val >= len ) cur = cur->par;
				   if( !cur->vis ) ans += cur->num;
				   cur->vis = 1;
				}
			}
			printf("%d\n", ans );
			for(int i = 0; i < tot; ++i ) que[i].vis = 0;
		}
	}
}


#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<bitset>
using namespace std;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#define inf 0x3f3f3f3f
#define eps 1e-9
#define FOR(i,s,t) for(int i = s; i < t; ++i )
#define REP(i,s,t) for( int i = s; i <= t; ++i )
#define pii pair<int,int>
#define MP make_pair
#define lson id << 1 , l , mid
#define rson id << 1 | 1 , mid + 1 , r
#define LL long long
#define N ( 2000000 +100)
#define M (200000 + 10)
#define mod 258280327

struct state {
	state *par, *son[26];
	int val, num;
	bool vis;
	void init() {
		par = NULL;
		memset(son, 0, sizeof son);
		val = 0;
		num = 0;
		vis = 0;
	}
}*root, *last, que[N];
int tot;

void extend( int w ){
	state *p = last;
	que[tot].init();
	state *np = &que[tot++];
	np->val = p->val+1;
	while( p&& p -> son[w] == 0 )
		p->son[w] = np, p = p->par;
	if( p == 0 )
		np->par = root;
	else {
		state *q = p->son[w];
		if( q->val == p->val+1 )
			np->par = q;
		else {
			state *nq = &que[tot++];
			*nq = *q;
			nq->val = p->val+1;
			q->par = np->par = nq;
			while( p && p ->son[w] == q ) {
				p->son[w] = nq, p = p->par;
			}
		}
	}
	last = np;
}

int n;
char s[N];
bool vis[N];
int cnt[N], rk[N];
int main () {
	int P;
	while( ~scanf("%s", s ) ) {
		tot = 0;
		que[tot].init();
		root = last = &que[tot++];
		int n = strlen(s);
		for(int i = 0; i < n; ++i)
			extend(s[i]-'a');
		state *cur = root;
		for(int i = 0; i < n; ++i) cur = cur->son[s[i]-'a'], cur->num++;
		for(int i = 0; i <= n; ++i) cnt[i] = 0;
		for(int i = 0; i < tot; ++i) cnt[que[i].val]++;
		for(int i = 1; i <= n; ++i) cnt[i] += cnt[i-1];
		for(int i = 0; i < tot; ++i) rk[--cnt[que[i].val]] = i;
		for(int i = tot-1; i >= 0; --i) {
			int v = rk[i];
			if(que[v].par!=NULL) que[v].par->num += que[v].num;
		}
		int t;
		scanf("%d", &t);
		while( t-- ) {
			memset( vis, 0, sizeof vis);
			scanf("%s", s);
			int len = strlen(s);
			for(int i = len; i < len*2-1; ++i) s[i] = s[i-len];
			state *cur = root;
			int l = 0, ans = 0;
			for(int i = 0; i < len*2-1; ++i)
			{
				int c = s[i] - 'a';
				if( cur->son[c] ) {
					l++;
					cur = cur->son[c];
				}
				else {
					while(cur && cur->son[c] == 0) cur = cur->par;
					if(cur == NULL) l = 0, cur = root;
					else l = cur->val+1, cur = cur->son[c];
				}
				if( l >= len ) {
				   while( cur->par && cur->par->val >= len ) cur = cur->par;
				   if( !cur->vis ) ans += cur->num;
				   cur->vis = 1;
				}
			}
			printf("%d\n", ans );
			for(int i = 0; i < tot; ++i ) que[i].vis = 0;
		}
	}
}


#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<iostream>
#define LL long long
#define INF 99999999
#define EPS 1e-9
#define N 2000000
struct SAM{
    int f,ch[26],len,sum,t;
}a[N];
int Time,last,T,b[N];
char s[N];
bool cmp(const int &p1,const int &p2){
    return a[p1].len<a[p2].len;
}
void insert(int c){
    int u=++T,tem=last;
    a[u].len=a[last].len+1;
    for(;tem!=-1&&!a[tem].ch[c];tem=a[tem].f)
        a[tem].ch[c]=u;
    if(tem!=-1){
        int next=a[tem].ch[c];
        if(a[next].len==a[tem].len+1) a[u].f=next;
        else{
            int note=++T;
            a[note]=a[next];
            a[note].len=a[tem].len+1;
            a[next].f=a[u].f=note;
            for(;tem!=-1&&a[tem].ch[c]==next;tem=a[tem].f)
                a[tem].ch[c]=note;
        }
    }
    last=u;
}
int main(){
    scanf("%s",s);
    a[0].f=-1;
    for(int i=0;s[i];i++) insert(s[i]-'a');
    for(int i=0,u=a[0].ch[s[i]-'a'];u;i++,u=a[u].ch[s[i]-'a']) ++a[u].sum;
    for(int i=0;i<=T;i++) b[i]=i;
    std::sort(b,b+1+T,cmp);
    for(int i=T;i;i--) a[a[b[i]].f].sum+=a[b[i]].sum;
    int Q;scanf("%d\n",&Q);
    while(Q--){
        ++Time;
        scanf("%s",s);
        int u=0,c,ans=0,l=0,len=strlen(s);
        for(int i=0;i<len+len;i++){
            c= i<len ? s[i]-'a' : s[i-len]-'a';
            if(a[u].ch[c])
                u=a[u].ch[c],++l;
            else{
                while(u!=-1&&!a[u].ch[c]) u=a[u].f;
                if(u==-1)
                    u=0,l=0;
                else
                    l=std::min(l,a[u].len+1),u=a[u].ch[c];
            }
            while(a[u].f!=-1&&a[a[u].f].len>=len)
                u=a[u].f,l=a[u].len;
            if(Time!=a[u].t&&l>=len)
                a[u].t=Time,ans+=a[u].sum;

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


#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<iostream>
#define LL long long
#define INF 99999999
#define N  2000100
struct Sam{
    int len,f,ch[26];
    void clear(){
        len=0,f=-1;
        for(int i=0;i<26;i++) ch[i]=-1;
    }
}e[N];
char s[N];
int last,T,L,Time,sum[N],rank[N],cnt[N],vis[N];
void begin(){
    e[T=last=0].clear();
    L=strlen(s);
}
int creat(){ e[++T].clear(); return T; }
void insert(int c){
    int now=creat(),tem=last;
    e[now].len=e[last].len+1; sum[now]=1;
    for(;tem!=-1&&e[tem].ch[c]==-1;tem=e[tem].f) e[tem].ch[c]=now;
    if(tem==-1) e[now].f=0;
    else{
        int next=e[tem].ch[c];
        if(e[tem].len+1==e[next].len) e[now].f=next;
        else{
            int note=creat();
            e[note]=e[next];
            e[note].len=e[tem].len+1;
            e[next].f=e[now].f=note;
            for(;tem!=-1&&e[tem].ch[c]==next;tem=e[tem].f) e[tem].ch[c]=note;
        }
    }
    last=now;
}
void prework(){
    for(int i=1;i<=T;i++) ++cnt[e[i].len];
    for(int i=1;i<=L;i++) cnt[i]+=cnt[i-1];
    for(int i=1;i<=T;i++) rank[cnt[e[i].len]--]=i;
    for(int i=T;i>=1;i--)
        if(e[rank[i]].f!=-1)
            sum[e[rank[i]].f]+=sum[rank[i]];
}
int main(){
    scanf("%s",s); begin();
    for(int i=0;i<L;i++) insert(s[i]-'a');
    prework();
    int Q; scanf("%d",&Q);
    while(Q--){
        scanf("%s",s); int len=strlen(s);
        for(int i=0;i<len;i++) s[i+len]=s[i]; len+=len;
        int ans=0; ++Time;
        for(int i=0,p=0,tem=0;i<len;i++){
            int c=s[i]-'a';
            if(e[p].ch[c]!=-1) ++tem,p=e[p].ch[c];
            else{
                while(p!=-1&&e[p].ch[c]==-1) p=e[p].f;
                if(p==-1)
                    tem=p=0;
                else
                    tem=e[p].len+1,p=e[p].ch[c];
            }
            while(e[p].f!=-1&&e[e[p].f].len>=len/2) tem=e[p=e[p].f].len;
            if(tem>=len/2&&vis[p]!=Time) ans+=sum[p],vis[p]=Time;
        }
        printf("%d\n",ans);
    }
}


#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<iostream>
#define LL long long
#define INF 99999999
#define EPS 1e-9
#define N 2000100
struct SAM{
    int f,ch[26],len,t;
}a[N];
int Time,last,sum[N],T,b[N];
char s[N];
bool cmp(const int &p1,const int &p2){
    return a[p1].len<a[p2].len;
}
void insert(int c){
    int u=++T,tem=last;
    a[u].len=a[last].len+1,++sum[T];
    for(;tem!=-1&&!a[tem].ch[c];tem=a[tem].f)
        a[tem].ch[c]=u;
    if(tem!=-1){
        int next=a[tem].ch[c];
        if(a[next].len==a[tem].len+1) a[u].f=next;
        else{
            int note=++T;
            a[note]=a[next];
            a[note].len=a[tem].len+1;
            a[next].f=a[u].f=note;
            for(;tem!=-1&&a[tem].ch[c]==next;tem=a[tem].f)
                a[tem].ch[c]=note;
        }
    }
    last=u;
}
int main(){
    scanf("%s",s);
    a[0].f=-1;
    for(int i=0;s[i];i++) insert(s[i]-'a');
    for(int i=0;i<=T;i++) b[i]=i;
    std::sort(b,b+1+T,cmp);
    for(int i=T;i;i--) sum[a[b[i]].f]+=sum[b[i]];
    int Q;scanf("%d\n",&Q);
    while(Q--){
        ++Time;
        scanf("%s",s);
        int u=0,c,ans=0,l=0,len=strlen(s);
        for(int i=0;i<len+len;i++){
            c= i<len ? s[i]-'a' : s[i-len]-'a';
            if(a[u].ch[c])
                u=a[u].ch[c],++l;
            else{
                while(u!=-1&&!a[u].ch[c]) u=a[u].f;
                if(u==-1)
                    u=0,l=0;
                else
                    l=a[u].len+1,u=a[u].ch[c];
            }
            while(a[u].f!=-1&&a[a[u].f].len>=len)
                u=a[u].f,l=a[u].len;
            if(Time!=a[u].t&&l>=len)
                a[u].t=Time,ans+=sum[u];

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


#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<iostream>
#define LL long long
#define INF 99999999
#define EPS 1e-9
#define N 500000
struct SAM{
    int f,ch[26],len,sum,t;
}a[N];
int Time,last,T,b[N];
char s[N];
bool cmp(const int &p1,const int &p2){
    return a[p1].len<a[p2].len;
}
void insert(int c){
    int u=++T,tem=last;
    a[u].len=a[last].len+1;
    for(;tem!=-1&&!a[tem].ch[c];tem=a[tem].f)
        a[tem].ch[c]=u;
    if(tem!=-1){
        int next=a[tem].ch[c];
        if(a[next].len==a[tem].len+1) a[u].f=next;
        else{
            int note=++T;
            a[note]=a[next];
            a[note].len=a[tem].len+1;
            a[next].f=a[u].f=note;
            for(;tem!=-1&&a[tem].ch[c]==next;tem=a[tem].f)
                a[tem].ch[c]=note;
        }
    }
    last=u;
}
int main(){
    scanf("%s",s);
    a[0].f=-1;
    for(int i=0;s[i];i++) insert(s[i]-'a');
    for(int i=0,u=a[0].ch[s[i]-'a'];u;i++,u=a[u].ch[s[i]-'a']) ++a[u].sum;
    for(int i=0;i<=T;i++) b[i]=i;
    std::sort(b,b+1+T,cmp);
    for(int i=T;i;i--) a[a[b[i]].f].sum+=a[b[i]].sum;
    int Q;scanf("%d\n",&Q);
    while(Q--){
        ++Time;
        scanf("%s",s);
        int u=0,c,ans=0,l=0,len=strlen(s);
        for(int i=0;i<len+len;i++){
            c= i<len ? s[i]-'a' : s[i-len]-'a';
            if(a[u].ch[c])
                u=a[u].ch[c],++l;
            else{
                while(u!=-1&&!a[u].ch[c]) u=a[u].f;
                if(u==-1)
                    u=0,l=0;
                else
                    l=std::min(l,a[u].len+1),u=a[u].ch[c];
            }
            while(a[u].f!=-1&&a[a[u].f].len>=len)
                u=a[u].f,l=a[u].len;
            if(Time!=a[u].t&&l>=len)
                a[u].t=Time,ans+=a[u].sum;

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


#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<iostream>
#define LL long long
#define INF 99999999
#define EPS 1e-9
#define N 2000100
struct SAM{
    int f,ch[26],len,t;
}a[N];
int Time,sum[N],last,T,b[N];
char s[N];
bool cmp(const int &p1,const int &p2){
    return a[p1].len<a[p2].len;
}
void insert(int c){
    int u=++T,tem=last;
    a[u].len=a[last].len+1; ++sum[T];
    for(;tem!=-1&&!a[tem].ch[c];tem=a[tem].f)
        a[tem].ch[c]=u;
    if(tem!=-1){
        int next=a[tem].ch[c];
        if(a[next].len==a[tem].len+1) a[u].f=next;
        else{
            int note=++T;
            a[note]=a[next];
            a[note].len=a[tem].len+1;
            a[next].f=a[u].f=note;
            for(;tem!=-1&&a[tem].ch[c]==next;tem=a[tem].f)
                a[tem].ch[c]=note;
        }
    }
    last=u;
}
int main(){
    scanf("%s",s);
    a[0].f=-1;
    for(int i=0;s[i];i++) insert(s[i]-'a');
    for(int i=0;i<=T;i++) b[i]=i;
    std::sort(b,b+1+T,cmp);
    for(int i=T;i;i--) sum[a[b[i]].f]+=sum[b[i]];
    int Q;scanf("%d\n",&Q);
    while(Q--){
        ++Time;
        scanf("%s",s);
        int u=0,c,ans=0,l=0,len=strlen(s);
        for(int i=0;i<len+len;i++){
            c= i<len ? s[i]-'a' : s[i-len]-'a';
            if(a[u].ch[c])
                u=a[u].ch[c],++l;
            else{
                while(u!=-1&&!a[u].ch[c]) u=a[u].f;
                if(u==-1)
                    u=0,l=0;
                else
                    l=std::min(l,a[u].len+1),u=a[u].ch[c];
            }
            while(a[u].f!=-1&&a[a[u].f].len>=len)
                u=a[u].f,l=a[u].len;
            if(Time!=a[u].t&&l>=len)
                a[u].t=Time,ans+=sum[u];
        }
        printf("%d\n",ans);
    }
}


#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<iostream>
#define LL long long
#define INF 99999999
#define EPS 1e-9
#define N 2000100
struct SAM{
    int f,ch[26],len,sum,t;
}a[N];
int Time,last,T,b[N];
char s[N];
bool cmp(const int &p1,const int &p2){
    return a[p1].len<a[p2].len;
}
void insert(int c){
    int u=++T,tem=last;
    a[u].len=a[last].len+1;
    for(;tem!=-1&&!a[tem].ch[c];tem=a[tem].f)
        a[tem].ch[c]=u;
    if(tem!=-1){
        int next=a[tem].ch[c];
        if(a[next].len==a[tem].len+1) a[u].f=next;
        else{
            int note=++T;
            a[note]=a[next];
            a[note].len=a[tem].len+1;
            a[next].f=a[u].f=note;
            for(;tem!=-1&&a[tem].ch[c]==next;tem=a[tem].f)
                a[tem].ch[c]=note;
        }
    }
    last=u;
}
int main(){
    scanf("%s",s);
    a[0].f=-1;
    for(int i=0;s[i];i++) insert(s[i]-'a');
    for(int i=0,u=a[0].ch[s[i]-'a'];u;i++,u=a[u].ch[s[i]-'a']) ++a[u].sum;
    for(int i=0;i<=T;i++) b[i]=i;
    std::sort(b,b+1+T,cmp);
    for(int i=T;i;i--) a[a[b[i]].f].sum+=a[b[i]].sum;
    int Q;scanf("%d\n",&Q);
    while(Q--){
        ++Time;
        scanf("%s",s);
        int u=0,c,ans=0,l=0,len=strlen(s);
        for(int i=0;i<len+len;i++){
            c= i<len ? s[i]-'a' : s[i-len]-'a';
            if(a[u].ch[c])
                u=a[u].ch[c],++l;
            else{
                while(u!=-1&&!a[u].ch[c]) u=a[u].f;
                if(u==-1)
                    u=0,l=0;
                else
                    l=std::min(l,a[u].len+1),u=a[u].ch[c];
            }
            while(a[u].f!=-1&&a[a[u].f].len>=len)
                u=a[u].f,l=a[u].len;
            if(Time!=a[u].t&&l>=len)
                a[u].t=Time,ans+=a[u].sum;

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


#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<iostream>
#define LL long long
#define INF 99999999
#define EPS 1e-9
#define N 5000000
struct SAM{
    int f,ch[26],len,sum,t;
}a[N];
int Time,last,T,b[N];
char s[N];
bool cmp(const int &p1,const int &p2){
    return a[p1].len<a[p2].len;
}
void insert(int c){
    int u=++T,tem=last;
    a[u].len=a[last].len+1;
    for(;tem!=-1&&!a[tem].ch[c];tem=a[tem].f)
        a[tem].ch[c]=u;
    if(tem!=-1){
        int next=a[tem].ch[c];
        if(a[next].len==a[tem].len+1) a[u].f=next;
        else{
            int note=++T;
            a[note]=a[next];
            a[note].len=a[tem].len+1;
            a[next].f=a[u].f=note;
            for(;tem!=-1&&a[tem].ch[c]==next;tem=a[tem].f)
                a[tem].ch[c]=note;
        }
    }
    last=u;
}
int main(){
    scanf("%s",s);
    a[0].f=-1;
    for(int i=0;s[i];i++) insert(s[i]-'a');
    for(int i=0,u=a[0].ch[s[i]-'a'];u;i++,u=a[u].ch[s[i]-'a']) ++a[u].sum;
    for(int i=0;i<=T;i++) b[i]=i;
    std::sort(b,b+1+T,cmp);
    for(int i=T;i;i--) a[a[b[i]].f].sum+=a[b[i]].sum;
    int Q;scanf("%d\n",&Q);
    while(Q--){
        ++Time;
        scanf("%s",s);
        int u=0,c,ans=0,l=0,len=strlen(s);
        for(int i=0;i<len+len;i++){
            c= i<len ? s[i]-'a' : s[i-len]-'a';
            if(a[u].ch[c])
                u=a[u].ch[c],++l;
            else{
                while(u!=-1&&!a[u].ch[c]) u=a[u].f;
                if(u==-1)
                    u=0,l=0;
                else
                    l=std::min(l,a[u].len+1),u=a[u].ch[c];
            }
            while(a[u].f!=-1&&a[a[u].f].len>=len)
                u=a[u].f,l=a[u].len;
            if(Time!=a[u].t&&l>=len)
                a[u].t=Time,ans+=a[u].sum;

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


#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<iostream>
#define LL long long
#define INF 99999999
#define N 200000
struct Point{
    int f,len,ch[26];
    void clear(){
        f=-1,len=0;
        for(int i=0;i<26;i++) ch[i]=-1;
    }
}e[N];
int last,T,L;
void begin(){ e[last=T=0].clear(); }
int creat(){ e[++T].clear(); return T;}
void insert(int c){
    int now=creat(),tem=last;
    e[now].len=e[tem].len+1;
    for(;tem!=-1&&e[tem].ch[c]==-1;tem=e[tem].f) e[tem].ch[c]=now;
    if(tem==-1) e[now].f=0;
    else{
        int next=e[tem].ch[c];
        if(e[tem].len+1==e[next].len) e[now].f=next;
        else{
            int note=creat();
            e[note]=e[next];
            e[note].len=e[tem].len+1;
            e[next].f=e[now].f=note;
            for(;tem!=-1&&e[tem].ch[c]==next;tem=e[tem].f) e[tem].ch[c]=note;
        }
    }
    last=now;
}
int cnt[N],rank[N],sum[N];
void sort(){
    for(int i=0;i<=L;i++) cnt[i]=0;
    for(int i=1;i<=T;i++) ++cnt[e[i].len];
    for(int i=1;i<=L;i++) cnt[i]+=cnt[i-1];
    for(int i=1;i<=T;i++) rank[cnt[e[i].len]--]=i;
    for(int p,i=T;i;i--){
        ++sum[p=rank[i]];
        for(int j=0;j<26;j++)
            if(e[p].ch[j]!=-1)
                sum[p]+=sum[e[p].ch[j]];
    }
}
void query(int K){
    for(int p=0;K;)
        for(int i=0;i<26;i++)if(e[p].ch[i]!=-1){
            if(sum[e[p].ch[i]]>=K){
                putchar('a'+i);
                p=e[p].ch[i],--K;//!
                break;
            }else
                K-=sum[e[p].ch[i]];
        }
    puts("");
}
char s[N];
int main(){
    begin(); scanf("%s",s); L=strlen(s);
    for(int i=0;i<L;i++) insert(s[i]-'a');
    sort();
    int Q,K; scanf("%d",&Q);
    while(Q--) scanf("%d",&K),query(K);
}


//Codeforces 235C
//by Cydiater
//2017.1.22
#include <iostream>
#include <queue>
#include <map>
#include <iomanip>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <bitset>
#include <set>
#include <vector>
using namespace std;
#define ll long long
#define up(i,j,n)	for(int i=j;i<=n;i++)
#define down(i,j,n)	for(int i=j;i>=n;i--)
#define cmax(a,b)	a=max(a,b)
#define cmin(a,b)	a=min(a,b)
const int MAXN=1e6+5;
const int oo=0x3f3f3f3f;
int rnk[MAXN],N,Right[MAXN],M,len,ans,next[MAXN],LEN;
char s[MAXN],t[MAXN];
struct SAM{
	int now,cnt,son[MAXN][26],step[MAXN],pre[MAXN],label[MAXN];
	SAM(){now=cnt=1;}
	void Extend(int nxt){
		int p=now,np=++cnt;now=np;
		step[np]=step[p]+1;Right[np]=1;
		for(;(!son[p][nxt])&&p;p=pre[p])son[p][nxt]=np;
		if(!p)pre[np]=1;
		else{
			int q=son[p][nxt],nq;
			if(step[q]==step[p]+1)pre[np]=q;
			else{
				step[(nq=++cnt)]=step[p]+1;
				memcpy(son[nq],son[q],sizeof(son[q]));
				pre[nq]=pre[q];
				pre[np]=pre[q]=nq;
				for(;son[p][nxt]==q;p=pre[p])son[p][nxt]=nq;
			}
		}
	}
	void Toposort(){
		up(i,1,cnt)label[step[i]]++;
		up(i,1,N)label[i]+=label[i-1];
		up(i,1,cnt)rnk[label[step[i]]--]=i;
		down(i,cnt,1){
			int node=rnk[i];
			Right[pre[node]]+=Right[node];
		}
	}
	void Go(){
		now=1;int Len=0;
		up(i,1,LEN){
			int nxt=s[i]-'a';
			for(;now&&(!son[now][nxt]);now=pre[now],Len=step[now]);
			if(!now){now=1;Len=0;}
			if(son[now][nxt]){now=son[now][nxt];Len++;}
			for(;now&&step[pre[now]]>=len;now=pre[now],Len=step[now]);
			if(step[pre[now]]<len&&step[now]>=len&&Len>=len)
				ans+=Right[now];
		}
	}
}sam;
namespace solution{
	void Prepare(){
		scanf("%s",s+1);
		N=strlen(s+1);
		up(i,1,N)sam.Extend(s[i]-'a');
		sam.Toposort();
	}
	void Solve(){
		cin>>M;
		while(M--){
			scanf("%s",s+1);
			len=strlen(s+1);
			memcpy(t,s,sizeof(s));
			int j=0;
			up(i,2,len){
				while(s[i]!=s[j+1]&&j)j=next[j];
				if(s[i]==s[j+1])j++;
				next[i]=j;
			}
			//up(i,1,len)printf("%c",s[i]);puts("");
			LEN=len+len;
			if(2*(len-next[len])<=len){
				LEN-=next[len];
				up(i,1,len-next[len])s[i]=t[next[len]+1+i];
				up(i,len-next[len],LEN)s[i]=t[i-(len-next[len])+1];
				LEN--;
			}else{
				up(i,len+1,LEN)s[i]=t[i-len];
				LEN--;
			}
			//up(i,1,LEN)printf("%c",s[i]);puts("");
			ans=0;
			sam.Go();
			printf("%d\n",ans);
		}
	}
}
int main(){
	//freopen("input.in","r",stdin);
	using namespace solution;
	Prepare();
	Solve();
	return 0;
}


//Codeforces 235C
//by Cydiater
//2017.1.22
#include <iostream>
#include <queue>
#include <map>
#include <iomanip>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <bitset>
#include <set>
#include <vector>
using namespace std;
#define ll long long
#define up(i,j,n)	for(int i=j;i<=n;i++)
#define down(i,j,n)	for(int i=j;i>=n;i--)
#define cmax(a,b)	a=max(a,b)
#define cmin(a,b)	a=min(a,b)
const int MAXN=1e6+5;
const int oo=0x3f3f3f3f;
int rnk[MAXN],N,Right[MAXN],M,len,ans,next[MAXN],LEN;
char s[MAXN],t[MAXN];
struct SAM{
	int now,cnt,son[MAXN][26],step[MAXN],pre[MAXN],label[MAXN];
	SAM(){now=cnt=1;}
	void Extend(int nxt){
		int p=now,np=++cnt;now=np;
		step[np]=step[p]+1;Right[np]=1;
		for(;(!son[p][nxt])&&p;p=pre[p])son[p][nxt]=np;
		if(!p)pre[np]=1;
		else{
			int q=son[p][nxt],nq;
			if(step[q]==step[p]+1)pre[np]=q;
			else{
				step[(nq=++cnt)]=step[p]+1;
				memcpy(son[nq],son[q],sizeof(son[q]));
				pre[nq]=pre[q];
				pre[np]=pre[q]=nq;
				for(;son[p][nxt]==q;p=pre[p])son[p][nxt]=nq;
			}
		}
	}
	void Toposort(){
		up(i,1,cnt)label[step[i]]++;
		up(i,1,N)label[i]+=label[i-1];
		up(i,1,cnt)rnk[label[step[i]]--]=i;
		down(i,cnt,1){
			int node=rnk[i];
			Right[pre[node]]+=Right[node];
		}
	}
	void Go(){
		now=1;int Len=0;
		up(i,1,LEN){
			int nxt=s[i]-'a';
			for(;now&&(!son[now][nxt]);now=pre[now],Len=step[now]);
			if(!now){now=1;Len=0;}
			if(son[now][nxt]){now=son[now][nxt];Len++;}
			for(;now&&step[pre[now]]>=len;now=pre[now],Len=step[now]);
			if(step[pre[now]]<len&&step[now]>=len&&Len>=len)
				ans+=Right[now];
		}
	}
}sam;
namespace solution{
	void Prepare(){
		scanf("%s",s+1);
		N=strlen(s+1);
		up(i,1,N)sam.Extend(s[i]-'a');
		sam.Toposort();
	}
	void Solve(){
		cin>>M;
		while(M--){
			scanf("%s",s+1);
			len=strlen(s+1);
			memcpy(t,s,sizeof(s));
			int j=0;
			up(i,2,len){
				while(s[i]!=s[j+1]&&j)j=next[j];
				if(s[i]==s[j+1])j++;
				next[i]=j;
			}
			//up(i,1,len)printf("%c",s[i]);puts("");
			LEN=len+len-1;
			if(2*(len-next[len])<=len){
				LEN-=next[len];
				up(i,len-next[len],LEN)s[i]=t[i-len+next[len]+1];
			}else up(i,len+1,LEN)s[i]=t[i-len];
			//up(i,1,LEN)printf("%c",s[i]);puts("");
			ans=0;
			sam.Go();
			printf("%d\n",ans);
		}
	}
}
int main(){
	//freopen("input.in","r",stdin);
	using namespace solution;
	Prepare();
	Solve();
	return 0;
}


//Codeforces 235C
//by Cydiater
//2017.1.22
#include <iostream>
#include <queue>
#include <map>
#include <iomanip>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <bitset>
#include <set>
#include <vector>
using namespace std;
#define ll long long
#define up(i,j,n)	for(int i=j;i<=n;i++)
#define down(i,j,n)	for(int i=j;i>=n;i--)
#define cmax(a,b)	a=max(a,b)
#define cmin(a,b)	a=min(a,b)
const int MAXN=1e6+5;
const int oo=0x3f3f3f3f;
int rnk[MAXN],N,Right[MAXN],M,len,ans,next[MAXN],LEN;
char s[MAXN];
struct SAM{
	int now,cnt,son[MAXN][26],step[MAXN],pre[MAXN],label[MAXN];
	SAM(){now=cnt=1;}
	void Extend(int nxt){
		int p=now,np=++cnt;now=np;
		step[np]=step[p]+1;Right[np]=1;
		for(;(!son[p][nxt])&&p;p=pre[p])son[p][nxt]=np;
		if(!p)pre[np]=1;
		else{
			int q=son[p][nxt],nq;
			if(step[q]==step[p]+1)pre[np]=q;
			else{
				step[(nq=++cnt)]=step[p]+1;
				memcpy(son[nq],son[q],sizeof(son[q]));
				pre[nq]=pre[q];
				pre[np]=pre[q]=nq;
				for(;son[p][nxt]==q;p=pre[p])son[p][nxt]=nq;
			}
		}
	}
	void Toposort(){
		up(i,1,cnt)label[step[i]]++;
		up(i,1,N)label[i]+=label[i-1];
		up(i,1,cnt)rnk[label[step[i]]--]=i;
		down(i,cnt,1){
			int node=rnk[i];
			Right[pre[node]]+=Right[node];
		}
	}
	void Go(){
		now=1;int Len=0;
		up(i,1,LEN){
			int nxt=s[i]-'a';
			for(;now&&(!son[now][nxt]||step[pre[now]]>=len);now=pre[now],Len=step[now]);
			if(!now){now=1;Len=0;}
			if(son[now][nxt]){now=son[now][nxt];Len++;}
			if(step[pre[now]]<len&&step[now]>=len&&Len>=len)ans+=Right[now];
		}
	}
}sam;
namespace solution{
	void Prepare(){
		scanf("%s",s+1);
		N=strlen(s+1);
		up(i,1,N)sam.Extend(s[i]-'a');
		sam.Toposort();
	}
	void Solve(){
		cin>>M;
		while(M--){
			scanf("%s",s+1);
			len=strlen(s+1);
			int j=0;
			up(i,2,len){
				while(s[i]!=s[j+1]&&j)j=next[j];
				if(s[i]==s[j+1])j++;
				next[i]=j;
			}
			LEN=len+len-1;
			if(2*(len-next[len])<=len)LEN-=next[len];
			up(i,len+1,LEN)s[i]=s[i-len];
			ans=0;
			sam.Go();
			printf("%d\n",ans);
		}
	}
}
int main(){
	//freopen("input.in","r",stdin);
	using namespace solution;
	Prepare();
	Solve();
	return 0;
}


//Codeforces 235C
//by Cydiater
//2017.1.22
#include <iostream>
#include <queue>
#include <map>
#include <iomanip>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <bitset>
#include <set>
#include <vector>
using namespace std;
#define ll long long
#define up(i,j,n)	for(int i=j;i<=n;i++)
#define down(i,j,n)	for(int i=j;i>=n;i--)
#define cmax(a,b)	a=max(a,b)
#define cmin(a,b)	a=min(a,b)
const int MAXN=1e6+5;
const int oo=0x3f3f3f3f;
int rnk[MAXN],N,Right[MAXN],M,len,ans,next[MAXN],LEN;
char s[MAXN];
struct SAM{
	int now,cnt,son[MAXN][26],step[MAXN],pre[MAXN],label[MAXN];
	SAM(){now=cnt=1;}
	void Extend(int nxt){
		int p=now,np=++cnt;now=np;
		step[np]=step[p]+1;Right[np]=1;
		for(;(!son[p][nxt])&&p;p=pre[p])son[p][nxt]=np;
		if(!p)pre[np]=1;
		else{
			int q=son[p][nxt],nq;
			if(step[q]==step[p]+1)pre[np]=q;
			else{
				step[(nq=++cnt)]=step[p]+1;
				memcpy(son[nq],son[q],sizeof(son[q]));
				pre[nq]=pre[q];
				pre[np]=pre[q]=nq;
				for(;son[p][nxt]==q;p=pre[p])son[p][nxt]=nq;
			}
		}
	}
	void Toposort(){
		up(i,1,cnt)label[step[i]]++;
		up(i,1,N)label[i]+=label[i-1];
		up(i,1,cnt)rnk[label[step[i]]--]=i;
		down(i,cnt,1){
			int node=rnk[i];
			Right[pre[node]]+=Right[node];
		}
	}
	void Go(){
		now=1;int Len=0;
		down(i,LEN,1){
			int nxt=s[i]-'a';
			for(;now&&(!son[now][nxt]);now=pre[now],Len=step[now]);
			if(!now){now=1;Len=0;}
			if(son[now][nxt]){now=son[now][nxt];Len++;}
			for(;now&&step[pre[now]]>=len;now=pre[now],Len=step[now]);
			if(step[pre[now]]<len&&step[now]>=len&&Len>=len)
				ans+=Right[now];
		}
	}
}sam;
namespace solution{
	void Prepare(){
		scanf("%s",s+1);
		N=strlen(s+1);
		up(i,1,N)sam.Extend(s[i]-'a');
		sam.Toposort();
	}
	void Solve(){
		cin>>M;
		while(M--){
			scanf("%s",s+1);
			len=strlen(s+1);
			up(i,1,len>>1)swap(s[i],s[len-i+1]);
			int j=0;
			up(i,2,len){
				while(s[i]!=s[j+1]&&j)j=next[j];
				if(s[i]==s[j+1])j++;
				next[i]=j;
			}
			LEN=len+len-1;
			if(2*(len-next[len])<=len)LEN-=next[len];
			up(i,len+1,LEN)s[i]=s[i-len];
			//up(i,1,len)printf("%c",s[i]);puts("");
			//up(i,1,LEN)printf("%c",s[i]);puts("");
			ans=0;
			sam.Go();
			printf("%d\n",ans);
		}
	}
}
int main(){
	freopen("input.in","r",stdin);
	using namespace solution;
	Prepare();
	Solve();
	return 0;
}


//Codeforces 235C
//by Cydiater
//2017.1.22
#include <iostream>
#include <queue>
#include <map>
#include <iomanip>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <bitset>
#include <set>
#include <vector>
using namespace std;
#define ll long long
#define up(i,j,n)	for(int i=j;i<=n;i++)
#define down(i,j,n)	for(int i=j;i>=n;i--)
#define cmax(a,b)	a=max(a,b)
#define cmin(a,b)	a=min(a,b)
const int MAXN=1e6+5;
const int oo=0x3f3f3f3f;
int rnk[MAXN],N,Right[MAXN],M,len,ans,next[MAXN],LEN;
char s[MAXN];
struct SAM{
	int now,cnt,son[MAXN][26],step[MAXN],pre[MAXN],label[MAXN];
	SAM(){now=cnt=1;}
	void Extend(int nxt){
		int p=now,np=++cnt;now=np;
		step[np]=step[p]+1;Right[np]=1;
		for(;(!son[p][nxt])&&p;p=pre[p])son[p][nxt]=np;
		if(!p)pre[np]=1;
		else{
			int q=son[p][nxt],nq;
			if(step[q]==step[p]+1)pre[np]=q;
			else{
				step[(nq=++cnt)]=step[p]+1;
				memcpy(son[nq],son[q],sizeof(son[q]));
				pre[nq]=pre[q];
				pre[np]=pre[q]=nq;
				for(;son[p][nxt]==q;p=pre[p])son[p][nxt]=nq;
			}
		}
	}
	void Toposort(){
		up(i,1,cnt)label[step[i]]++;
		up(i,1,N)label[i]+=label[i-1];
		up(i,1,cnt)rnk[label[step[i]]--]=i;
		down(i,cnt,1){
			int node=rnk[i];
			Right[pre[node]]+=Right[node];
		}
	}
	void Go(){
		now=1;int Len=0;
		down(i,LEN,1){
			int nxt=s[i]-'a';
			for(;now&&(!son[now][nxt]);now=pre[now],Len=step[now]);
			if(!now){now=1;Len=0;}
			if(son[now][nxt]){now=son[now][nxt];Len++;}
			for(;now&&step[pre[now]]>=len;now=pre[now],Len=step[now]);
			if(step[pre[now]]<len&&step[now]>=len&&Len>=len)
				ans+=Right[now];
		}
	}
}sam;
namespace solution{
	void Prepare(){
		scanf("%s",s+1);
		N=strlen(s+1);
		up(i,1,N)sam.Extend(s[i]-'a');
		sam.Toposort();
	}
	void Solve(){
		cin>>M;
		while(M--){
			scanf("%s",s+1);
			len=strlen(s+1);
			up(i,1,len>>1)swap(s[i],s[len-i+1]);
			int j=0;
			up(i,2,len){
				while(s[i]!=s[j+1]&&j)j=next[j];
				if(s[i]==s[j+1])j++;
				next[i]=j;
			}
			LEN=len+len-1;
			if(2*(len-next[len])<=len)LEN-=next[len];
			up(i,len+1,LEN)s[i]=s[i-len];
			//up(i,1,len)printf("%c",s[i]);puts("");
			//up(i,1,LEN)printf("%c",s[i]);puts("");
			ans=0;
			sam.Go();
			printf("%d\n",ans);
		}
	}
}
int main(){
	//freopen("input.in","r",stdin);
	using namespace solution;
	Prepare();
	Solve();
	return 0;
}


//Codeforces 235C
//by Cydiater
//2017.1.22
#include <iostream>
#include <queue>
#include <map>
#include <iomanip>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <bitset>
#include <set>
#include <vector>
using namespace std;
#define ll long long
#define up(i,j,n)	for(int i=j;i<=n;i++)
#define down(i,j,n)	for(int i=j;i>=n;i--)
#define cmax(a,b)	a=max(a,b)
#define cmin(a,b)	a=min(a,b)
const int MAXN=2e6+5;
const int oo=0x3f3f3f3f;
int rnk[MAXN],N,Right[MAXN],M,len,ans,flag[MAXN];
char s[MAXN];
struct SAM{
	int now,cnt,son[MAXN][26],step[MAXN],pre[MAXN],label[MAXN];
	SAM(){now=cnt=1;}
	void Extend(int nxt){
		int p=now,np=++cnt;now=np;
		step[np]=step[p]+1;Right[np]=1;
		for(;(!son[p][nxt])&&p;p=pre[p])son[p][nxt]=np;
		if(!p)pre[np]=1;
		else{
			int q=son[p][nxt],nq;
			if(step[q]==step[p]+1)pre[np]=q;
			else{
				step[(nq=++cnt)]=step[p]+1;
				memcpy(son[nq],son[q],sizeof(son[q]));
				pre[nq]=pre[q];
				pre[np]=pre[q]=nq;
				for(;son[p][nxt]==q;p=pre[p])son[p][nxt]=nq;
			}
		}
	}
	void Toposort(){
		up(i,1,cnt)label[step[i]]++;
		up(i,1,N)label[i]+=label[i-1];
		up(i,1,cnt)rnk[label[step[i]]--]=i;
		down(i,cnt,1){
			int node=rnk[i];
			Right[pre[node]]+=Right[node];
		}
	}
	void Go(){
		now=1;int Len=0;
		up(i,1,(len<<1)-1){
			int nxt=s[i]-'a';
			for(;now&&(!son[now][nxt]);now=pre[now],Len=step[now]);
			if(!now){now=1;Len=0;}
			if(son[now][nxt]){now=son[now][nxt];Len++;}
			for(;now&&step[pre[now]]>=len;now=pre[now],Len=step[now]);
			if(step[pre[now]]<len&&step[now]>=len&&Len>=len){
				if(flag[now]!=M+1){
					ans+=Right[now];
					flag[now]=M+1;
				}
			}
		}
	}
}sam;
namespace solution{
	void Prepare(){
		scanf("%s",s+1);
		N=strlen(s+1);
		up(i,1,N)sam.Extend(s[i]-'a');
		sam.Toposort();
	}
	void Solve(){
		cin>>M;
		while(M--){
			scanf("%s",s+1);
			len=strlen(s+1);
			up(i,1,len-1)s[i+len]=s[i];
			ans=0;
			sam.Go();
			printf("%d\n",ans);
		}
	}
}
int main(){
	//freopen("input.in","r",stdin);
	using namespace solution;
	Prepare();
	Solve();
	return 0;
}


//Codeforces 235C
//by Cydiater
//2017.1.22
#include <iostream>
#include <queue>
#include <map>
#include <iomanip>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <bitset>
#include <set>
#include <vector>
using namespace std;
#define ll long long
#define up(i,j,n)	for(int i=j;i<=n;i++)
#define down(i,j,n)	for(int i=j;i>=n;i--)
#define cmax(a,b)	a=max(a,b)
#define cmin(a,b)	a=min(a,b)
const int MAXN=2e6+5;
const int oo=0x3f3f3f3f;
int rnk[MAXN],N,Right[MAXN],M,len,ans,flag[MAXN];
char s[MAXN];
struct SAM{
	int now,cnt,son[MAXN][26],step[MAXN],pre[MAXN],label[MAXN];
	SAM(){now=cnt=1;}
	void Extend(int nxt){
		int p=now,np=++cnt;now=np;
		step[np]=step[p]+1;Right[np]=1;
		for(;(!son[p][nxt])&&p;p=pre[p])son[p][nxt]=np;
		if(!p)pre[np]=1;
		else{
			int q=son[p][nxt],nq;
			if(step[q]==step[p]+1)pre[np]=q;
			else{
				step[(nq=++cnt)]=step[p]+1;
				memcpy(son[nq],son[q],sizeof(son[q]));
				pre[nq]=pre[q];
				pre[np]=pre[q]=nq;
				for(;son[p][nxt]==q;p=pre[p])son[p][nxt]=nq;
			}
		}
	}
	void Toposort(){
		up(i,1,cnt)label[step[i]]++;
		up(i,1,N)label[i]+=label[i-1];
		up(i,1,cnt)rnk[label[step[i]]--]=i;
		down(i,cnt,1){
			int node=rnk[i];
			Right[pre[node]]+=Right[node];
		}
	}
	void Go(){
		now=1;int Len=0;
		up(i,1,(len<<1)-1){
			int nxt=s[i]-'a';
			for(;now&&(!son[now][nxt]);now=pre[now],Len=step[now]);
			if(!now){now=1;Len=0;}
			if(son[now][nxt]){now=son[now][nxt];Len++;}
			for(;now&&step[pre[now]]>=len;now=pre[now],Len=step[now]);
			if(step[pre[now]]<len&&step[now]>=len&&Len>=len){
				if(flag[now]!=M){
					ans+=Right[now];
					flag[now]=M;
				}
			}
		}
	}
}sam;
namespace solution{
	void Prepare(){
		scanf("%s",s+1);
		N=strlen(s+1);
		up(i,1,N)sam.Extend(s[i]-'a');
		sam.Toposort();
	}
	void Solve(){
		cin>>M;
		while(M--){
			scanf("%s",s+1);
			len=strlen(s+1);
			up(i,1,len-1)s[i+len]=s[i];
			ans=0;
			sam.Go();
			printf("%d\n",ans);
		}
	}
}
int main(){
	//freopen("input.in","r",stdin);
	using namespace solution;
	Prepare();
	Solve();
	return 0;
}


//Codeforces 235C
//by Cydiater
//2017.1.22
#include <iostream>
#include <queue>
#include <map>
#include <iomanip>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <bitset>
#include <set>
#include <vector>
using namespace std;
#define ll long long
#define up(i,j,n)	for(int i=j;i<=n;i++)
#define down(i,j,n)	for(int i=j;i>=n;i--)
#define cmax(a,b)	a=max(a,b)
#define cmin(a,b)	a=min(a,b)
const int MAXN=2e6+5;
const int oo=0x3f3f3f3f;
int rnk[MAXN],N,Right[MAXN],M,len,ans;
char s[MAXN];
bool flag[MAXN];
struct SAM{
	int now,cnt,son[MAXN][26],step[MAXN],pre[MAXN],label[MAXN];
	SAM(){now=cnt=1;}
	void Extend(int nxt){
		int p=now,np=++cnt;now=np;
		step[np]=step[p]+1;Right[np]=1;
		for(;(!son[p][nxt])&&p;p=pre[p])son[p][nxt]=np;
		if(!p)pre[np]=1;
		else{
			int q=son[p][nxt],nq;
			if(step[q]==step[p]+1)pre[np]=q;
			else{
				step[(nq=++cnt)]=step[p]+1;
				memcpy(son[nq],son[q],sizeof(son[q]));
				pre[nq]=pre[q];
				pre[np]=pre[q]=nq;
				for(;son[p][nxt]==q;p=pre[p])son[p][nxt]=nq;
			}
		}
	}
	void Toposort(){
		up(i,1,cnt)label[step[i]]++;
		up(i,1,N)label[i]+=label[i-1];
		up(i,1,cnt)rnk[label[step[i]]--]=i;
		down(i,cnt,1){
			int node=rnk[i];
			Right[pre[node]]+=Right[node];
		}
	}
	void Go(){
		now=1;int Len=0;
		up(i,1,(len<<1)-1){
			int nxt=s[i]-'a';
			for(;now&&(!son[now][nxt]);now=pre[now],Len=step[now]);
			if(!now){now=1;Len=0;}
			if(son[now][nxt]){now=son[now][nxt];Len++;}
			for(;now&&step[pre[now]]>=len;now=pre[now],Len=step[now]);
			if(step[pre[now]]<len&&step[now]>=len&&Len>=len){
				if(!flag[now]){
					ans+=Right[now];
					flag[now]=1;
				}
			}
		}
	}
}sam;
namespace solution{
	void Prepare(){
		scanf("%s",s+1);
		N=strlen(s+1);
		up(i,1,N)sam.Extend(s[i]-'a');
		sam.Toposort();
	}
	void Solve(){
		cin>>M;
		while(M--){
			scanf("%s",s+1);
			memset(flag,0,sizeof(flag));
			len=strlen(s+1);
			up(i,1,len-1)s[i+len]=s[i];
			ans=0;
			sam.Go();
			printf("%d\n",ans);
		}
	}
}
int main(){
	//freopen("input.in","r",stdin);
	using namespace solution;
	Prepare();
	Solve();
	return 0;
}


//Codeforces 235C
//by Cydiater
//2017.1.22
#include <iostream>
#include <queue>
#include <map>
#include <iomanip>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <bitset>
#include <set>
#include <vector>
using namespace std;
#define ll long long
#define up(i,j,n)	for(int i=j;i<=n;i++)
#define down(i,j,n)	for(int i=j;i>=n;i--)
#define cmax(a,b)	a=max(a,b)
#define cmin(a,b)	a=min(a,b)
const int MAXN=1e6+5;
const int oo=0x3f3f3f3f;
int rnk[MAXN],N,Right[MAXN],M,len,ans,next[MAXN],LEN;
char s[MAXN];
struct SAM{
	int now,cnt,son[MAXN][26],step[MAXN],pre[MAXN],label[MAXN];
	SAM(){now=cnt=1;}
	void Extend(int nxt){
		int p=now,np=++cnt;now=np;
		step[np]=step[p]+1;Right[np]=1;
		for(;(!son[p][nxt])&&p;p=pre[p])son[p][nxt]=np;
		if(!p)pre[np]=1;
		else{
			int q=son[p][nxt],nq;
			if(step[q]==step[p]+1)pre[np]=q;
			else{
				step[(nq=++cnt)]=step[p]+1;
				memcpy(son[nq],son[q],sizeof(son[q]));
				pre[nq]=pre[q];
				pre[np]=pre[q]=nq;
				for(;son[p][nxt]==q;p=pre[p])son[p][nxt]=nq;
			}
		}
	}
	void Toposort(){
		up(i,1,cnt)label[step[i]]++;
		up(i,1,N)label[i]+=label[i-1];
		up(i,1,cnt)rnk[label[step[i]]--]=i;
		down(i,cnt,1){
			int node=rnk[i];
			Right[pre[node]]+=Right[node];
		}
	}
	void Go(){
		now=1;int Len=0;
		up(i,1,LEN){
			int nxt=s[i]-'a';
			for(;now&&(!son[now][nxt]);now=pre[now],Len=step[now]);
			if(!now){now=1;Len=0;}
			if(son[now][nxt]){now=son[now][nxt];Len++;}
			for(;now&&step[pre[now]]>=len;now=pre[now],Len=step[now]);
			if(step[pre[now]]<len&&step[now]>=len&&Len>=len)
				ans+=Right[now];
		}
	}
}sam;
namespace solution{
	void Prepare(){
		scanf("%s",s+1);
		N=strlen(s+1);
		up(i,1,N)sam.Extend(s[i]-'a');
		sam.Toposort();
	}
	void Solve(){
		cin>>M;
		while(M--){
			scanf("%s",s+1);
			len=strlen(s+1);
			int j=0;
			up(i,2,len){
				while(s[i]!=s[j+1]&&j)j=next[j];
				if(s[i]==s[j+1])j++;
				next[i]=j;
			}
			LEN=len+len-1;
			if(2*(len-next[len])<=len)LEN-=next[len];
			up(i,len+1,LEN)s[i]=s[i-len];
			ans=0;
			sam.Go();
			printf("%d\n",ans);
		}
	}
}
int main(){
	//freopen("input.in","r",stdin);
	using namespace solution;
	Prepare();
	Solve();
	return 0;
}


//Codeforces 235C
//by Cydiater
//2017.1.22
#include <iostream>
#include <queue>
#include <map>
#include <iomanip>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <bitset>
#include <set>
#include <vector>
using namespace std;
#define ll long long
#define up(i,j,n)	for(int i=j;i<=n;i++)
#define down(i,j,n)	for(int i=j;i>=n;i--)
#define cmax(a,b)	a=max(a,b)
#define cmin(a,b)	a=min(a,b)
const int MAXN=1e6+5;
const int oo=0x3f3f3f3f;
int rnk[MAXN],N,Right[MAXN],M,len,ans,next[MAXN],LEN;
char s[MAXN],t[MAXN];
bool flag[MAXN];
struct SAM{
	int now,cnt,son[MAXN][26],step[MAXN],pre[MAXN],label[MAXN];
	SAM(){now=cnt=1;}
	void Extend(int nxt){
		int p=now,np=++cnt;now=np;
		step[np]=step[p]+1;Right[np]=1;
		for(;(!son[p][nxt])&&p;p=pre[p])son[p][nxt]=np;
		if(!p)pre[np]=1;
		else{
			int q=son[p][nxt],nq;
			if(step[q]==step[p]+1)pre[np]=q;
			else{
				step[(nq=++cnt)]=step[p]+1;
				memcpy(son[nq],son[q],sizeof(son[q]));
				pre[nq]=pre[q];
				pre[np]=pre[q]=nq;
				for(;son[p][nxt]==q;p=pre[p])son[p][nxt]=nq;
			}
		}
	}
	void Toposort(){
		up(i,1,cnt)label[step[i]]++;
		up(i,1,N)label[i]+=label[i-1];
		up(i,1,cnt)rnk[label[step[i]]--]=i;
		down(i,cnt,1){
			int node=rnk[i];
			Right[pre[node]]+=Right[node];
		}
	}
	void Go(){
		now=1;int Len=0;
		up(i,1,(len<<1)-1){
			int nxt=s[i]-'a';
			for(;now&&(!son[now][nxt]);now=pre[now],Len=step[now]);
			if(!now){now=1;Len=0;}
			if(son[now][nxt]){now=son[now][nxt];Len++;}
			for(;now&&step[pre[now]]>=len;now=pre[now],Len=step[now]);
			if(step[pre[now]]<len&&step[now]>=len&&Len>=len){
				if(!flag[now]){
					ans+=Right[now];
					flag[now]=1;
				}
			}
		}
	}
}sam;
namespace solution{
	void Prepare(){
		scanf("%s",s+1);
		N=strlen(s+1);
		up(i,1,N)sam.Extend(s[i]-'a');
		sam.Toposort();
	}
	void Solve(){
		cin>>M;
		while(M--){
			scanf("%s",s+1);
			memset(flag,0,sizeof(flag));
			len=strlen(s+1);
			up(i,1,len-1)s[i+len]=s[i];
			ans=0;
			sam.Go();
			printf("%d\n",ans);
		}
	}
}
int main(){
	//freopen("input.in","r",stdin);
	using namespace solution;
	Prepare();
	Solve();
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
using namespace std;
const int M=2000007;

char s[M];
int n,m;
int last,tot;
int ch[M][26];
int stp[M],fa[M];
int nw[M],right[M];
int pos[M],sum[M];

int newnode(int ss){
	stp[++tot]=ss;
	return tot;
}

int ext(int p,int q,int d){
	int nq=newnode(stp[p]+1);
	fa[nq]=fa[q]; fa[q]=nq;
	memcpy(ch[nq],ch[q],sizeof(ch[q]));
	for(;p&&ch[p][d]==q;p=fa[p]) ch[p][d]=nq;
	return nq;
}

int sam(int p,int d){
	int np=ch[p][d];
	if(np) return (stp[p]+1==stp[np]) ? np : ext(p,np,d);

	np=newnode(stp[p]+1);
	for(;p&&!ch[p][d];p=fa[p]) ch[p][d]=np;
	if(!p) fa[np]=1;
	else{
		int q=ch[p][d];
		fa[np]= (stp[p]+1==stp[q]) ? q : ext(p,q,d);
	}
	return np;
}

void match(){
	int p=1,len=0,i,d;
	n=strlen(s+1);
	for(i=1;i<=n;i++) s[i+n]=s[i];
	for(i=1;i<=n*2;i++){
		d=s[i]-'a';
		if(!ch[p][d]){
			for(;p&&!ch[p][d];p=fa[p]);
			if(!p) p=1,len=0;
			else{
				len=stp[p]+1;
				p=ch[p][d];
				nw[p]=max(nw[p],len);
			}
		}
		else{
			len++;
			p=ch[p][d];
			nw[p]=max(nw[p],len);
		}
	}

	int res=0;
	for(i=tot;i>0;i--){
		p=pos[i];
		nw[p]=min(nw[p],stp[p]);
		if(nw[p]>=n&&n>stp[fa[p]])res+=right[p];
		if(fa[p]) nw[fa[p]]=max(nw[fa[p]],nw[p]);
		nw[p]=0;
	}
	printf("%d\n",res);
}

int main(){
/*
	#ifndef ONLINE_JUDGE
	freopen("a.in","r",stdin);
	#endif
*/
	int i;

	scanf("%s",s+1);
	n=strlen(s+1);
	last=tot=1;
	for(i=1;i<=n;i++){
		last=sam(last,s[i]-'a');
		right[last]=1;
	}
	for(i=1;i<=tot;i++) sum[stp[i]]++;
	for(i=1;i<=tot;i++) sum[i]+=sum[i-1];
	for(i=1;i<=tot;i++) pos[sum[stp[i]]--]=i;
	for(i=tot;i>1;i--) right[fa[pos[i]]]+=right[pos[i]];

	scanf("%d",&m);
	while(m--){
		scanf("%s",s+1);
		match();
	}

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
using namespace std;
const int M=2000007;

char s[M];
int n,m;
int last,tot;
int ch[M][26];
int stp[M],fa[M];
int nw[M],right[M];
int pos[M],sum[M];
int vis[M],T;

int newnode(int ss){
	stp[++tot]=ss;
	return tot;
}

int ext(int p,int q,int d){
	int nq=newnode(stp[p]+1);
	fa[nq]=fa[q]; fa[q]=nq;
	memcpy(ch[nq],ch[q],sizeof(ch[q]));
	for(;p&&ch[p][d]==q;p=fa[p]) ch[p][d]=nq;
	return nq;
}

int sam(int p,int d){
	int np=ch[p][d];
	if(np) return (stp[p]+1==stp[np]) ? np : ext(p,np,d);

	np=newnode(stp[p]+1);
	for(;p&&!ch[p][d];p=fa[p]) ch[p][d]=np;
	if(!p) fa[np]=1;
	else{
		int q=ch[p][d];
		fa[np]= (stp[p]+1==stp[q]) ? q : ext(p,q,d);
	}
	return np;
}

void match(){
	int p=1,len=0,i,d;
	int res=0;
	n=strlen(s+1);
	for(i=1;i<=n;i++) s[i+n]=s[i];
	for(i=1;i<=n*2;i++){
		d=s[i]-'a';
		if(!ch[p][d]){
			for(;p&&!ch[p][d];p=fa[p]);
			if(!p) p=1,len=0;
			else{
				len=stp[p]+1;
				p=ch[p][d];
				nw[p]=max(nw[p],len);
			}
		}
		else{
			len++;
			p=ch[p][d];
			nw[p]=max(nw[p],len);
		}
		if(len>=n&&n>stp[fa[p]]&&vis[p]!=T){
			vis[p]=T;
			res+=right[p];
		}
	}
	printf("%d\n",res);
}

int main(){

	#ifndef ONLINE_JUDGE
	freopen("a.in","r",stdin);
	#endif

	int i;

	scanf("%s",s+1);
	n=strlen(s+1);
	last=tot=1;
	for(i=1;i<=n;i++){
		last=sam(last,s[i]-'a');
		right[last]=1;
	}
	for(i=1;i<=tot;i++) sum[stp[i]]++;
	for(i=1;i<=tot;i++) sum[i]+=sum[i-1];
	for(i=1;i<=tot;i++) pos[sum[stp[i]]--]=i;
	for(i=tot;i>1;i--) right[fa[pos[i]]]+=right[pos[i]];

	scanf("%d",&m);
	while(m--){
		scanf("%s",s+1);
		T++;
		match();
	}

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
using namespace std;
const int M=2000007;

char s[M];
int n,m;
int last,tot;
int ch[M][26];
int stp[M],fa[M];
int right[M];
int pos[M],sum[M];
int vis[M],T;

int newnode(int ss){
	stp[++tot]=ss;
	return tot;
}

int ext(int p,int q,int d){
	int nq=newnode(stp[p]+1);
	fa[nq]=fa[q]; fa[q]=nq;
	memcpy(ch[nq],ch[q],sizeof(ch[q]));
	for(;p&&ch[p][d]==q;p=fa[p]) ch[p][d]=nq;
	return nq;
}

int sam(int p,int d){
	int np=ch[p][d];
	if(np) return (stp[p]+1==stp[np]) ? np : ext(p,np,d);

	np=newnode(stp[p]+1);
	for(;p&&!ch[p][d];p=fa[p]) ch[p][d]=np;
	if(!p) fa[np]=1;
	else{
		int q=ch[p][d];
		fa[np]= (stp[p]+1==stp[q]) ? q : ext(p,q,d);
	}
	return np;
}

void match(){
	int p=1,len=0,i,d;
	int res=0;
	n=strlen(s+1);
	for(i=1;i<=n;i++) s[i+n]=s[i];
	for(i=1;i<=n*2;i++){
		d=s[i]-'a';
		if(!ch[p][d]){
			for(;p&&!ch[p][d];p=fa[p]);
			if(!p) p=1,len=0;
			else{
				len=stp[p]+1;
				p=ch[p][d];
			}
		}
		else{
			len++;
			p=ch[p][d];
		}

		if(len>=n){
			for(;n<=stp[fa[p]];p=fa[p]);
			len=stp[p];
			if(vis[p]!=T){
				vis[p]=T;
				res+=right[p];
			}
		}
	}
	printf("%d\n",res);
}

int main(){

	#ifndef ONLINE_JUDGE
	freopen("a.in","r",stdin);
	#endif

	int i;

	scanf("%s",s+1);
	n=strlen(s+1);
	last=tot=1;
	for(i=1;i<=n;i++){
		last=sam(last,s[i]-'a');
		right[last]=1;
	}
	for(i=1;i<=tot;i++) sum[stp[i]]++;
	for(i=1;i<=tot;i++) sum[i]+=sum[i-1];
	for(i=1;i<=tot;i++) pos[sum[stp[i]]--]=i;
	for(i=tot;i>1;i--) right[fa[pos[i]]]+=right[pos[i]];

	scanf("%d",&m);
	while(m--){
		scanf("%s",s+1);
		T++;
		match();
	}

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
using namespace std;
const int M=2000007;

char s[M];
int n,m;
int last,tot;
int ch[M][26];
int stp[M],fa[M];
int right[M];
int pos[M],sum[M];
int vis[M],T;

int newnode(int ss){
	stp[++tot]=ss;
	return tot;
}

int ext(int p,int q,int d){
	int nq=newnode(stp[p]+1);
	fa[nq]=fa[q]; fa[q]=nq;
	memcpy(ch[nq],ch[q],sizeof(ch[q]));
	for(;p&&ch[p][d]==q;p=fa[p]) ch[p][d]=nq;
	return nq;
}

int sam(int p,int d){
	int np=ch[p][d];
	if(np) return (stp[p]+1==stp[np]) ? np : ext(p,np,d);

	np=newnode(stp[p]+1);
	for(;p&&!ch[p][d];p=fa[p]) ch[p][d]=np;
	if(!p) fa[np]=1;
	else{
		int q=ch[p][d];
		fa[np]= (stp[p]+1==stp[q]) ? q : ext(p,q,d);
	}
	return np;
}

void match(){
	int p=1,len=0,i,d;
	int res=0;
	n=strlen(s+1);
	for(i=1;i<=n;i++) s[i+n]=s[i];
	for(i=1;i<=n*2;i++){
		d=s[i]-'a';
		if(!ch[p][d]){
			for(;p&&!ch[p][d];p=fa[p]);
			if(!p) p=1,len=0;
			else{
				len=stp[p]+1;
				p=ch[p][d];
			}
		}
		else{
			len++;
			p=ch[p][d];
		}

		if(len>=n){
			int q;//**
			for(q=p;n<=stp[fa[q]];q=fa[q]);
			if(vis[q]!=T){
				vis[q]=T;
				res+=right[q];
			}
		}
	}
	printf("%d\n",res);
}

int main(){

	#ifndef ONLINE_JUDGE
	freopen("a.in","r",stdin);
	#endif

	int i;

	scanf("%s",s+1);
	n=strlen(s+1);
	last=tot=1;
	for(i=1;i<=n;i++){
		last=sam(last,s[i]-'a');
		right[last]=1;
	}
	for(i=1;i<=tot;i++) sum[stp[i]]++;
	for(i=1;i<=tot;i++) sum[i]+=sum[i-1];
	for(i=1;i<=tot;i++) pos[sum[stp[i]]--]=i;
	for(i=tot;i>1;i--) right[fa[pos[i]]]+=right[pos[i]];

	scanf("%d",&m);
	while(m--){
		scanf("%s",s+1);
		T++;
		match();
	}

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
using namespace std;
const int M=2000007;

char s[M];
int n,m;
int last,tot;
int ch[M][26];
int stp[M],fa[M];
int right[M];
int pos[M],sum[M];
int vis[M],T;

int newnode(int ss){
	stp[++tot]=ss;
	return tot;
}

int ext(int p,int q,int d){
	int nq=newnode(stp[p]+1);
	fa[nq]=fa[q]; fa[q]=nq;
	memcpy(ch[nq],ch[q],sizeof(ch[q]));
	for(;p&&ch[p][d]==q;p=fa[p]) ch[p][d]=nq;
	return nq;
}

int sam(int p,int d){
	int np=ch[p][d];
	if(np) return (stp[p]+1==stp[np]) ? np : ext(p,np,d);

	np=newnode(stp[p]+1);
	for(;p&&!ch[p][d];p=fa[p]) ch[p][d]=np;
	if(!p) fa[np]=1;
	else{
		int q=ch[p][d];
		fa[np]= (stp[p]+1==stp[q]) ? q : ext(p,q,d);
	}
	return np;
}

void match(){
	int p=1,len=0,i,d;
	int res=0;
	n=strlen(s+1);
	for(i=1;i<=n;i++) s[i+n]=s[i];
	for(i=1;i<=n*2;i++){
		d=s[i]-'a';
		if(!ch[p][d]){
			for(;p&&!ch[p][d];p=fa[p]);
			if(!p) p=1,len=0;
			else{
				len=stp[p]+1;
				p=ch[p][d];
			}
		}
		else{
			len++;
			p=ch[p][d];
		}

		if(len>=n){
			for(;n<=stp[fa[p]];p=fa[p]);
			if(vis[p]!=T){
				vis[p]=T;
				res+=right[p];
			}
		}
	}
	printf("%d\n",res);
}

int main(){

	int i;

	scanf("%s",s+1);
	n=strlen(s+1);
	last=tot=1;
	for(i=1;i<=n;i++){
		last=sam(last,s[i]-'a');
		right[last]=1;
	}
	for(i=1;i<=tot;i++) sum[stp[i]]++;
	for(i=1;i<=tot;i++) sum[i]+=sum[i-1];
	for(i=1;i<=tot;i++) pos[sum[stp[i]]--]=i;
	for(i=tot;i>1;i--) right[fa[pos[i]]]+=right[pos[i]];

	scanf("%d",&m);
	while(m--){
		scanf("%s",s+1);
		T++;
		match();
	}

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
using namespace std;
const int M=2000007;

char s[M];
int n,m;
int last,tot;
int ch[M][26];
int stp[M],fa[M];
int right[M];
int pos[M],sum[M];
int vis[M],T;

int newnode(int ss){
	stp[++tot]=ss;
	return tot;
}

int ext(int p,int q,int d){
	int nq=newnode(stp[p]+1);
	fa[nq]=fa[q]; fa[q]=nq;
	memcpy(ch[nq],ch[q],sizeof(ch[q]));
	for(;p&&ch[p][d]==q;p=fa[p]) ch[p][d]=nq;
	return nq;
}

int sam(int p,int d){
	int np=ch[p][d];
	if(np) return (stp[p]+1==stp[np]) ? np : ext(p,np,d);

	np=newnode(stp[p]+1);
	for(;p&&!ch[p][d];p=fa[p]) ch[p][d]=np;
	if(!p) fa[np]=1;
	else{
		int q=ch[p][d];
		fa[np]= (stp[p]+1==stp[q]) ? q : ext(p,q,d);
	}
	return np;
}

void match(){
	int p=1,len=0,i,d;
	int res=0;
	n=strlen(s+1);
	for(i=1;i<=n;i++) s[i+n]=s[i];
	for(i=1;i<=n*2;i++){
		d=s[i]-'a';
		if(!ch[p][d]){
			for(;p&&!ch[p][d];p=fa[p]);
			if(!p) p=1,len=0;
			else{
				len=stp[p]+1;
				p=ch[p][d];
			}
		}
		else{
			len++;
			p=ch[p][d];
		}

		if(len>=n){
			for(;n<=stp[fa[p]];p=fa[p]);
			len=n;
			if(vis[p]!=T){
				vis[p]=T;
				res+=right[p];
			}
		}
	}
	printf("%d\n",res);
}

int main(){

	#ifndef ONLINE_JUDGE
	freopen("a.in","r",stdin);
	#endif

	int i;

	scanf("%s",s+1);
	n=strlen(s+1);
	last=tot=1;
	for(i=1;i<=n;i++){
		last=sam(last,s[i]-'a');
		right[last]=1;
	}
	for(i=1;i<=tot;i++) sum[stp[i]]++;
	for(i=1;i<=tot;i++) sum[i]+=sum[i-1];
	for(i=1;i<=tot;i++) pos[sum[stp[i]]--]=i;
	for(i=tot;i>1;i--) right[fa[pos[i]]]+=right[pos[i]];

	scanf("%d",&m);
	while(m--){
		scanf("%s",s+1);
		T++;
		match();
	}

	return 0;
}


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

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

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

int tot;

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

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

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

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

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

  }
  last = np;
}

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

int c[Maxn], topo[Maxn];

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

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

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

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

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

  State *now = rt;

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

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

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

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

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


#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

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

  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1, u, v; i <= m; ++i) {
    scanf("%d%d", &u, &v);
    G[u][v] = G[v][u] = 1;
  }

  return 0;
}