#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, inf = 2e9;
string s, t1, t2;
int n;
map<pair<int, int>, int> used;
map<int, vector<int> > g;
inline int get_hash(string &s) {
  int cur = 0, p = 1;
  for (auto it : s) {
    cur += it * p;
    p *= 45;
  }
  return cur;
}
inline int get(int x, int y, int size_x, int size_y) {
  if (used.find(make_pair(x, y)) != used.end()) {
    return used[make_pair(x, y)];
  }
  if (g[x].size() > g[y].size()) {
    swap(size_x, size_y);
    swap(x, y);
  }
  int ans = inf;
  for (auto it : g[x]) {
    int pos = lower_bound(g[y].begin(), g[y].end(), it) - g[y].begin();
    if (pos > 0) {
      ans = min(ans, max(it + size_x, g[y][pos - 1] + size_y) - g[y][pos - 1]);
    }
    if (pos < g[y].size()) {
      ans = min(ans, max(it + size_x, g[y][pos] + size_y) - it);
    }
  }
  if (ans == inf) {
    ans = -1;
  }
  return used[make_pair(x, y)] = used[make_pair(y, x)] = ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> s >> n;
  for (int i = 0; i < s.size(); i++) {
    string t = "";
    for (int j = i; j < min(i + 4, (int)s.size()); j++) {
      t += s[j];
      g[get_hash(t)].push_back(i);
    }
  }
  for (int i = 1; i <= n; i++) {
    cin >> t1 >> t2;
    cout << get(get_hash(t1), get_hash(t2), t1.size(), t2.size()) << "\n";
  }
}
