#include <bits/stdc++.h>
using namespace std;
const int maxn = 27, maxm = 5e4 + 20, inf = 1e9;
vector<int> ar[maxn][maxn][maxn][maxn];
int q, a[maxm], bs[4], bs2[4];
string s, qs, qss;
map<pair<string, string>, int> mp;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> s >> q;
  for (int i = 0; i < s.size(); i++)
    a[i] = (int)s[i] - 'a', ar[(int)s[i] - 'a'][26][26][26].push_back(i);
  for (int i = 0; i < (int)s.size() - 1; i++)
    ar[a[i]][a[i + 1]][26][26].push_back(i);
  for (int i = 0; i < (int)s.size() - 2; i++)
    ar[a[i]][a[i + 1]][a[i + 2]][26].push_back(i);
  for (int i = 0; i < (int)s.size() - 3; i++)
    ar[a[i]][a[i + 1]][a[i + 2]][a[i + 3]].push_back(i);
  while (q--) {
    cin >> qs >> qss;
    if (qss < qs) swap(qss, qs);
    fill(bs, bs + 4, 26), fill(bs2, bs2 + 4, 26);
    for (int i = 0; i < qs.size(); i++) bs[i] = qs[i] - 'a';
    for (int i = 0; i < qss.size(); i++) bs2[i] = qss[i] - 'a';
    if (ar[bs[0]][bs[1]][bs[2]][bs[3]].size() >
        ar[bs2[0]][bs2[1]][bs2[2]][bs2[3]].size())
      swap(qs, qss), swap(bs, bs2);
    if (mp.count(make_pair(qs, qss))) {
      cout << mp[make_pair(qs, qss)] << endl;
      continue;
    }
    int ans = inf, j;
    for (int i : ar[bs[0]][bs[1]][bs[2]][bs[3]]) {
      auto it = upper_bound(ar[bs2[0]][bs2[1]][bs2[2]][bs2[3]].begin(),
                            ar[bs2[0]][bs2[1]][bs2[2]][bs2[3]].end(), i);
      if (it != ar[bs2[0]][bs2[1]][bs2[2]][bs2[3]].end()) {
        j = *it;
        ans = min(ans, max(i + (int)qs.size(), j + (int)qss.size()) - i);
      }
      if (it != ar[bs2[0]][bs2[1]][bs2[2]][bs2[3]].begin()) {
        it--;
        j = *it;
        ans = min(ans, max(i + (int)qs.size(), j + (int)qss.size()) - j);
      }
    }
    if (ans == inf) ans = -1;
    mp[make_pair(qs, qss)] = ans;
    cout << ans << endl;
  }
}
