#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double eps = 1e-6;
template <typename A, typename B>
ostream& operator<<(ostream& os, const pair<A, B>& p) {
  return os << p.first << "," << p.second;
}
template <typename A>
ostream& operator<<(ostream& os, const vector<A>& v) {
  for (int i = 0; i < v.size(); i++) {
    os << v[i] << ";";
  }
  return os;
}
template <typename A>
ostream& operator<<(ostream& os, const set<A>& v) {
  for (typename set<A>::const_iterator it = v.begin(); it != v.end(); ++it) {
    os << *it << ";";
  }
  return os;
}
template <typename A, typename B>
ostream& operator<<(ostream& os, const map<A, B>& v) {
  for (typename map<A, B>::const_iterator it = v.begin(); it != v.end(); ++it) {
    os << it->first << ":" << it->second << ";";
  }
  return os;
}
string a;
char buf[1 << 20];
char buf2[1 << 20];
vector<int> where[550550];
int toind(const string& s) {
  string x(4, 0);
  assert(x.size() == 4);
  for (int i = s.size() - 1; i >= 0; i--) {
    x[4 - (s.size() - i)] = s[i] - 'a' + 1;
  }
  int res = 0;
  for (int i = 0; i < 4; i++) {
    res = res * 27 + x[i];
  }
  return res;
}
int getlen(int ind) {
  int a[4] = {0, 0, 0, 0};
  for (int i = 0; i < 4; i++) {
    a[3 - i] = ind % 27;
    ind /= 27;
  }
  int nonul = 0;
  for (; nonul < 4 && !a[nonul]; nonul++)
    ;
  return 4 - nonul;
}
string tostr(int ind) {
  char a[4] = {0, 0, 0, 0};
  for (int i = 0; i < 4; i++) {
    a[3 - i] = ind % 27;
    ind /= 27;
  }
  for (int i = 0; i < 4; i++) {
    if (a[i] != 0) {
      a[i] = 'a' + (a[i] - 1);
    }
  }
  int nonul = 0;
  for (; nonul < 4 && !a[nonul]; nonul++)
    ;
  return string(a + nonul, a + 4);
}
int solve(pair<int, int> p) {
  int z1 = p.first;
  int z2 = p.second;
  int l1 = getlen(z1);
  int l2 = getlen(z2);
  int d1 = where[z1].size();
  int d2 = where[z2].size();
  if (d1 > d2) {
    swap(z1, z2);
    swap(l1, l2);
    swap(d1, d2);
  }
  const auto& w2 = where[z2];
  int res = inf;
  for (int p1 : where[z1]) {
    auto it_hi = lower_bound(w2.begin(), w2.end(), p1);
    if (it_hi != w2.end() && *it_hi == p1) {
      res = max(l1, l2);
      return res;
    }
    if (it_hi != w2.end()) {
      res = min(res, max(l1, l2 + *it_hi - p1));
    }
    auto it_lo = it_hi;
    if (it_lo != w2.begin()) {
      --it_lo;
      res = min(res, max(l2, l1 + p1 - *it_lo));
    }
  }
  if (res == inf) {
    res = -1;
  }
  return res;
}
int main() {
  scanf("%s", buf);
  a = string(buf);
  for (int i = 0; i < a.size(); i++) {
    where[toind(a.substr(i, 1))].push_back(i);
  }
  for (int i = 0; i + 1 < a.size(); i++) {
    where[toind(a.substr(i, 2))].push_back(i);
  }
  for (int i = 0; i + 2 < a.size(); i++) {
    where[toind(a.substr(i, 3))].push_back(i);
  }
  for (int i = 0; i + 3 < a.size(); i++) {
    where[toind(a.substr(i, 4))].push_back(i);
  }
  int q;
  cin >> q;
  vector<pair<pair<int, int>, int>> requests;
  for (int i = 0; i < q; i++) {
    scanf("%s%s", buf, buf2);
    requests.push_back(make_pair(pair<int, int>(toind(buf), toind(buf2)), i));
    int z = requests.size() - 1;
    if (requests[z].first.first > requests[z].first.second) {
      swap(requests[z].first.first, requests[z].first.second);
    }
  }
  sort(requests.begin(), requests.end());
  map<pair<int, int>, int> ans;
  vector<int> res(q);
  for (int i = 0; i < requests.size(); i++) {
    if (ans.count(requests[i].first)) {
      res[requests[i].second] = ans[requests[i].first];
      continue;
    }
    int w = solve(requests[i].first);
    ans[requests[i].first] = w;
    res[requests[i].second] = ans[requests[i].first];
  }
  for (int i = 0; i < res.size(); i++) {
    printf("%d\n", res[i]);
  }
  return 0;
}
