#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 10;
const long long int INFLL = 1e18 + 10;
const long double EPS = 1e-8;
const long double EPSLD = 1e-14;
const long long int MOD = 1e9 + 7;
template <class T>
T &chmin(T &a, const T &b) {
  return a = min(a, b);
}
template <class T>
T &chmax(T &a, const T &b) {
  return a = max(a, b);
}
int hs(string s) {
  int res = 0;
  for (auto &w : s) res = res * 27 + (w - 'a' + 1);
  return res;
}
const int B = 1000;
const int MAX_N = 60010;
const int MAX_H = 600010;
int qn;
char cs[MAX_N];
string s;
char ca[MAX_N], cb[MAX_N];
string sa, sb;
vector<int> pos[MAX_H];
int len[MAX_H];
vector<int> la;
vector<vector<int> > lans;
int main() {
  scanf("%s", cs);
  s = cs;
  int n = s.length();
  for (int i = (0); i < (int)(n); i++)
    for (int j = (1); j < (int)(5); j++)
      if (i + j <= n) {
        int has = hs(s.substr(i, j));
        pos[has].emplace_back(i);
        len[has] = j;
      }
  for (int i = (0); i < (int)(MAX_H); i++)
    if (pos[i].size() >= B) {
      la.emplace_back(i);
      vector<int> res(MAX_H, INF);
      int p = 0;
      for (int j = (0); j < (int)(n); j++) {
        for (int k = (1); k < (int)(5); k++)
          if (j + k <= n) {
            int has = hs(s.substr(j, k));
            if (p < pos[i].size())
              chmin(res[has], max(j + k, pos[i][p] + len[i]) - j);
            if (p > 0)
              chmin(res[has],
                    max(j + k, pos[i][p - 1] + len[i]) - pos[i][p - 1]);
          }
        if (p < pos[i].size() && pos[i][p] == j) p++;
      }
      lans.emplace_back(res);
    }
  scanf("%d", &qn);
  for (int query_cnt = (0); query_cnt < (int)(qn); query_cnt++) {
    scanf("%s %s", ca, cb);
    sa = ca;
    sb = cb;
    int a = hs(sa);
    int b = hs(sb);
    int ans = INF;
    if (pos[a].size() >= B) {
      ans = lans[lower_bound((la).begin(), (la).end(), a) - la.begin()][b];
    } else if (pos[b].size() >= B) {
      ans = lans[lower_bound((la).begin(), (la).end(), b) - la.begin()][a];
    } else {
      int p = 0;
      for (auto &w : pos[b]) {
        while (p < pos[a].size() && pos[a][p] < w) p++;
        if (p < pos[a].size())
          chmin(ans, max(w + len[b], pos[a][p] + len[a]) - w);
        if (p > 0)
          chmin(ans, max(w + len[b], pos[a][p - 1] + len[a]) - pos[a][p - 1]);
      }
    }
    if (ans == INF) ans = -1;
    printf("%d\n", ans);
  }
  return 0;
}
