#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T ABS(T a) {
  return a > 0 ? a : -a;
}
template <typename T>
inline T MIN(T a, T b) {
  return a < b ? a : b;
}
template <typename T>
inline T MAX(T a, T b) {
  return a > b ? a : b;
}
template <typename T>
inline T CHKMIN(T &a, T b) {
  if (a > b) a = b;
  return a;
}
template <typename T>
inline T CHKMAX(T &a, T b) {
  if (a < b) a = b;
  return a;
}
template <typename T>
inline void SWAP(T &a, T &b) {
  static T c;
  c = a;
  a = b;
  b = c;
}
template <typename T, typename... T0>
T MAX(T a, T b, T0... c) {
  return a > b ? MAX(a, c...) : MAX(b, c...);
}
template <typename T, typename... T0>
T MIN(T a, T b, T0... c) {
  return a < b ? MIN(a, c...) : MIN(b, c...);
}
template <typename T, int n>
void myin(T a[]) {
  for (int i = (0); i < (int)(n); ++i) cin >> a[i];
}
template <typename T>
void myin(T &a) {
  cin >> a;
}
template <typename T>
void print(T a) {
  cout << a << ' ';
}
template <typename T, typename... T0>
void print(T a, T0... b) {
  print(a);
  print(b...);
}
template <typename T>
void println(T a) {
  cout << a << endl;
}
template <typename T, typename... T0>
void println(T a, T0... b) {
  print(a);
  println(b...);
}
typedef int (*CMP)(const void *, const void *);
template <typename T>
int ltcmp(const T *a, const T *b) {
  if (*a != *b) return *a < *b ? -1 : 1;
  return 0;
}
template <typename T, unsigned int n>
int ltcmp(const T a[], const T b[]) {
  for (int i = (0); i < (int)(n); ++i)
    if (a[i] != b[i]) return a[i] < b[i] ? -1 : 1;
  return 0;
}
template <typename T>
int gtcmp(const T *a, const T *b) {
  if (*a != *b) return *a > *b ? -1 : 1;
  return 0;
}
template <typename T, unsigned int n>
int gtcmp(const T a[], const T b[]) {
  for (int i = (0); i < (int)(n); ++i)
    if (a[i] != b[i]) return a[i] > b[i] ? -1 : 1;
  return 0;
}
template <typename T>
T bsrch(T beg, T end, std::function<int(T)> f) {
  T mid;
  while (beg < end) {
    mid = (beg + end) / 2;
    if (f(mid)) {
      end = mid;
    } else {
      beg = mid + 1;
    }
  }
  return end;
}
char in[60000], A[10], B[10];
vector<int> vs[600000];
map<pair<int, int>, int> mk;
int n, len, a, b;
int c2i(char *s) {
  int ret = 0;
  for (int i = 0; s[i]; ++i) {
    ret = ret * 27 + s[i] - 'a' + 1;
  }
  return ret;
}
void init() {
  for (int i = (0); i < (int)(len); ++i) {
    int x = 0;
    for (int j = (0); j < (int)(4); ++j) {
      if (i + j >= len) break;
      x = x * 27 + in[i + j] - 'a' + 1;
      vs[x].push_back(i);
    }
  }
}
int main() {
  while (scanf("%s", in) != EOF) {
    len = strlen(in);
    for (int i = (0); i < (int)(600000); ++i) vs[i].clear();
    mk.clear();
    init();
    scanf("%d", &n);
    while (n--) {
      scanf("%s %s", A, B);
      a = c2i(A);
      b = c2i(B);
      int ans;
      if (mk.find(make_pair(a, b)) == mk.end()) {
        ans = len + 10;
        int blen = strlen(B);
        int alen = strlen(A);
        int i = 0, j = 0;
        while (i < vs[a].size() && j < vs[b].size()) {
          CHKMIN(ans, MAX(vs[b][j] + blen, vs[a][i] + alen) -
                          MIN(vs[a][i], vs[b][j]));
          if (vs[a][i] == vs[b][j]) break;
          if (vs[a][i] < vs[b][j])
            ++i;
          else
            ++j;
        }
        if (ans > len) ans = -1;
        mk[make_pair(a, b)] = ans;
        mk[make_pair(b, a)] = ans;
      } else {
        ans = mk[make_pair(a, b)];
      }
      printf("%d\n", ans);
    }
  }
}
