#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
template <class T, class U>
inline void checkmin(T &x, U y) {
  if (y < x) x = y;
}
template <class T, class U>
inline void checkmax(T &x, U y) {
  if (y > x) x = y;
}
template <class T, class U>
inline bool ifmin(T &x, U y) {
  if (y < x) return x = y, true;
  return false;
}
template <class T, class U>
inline bool ifmax(T &x, U y) {
  if (y > x) return x = y, true;
  return false;
}
template <class T>
inline void sort(T &a) {
  sort(a.begin(), a.end());
}
template <class T>
inline void rsort(T &a) {
  sort(a.rbegin(), a.rend());
}
template <class T>
inline void reverse(T &a) {
  reverse(a.begin(), a.end());
}
template <class T>
inline istream &operator>>(istream &stream, vector<T> &a) {
  for (auto &i : a) stream >> i;
  return stream;
}
int len(int a, int b, int c, int d) { return max(b, d) - min(a, c); }
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(15);
  srand(85453532);
  string second;
  cin >> second;
  map<string, vector<int>> ind;
  for (int i = 0; i < int(second.size()); ++i) {
    string news = "";
    for (int j = i; j < i + 4 && j < int(second.size()); ++j) {
      news += second[j];
      ind[news].push_back(i);
    }
  }
  int q;
  cin >> q;
  map<pair<string, string>, int> counted;
  while (q--) {
    string s1, s2;
    cin >> s1 >> s2;
    if (int(ind[s1].size()) > int(ind[s2].size())) swap(s1, s2);
    vector<int> &a1 = ind[s1], &a2 = ind[s2];
    if (counted.count({s1, s2}))
      cout << counted[{s1, s2}];
    else {
      int t = 1000000007;
      for (auto j : a1) {
        int l = -1, r = int(a2.size());
        while (r - l > 1) {
          int mid = r + l >> 1;
          if (a2[mid] >= j)
            r = mid;
          else
            l = mid;
        }
        if (r < int(a2.size()))
          checkmin(t,
                   len(j, j + int(s1.size()), a2[r], a2[r] + int(s2.size())));
        if (l >= 0)
          checkmin(t,
                   len(j, j + int(s1.size()), a2[l], a2[l] + int(s2.size())));
      }
      if (t == 1000000007) t = -1;
      counted[{s1, s2}] = t;
      cout << t;
    }
    cout << '\n';
  }
  return 0;
}
