#include <bits/stdc++.h>
using namespace std;
const int Q = (int)1e5;
const int BUBEN = 600;
const int INF = (int)1e9;
int nextInt() {
  int res;
  assert(scanf("%d", &res) == 1);
  return res;
}
string nextToken() {
  static const int S = 50000;
  static char buffer[S + 1];
  assert(scanf("%s", buffer) == 1);
  return string(buffer);
}
int hashCode(const string &s) {
  int res = 1;
  for (int i = 0; i < (int)s.size(); ++i) res = res * 32 + s[i] - 'a';
  res = res * 32 + (int)s.size();
  return res;
}
int answer[Q];
int main() {
  string s = nextToken();
  map<int, vector<int> > appearance;
  for (int len = 1; len <= 4; ++len)
    for (int i = 0; i + len <= (int)s.size(); ++i)
      appearance[hashCode(s.substr(i, len))].push_back(i);
  int queryCount = nextInt();
  map<int, vector<pair<int, int> > > bigQuery;
  for (int q = 0; q < queryCount; ++q) {
    int x = hashCode(nextToken());
    int y = hashCode(nextToken());
    answer[q] = INF;
    if (appearance.count(x) == 0 || appearance.count(y) == 0) {
      answer[q] = -1;
    } else if (appearance[x].size() > BUBEN) {
      bigQuery[x].push_back(make_pair(y, q));
    } else if (appearance[y].size() > BUBEN) {
      bigQuery[y].push_back(make_pair(x, q));
    } else {
      const vector<int> &ax = appearance[x];
      const vector<int> &ay = appearance[y];
      for (int i = 0, j = 0; i < (int)ax.size(); ++i) {
        while (j + 1 < (int)ay.size() && ay[j + 1] <= ax[i]) ++j;
        for (int k = j; k < j + 2 && k < (int)ay.size(); ++k)
          answer[q] = min(answer[q], max(ax[i] + (x & 31), ay[k] + (y & 31)) -
                                         min(ax[i], ay[k]));
      }
      swap(x, y);
    }
  }
  for (map<int, vector<pair<int, int> > >::const_iterator it = bigQuery.begin();
       it != bigQuery.end(); ++it) {
    map<int, int> shortest;
    int x = it->first;
    const vector<int> &ax = appearance[x];
    for (int len = 1; len <= 4; ++len) {
      for (int i = 0, j = 0; i + len <= (int)s.size(); ++i) {
        int y = hashCode(s.substr(i, len));
        while (j + 1 < (int)ax.size() && ax[j + 1] <= i) ++j;
        int dist = INF;
        for (int k = j; k < j + 2 && k < (int)ax.size(); ++k)
          dist = min(dist, max(i + len, ax[k] + (x & 31)) - min(i, ax[k]));
        if (shortest.count(y) == 0)
          shortest[y] = dist;
        else
          shortest[y] = min(shortest[y], dist);
      }
    }
    for (int i = 0; i < (int)it->second.size(); ++i)
      answer[it->second[i].second] = shortest[it->second[i].first];
  }
  for (int q = 0; q < queryCount; ++q) printf("%d\n", answer[q]);
  return 0;
}
