#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "{" << p.first << "," << p.second << "}";
  return os;
}
const int N = 1e6 + 5;
const int oo = 1e9 + 7;
int Hash(const string &s) {
  int ret = 0;
  for (char c : s) ret = ret * 27 + c - 'a' + 1;
  return ret;
}
vector<int> pos[N];
map<pair<int, int>, int> ans;
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  string s;
  cin >> s;
  int n = s.size();
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= 4 and i + j <= n; j++) {
      pos[Hash(s.substr(i, j))].push_back(i);
    }
  }
  int q;
  cin >> q;
  while (q--) {
    string a, b;
    cin >> a >> b;
    int aa = Hash(a), bb = Hash(b), asz = a.size(), bsz = b.size();
    if (pos[aa].size() > pos[bb].size()) swap(aa, bb), swap(asz, bsz);
    int &anss = ans[make_pair(aa, bb)];
    if (anss != 0) {
    } else {
      anss = oo;
      for (int i : pos[aa]) {
        int l =
            upper_bound(pos[bb].begin(), pos[bb].end(), i) - pos[bb].begin();
        l--;
        if (0 <= l)
          anss = min(anss, int(max(i + asz, pos[bb][l] + bsz) - pos[bb][l]));
        l = lower_bound(pos[bb].begin(), pos[bb].end(), i) - pos[bb].begin();
        if (l < pos[bb].size())
          anss = min(anss, int(max(i + asz, pos[bb][l] + bsz) - i));
      }
      if (anss == oo) anss = -1;
    }
    cout << anss << "\n";
  }
}
