#include <bits/stdc++.h>
using namespace std;
const int Q = 100100, rt = 223, inf = 1 << 29;
int ans[Q], todo[Q];
pair<int, int> look[Q];
int a[Q], b[Q];
string s;
unordered_map<int, unordered_set<int>> post;
map<pair<int, int>, int> ans2;
unordered_map<int, vector<int>> matches;
int f(int x, int xs, int y, int ys) { return max(x + xs, y + ys) - min(x, y); }
int S[100000][5];
map<string, int> intern;
int interned_size[500000];
int cnt = 1;
int get(const string& s) {
  auto it = intern.find(s);
  if (it == end(intern)) {
    interned_size[cnt] = s.size();
    return intern[s] = cnt++;
  }
  return it->second;
}
int brute(int a, int b) {
  int res = inf;
  vector<int>&ma = matches[a], &mb = matches[b];
  for (int i = 0; i < ma.size(); ++i) {
    int j = lower_bound(begin(mb), end(mb), ma[i]) - begin(mb);
    if (j < mb.size())
      res = min(res, f(ma[i], interned_size[a], mb[j], interned_size[b]));
  }
  return res;
}
int main() {
  cin >> s;
  for (int i = 0; i < s.size(); ++i) {
    for (int w = 1; w <= 4; ++w) {
      S[i][w] = get(s.substr(i, w));
      matches[S[i][w]].push_back(i);
    }
  }
  int q;
  cin >> q;
  fill(ans, ans + q, inf);
  for (int i = 0; i < q; ++i) {
    string as, bs;
    cin >> as >> bs;
    int a = get(as), b = get(bs);
    if (max(matches[a].size(), matches[b].size()) < rt) {
      ans[i] = min(brute(a, b), brute(b, a));
    } else {
      if (matches[a].size() < matches[b].size()) swap(a, b);
      post[a].insert(b);
      look[i] = make_pair(a, b);
      todo[i] = 1;
    }
  }
  for (auto& it : post) {
    const int a = it.first;
    auto& m = it.second;
    int last = matches[a][0];
    for (int i = last; i < s.size(); ++i) {
      for (int w = 1; w <= 4; ++w)
        if (S[i][w] == a) last = i;
      for (int w = 1; w <= 4; ++w) {
        int b = S[i][w];
        if (m.find(b) == end(m)) continue;
        auto st = make_pair(a, b);
        int sol = f(last, interned_size[a], i, interned_size[b]);
        auto fi = ans2.find(st);
        if (fi == end(ans2) || sol < fi->second) ans2[st] = sol;
      }
    }
    last = matches[a].back();
    for (int i = last; i >= 0; --i) {
      for (int w = 1; w <= 4; ++w)
        if (S[i][w] == a) last = i;
      for (int w = 1; w <= 4; ++w) {
        int b = S[i][w];
        if (m.find(b) == end(m)) continue;
        auto st = make_pair(a, b);
        int sol = f(last, interned_size[a], i, interned_size[b]);
        auto fi = ans2.find(st);
        if (fi == end(ans2) || sol < fi->second) ans2[st] = sol;
      }
    }
  }
  for (int i = 0; i < q; ++i) {
    if (todo[i]) ans[i] = ans2.find(look[i]) != end(ans2) ? ans2[look[i]] : inf;
    cout << (ans[i] == inf ? -1 : ans[i]) << endl;
  }
}
