#include <bits/stdc++.h>
using namespace std;
template <typename T>
T BigMod(T b, T p, T m) {
  if (p == 0) return 1;
  if (p % 2 == 0) {
    T s = BigMod(b, p / 2, m);
    return ((s % m) * (s % m)) % m;
  }
  return ((b % m) * (BigMod(b, p - 1, m) % m)) % m;
}
template <typename T>
T ModInv(T b, T m) {
  return BigMod(b, m - 2, m);
}
template <typename T>
T in() {
  char ch;
  T n = 0;
  bool ng = false;
  while (1) {
    ch = getchar();
    if (ch == '-') {
      ng = true;
      ch = getchar();
      break;
    }
    if (ch >= '0' && ch <= '9') break;
  }
  while (1) {
    n = n * 10 + (ch - '0');
    ch = getchar();
    if (ch < '0' || ch > '9') break;
  }
  return (ng ? -n : n);
}
template <typename T>
T POW(T B, T printf) {
  if (printf == 0) return 1;
  if (printf & 1)
    return B * POW(B, printf - 1);
  else
    return (POW(B, printf / 2) * POW(B, printf / 2));
}
template <typename T>
T Bigmod(T b, T p, T m) {
  if (p == 0)
    return 1;
  else if (!(p & 1))
    return (Bigmod(b, p / 2, m) * Bigmod(b, p / 2, m)) % m;
  else
    return ((b % m) * Bigmod(b, p - 1, m)) % m;
}
template <typename T>
T Dis(T x1, T y1, T x2, T y2) {
  return sqrt((x1 - x2 * x1 - x2) + (y1 - y2 * y1 - y2));
}
template <typename T>
T Angle(T x1, T y1, T x2, T y2) {
  return atan(double(y1 - y2) / double(x1 - x2));
}
template <typename T>
T DIFF(T a, T b) {
  T d = a - b;
  if (d < 0)
    return -d;
  else
    return d;
}
template <typename T>
T ABS(T a) {
  if (a < 0)
    return -a;
  else
    return a;
}
template <typename T>
T gcd(T a, T b) {
  if (a < 0) return gcd(-a, b);
  if (b < 0) return gcd(a, -b);
  return (b == 0) ? a : gcd(b, a % b);
}
template <typename T>
T lcm(T a, T b) {
  if (a < 0) return lcm(-a, b);
  if (b < 0) return lcm(a, -b);
  return a * (b / gcd(a, b));
}
template <typename T>
T euclide(T a, T b, T &x, T &y) {
  if (a < 0) {
    T d = euclide(-a, b, x, y);
    x = -x;
    return d;
  }
  if (b < 0) {
    T d = euclide(a, -b, x, y);
    y = -y;
    return d;
  }
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  } else {
    T d = euclide(b, a % b, x, y);
    T t = x;
    x = y;
    y = t - (a / b) * y;
    return d;
  }
}
template <typename T>
void ia(T a[], int n) {
  for (int i = 0; i < n; i++) cin >> a[i];
}
template <typename T>
void pa(T a[], int n) {
  for (int i = 0; i < n - 1; i++) cout << a[i] << " ";
  cout << a[n - 1] << endl;
}
template <typename T>
long long int isLeft(T a, T b, T c) {
  return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y);
}
int Set(int N, int pos) { return N = N | (1 << pos); }
int Reset(int N, int pos) { return N = N & ~(1 << pos); }
bool Check(int N, int pos) { return (bool)(N & (1 << pos)); }
template <class T, class first>
inline T togglebit(T a, first i) {
  T t = 1;
  return (a ^ (t << i));
}
int toInt(string s) {
  int sm;
  stringstream ss(s);
  ss >> sm;
  return sm;
}
int toLlint(string s) {
  long long int sm;
  stringstream ss(s);
  ss >> sm;
  return sm;
}
int cdigittoint(char ch) { return ch - '0'; }
bool isVowel(char ch) {
  ch = toupper(ch);
  if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E')
    return true;
  return false;
}
bool isConst(char ch) {
  if (isalpha(ch) && !isVowel(ch)) return true;
  return false;
}
double DEG(double x) { return (180.0 * x) / (2.0 * acos(0.0)); }
double RAD(double x) { return (x * (double)2.0 * acos(0.0)) / (180.0); }
struct Node {
  int ch[26];
  int id;
  int cnt;
  Node() {
    for (int i = 0; i < 26; i++) ch[i] = -1;
    id = -1;
  }
};
int cnt;
char s[200007], ss[10];
Node Tree[200007];
vector<int> ids[200007];
int avail;
map<pair<int, int>, int> vis;
int ans[200007];
vector<pair<int, int> > offline[200007];
int szs[200007];
int len[200007];
void Insert(int x, int n, int id) {
  int idx = 0;
  for (int i = x, j = 0; i < n && j < 4; i++, j++) {
    int dg = s[i] - 'a';
    if (Tree[idx].ch[dg] == -1) {
      Tree[idx].ch[dg] = ++avail;
      Tree[avail] = Node();
    }
    idx = Tree[idx].ch[dg];
    if (Tree[idx].id == -1) {
      Tree[idx].id = ++cnt;
    }
    ids[Tree[idx].id].push_back(id);
    szs[Tree[idx].id] = j;
  }
}
int QueryID(char *s) {
  int idx = 0;
  for (int i = 0; s[i]; i++) {
    int dg = s[i] - 'a';
    if (Tree[idx].ch[dg] == -1) return -1;
    idx = Tree[idx].ch[dg];
  }
  return Tree[idx].id;
}
void Traverse(int x, int n) {
  int idx = 0;
  for (int i = x, j = 0; i < n && j < 4; i++, j++) {
    int dg = s[i] - 'a';
    idx = Tree[idx].ch[dg];
    int id = Tree[idx].id;
    for (int k = 0; k < (int)offline[id].size(); k++) {
      int yy = offline[id][k].first;
      int zz = offline[id][k].second;
      int mn = 1000000007;
      int &jj = len[yy];
      while (jj < (int)ids[yy].size() && ids[yy][jj] < x) jj++;
      if (jj < (int)ids[yy].size())
        mn = min(mn, max(ids[yy][jj] - x + szs[yy] + 1, szs[id] + 1));
      if (jj > 0)
        mn = min(mn, max(x + szs[id] - ids[yy][jj - 1] + 1, szs[yy] + 1));
      ans[zz] = min(ans[zz], mn);
    }
  }
}
int main() {
  scanf("%s", s);
  int n = strlen(s);
  Tree[0] = Node();
  for (int i = 0; i < n; i++) Insert(i, n, i);
  int sq = sqrt(n) + 2;
  int(m);
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    scanf("%s", ss);
    int id1 = QueryID(ss);
    int ln1 = strlen(ss);
    scanf("%s", ss);
    int id2 = QueryID(ss);
    int ln2 = strlen(ss);
    if (id1 == -1 || id2 == -1) {
      ans[i] = 1000000007;
      continue;
    }
    int sz1 = ids[id1].size();
    int sz2 = ids[id2].size();
    if (sz1 < sz2)
      swap(sz1, sz2), swap(id1, id2), swap(ln1, ln2);
    else if (sz1 == sz2)
      if (id1 > id2) swap(sz1, sz2), swap(id1, id2), swap(ln1, ln2);
    if (vis[make_pair(id1, id2)]) {
      ans[i] = -vis[make_pair(id1, id2)];
      continue;
    }
    vis[make_pair(id1, id2)] = i;
    if (sz1 > sq) {
      offline[id2].push_back(make_pair(id1, i));
      ans[i] = 1000000007;
      continue;
    }
    int ii = 0, jj = 0;
    int mn = 1000000007;
    while (ii < (int)ids[id1].size() && jj < (int)ids[id2].size()) {
      if (ids[id1][ii] > ids[id2][jj]) {
        mn = min(mn, max(ln2, ids[id1][ii] + ln1 - ids[id2][jj]));
        jj++;
      } else {
        mn = min(mn, max(ln1, ids[id2][jj] + ln2 - ids[id1][ii]));
        ii++;
      }
    }
    ans[i] = mn;
  }
  for (int i = 1; i <= cnt; i++) sort(offline[i].begin(), offline[i].end());
  for (int i = 0; i < n; i++) {
    Traverse(i, n);
  }
  for (int i = 1; i <= m; i++) {
    if (ans[i] == 1000000007)
      printf("%d\n", -1);
    else if (ans[i] < 0)
      printf("%d\n", ans[-ans[i]]);
    else
      printf("%d\n", ans[i]);
  }
  return 0;
}
