#include <bits/stdc++.h>
using namespace std;
const int p = 37;
const int N = 5e4 + 10;
map<int, vector<int> > sav;
map<pair<int, int>, int> savans;
char s[N], t[10];
int main() {
  int n, q;
  vector<int> pos1, pos2;
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (int j = 1; j <= 4; j++) {
    for (int i = j, sum; i <= n; i++) {
      sum = 0;
      for (int k = i - j + 1; k <= i; k++) sum = sum * p + (s[k] - 'a' + 1);
      sav[sum].push_back(i - j + 1);
    }
  }
  scanf("%d", &q);
  for (int i = 1, len1, len2, sum1, sum2, sz1, sz2, ans, d; i <= q; i++) {
    scanf("%s", t + 1);
    len1 = strlen(t + 1);
    sum1 = 0;
    for (int j = 1; j <= len1; j++) sum1 = sum1 * p + (t[j] - 'a' + 1);
    scanf("%s", t + 1);
    len2 = strlen(t + 1);
    sum2 = 0;
    for (int j = 1; j <= len2; j++) sum2 = sum2 * p + (t[j] - 'a' + 1);
    if (savans.count({sum1, sum2})) {
      printf("%d\n", savans[{sum1, sum2}]);
      continue;
    }
    if (!sav.count(sum1) || !sav.count(sum2)) {
      printf("-1\n");
      continue;
    }
    pos1 = sav[sum1];
    sz1 = (int)pos1.size();
    pos2 = sav[sum2];
    sz2 = (int)pos2.size();
    ans = 1e9;
    if ((int)pos1.size() < (int)pos2.size()) {
      for (auto it : pos1) {
        d = lower_bound(pos2.begin(), pos2.end(), it) - pos2.begin();
        if (d < sz2) ans = min(ans, max(it + len1, pos2[d] + len2) - it);
        if (d) ans = min(ans, max(it + len1, pos2[d - 1] + len2) - pos2[d - 1]);
      }
    } else {
      for (auto it : pos2) {
        d = lower_bound(pos1.begin(), pos1.end(), it) - pos1.begin();
        if (d < sz1) ans = min(ans, max(it + len2, pos1[d] + len1) - it);
        if (d) ans = min(ans, max(it + len2, pos1[d - 1] + len1) - pos1[d - 1]);
      }
    }
    printf("%d\n", ans);
    savans[{sum1, sum2}] = ans;
    savans[{sum2, sum1}] = ans;
  }
  return 0;
}
