#include <bits/stdc++.h>
using namespace std;
struct node {
  int x;
  int y;
  node(int x, int y) {
    this->x = x;
    this->y = y;
  }
};
map<pair<int, int>, int> mp;
vector<int> q[531450];
char s[500100];
int check(int x, int y, int lenx, int leny) {
  int minn = 0x3f3f3f3f;
  for (int i = 0, j = 0; i < q[x].size(); i++) {
    for (; j < q[y].size() && q[y][j] < q[x][i]; j++)
      ;
    if (j >= q[y].size()) break;
    minn =
        min(max(q[x][i] + lenx, q[y][j] + leny) - min(q[x][i], q[y][j]), minn);
  }
  for (int j = q[y].size() - 1, i = q[x].size() - 1; i >= 0; i--) {
    for (; j >= 0 && q[y][j] > q[x][i]; j--)
      ;
    if (j < 0) break;
    minn =
        min(max(q[x][i] + lenx, q[y][j] + leny) - min(q[x][i], q[y][j]), minn);
  }
  if (minn == 0x3f3f3f3f) minn = -1;
  return minn;
}
int main() {
  scanf("%s", s);
  int len = strlen(s);
  for (int l = 1; l <= 4; l++) {
    for (int i = 0; i + l - 1 < len; i++) {
      int tmp = 0;
      for (int j = l - 1; j >= 0; j--) tmp = (tmp * 27) + s[i + j] - 'a' + 1;
      q[tmp].push_back(i);
    }
  }
  int t;
  scanf("%d", &t);
  while (t--) {
    char a[5];
    char b[5];
    scanf("%s%s", a, b);
    int aa = 0, bb = 0;
    int lena = strlen(a);
    int lenb = strlen(b);
    for (int i = lena - 1; i >= 0; i--) {
      aa = aa * 27 + a[i] - 'a' + 1;
    }
    for (int i = lenb - 1; i >= 0; i--) {
      bb = bb * 27 + b[i] - 'a' + 1;
    }
    if (mp[make_pair(aa, bb)] != 0) {
      printf("%d\n", mp[make_pair(aa, bb)]);
      continue;
    }
    int sum = check(aa, bb, lena, lenb);
    mp[make_pair(aa, bb)] = sum;
    printf("%d\n", sum);
  }
  return 0;
}
