#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void umax(T &a, T b) {
  if (a < b) a = b;
}
template <class T>
inline void umin(T &a, T b) {
  if (a > b) a = b;
}
template <class T>
inline T abs(T a) {
  return a > 0 ? a : -a;
}
template <class T>
inline T gcd(T a, T b) {
  return __gcd(a, b);
}
template <class T>
inline T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
const int inf = 1e9 + 143;
const long long longinf = 1e18 + 143;
inline int read() {
  int x;
  scanf(" %d", &x);
  return x;
}
const int N = 2000555;
int n;
char s[N];
vector<int> loc[N];
map<pair<int, int>, int> seen;
int calc(int h1, int h2, int len1, int len2) {
  if (loc[h1].size() > loc[h2].size()) {
    swap(h1, h2);
    swap(len1, len2);
  }
  int ans = inf;
  for (int i = 0; i < loc[h1].size(); i++) {
    int x = loc[h1][i];
    int y = lower_bound(loc[h2].begin(), loc[h2].end(), x) - loc[h2].begin();
    0;
    if (y < loc[h2].size()) {
      0;
      umin(ans, max(loc[h2][y] + len2 - 1, x + len1 - 1) - x + 1);
    }
    if (y - 1 >= 0) {
      --y;
      0;
      umin(ans, max(loc[h2][y] + len2 - 1, x + len1 - 1) - loc[h2][y] + 1);
    }
  }
  return ans;
}
int main() {
  scanf(" %s", s + 1);
  n = strlen(s + 1);
  for (int i = 1; i <= n; i++) {
    int hash = 0;
    for (int len = 1; len <= 4 && i + len - 1 <= n; len++) {
      hash *= 27;
      hash += s[i + len - 1] - 'a' + 1;
      0;
      loc[hash].push_back(i);
    }
  }
  int m = read();
  char s1[10], s2[10];
  for (int i = 0; i < m; i++) {
    scanf(" %s %s", s1, s2);
    int len1 = strlen(s1);
    int len2 = strlen(s2);
    int h1 = 0, h2 = 0;
    for (int j = 0; j < len1; j++) {
      h1 *= 27;
      h1 += s1[j] - 'a' + 1;
    }
    for (int j = 0; j < len2; j++) {
      h2 *= 27;
      h2 += s2[j] - 'a' + 1;
    }
    0;
    0;
    if (h1 > h2) {
      swap(h1, h2);
      swap(len1, len2);
    }
    if (!seen.count(make_pair(h1, h2))) {
      seen[make_pair(h1, h2)] = calc(h1, h2, len1, len2);
    }
    int t = seen[make_pair(h1, h2)];
    printf("%d\n", t == inf ? -1 : t);
  }
  return 0;
}
