#include <bits/stdc++.h>
using namespace std;
namespace star {
const int maxn = 1e6 + 10, siz = 500, ub = 1 << 20, INF = 0x3f3f3f3f;
char s[maxn], A[5], B[5];
int cnt[1 << 21], a[maxn][4], n, ans[70000][410], big[70000];
int dfn[1 << 21], ldfn[maxn];
int id(char const *s, int len) {
  int res = 0;
  while (len) len--, res <<= 5, res += (*s++) + 1 - 'a';
  return res;
}
vector<int> c[70000];
inline int solve(int x, int y) {
  int lx = ldfn[x], ly = ldfn[y];
  int ans = n, l = 0, r = 0;
  while (l < (int)c[x].size() - 1 or r < (int)c[y].size() - 1)
    if (l == (int)c[x].size() - 1)
      ans = min(ans, max(c[x].at(l) + lx, c[y].at(r) + ly) -
                         min(c[x].at(l), c[y].at(r))),
      r++;
    else if (r == (int)c[y].size() - 1)
      ans = min(ans, max(c[x].at(l) + lx, c[y].at(r) + ly) -
                         min(c[x].at(l), c[y].at(r))),
      l++;
    else
      ans = min(ans, max(c[x].at(l) + lx, c[y].at(r) + ly) -
                         min(c[x].at(l), c[y].at(r))),
      (c[x].at(l) <= c[y].at(r) ? l++ : r++);
  ans = min(
      ans, max(c[x].at(l) + lx, c[y].at(r) + ly) - min(c[x].at(l), c[y].at(r)));
  return ans;
}
int Siz[1 << 21];
inline void work() {
  scanf("%s", s);
  n = strlen(s);
  for (int l = 0; l < 4; l++)
    for (int i = 0; i < n - l; i++) ++cnt[a[i][l] = id(s + i, l + 1)];
  for (int i = 1; i < ub; i++)
    if (cnt[i]) dfn[i] = ++dfn[0];
  for (int l = 0; l < 4; l++)
    for (int i = 0; i < n - l; i++)
      c[a[i][l] = dfn[a[i][l]]].push_back(i), ldfn[a[i][l]] = l + 1;
  memset(ans, 0x3f, sizeof ans);
  for (int i = 1; i <= dfn[0]; i++)
    if (c[i].size() >= siz) {
      big[i] = ++big[0];
      for (int p = 0; p < c[i].size() - 1; p++) {
        int x = c[i][p], y = c[i][p + 1];
        for (int k = x; k < y; k++)
          for (int l = 0; l < 4; l++)
            ans[a[k][l]][big[i]] =
                min(ans[a[k][l]][big[i]], min(max(x + ldfn[i], k + l + 1) - x,
                                              max(y + ldfn[i], k + l + 1) - k));
      }
      for (int k = 0; k < c[i][0]; k++)
        for (int l = 0; l < 4; l++)
          ans[a[k][l]][big[i]] =
              min(ans[a[k][l]][big[i]], max(c[i][0] + ldfn[i], k + l + 1) - k);
      for (int k = c[i].back(); k < n; k++)
        for (int l = 0; l < 4; l++)
          ans[a[k][l]][big[i]] =
              min(ans[a[k][l]][big[i]],
                  max(c[i].back() + ldfn[i], k + l + 1) - c[i].back());
    }
  int Q;
  scanf("%d", &Q);
  while (Q--) {
    scanf("%s %s", A, B);
    int x = dfn[id(A, strlen(A))], y = dfn[id(B, strlen(B))];
    if (!x or !y)
      puts("-1");
    else if (big[x])
      printf("%d\n", ans[y][big[x]]);
    else if (big[y])
      printf("%d\n", ans[x][big[y]]);
    else
      printf("%d\n", solve(x, y));
  }
}
}  // namespace star
signed main() {
  try {
    star::work();
  } catch (...) {
    exit(123456789);
  }
  return 0;
}
