#include <bits/stdc++.h>
using namespace std;
int n, q, ans = 5e5 + 10;
string s, s1, s2;
map<string, vector<int>> f;
map<pair<string, string>, int> mp;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> s;
  int n = s.size();
  for (int i = 0, j; i < n; i++)
    for (int j = 1; j + i <= n, j <= 4; j++) f[s.substr(i, j)].push_back(i);
  vector<int> vec1, vec2;
  cin >> q;
  while (q--) {
    cin >> s1 >> s2;
    vec1 = f[s1], vec2 = f[s2];
    if (vec1.size() == 0 || vec2.size() == 0) {
      cout << -1 << endl;
      continue;
    }
    if (vec1.size() > vec2.size()) swap(vec1, vec2), swap(s1, s2);
    if (mp.count({s1, s2})) {
      cout << mp[{s1, s2}] << endl;
      continue;
    }
    ans = 5e5 + 10;
    for (int i : vec1) {
      int ind = lower_bound(vec2.begin(), vec2.end(), i) - vec2.begin();
      if (ind != vec2.size()) {
        int val2 = vec2[ind];
        ans = min(ans, max((int)s1.size() + i, (int)s2.size() + val2) - i);
      }
      if (ind != 0) {
        int val1 = vec2[ind - 1];
        ans = min(ans, max((int)s1.size() + i, (int)s2.size() + val1) - val1);
      }
    }
    cout << (mp[{s1, s2}] = ans) << endl;
  }
  return 0;
}
