#include <bits/stdc++.h>
int ct, n, m, tct, cnt[540010], mp[540010], t[540010], a[50010][4],
    lmax = 27 * 27 * 27 * 27, B, pool[200010], *pt(pool),
    *v[540010], ans[70010][402];
char s[50010], x[10], y[10];
int nd(const char* st, const char* ed) {
  int res = 0;
  while (st != ed) res = res * 27 + (*st++ - 'a' + 1);
  return res;
}
int leng(int i) {
  if (i < 27) return 1;
  if (i < 27 * 27) return 2;
  if (i < 27 * 27 * 27) return 3;
  return 4;
}
int main() {
  scanf("%s%d", s, &m);
  n = strlen(s);
  B = 500;
  for (int i = 0; i < n; i++) {
    if (i <= n - 4) ++cnt[a[i][3] = nd(s + i, s + i + 4)];
    if (i <= n - 3) ++cnt[a[i][2] = nd(s + i, s + i + 3)];
    if (i <= n - 2) ++cnt[a[i][1] = nd(s + i, s + i + 2)];
    ++cnt[a[i][0] = nd(s + i, s + i + 1)];
  }
  for (int i = 1; i < lmax; i++)
    if (cnt[i]) {
      mp[i] = ++ct;
      v[mp[i]] = pt, pt += cnt[i];
      cnt[i] = 0;
    }
  for (int k = 0; k <= 3; k++)
    for (int j = 0; j < n - k; j++) {
      int p = mp[a[j][k]];
      v[p][cnt[p]++] = j;
    }
  for (int i = 1; i <= lmax; i++)
    if (cnt[mp[i]] >= B) {
      t[mp[i]] = ++tct;
      int q = t[mp[i]], d = leng(i);
      for (int j = 1; j <= ct; j++) ans[j][q] = 0x3f3f3f3f;
      int rl = -5, ll = -5;
      for (int k = 0; k <= 3; k++) {
        rl = -5;
        for (int j = 0; j < n - k; j++) {
          int p = mp[a[j][k]];
          if (a[j][d - 1] == i) ll = j - 1, rl = j + d - 1;
          if (rl != -5)
            ans[p][q] = std::min(ans[p][q], std::max(j + k, rl) - ll);
        }
      }
      for (int k = 0; k <= 3; k++) {
        rl = -5;
        for (int j = n - k - 1; ~j; j--) {
          int p = mp[a[j][k]];
          if (a[j][d - 1] == i) ll = j, rl = j + d;
          if (rl != -5)
            ans[p][q] =
                std::min(ans[p][q], std::max(rl, j + k + 1) - std::min(ll, j));
        }
      }
      ans[mp[i]][q] = d;
    }
  while (m--) {
    scanf("%s%s", x, y);
    int xw = strlen(x), yw = strlen(y);
    int x1 = nd(x, x + xw), y1 = nd(y, y + yw);
    if (!mp[x1] || !mp[y1]) {
      puts("-1");
    } else {
      x1 = mp[x1], y1 = mp[y1];
      if (cnt[x1] > cnt[y1]) x1 ^= y1 ^= x1 ^= y1, xw ^= yw ^= xw ^= yw;
      if (cnt[y1] >= B) {
        printf("%d\n", ans[x1][t[y1]]);
      } else {
        int i = 0, j = 0, ans = 0x3f3f3f3f;
        while (i < cnt[x1] && j < cnt[y1]) {
          if (v[x1][i] < v[y1][j])
            ans = std::min(ans,
                           std::max(v[x1][i] + xw, v[y1][j] + yw) - v[x1][i]),
            ++i;
          else
            ans = std::min(ans,
                           std::max(v[x1][i] + xw, v[y1][j] + yw) - v[y1][j]),
            ++j;
        }
        printf("%d\n", ans);
      }
    }
  }
  return 0;
}
