#include <bits/stdc++.h>
using namespace std;
const int INF = 100000000;
void imprimirVector(vector<int> v) {
  if (!v.empty()) {
    int p = int(v.size());
    cout << "[";
    for (int i = (0); i < (int)(p - 1); i++) cout << v[i] << ",";
    cout << v[p - 1] << "]" << endl;
  } else
    cout << "[]" << endl;
}
void mandatory_advance(int &i, int &j, const vector<int> &h,
                       const vector<int> &p, int max_distance) {
  int n = int(h.size()), m = int(p.size());
  while (i < n && j < m && h[i] <= p[j]) {
    while (j < m && p[j] <= h[i] + max_distance) j++;
    i++;
  }
}
const int ALPH = 32;
int char_hash(char x) { return x - 'a' + 1; }
int hashing(const string &s) {
  int n = int(s.size());
  int mult = 1, h = 0;
  for (int i = int(n) - 1; i >= 0; i--) {
    h += char_hash(s[i]) * mult;
    mult *= ALPH;
  }
  return h;
}
int length(int h) {
  int l = 0;
  while (h > 0) {
    l++;
    h /= ALPH;
  }
  return l;
}
const int MAX_H = 1048576;
const int MAX_S = 4;
int int_sqrt(int x) {
  int k = 1;
  while (k * k <= x) k++;
  return k - 1;
}
vector<int> app[MAX_H];
unordered_map<int, int> ans[MAX_H];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  string s;
  int q;
  while (cin >> s >> q) {
    int n = int(s.size());
    set<int> hashes;
    vector<vector<int> > mem_hash(n, vector<int>(MAX_S));
    for (int i = (0); i < (int)(n); i++)
      for (int k = (0); k < (int)(MAX_S); k++) {
        if (i + k < n) {
          mem_hash[i][k] = hashing(s.substr(i, k + 1));
          hashes.insert(mem_hash[i][k]);
          app[mem_hash[i][k]].push_back(i);
        }
      }
    int MAX_APP = 1000;
    for (const auto &h : hashes) {
      int h_app = int(app[h].size());
      if (h_app > MAX_APP) {
        for (const auto &h2 : hashes) ans[h][h2] = INF;
        int l = length(h);
        int j = 0;
        for (int i = (0); i < (int)(n); i++)
          for (int k = (0); k < (int)(MAX_S); k++) {
            if (i + k < n) {
              if (j < h_app && i > app[h][j]) j++;
              if (j > 0) {
                int end = max(i + k, app[h][j - 1] + l - 1);
                int start = min(i, app[h][j - 1]);
                ans[h][mem_hash[i][k]] =
                    min(ans[h][mem_hash[i][k]], end - start + 1);
              }
              if (j < h_app) {
                int end = max(i + k, app[h][j] + l - 1);
                int start = min(i, app[h][j]);
                ans[h][mem_hash[i][k]] =
                    min(ans[h][mem_hash[i][k]], end - start + 1);
              }
            }
          }
      }
    }
    for (int k = (0); k < (int)(q); k++) {
      string a, b;
      cin >> a >> b;
      int ha = hashing(a), hb = hashing(b);
      int app_a = int(app[ha].size()), app_b = int(app[hb].size());
      int length_a = int(a.size()), length_b = int(b.size());
      if (app_a > app_b) {
        swap(ha, hb);
        swap(app_a, app_b);
        swap(length_a, length_b);
      }
      if (ans[ha].find(hb) == ans[ha].end()) {
        ans[ha][hb] = INF;
        ans[hb][ha] = INF;
        if (app_a <= MAX_APP) {
          for (int _ = (0); _ < (int)(2); _++) {
            swap(ha, hb);
            swap(app_a, app_b);
            swap(length_a, length_b);
            int j = 0;
            for (int i = (0); i < (int)(app_a); i++) {
              while (j < app_b && app[ha][i] > app[hb][j]) j++;
              if (j < app_b) {
                int start = min(app[ha][i], app[hb][j]);
                int end =
                    max(app[ha][i] + length_a - 1, app[hb][j] + length_b - 1);
                ans[ha][hb] = min(ans[ha][hb], end - start + 1);
                ans[hb][ha] = min(ans[hb][ha], end - start + 1);
              }
            }
          }
        }
      }
      if (ans[ha][hb] == INF)
        cout << "-1\n";
      else
        cout << ans[ha][hb] << "\n";
    }
  }
  return 0;
}
