#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "{";
  for (const T &a : v) out << a << ", ";
  out << "}";
  return out;
}
template <typename S, typename T>
ostream &operator<<(ostream &out, const pair<S, T> &p) {
  out << "(" << p.first << ", " << p.second << ")";
  return out;
}
int z, n, q;
string s, a, b;
map<pair<string, string>, int> ans;
map<int, vector<int> > hashes;
int get_hash(string &s) {
  int ret = 0;
  for (int i = 0; i < (int)s.size(); i++)
    ret = (ret + s[i]) * 29 + (s[i] - 'a' + 1);
  return ret;
}
int solve(string &a, string &b) {
  if (ans.count({a, b})) return ans[{a, b}];
  int ha = get_hash(a);
  int hb = get_hash(b);
  if (hashes[ha].size() > hashes[hb].size()) return solve(b, a);
  auto last = hashes[hb].begin();
  int ret = 4000007;
  for (int i = 0; i < (int)hashes[ha].size(); i++) {
    last = lower_bound(last, hashes[hb].end(), hashes[ha][i]) - 1;
    ret = min(ret, max(hashes[ha][i] + (int)a.size(), *last + (int)b.size()) -
                       min(hashes[ha][i], *last));
    if (++last == hashes[hb].end()) break;
    ret = min(ret, max(hashes[ha][i] + (int)a.size(), *last + (int)b.size()) -
                       min(hashes[ha][i], *last));
  }
  ret = (ret == 4000007 ? -1 : ret);
  ans[{a, b}] = ans[{b, a}] = ret;
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> s;
  for (int i = 0; i < (int)s.size(); i++) {
    string t = "";
    for (int j = 0; j < (int)min(4, (int)s.size() - i); j++) {
      t += s[i + j];
      hashes[get_hash(t)].push_back(i);
    }
  }
  cin >> q;
  while (q--) {
    cin >> a >> b;
    cout << solve(a, b) << "\n";
  }
}
