#include <bits/stdc++.h>
using namespace std;
const int MAXN = 6e5 + 10;
map<pair<int, int>, int> mp;
vector<int> st[MAXN];
string s;
int n, q;
int conv(string &a) {
  int res = 0;
  for (int i = (int)a.size() - 1; i >= 0; i--) {
    res *= 27;
    res += (a[i] - 'a') + 1;
  }
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> s;
  n = s.size();
  for (int i = 0; i < n; i++) {
    string t;
    for (int j = 0; j < 4 && i + j < n; j++) {
      t.push_back(s[i + j]);
      st[conv(t)].push_back(i);
    }
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    string a, b;
    cin >> a >> b;
    int cva = conv(a), cvb = conv(b), ans = 1e9;
    if (st[cva].size() > st[cvb].size()) swap(cva, cvb), swap(a, b);
    if (mp.find({cva, cvb}) != mp.end()) {
      cout << mp[{cva, cvb}] << '\n';
      continue;
    }
    for (int i = 0; i < st[cva].size(); i++) {
      int S = st[cva][i], E = 0;
      vector<int>::iterator c = lower_bound(st[cvb].begin(), st[cvb].end(), S);
      if (c != st[cvb].end()) {
        E = max(*c + b.size(), S + a.size());
        ans = min(E - S, ans);
      }
      if (c != st[cvb].begin()) {
        c--;
        S = max(S + a.size(), *c + b.size());
        ans = min(S - *c, ans);
      }
    }
    if (ans == 1e9)
      cout << -1 << '\n', mp[{cva, cvb}] = -1;
    else
      cout << ans << '\n', mp[{cva, cvb}] = ans;
  }
}
