#include <bits/stdc++.h>
using namespace std;
namespace FGF {
int n, m;
const int N = 1e5 + 5;
char s[N], a[10], b[10];
int mp[500005], id, pos[8][N], h[N], cnt, f[510][N], len[N];
vector<int> g[N];
int getnum(char *s, int l) {
  int num = 0;
  for (int i = 0; i < l; i++) num = num * 26 + s[i] - 'a' + 1;
  return num;
}
void work() {
  scanf("%s%d", s + 1, &m);
  n = strlen(s + 1);
  for (int l = 1; l < 5; l++) {
    for (int i = 1; i <= n - l + 1; i++) {
      int num = getnum(s + i, l);
      if (!mp[num]) mp[num] = ++id, len[id] = l;
      pos[l][i] = mp[num];
      g[mp[num]].push_back(i);
    }
  }
  memset(f, 0x3f, sizeof(f));
  for (int i = 1; i <= id; i++) {
    int siz = g[i].size();
    if (siz >= 500) {
      h[i] = ++cnt;
      for (int j = 1; j < siz; j++) {
        int x = g[i][j - 1], y = g[i][j];
        for (int k = x; k <= y; k++)
          for (int l = 1; l <= 4; l++)
            f[cnt][pos[l][k]] = min(
                f[cnt][pos[l][k]],
                min(max(x + len[i], k + l) - x, max(y + len[i], k + l) - k));
      }
      for (int k = g[i][siz - 1], x = k; k <= n; k++)
        for (int l = 1; l <= 4; l++)
          f[cnt][pos[l][k]] =
              min(f[cnt][pos[l][k]], max(x + len[i], k + l) - x);
      for (int k = 1, x = g[i][0]; k < g[i][0]; k++)
        for (int l = 1; l <= 4; l++)
          f[cnt][pos[l][k]] =
              min(f[cnt][pos[l][k]], max(x + len[i], k + l) - k);
    }
  }
  while (m--) {
    scanf("%s%s", a + 1, b + 1);
    int x = mp[getnum(a + 1, strlen(a + 1))],
        y = mp[getnum(b + 1, strlen(b + 1))];
    if (!x || !y)
      puts("-1");
    else if (h[x])
      printf("%d\n", f[h[x]][y]);
    else if (h[y])
      printf("%d\n", f[h[y]][x]);
    else {
      int siz1 = g[x].size(), siz2 = g[y].size(), k = 0, ans = 0x3f3f3f3f;
      for (int i = 0; i < siz1; i++) {
        while (k < siz2 - 1 && g[x][i] > g[y][k]) k++;
        ans = min(ans, max(g[x][i] + len[x], g[y][k] + len[y]) -
                           min(g[x][i], g[y][k]));
        if (k)
          ans = min(ans, max(g[x][i] + len[x], g[y][k - 1] + len[y]) -
                             min(g[x][i], g[y][k - 1]));
      }
      printf("%d\n", ans);
    }
  }
}
}  // namespace FGF
int main() {
  FGF::work();
  return 0;
}
