#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
vector<int> hv[1000005];
int tohash(string S) {
  int bs = 1, res = 0;
  for (int k = 0; k < (S.length()); k++) {
    res += (S[k] - 96) * bs;
    bs *= 27;
  }
  return res;
}
string ori;
map<pair<int, int>, int> ans;
int getans(int a, int b, int la, int lb) {
  if (hv[a].size() > hv[b].size()) {
    swap(a, b);
    swap(la, lb);
  }
  int ans = INF;
  for (int i = 0; i < (hv[a].size()); i++) {
    int j = lower_bound(hv[b].begin(), hv[b].end(), hv[a][i]) - hv[b].begin();
    if (j < hv[b].size()) ans = min(ans, max(la, hv[b][j] - hv[a][i] + lb));
    if (j > 0) ans = min(ans, max(lb, hv[a][i] - hv[b][j - 1] + la));
  }
  return ans == INF ? -1 : ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> ori;
  for (int i = 0; i < (ori.size()); i++)
    for (int j = 1; j <= (4); j++) hv[tohash(ori.substr(i, j))].push_back(i);
  int q, a, b;
  cin >> q;
  for (int k = 0; k < (q); k++) {
    string s1, s2;
    cin >> s1 >> s2;
    a = tohash(s1);
    b = tohash(s2);
    if (ans.find(make_pair(a, b)) == ans.end())
      ans[make_pair(a, b)] = getans(a, b, s1.length(), s2.length());
    cout << ans[make_pair(a, b)] << '\n';
  }
  return 0;
}
