#include <bits/stdc++.h>
using namespace std;
string s;
map<string, vector<int> > m;
map<string, int> memoria;
string codigo(string s1, string s2) { return s1 + "#" + s2; }
int calculadicot(int la, vector<int> &va, int lb, vector<int> &vb) {
  int sol = 1000000000;
  for (int ia = 0; ia < int(va.size()); ia++) {
    int inf = -1;
    int sup = int(vb.size());
    while (inf + 1 < sup) {
      int med = (inf + sup) / 2;
      if (vb[med] <= va[ia])
        inf = med;
      else
        sup = med;
    }
    if (inf >= 0) sol = min(sol, max(va[ia] + la, vb[inf] + lb) - vb[inf]);
    if (sup < int(vb.size()))
      sol = min(sol, max(va[ia] + la, vb[sup] + lb) - va[ia]);
  }
  return sol;
}
int calculafusion(int la, vector<int> &va, int lb, vector<int> &vb) {
  int ia = 0;
  int ib = 0;
  int sol = 1000000000;
  while (ia < int(va.size()) and ib < int(vb.size())) {
    if (va[ia] < vb[ib]) {
      sol = min(sol, max(va[ia] + la, vb[ib] + lb) - va[ia]);
      ia++;
    } else {
      sol = min(sol, max(va[ia] + la, vb[ib] + lb) - vb[ib]);
      ib++;
    }
  }
  return sol;
}
int calcula(int la, vector<int> &va, int lb, vector<int> &vb) {
  if (int(va.size()) > int(vb.size())) return calcula(lb, vb, la, va);
  if (int(va.size()) == 0) return -1;
  if (int(va.size()) * 10 > int(va.size()) + int(vb.size()))
    return calculafusion(la, va, lb, vb);
  return calculadicot(la, va, lb, vb);
}
int main() {
  cin >> s;
  for (int i = 0; i < int(s.size()); i++)
    for (int l = 1; l <= 4; l++) m[s.substr(i, l)].push_back(i);
  int q;
  cin >> q;
  for (int cas = 0; cas < q; cas++) {
    string a, b;
    cin >> a >> b;
    if (a > b) swap(a, b);
    string c = codigo(a, b);
    if (memoria.count(c))
      cout << memoria[c] << endl;
    else {
      vector<int> &va = m[a];
      vector<int> &vb = m[b];
      memoria[c] = calcula(int(a.size()), va, int(b.size()), vb);
      cout << memoria[c] << endl;
    }
  }
}
