#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)1e6 + 5;
const int mod = (int)1e9 + 7;
const long long inf = 1LL << 50;
string s;
int n, q;
vector<int> pos[maxn];
map<pair<int, int>, int> res;
int H(string a) {
  int ret = 0;
  for (int i = 0; i < a.size(); i++) {
    ret *= 26;
    ret += a[i] - 'a' + 1;
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> s >> q;
  n = s.size();
  for (int i = 0; i < n; i++)
    for (int len = 1; len <= 4; len++)
      if (i + len <= n) pos[H(s.substr(i, len))].push_back(i);
  while (q--) {
    string a, b;
    cin >> a >> b;
    int ha = H(a);
    int hb = H(b);
    if (pos[ha].size() == 0 || pos[hb].size() == 0) {
      cout << "-1\n";
      continue;
    }
    if (res.count(make_pair(H(a), H(b)))) {
      cout << res[make_pair(H(a), H(b))] << endl;
      continue;
    }
    int flag = 0;
    if (a.size() > b.size()) swap(a, b);
    for (int len = 1; len <= b.size(); len++) {
      for (int i = 0; i < b.size(); i++) {
        if (i + len <= b.size() && b.substr(i, len) == a) {
          cout << b.size() << endl;
          flag = 1;
          break;
        }
      }
    }
    if (flag) continue;
    ha = H(a);
    hb = H(b);
    if (pos[ha].size() > pos[hb].size()) {
      swap(ha, hb);
      swap(a, b);
    }
    int tmp = mod;
    for (int i = 0; i < pos[ha].size(); i++) {
      int j = lower_bound(pos[hb].begin(), pos[hb].end(), pos[ha][i]) -
              pos[hb].begin();
      if (j >= 0 && j < pos[hb].size()) {
        tmp = min(tmp, (int)(pos[hb][j] + b.size() - pos[ha][i]));
      }
      j--;
      if (j >= 0 && j < pos[hb].size()) {
        tmp = min(tmp, (int)(pos[ha][i] + a.size() - pos[hb][j]));
      }
    }
    res[make_pair(ha, hb)] = res[make_pair(hb, ha)] = tmp;
    cout << tmp << endl;
  }
}
