#include <bits/stdc++.h>
using namespace std;
const int N = 5e4 + 5, M = 5.5e5 + 5;
int n, Q, ans;
char s[N], a[5], b[5];
map<pair<int, int>, int> mp;
vector<int> p[M];
int ID(char* s, int fi, int l) {
  int id = 0;
  for (int i = 0; i < l; ++i) id = id * 26 + s[fi + i] - 'a' + 1;
  return id;
}
int main() {
  scanf("%s%d", s + 1, &Q), n = strlen(s + 1);
  for (int l = 1; l < 5; ++l)
    for (int i = 1; i <= n - l + 1; ++i) p[ID(s, i, l)].push_back(i);
  while (Q--) {
    scanf("%s%s", a + 1, b + 1), ans = M;
    int l1 = strlen(a + 1), l2 = strlen(b + 1);
    int d1 = ID(a, 1, l1), d2 = ID(b, 1, l2), tmp;
    if (d1 > d2) swap(d1, d2), swap(l1, l2), swap(a, b);
    int s1 = p[d1].size(), s2 = p[d2].size();
    if (tmp = mp[make_pair(d1, d2)]) {
      printf("%d\n", tmp);
      continue;
    }
    for (int p1 = 0, p2 = 0; p1 < s1 && p2 < s2; ++p1) {
      while (p2 < s2 && p[d2][p2] < p[d1][p1]) ++p2;
      if (p2 < s2)
        ans = min(ans, max(p[d1][p1] + l1, p[d2][p2] + l2) - p[d1][p1]);
    }
    for (int p1 = 0, p2 = 0; p1 < s1 && p2 < s2; ++p2) {
      while (p1 < s1 && p[d1][p1] < p[d2][p2]) ++p1;
      if (p1 < s1)
        ans = min(ans, max(p[d1][p1] + l1, p[d2][p2] + l2) - p[d2][p2]);
    }
    if (ans == M) ans = -1;
    printf("%d\n", mp[make_pair(d1, d2)] = ans);
  }
  return 0;
}
