#include <bits/stdc++.h>
using namespace std;
const int N = 1100000;
bool vis[N];
int dp[N], ret[N];
char s[N];
vector<int> vec[N];
vector<pair<int, int> > query[N], all;
int main() {
  scanf("%s", s);
  int len = strlen(s);
  for (int i = 0; i < len; i++) s[i]++;
  for (int i = 0; i < len; i++) {
    int cur = 0;
    for (int j = i; j < i + 4 && j < len; j++) {
      cur = cur * 27 + s[j] - 'a';
      for (int k = j + 1; k < i + 4; k++) cur *= 27;
      vec[cur].push_back(i);
      all.push_back(make_pair(cur, j - i + 1));
      vis[cur] = true;
      for (int k = j + 1; k < i + 4; k++) cur /= 27;
    }
  }
  sort(all.begin(), all.end());
  all.erase(unique(all.begin(), all.end()), all.end());
  int q;
  scanf("%d", &q);
  int T = int(len / sqrt(q));
  for (int qnum = 0; qnum < q; qnum++) {
    ret[qnum] = len + 1;
    char s1[11], s2[11];
    scanf("%s%s", s1, s2);
    int len1 = strlen(s1), len2 = strlen(s2);
    for (int i = 0; i < len1; i++) s1[i]++;
    for (int i = 0; i < len2; i++) s2[i]++;
    for (int i = len1; i < 4; i++) s1[i] = 'a';
    for (int i = len2; i < 4; i++) s2[i] = 'a';
    int c1 = 0, c2 = 0;
    for (int i = 0; i < 4; i++)
      c1 = c1 * 27 + s1[i] - 'a', c2 = c2 * 27 + s2[i] - 'a';
    if (vec[c1].size() < vec[c2].size()) swap(c1, c2), swap(len1, len2);
    if (vec[c1].size() > T) {
      query[c1].push_back(make_pair(c2, qnum));
      continue;
    } else {
      vector<pair<int, int> > meg;
      for (int i = 0, j = 0; i < vec[c1].size() || j < vec[c2].size();) {
        if (i == vec[c1].size() ||
            j != vec[c2].size() && vec[c1][i] >= vec[c2][j])
          meg.push_back(make_pair(vec[c2][j++], 2));
        else
          meg.push_back(make_pair(vec[c1][i++], 1));
      }
      int last[3] = {-1, -1, -1};
      int l[3] = {0, len1, len2};
      for (int i = 0; i < meg.size(); i++) {
        int pos = meg[i].first, id = meg[i].second;
        if (~last[3 - id])
          ret[qnum] = min(ret[qnum],
                          max(pos + l[id] - 1, last[3 - id] + l[3 - id] - 1) -
                              last[3 - id] + 1);
        last[id] = pos;
      }
    }
  }
  for (auto x : all) {
    int c1 = x.first, ll = x.second;
    if (vec[c1].size() <= T) continue;
    for (auto y : all) dp[y.first] = len + 1;
    for (int i = 0, j = -1, k = 0; i < len; i++) {
      if (k < vec[c1].size() && i == vec[c1][k]) j = vec[c1][k++];
      int cur = 0;
      for (int ii = i; ii < i + 4 && ii < len; ii++) {
        cur = cur * 27 + s[ii] - 'a';
        for (int kk = ii + 1; kk < i + 4; kk++) cur *= 27;
        if (~j) dp[cur] = min(dp[cur], max(j + ll - 1, ii) - j + 1);
        for (int kk = ii + 1; kk < i + 4; kk++) cur /= 27;
      }
    }
    for (int i = len - 1, j = -1, k = vec[c1].size() - 1; i >= 0; i--) {
      if (~k && i == vec[c1][k]) j = vec[c1][k--];
      int cur = 0;
      for (int ii = i; ii < i + 4 && ii < len; ii++) {
        cur = cur * 27 + s[ii] - 'a';
        for (int kk = ii + 1; kk < i + 4; kk++) cur *= 27;
        if (~j) dp[cur] = min(dp[cur], max(j + ll - 1, ii) - i + 1);
        for (int kk = ii + 1; kk < i + 4; kk++) cur /= 27;
      }
    }
    for (auto y : query[c1]) {
      int c2 = y.first, qnum = y.second;
      if (vis[c2]) ret[qnum] = dp[c2];
    }
  }
  for (int qnum = 0; qnum < q; qnum++)
    printf("%d\n", ret[qnum] == len + 1 ? -1 : ret[qnum]);
  return 0;
}
