#include <bits/stdc++.h>
using namespace std;
int n, nd, ed, hcnt;
int lbl[555000];
int q[555000], v[555000], cnt[555000], h[555000], ptr[555000], q1[555000],
    q2[555000], len[555000];
int l[555000], qd[200005], qn[200005], ql[200005], ret[200005];
vector<int> occ[555000];
char s[50005];
const int Thd = 300;
unordered_map<int, int> Ans[555000];
int add(char s[]) {
  int r = 0;
  for (int i = 0; s[i]; ++i) {
    r = r * 27 + (s[i] - 'a' + 1);
  }
  if (!lbl[r]) lbl[r] = ++nd, len[nd] = strlen(s);
  return lbl[r];
}
void match(char s[], int phase) {
  memset(v, -1, sizeof(v));
  for (int i = 0; s[i]; i++) {
    for (int j = (1); j <= (4); j++) {
      if (i + 1 < j) break;
      int r = 0;
      for (int k = (i - j + 1); k <= (i); k++) r = r * 27 + (s[k] - 'a' + 1);
      if (!lbl[r]) continue;
      int t = lbl[r];
      if (!phase) {
        v[t] = i - j + 1;
        occ[t].push_back(i);
        if (cnt[t] <= Thd) {
          for (int j = (l[t]); j; j = qn[j]) {
            if (v[qd[j]] >= 0)
              ret[ql[j]] = min(ret[ql[j]],
                               max(i - v[qd[j]] + 1, max(len[t], len[qd[j]])));
          }
        }
      } else {
        int beg = i - j + 1;
        for (int j = (0); j < (hcnt); j++) {
          while (ptr[j] < occ[h[j]].size() && occ[h[j]][ptr[j]] < i) ++ptr[j];
          if (ptr[j] == occ[h[j]].size()) continue;
          int cur = occ[h[j]][ptr[j]] - beg + 1;
          cur = max(cur, max(len[t], len[h[j]]));
          Ans[t][h[j]] = min(Ans[t][h[j]], cur);
        }
      }
    }
  }
}
void add(int x, int y, int w) {
  qd[++ed] = y, qn[ed] = l[x], l[x] = ed, ql[ed] = w, ++cnt[x];
}
int main() {
  scanf("%s", s);
  scanf("%d", &n);
  for (int i = (0); i < (n); i++) {
    char s1[6], s2[6];
    int a, b;
    scanf("%s%s", s1, s2);
    q1[i] = a = add(s1);
    q2[i] = b = add(s2);
    add(a, b, i);
    add(b, a, i);
    Ans[a][b] = Ans[b][a] = ret[i] = (1 << 30);
  }
  for (int i = (0); i < (555000); i++) {
    if (cnt[i] > Thd) ptr[hcnt] = 0, h[hcnt++] = i;
  }
  match(s, 0);
  match(s, 1);
  for (int i = (0); i < (n); i++) {
    ret[i] = min(ret[i], min(Ans[q1[i]][q2[i]], Ans[q2[i]][q1[i]]));
    printf("%d\n", ret[i] >= (1 << 30) ? -1 : ret[i]);
  }
  return 0;
}
