#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const int inf = numeric_limits<int>::max() / 2;
const long double eps = 1e-9;
const long double pi = acos(-1);
template <typename T>
inline bool mineq(T& a, T b) {
  return (a > b) ? (a = b, 1) : 0;
}
template <typename T>
inline bool maxeq(T& a, T b) {
  return (a < b) ? (a = b, 1) : 0;
}
inline void solve(), read();
const string file = "";
signed main() {
  if (file != "") {
    freopen((file + ".in").c_str(), "r", stdin);
    freopen((file + ".out").c_str(), "w", stdout);
  }
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ;
  read();
  solve();
  return 0;
}
int n, m;
string s;
vector<pair<string, string>> qs;
unordered_map<string, vector<int>> cnt;
map<pair<string, string>, vector<int>> ms;
void calc(int len = 1) {
  for (int i = 0; i + len - 1 < n; i++) {
    string t = "";
    for (int j = 0; j < len; j++) {
      t += s[i + j];
    }
    cnt[t].push_back(i);
  }
}
inline void solve() {
  for (int i = 1; i <= 4; i++) {
    calc(i);
  }
  for (int i = 0; i < m; i++) {
    string a, b;
    tie(a, b) = qs[i];
    if ((int)((cnt[a]).size()) > (int)((cnt[b]).size())) {
      swap(a, b);
    }
    ms[{a, b}].push_back(i);
  }
  vector<int> ans(m, 0);
  for (auto& p : ms) {
    int cur = inf;
    int i = 0, j = 0;
    vector<int>& v1 = cnt[p.first.first];
    vector<int>& v2 = cnt[p.first.second];
    while (i < (int)((v1).size()) && j < (int)((v2).size())) {
      int l0 = v1[i];
      int r0 = l0 + (int)((p.first.first).size()) - 1;
      int l1 = v2[j];
      int r1 = l1 + (int)((p.first.second).size()) - 1;
      mineq(cur, max(r0, r1) - min(l0, l1) + 1);
      if (l0 < l1) {
        ++i;
      } else if (l1 < l0) {
        ++j;
      } else {
        ++i, ++j;
      }
    }
    if (cur == inf) {
      cur = -1;
    }
    for (int i : p.second) {
      ans[i] = cur;
    }
  }
  for (int i = 0; i < m; i++) {
    cout << ans[i] << "\n";
  }
}
inline void read() {
  cin >> s;
  n = (int)((s).size());
  cin >> m;
  for (int i = 0; i < m; i++) {
    string a, b;
    cin >> a >> b;
    qs.push_back({a, b});
  }
}
