#include <bits/stdc++.h>
using namespace std;
template <typename T>
T nextInt() {
  T x = 0, p = 1;
  char ch;
  do {
    ch = getchar();
  } while (ch <= ' ');
  if (ch == '-') {
    p = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + (ch - '0');
    ch = getchar();
  }
  return x * p;
}
const int maxN = (int)8e5 + 500;
const int mod = (int)1e9 + 7;
const int INF = (int)1e9 + 5;
const long long LLINF = (long long)1e18 + 5;
struct query {
  int x, y;
  int idx;
  query() {}
  query(int x, int y, int idx) : x(x), y(y), idx(idx) {}
};
int HASH(char s[5]) {
  int n = strlen(s);
  int res = 0;
  int pw = 1;
  for (int i = 0; i < n; ++i) {
    res += (s[i] - 'a' + 1) * pw;
    pw *= 31;
  }
  return res;
}
vector<int> freqS[maxN];
vector<int> freqF[maxN];
char s[maxN];
int n;
char temp[5];
vector<query> queries[maxN];
int solve(int x, int y) {
  int ret = INF;
  int r = 0;
  for (int l = 0; l < freqS[x].size(); ++l) {
    while (r < freqF[y].size() && freqS[y][r] < freqS[x][l]) r++;
    if (r < freqF[y].size()) {
      ret = min(ret, max(freqF[y][r], freqF[x][l]) - freqS[x][l] + 1);
    }
  }
  return ret;
}
int answer[maxN];
int hh[maxN][5];
int last[maxN];
int best[maxN];
void process(int x) {
  for (int l = 1; l <= 4; ++l) {
    int ptr = 0;
    for (int i = l - 1; i < n; ++i) {
      while (ptr < freqF[x].size() && freqF[x][ptr] <= i) ptr++;
      if (ptr > 0 && (ptr == freqF[x].size() || freqF[x][ptr] > i)) ptr--;
      int h = hh[i][l];
      if (last[h] != x) {
        last[h] = x;
        best[h] = INF;
      }
      if (ptr >= 0 && ptr < freqF[x].size() && freqF[x][ptr] <= i)
        best[h] = min(best[h], i - min(freqS[x][ptr], i - l + 1) + 1);
    }
  }
  for (int l = 1; l <= 4; ++l) {
    int ptr = (int)freqS[x].size() - 1;
    for (int i = n - l; i >= 0; --i) {
      while (ptr >= 0 && freqS[x][ptr] >= i) ptr--;
      if (ptr + 1 < freqS[x].size() && (ptr == -1 || freqS[x][ptr] < i)) ptr++;
      int h = hh[i + l - 1][l];
      if (last[h] != x) {
        last[h] = x;
        best[h] = INF;
      }
      if (ptr >= 0 && ptr < freqS[x].size() && freqS[x][ptr] >= i)
        best[h] = min(best[h], max(freqF[x][ptr], i + l - 1) - i + 1);
    }
  }
  for (auto y : queries[x]) {
    if (last[y.y] != x) {
      last[y.y] = x;
      best[y.y] = INF;
    }
    answer[y.idx] = best[y.y];
  }
}
bool in(char a[5], char b[5]) {
  int n = strlen(a);
  int m = strlen(b);
  for (int i = 0; i < m - n + 1; ++i) {
    bool ok = true;
    for (int j = 0; j < n; ++j) {
      if (a[j] != b[i + j]) ok = false;
    }
    if (ok) return true;
  }
  return false;
}
int main() {
  gets(s);
  n = strlen(s);
  for (int i = 0; i < n; ++i) {
    for (int j = 1; j <= 4 && j + i - 1 < n; ++j) {
      temp[j - 1] = s[i + j - 1];
      temp[j] = '\0';
      int h = HASH(temp);
      freqS[h].push_back(i);
      freqF[h].push_back(i + j - 1);
      hh[i + j - 1][j] = h;
    }
  }
  const int T = (int)sqrt(n + 0.0);
  int q = nextInt<int>();
  for (int i = 0; i < q; ++i) {
    char foo[5], bar[5];
    scanf("%s %s\n", foo, bar);
    int h1 = HASH(foo);
    int h2 = HASH(bar);
    if (in(foo, bar) && !freqS[h2].empty()) {
      answer[i] = strlen(bar);
      continue;
    }
    if (in(bar, foo) && !freqS[h1].empty()) {
      answer[i] = strlen(foo);
      continue;
    }
    if (freqS[h1].size() > T) {
      queries[h1].push_back(query(h1, h2, i));
    } else if (freqS[h2].size() > T) {
      queries[h2].push_back(query(h2, h1, i));
    } else {
      answer[i] = min(solve(h1, h2), solve(h2, h1));
    }
  }
  fill(best, best + maxN, INF);
  for (int i = 0; i < maxN; ++i) {
    if (queries[i].empty()) continue;
    process(i);
  }
  for (int i = 0; i < q; ++i) {
    if (answer[i] == INF) answer[i] = -1;
    printf("%d\n", answer[i]);
  }
  return 0;
}
