#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define rep(i,x,y) for(i=x;i<=y;i++)
#define pb push_back
#define mp make_pair
#define upmin(x,y) x=min(x,y)
const int L=50010,R=4,N=L*R,Q=100010,inf=int(1e9);
int l,q,g,all,i,j,k,p,x,y,p1,p2,res,tot;char s[L],s0[11];
int h[600000],ans[Q],d[R+1][L],tmp[N],li[N],l2[N],pos[N],num[N],vg[N],la[L],ne[L];vector<int> v[N];
struct ask{int x,y,lx,ly,nn;}c[Q];
bool cmp(ask a,ask b){return mp(a.x,a.y)<mp(b.x,b.y);}
int ins(int x){if(h[x])return h[x];num[++tot]=x;return h[x]=tot;}
int getstr(int&l0){
scanf("%s",s0+1);int i,res;l0=strlen(s0+1);
for(res=0,i=1;i<=l0;i++)res=res*27+(s0[i]-'a'+1);return res;
}
void calcd(int id,int l0){
int i,j,k,la0,ne0,now;
rep(j,1,R){
for(int k=0;k<=l;k++)la[k]=-inf,ne[k]=inf;
for(int k=0;k<vg[id];k++){
now=v[id][k];
la0=(k==0)?1:v[id][k-1]+1;
ne0=(k==vg[id]-1)?l:v[id][k+1]-1;
rep(i,now,ne0)la[i]=now;
rep(i,la0,now)ne[i]=now;
}
rep(i,1,l)d[j][i]=min(max(i+j,ne[i]+l0)-i,max(i+j,la[i]+l0)-la[i]);
}
}
int main(){
scanf("%s%d",s+1,&q);l=strlen(s+1);
rep(i,1,l)for(res=0,j=1;j<=R&&i+j-1<=l;j++)
l2[++all]=j,v[pos[all]=ins(res=res*27+(s[i+j-1]-'a'+1))].pb(li[all]=i);
rep(i,1,tot)vg[i]=v[i].size();
rep(i,1,q){
int ida=h[getstr(p1)],idb=h[getstr(p2)];
if(ida==0||idb==0)ans[i]=-1;
else{ans[i]=inf;if(vg[ida]<vg[idb])swap(ida,ida);c[++g]=(ask){ida,idb,p1,p2,i};}
}
sort(c+1,c+1+g,cmp);
for(i=1;i<=g;i=j+1){
long long totcnt=0;
for(j=i;j<=g&&c[j].x==c[i].x;j++)totcnt+=vg[c[j].y]+vg[c[i].x];j--;
if(totcnt>all){
calcd(c[i].x,c[i].lx);
rep(k,1,tot)tmp[k]=inf;
rep(k,1,all)upmin(tmp[pos[k]],d[l2[k]][li[k]]);
rep(k,i,j)upmin(ans[c[k].nn],tmp[c[k].y]);
}
else
rep(k,i,j)
for(p1=p2=0,x=c[k].x,y=c[k].y;p1<vg[x]&&p2<vg[y];v[x][p1]<=v[y][p2]?p1++:p2++)
upmin(ans[c[k].nn],max(v[x][p1]+c[k].lx,v[y][p2]+c[k].ly)-min(v[x][p1],v[y][p2]));
}
rep(i,1,q)printf("%d\n",ans[i]);
return 0;
}
I2luY2x1ZGU8Y3N0ZGlvPgojaW5jbHVkZTxjc3RyaW5nPgojaW5jbHVkZTxhbGdvcml0aG0+CiNpbmNsdWRlPHZlY3Rvcj4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSByZXAoaSx4LHkpIGZvcihpPXg7aTw9eTtpKyspCiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgbXAgbWFrZV9wYWlyCiNkZWZpbmUgdXBtaW4oeCx5KSB4PW1pbih4LHkpCmNvbnN0IGludCBMPTUwMDEwLFI9NCxOPUwqUixRPTEwMDAxMCxpbmY9aW50KDFlOSk7CmludCBsLHEsZyxhbGwsaSxqLGsscCx4LHkscDEscDIscmVzLHRvdDtjaGFyIHNbTF0sczBbMTFdOwppbnQgaFs2MDAwMDBdLGFuc1tRXSxkW1IrMV1bTF0sdG1wW05dLGxpW05dLGwyW05dLHBvc1tOXSxudW1bTl0sdmdbTl0sbGFbTF0sbmVbTF07dmVjdG9yPGludD4gdltOXTsKc3RydWN0IGFza3tpbnQgeCx5LGx4LGx5LG5uO31jW1FdOwpib29sIGNtcChhc2sgYSxhc2sgYil7cmV0dXJuIG1wKGEueCxhLnkpPG1wKGIueCxiLnkpO30KaW50IGlucyhpbnQgeCl7aWYoaFt4XSlyZXR1cm4gaFt4XTtudW1bKyt0b3RdPXg7cmV0dXJuIGhbeF09dG90O30KaW50IGdldHN0cihpbnQmbDApewogICAgc2NhbmYoIiVzIixzMCsxKTtpbnQgaSxyZXM7bDA9c3RybGVuKHMwKzEpOwogICAgZm9yKHJlcz0wLGk9MTtpPD1sMDtpKyspcmVzPXJlcyoyNysoczBbaV0tJ2EnKzEpO3JldHVybiByZXM7Cn0Kdm9pZCBjYWxjZChpbnQgaWQsaW50IGwwKXsKICAgIGludCBpLGosayxsYTAsbmUwLG5vdzsKICAgIHJlcChqLDEsUil7CiAgICAgICAgZm9yKGludCBrPTA7azw9bDtrKyspbGFba109LWluZixuZVtrXT1pbmY7CiAgICAgICAgZm9yKGludCBrPTA7azx2Z1tpZF07aysrKXsKICAgICAgICAgICAgbm93PXZbaWRdW2tdOwogICAgICAgICAgICBsYTA9KGs9PTApPzE6dltpZF1bay0xXSsxOwogICAgICAgICAgICBuZTA9KGs9PXZnW2lkXS0xKT9sOnZbaWRdW2srMV0tMTsKICAgICAgICAgICAgcmVwKGksbm93LG5lMClsYVtpXT1ub3c7CiAgICAgICAgICAgIHJlcChpLGxhMCxub3cpbmVbaV09bm93OwogICAgICAgIH0KICAgICAgICByZXAoaSwxLGwpZFtqXVtpXT1taW4obWF4KGkraixuZVtpXStsMCktaSxtYXgoaStqLGxhW2ldK2wwKS1sYVtpXSk7CiAgICB9Cn0KaW50IG1haW4oKXsKICAgIHNjYW5mKCIlcyVkIixzKzEsJnEpO2w9c3RybGVuKHMrMSk7CglyZXAoaSwxLGwpZm9yKHJlcz0wLGo9MTtqPD1SJiZpK2otMTw9bDtqKyspCiAgICBsMlsrK2FsbF09aix2W3Bvc1thbGxdPWlucyhyZXM9cmVzKjI3KyhzW2krai0xXS0nYScrMSkpXS5wYihsaVthbGxdPWkpOwogICAgcmVwKGksMSx0b3QpdmdbaV09dltpXS5zaXplKCk7CiAgICByZXAoaSwxLHEpewogICAgICAgIGludCBpZGE9aFtnZXRzdHIocDEpXSxpZGI9aFtnZXRzdHIocDIpXTsKICAgICAgICBpZihpZGE9PTB8fGlkYj09MClhbnNbaV09LTE7CiAgICAgICAgZWxzZXthbnNbaV09aW5mO2lmKHZnW2lkYV08dmdbaWRiXSlzd2FwKGlkYSxpZGEpO2NbKytnXT0oYXNrKXtpZGEsaWRiLHAxLHAyLGl9O30KICAgIH0KICAgIHNvcnQoYysxLGMrMStnLGNtcCk7CiAgICBmb3IoaT0xO2k8PWc7aT1qKzEpewogICAgICAgIGxvbmcgbG9uZyB0b3RjbnQ9MDsKICAgICAgICBmb3Ioaj1pO2o8PWcmJmNbal0ueD09Y1tpXS54O2orKyl0b3RjbnQrPXZnW2Nbal0ueV0rdmdbY1tpXS54XTtqLS07CiAgICAgICAgaWYodG90Y250PmFsbCl7CiAgICAgICAgICAgIGNhbGNkKGNbaV0ueCxjW2ldLmx4KTsgICAgCiAgICAgICAgICAgIHJlcChrLDEsdG90KXRtcFtrXT1pbmY7CiAgICAgICAgICAgIHJlcChrLDEsYWxsKXVwbWluKHRtcFtwb3Nba11dLGRbbDJba11dW2xpW2tdXSk7CiAgICAgICAgICAgIHJlcChrLGksail1cG1pbihhbnNbY1trXS5ubl0sdG1wW2Nba10ueV0pOwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgcmVwKGssaSxqKQogICAgICAgIGZvcihwMT1wMj0wLHg9Y1trXS54LHk9Y1trXS55O3AxPHZnW3hdJiZwMjx2Z1t5XTt2W3hdW3AxXTw9dlt5XVtwMl0/cDErKzpwMisrKQogICAgICAgIHVwbWluKGFuc1tjW2tdLm5uXSxtYXgodlt4XVtwMV0rY1trXS5seCx2W3ldW3AyXStjW2tdLmx5KS1taW4odlt4XVtwMV0sdlt5XVtwMl0pKTsKICAgIH0KICAgIHJlcChpLDEscSlwcmludGYoIiVkXG4iLGFuc1tpXSk7CiAgICByZXR1cm4gMDsKfQo=