#include <bits/stdc++.h>
signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0), std::cout.tie(0);
  std::string s;
  std::cin >> s;
  int n = (int)s.size();
  std::map<std::string, std::vector<int> > occur;
  for (int j = 1; j <= 4; j++)
    for (int i = 0; i + j <= n; i++) occur[s.substr(i, j)].push_back(i);
  std::map<std::pair<std::string, std::string>, int> rec;
  int q;
  std::cin >> q;
  while (q--) {
    std::string a, b;
    std::cin >> a >> b;
    if (a < b) std::swap(a, b);
    if (!occur.count(a) || !occur.count(b)) {
      std::cout << -1 << '\n';
    } else if (rec.count(std::make_pair(a, b))) {
      std::cout << rec[std::make_pair(a, b)] << '\n';
    } else {
      int i = 0, j = 0, ans = 1e9;
      std::vector<int>& x = occur.find(a)->second;
      std::vector<int>& y = occur.find(b)->second;
      while (i < (int)x.size() && j < (int)y.size()) {
        int l = std::min(x[i], y[j]);
        int r = std::max(x[i] + (int)a.size(), y[j] + (int)b.size());
        ans = std::min(ans, r - l);
        x[i] < y[j] ? ++i : ++j;
      }
      std::cout << (rec[std::make_pair(a, b)] = ans) << '\n';
    }
  }
  return 0;
}
