#include <bits/stdc++.h>
using namespace std;
char a[100010];
vector<int> qe[710000];
map<pair<int, int>, int> dp;
long long hehe[20];
long long find(int x, int y) {
  int k = y, n = 1;
  while (k) {
    n++;
    k /= 10;
  }
  return hehe[n] * x + y;
}
int get(char a[], int n) {
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = ans * 27 + a[i] - 'a' + 1;
  }
  return ans;
}
int getans(int x, int y, int n1, int n2) {
  int ans = 78974453;
  int ll, rr, l, r;
  for (int i = 0; i < qe[x].size(); i++) {
    l = qe[x][i];
    r = l + n1;
    int p = int(lower_bound(qe[y].begin(), qe[y].end(), l) - qe[y].begin());
    if (p < qe[y].size()) {
      rr = qe[y][p] + n2;
      if (r > rr) rr = r;
      ans = min(ans, rr - l);
    }
    p--;
    if (p >= 0) {
      rr = qe[y][p] + n2;
      if (r > rr) rr = r;
      ans = min(ans, rr - qe[y][p]);
    }
  }
  return ans;
}
int main() {
  int n, i, j, k, m;
  int x, y;
  char s1[5], s2[5];
  hehe[1] = 1;
  for (i = 2; i <= 15; i++) hehe[i] = hehe[i - 1] * 10;
  scanf("%s", a);
  n = strlen(a);
  for (i = 0; i < n; i++) {
    for (j = 1; j < 5; j++) {
      if (j + i > n) break;
      k = get(a + i, j);
      qe[k].push_back(i);
    }
  }
  scanf("%d", &m);
  while (m--) {
    scanf("%s%s", s1, s2);
    int xx = strlen(s1);
    int yy = strlen(s2);
    x = get(s1, xx);
    y = get(s2, yy);
    if (qe[x].size() > qe[x].size() ||
        (qe[x].size() == qe[y].size() && x > y)) {
      swap(x, y);
      swap(xx, yy);
    }
    int &ans = dp[pair<int, int>(x, y)];
    if (!ans) ans = getans(x, y, xx, yy);
    printf("%d\n", ans == 78974453 ? -1 : ans);
  }
  return 0;
}
