#include <bits/stdc++.h>
using namespace std;
const int N = 61000;
vector<int> pos[N * 10];
vector<pair<int, int> > ask[N * 10];
char a[N];
int n, q;
char b[6];
int x[N * 2], y[N * 2];
int ans[N * 2];
char t[6];
bool yd[N * 2];
int S = 405;
int shao[N * 10], duo[N * 10];
inline void minit(int &a, const int &b) {
  if (b < a) a = b;
}
int len[N * 10];
int tt[N * 2];
bool is[N * 2];
inline int work(int xx, int yy) {
  int rp = 23333333;
  int n1, n2;
  n1 = n2 = 0;
  int le = pos[xx].size() + pos[yy].size();
  for (int i = (1); i <= (int)le; i++)
    if (n1 == pos[xx].size()) {
      tt[i] = pos[yy][n2];
      n2++;
      is[i] = 0;
    } else if (n2 == pos[yy].size()) {
      tt[i] = pos[xx][n1];
      n1++;
      is[i] = 1;
    } else if (pos[xx][n1] < pos[yy][n2]) {
      tt[i] = pos[xx][n1];
      n1++;
      is[i] = 1;
    } else {
      tt[i] = pos[yy][n2];
      n2++;
      is[i] = 0;
    }
  int jin = -233333333;
  for (int i = (1); i <= (int)le; i++)
    if (is[i])
      jin = tt[i] - len[xx] + 1;
    else
      minit(rp, tt[i] - jin + 1);
  jin = 23333333;
  for (int i = (le); i >= (int)1; i--)
    if (is[i])
      jin = tt[i];
    else
      minit(rp, jin - (tt[i] - len[yy] + 1) + 1);
  return rp;
}
int rp[N * 10];
bool shi[N * 10];
pair<int, int> f[N * 10];
int top;
int main() {
  scanf("%s", a + 1);
  n = strlen(a + 1);
  memset(ans, 63, sizeof ans);
  for (int i = (0); i <= (int)26; i++) len[i] = 1;
  for (int i = (27); i <= (int)27 * 27 - 1; i++) len[i] = 2;
  for (int i = (27 * 27); i <= (int)27 * 27 * 27 - 1; i++) len[i] = 3;
  for (int i = (27 * 27 * 27); i <= (int)27 * 27 * 27 * 27 - 1; i++) len[i] = 4;
  for (int i = (1); i <= (int)n; i++)
    for (int j = (i - 3); j <= (int)i; j++)
      if (j > 0) {
        int bo = 0;
        for (int k = (j); k <= (int)i; k++) {
          bo *= 27;
          bo += a[k] - 'a' + 1;
        }
        pos[bo].push_back(i);
      }
  scanf("%d", &q);
  for (int i = (1); i <= (int)q; i++) {
    scanf("%s", b + 1);
    int m = strlen(b + 1);
    for (int j = (1); j <= (int)m; j++) {
      x[i] *= 27;
      x[i] += b[j] - 'a' + 1;
    }
    for (int j = (1); j <= (int)m; j++) t[j] = b[j];
    int m2 = m;
    scanf("%s", b + 1);
    m = strlen(b + 1);
    for (int j = (1); j <= (int)m; j++) {
      y[i] *= 27;
      y[i] += b[j] - 'a' + 1;
    }
    if (m2 >= m) {
      for (int j = (1); j <= (int)m2 - m + 1; j++) {
        bool fl = 1;
        for (int k = (j); k <= (int)j + m - 1; k++)
          if (t[k] != b[k - j + 1]) fl = 0;
        if (fl && pos[x[i]].size()) {
          ans[i] = m2;
          yd[i] = 1;
        }
      }
    } else {
      for (int j = (1); j <= (int)m - m2 + 1; j++) {
        bool fl = 1;
        for (int k = (j); k <= (int)j + m2 - 1; k++)
          if (b[k] != t[k - j + 1]) fl = 0;
        if (fl && pos[y[i]].size()) {
          ans[i] = m;
          yd[i] = 1;
        }
      }
    }
  }
  for (int i = (1); i <= (int)q; i++)
    if (pos[x[i]].size() <= S && pos[y[i]].size() <= S && !yd[i]) {
      ans[i] = work(x[i], y[i]);
    } else if (!yd[i]) {
      if (pos[x[i]].size() > S)
        ask[x[i]].push_back(pair<int, int>(y[i], i));
      else
        ask[y[i]].push_back(pair<int, int>(x[i], i));
    }
  for (int i = (0); i <= (int)27 * 27 * 27 * 27 - 1; i++)
    if (pos[i].size() > S) {
      for (int j = (0); j <= (int)ask[i].size() - 1; j++) {
        shi[ask[i][j].first] = 1;
        rp[ask[i][j].first] = 23333333;
      }
      shi[i] = 1;
      top = 0;
      for (int r = (1); r <= (int)n; r++)
        for (int l = (r - 3); l <= (int)r; l++)
          if (l > 0) {
            int bo = 0;
            for (int k = (l); k <= (int)r; k++) {
              bo *= 27;
              bo += a[k] - 'a' + 1;
            }
            if (!shi[bo]) continue;
            f[++top] = pair<int, int>(r, bo);
          }
      int jin = -23333333;
      for (int j = (1); j <= (int)top; j++)
        if (f[j].second == i)
          jin = f[j].first - len[i] + 1;
        else
          minit(rp[f[j].second], f[j].first - jin + 1);
      jin = 233333333;
      for (int j = (top); j >= (int)1; j--)
        if (f[j].second == i)
          jin = f[j].first;
        else
          minit(rp[f[j].second], jin - (f[j].first - len[f[j].second] + 1) + 1);
      for (int j = (0); j <= (int)ask[i].size() - 1; j++) {
        minit(ans[ask[i][j].second], rp[ask[i][j].first]);
        shi[ask[i][j].first] = 0;
      }
    }
  for (int i = (1); i <= (int)q; i++)
    if (ans[i] > n)
      printf("-1\n");
    else
      printf("%d\n", ans[i]);
  return 0;
}
