#include <bits/stdc++.h>
using namespace std;
const int sea = 5e4 + 10;
const int ocean = 6e5 + 10;
char s[sea];
int n, m, ans;
vector<int> v[ocean];
map<pair<int, int>, int> mp;
int get(int x, int y, char* ss) { return (x * 27) + ss[y] - 'a' + 1; }
int main() {
  scanf("%s", s);
  n = strlen(s);
  for (int k = 1; k <= 4; k++) {
    for (int i = 0; i + k - 1 < n; i++) {
      int temp = 0;
      for (int j = k - 1; j >= 0; j--) temp = get(temp, i + j, s);
      v[temp].push_back(i);
    }
  }
  scanf("%d", &m);
  while (m--) {
    char s1[6], s2[6];
    scanf("%s%s", s1, s2);
    int l1 = strlen(s1), l2 = strlen(s2);
    int t1 = 0, t2 = 0;
    for (int i = l1 - 1; i >= 0; i--) t1 = get(t1, i, s1);
    for (int i = l2 - 1; i >= 0; i--) t2 = get(t2, i, s2);
    if (mp[make_pair(t1, t2)] != 0) {
      printf("%d\n", mp[make_pair(t1, t2)]);
      continue;
    }
    int ans = n + 2;
    for (int i = 0, j = 0; i < v[t1].size(); i++) {
      while (j < v[t2].size() && v[t2][j] < v[t1][i]) j++;
      if (j >= v[t2].size()) break;
      ans =
          min(ans, max(v[t2][j] + l2, v[t1][i] + l1) - min(v[t1][i], v[t2][j]));
    }
    for (int i = v[t1].size() - 1, j = v[t2].size() - 1; i >= 0; i--) {
      while (j >= 0 && v[t2][j] > v[t1][i]) j--;
      if (j < 0) break;
      ans =
          min(ans, max(v[t2][j] + l2, v[t1][i] + l1) - min(v[t1][i], v[t2][j]));
    }
    if (ans == n + 2) ans = -1;
    mp[make_pair(t1, t2)] = ans;
    printf("%d\n", ans);
  }
  return 0;
}
