#include <bits/stdc++.h>
using namespace std;
string g;
int s, q;
int ct = 0;
vector<int> str[5][27 * 27 * 27 * 27];
map<long long, long long> visitedq;
map<long long, long long> query;
long long power[5] = {1, 26, 676, 26 * 676, 676 * 676};
int main() {
  ios_base::sync_with_stdio(false);
  cin >> g;
  s = g.length();
  cin >> q;
  for (int k = 1; k <= 4; ++k) {
    if (s < k) break;
    int x = 0;
    for (int i = 0; i < k; ++i) x = 26 * x + g[i] - 'a';
    str[k][x].push_back(0);
    for (int i = 1; i < s - k + 1; ++i) {
      x -= (g[i - 1] - 'a') * power[k - 1];
      x *= 26;
      x += g[i + k - 1] - 'a';
      str[k][x].push_back(i);
    }
  }
  for (int w = 0; w < q; ++w) {
    string x, y;
    cin >> x >> y;
    long long p1, p2;
    long long l1 = x.length(), l2 = y.length(), s1, s2;
    long long h1 = 0, h2 = 0;
    for (int i = 0; i < l1; ++i) h1 = 26 * h1 + x[i] - 'a';
    for (int i = 0; i < l2; ++i) h2 = 26 * h2 + y[i] - 'a';
    if (str[l1][h1].size() == 0 || str[l2][h2].size() == 0) {
      cout << "-1\n";
      continue;
    }
    s1 = str[l1][h1].size();
    s2 = str[l2][h2].size();
    if (pair<int, int>(h1, l1) > pair<int, int>(h2, l2)) {
      swap(h1, h2);
      swap(l1, l2);
      swap(s1, s2);
    }
    if (visitedq.count(100 * (1000000 * h1 + h2) + 10 * l1 + l2) > 0) {
      cout << query[100 * (1000000 * h1 + h2) + 10 * l1 + l2] << "\n";
      continue;
    }
    if (str[l1][h1].size() > str[l2][h2].size()) {
      swap(h1, h2);
      swap(l1, l2);
      swap(s1, s2);
    }
    long long mini = 1000000;
    for (int i = 0; i < str[l1][h1].size(); ++i) {
      p1 = lower_bound(str[l2][h2].begin(), str[l2][h2].end(), str[l1][h1][i]) -
           str[l2][h2].begin();
      if (p1 <= s2 - 1)
        mini = min(mini, max((long long)str[l2][h2][p1] + l2,
                             (long long)str[l1][h1][i] + l1) -
                             (long long)str[l1][h1][i]);
      if (p1 == 0) continue;
      --p1;
      mini = min(mini, max((long long)str[l2][h2][p1] + l2,
                           (long long)str[l1][h1][i] + l1) -
                           (long long)str[l2][h2][p1]);
    }
    if (pair<int, int>(h1, l1) > pair<int, int>(h2, l2)) {
      swap(h1, h2);
      swap(l1, l2);
    }
    visitedq[100 * (1000000 * h1 + h2) + 10 * l1 + l2] = 1;
    query[100 * (1000000 * h1 + h2) + 10 * l1 + l2] = mini;
    cout << mini << "\n";
    ++ct;
  }
}
