#include <bits/stdc++.h>
using namespace std;
const int N = int(5e4) + 1;
const int M = int(1e6) + 1;
int n, q;
char s[N], sa[5], sb[5];
vector<int> v[M];
map<pair<int, int>, int> ans;
int hashCode(char *s, int l) {
  int t = 0;
  for (int i = 0; i < l; ++i) t = t * 27 + s[i] - 'a' + 1;
  return t;
}
int solve(int a, int b, int la, int lb) {
  if ((int)(v[a].size()) > (int)(v[b].size())) swap(a, b), swap(la, lb);
  if ((int)(v[a].size()) == (int)(v[b].size()) && a > b)
    swap(a, b), swap(la, lb);
  int &qq = ans[make_pair(a, b)];
  if (qq) return qq;
  qq = M;
  for (vector<int>::iterator it = v[a].begin(); it != v[a].end(); ++it) {
    int pos = (int)(lower_bound(v[b].begin(), v[b].end(), *it) - v[b].begin());
    if (pos < (int)(v[b].size()))
      qq = min(qq, max(v[b][pos] + lb, *it + la) - *it);
    if (pos > 0)
      qq = min(qq, max(*it + la, v[b][pos - 1] + lb) - v[b][pos - 1]);
  }
  if (qq == M) qq = -1;
  return qq;
}
int main() {
  scanf("%s", s);
  n = strlen(s);
  for (int len = 1; len < 5; ++len)
    for (int i = 0; i + len <= n; ++i) {
      v[hashCode(s + i, len)].push_back(i);
    }
  scanf("%d", &q);
  while (q--) {
    scanf("%s%s", sa, sb);
    int la = strlen(sa);
    int lb = strlen(sb);
    int a = hashCode(sa, la);
    int b = hashCode(sb, lb);
    printf("%d\n", solve(a, b, la, lb));
  }
  return 0;
}
