#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T sqr(T x) {
  return x * x;
}
const double EPS = 1e-9;
const int MAXN = 5 * int(1e4) + 10;
const int INF = int(1e8);
const int BASE = 27;
char second[MAXN];
vector<int> pos[BASE * BASE * BASE * BASE + BASE * BASE * BASE + BASE * BASE +
                BASE + 10];
map<pair<int, int>, int> answer;
inline int code(char c) { return int(c - 'a') + 1; }
inline int calcHash(char* t) {
  int len = (int)strlen(t);
  int hash = 0;
  for (int i = 0; i < (int)len; ++i) {
    hash = hash * BASE + code(t[i]);
  }
  return hash;
}
int main() {
  gets(second);
  int n = (int)strlen(second);
  for (int i = 0; i < (int)n; ++i) {
    int hash = 0;
    for (int len = 1; len <= 4 && i + len - 1 < n; ++len) {
      hash = hash * BASE + code(second[i + len - 1]);
      pos[hash].push_back(i);
    }
  }
  int q;
  scanf("%d\n", &q);
  while (q-- > 0) {
    char a[6];
    char b[6];
    scanf("%s %s\n", a, b);
    int hashA = calcHash(a);
    int hashB = calcHash(b);
    int la = (int)strlen(a);
    int lb = (int)strlen(b);
    pair<int, int> id = make_pair(hashA, hashB);
    if (answer.count(id)) {
      printf("%d\n", answer[id]);
      continue;
    }
    if (((int)(pos[hashA]).size()) > ((int)(pos[hashB]).size())) {
      swap(hashA, hashB);
      swap(la, lb);
    }
    int ans = INF;
    for (int i = 0; i < (int)((int)(pos[hashA]).size()); ++i) {
      int j =
          upper_bound((pos[hashB]).begin(), (pos[hashB]).end(), pos[hashA][i]) -
          pos[hashB].begin();
      if (j < ((int)(pos[hashB]).size())) {
        int r = max(pos[hashB][j] + lb - 1, pos[hashA][i] + la - 1);
        int l = min(pos[hashA][i], pos[hashB][j]);
        ans = min(ans, r - l + 1);
      }
      --j;
      if (j >= 0) {
        int r = max(pos[hashB][j] + lb - 1, pos[hashA][i] + la - 1);
        int l = min(pos[hashA][i], pos[hashB][j]);
        ans = min(ans, r - l + 1);
      }
    }
    if (ans == INF) {
      ans = -1;
    }
    answer[id] = ans;
    printf("%d\n", ans);
  }
  return 0;
}
