#include <bits/stdc++.h>
using namespace std;
const int N = 500 * 1001, M = 27, inf = 1e9 + 7;
int n, m;
vector<int> L[2 * N];
map<pair<int, int>, int> mp;
string s;
int _mul(int a, int b) { return a * b; }
int _sum(int a, int b) {
  a += b;
  return a;
}
int get_hash(string t) {
  int pw = 1, res = 0;
  for (int i = 0; i < t.size(); i++) {
    res = _sum(res, _mul((int)(t[i] - 'a' + 1), pw));
    pw = _mul(pw, M);
  }
  return res;
}
int solve(string a, string b) {
  int ha = get_hash(a), hb = get_hash(b), sza = a.size(), szb = b.size();
  if (make_pair(L[ha].size(), ha) > make_pair(L[hb].size(), hb)) {
    swap(ha, hb);
    swap(sza, szb);
  }
  pair<int, int> p = {ha, hb};
  if (mp.count(p)) return mp[p];
  mp[p] = inf;
  for (int x : L[ha]) {
    int ind = lower_bound(L[hb].begin(), L[hb].end(), x) - L[hb].begin();
    if (ind != L[hb].size())
      mp[p] = min(mp[p], max(x + sza, L[hb][ind] + szb) - x);
    if (ind--) mp[p] = min(mp[p], max(x + sza, L[hb][ind] + szb) - L[hb][ind]);
  }
  if (mp[p] == inf) mp[p] = -1;
  return mp[p];
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> s;
  n = s.size();
  for (int i = 0; i < n; i++)
    for (int j = 1; j <= min(4, n - i); j++) {
      L[get_hash(s.substr(i, j))].push_back(i);
    }
  cin >> m;
  while (m--) {
    string a, b;
    cin >> a >> b;
    cout << solve(a, b) << "\n";
  }
}
