#include <bits/stdc++.h>
using namespace std;
const int N = 5e4 + 4, L = 475255, SQ = 300, inf = 1e6;
int p[L], Label[L], len[L], ans[SQ][N << 2], pw[4], ps[4];
vector<int> pos[L];
int GetHash(string s) {
  int res = 0;
  for (int i = 0; i < (int)s.size(); i++) res += pw[i] * (s[i] - 'a' + 1);
  return res;
}
int GetLen(int i, int l1, int j, int l2) {
  int r = max(i + l1 - 1, j + l2 - 1);
  return r - i + 1;
}
bool cmp(int i, int j) { return (int)pos[i].size() > (int)pos[j].size(); }
bool cmpv(const vector<int> &v1, const vector<int> &v2) {
  return (int)v1.size() > (int)v2.size();
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ps[0] = pw[0] = 1;
  for (int i = 1; i < 4; i++) pw[i] = pw[i - 1] * 26, ps[i] = ps[i - 1] + pw[i];
  for (int i = 1; i < L; i++) p[i] = i, pos[i].push_back(-inf);
  for (int i = 1; i < SQ; i++)
    for (int j = 1; j < (N << 2); j++) ans[i][j] = inf;
  string s;
  cin >> s;
  int n = (int)s.size();
  for (int i = 0; i < n; i++) {
    for (int j = 1; j + i - 1 < n && j <= 4; j++)
      pos[GetHash(s.substr(i, j))].push_back(i);
  }
  for (int i = 1; i < L; i++) pos[i].push_back(inf);
  sort(p + 1, p + L, cmp), sort(pos + 1, pos + L, cmpv);
  for (int i = 1; i < L; i++)
    for (int j = 3; j >= 0; j--)
      if (p[i] >= ps[j]) {
        len[i] = j + 1;
        break;
      }
  for (int i = 1; i < L; i++) Label[p[i]] = i;
  for (int k = 1; k < SQ; k++) {
    int pnt = 0;
    for (int i = 0; i < n; i++) {
      while (pos[k][pnt] <= i) pnt++;
      for (int j = 1; j + i - 1 < n && j <= 4; j++) {
        int h = Label[GetHash(s.substr(i, j))];
        ans[k][h] = min(ans[k][h], min(GetLen(i, j, pos[k][pnt], len[k]),
                                       GetLen(pos[k][pnt - 1], len[k], i, j)));
      }
    }
  }
  int q;
  cin >> q;
  while (q--) {
    string A, B;
    cin >> A >> B;
    int a = Label[GetHash(A)], b = Label[GetHash(B)];
    if ((int)pos[a].size() == 2 || (int)pos[b].size() == 2) {
      cout << -1 << "\n";
      continue;
    }
    if (a > b) swap(a, b);
    if (a < SQ) {
      cout << (ans[a][b] >= inf ? -1 : ans[a][b]) << "\n";
      continue;
    }
    int res = inf, pnt = 0;
    for (int i : pos[b]) {
      if (abs(i) == inf) continue;
      while (pos[a][pnt] <= i) pnt++;
      res = min(res, min(GetLen(i, len[b], pos[a][pnt], len[a]),
                         GetLen(pos[a][pnt - 1], len[a], i, len[b])));
    }
    cout << (res >= inf ? -1 : res) << "\n";
  }
  return 0;
}
