#include <bits/stdc++.h>
using namespace std;
char s[50005];
map<string, int> st;
int sq = 300;
vector<int> p[500100];
vector<int> h;
string s1[500100];
int res[1010][1010];
void pro(int x1, int y1) {
  int x = h[x1];
  int y = h[y1];
  int pri = -1, prj = -1;
  int mn = 1e9;
  int r;
  for (int i = 0, j = 0; i < p[x].size() || j < p[y].size();) {
    if (i < p[x].size() && j < p[y].size()) {
      r = max(p[x][i] + s1[x].size(), p[y][j] + s1[y].size()) -
          min(p[x][i], p[y][j]);
      mn = min(mn, r);
    }
    if (i >= p[x].size())
      prj = p[y][j], j++;
    else if (j >= p[y].size())
      pri = p[x][i], i++;
    else if (p[x][i] < p[y][j])
      pri = p[x][i], i++;
    else
      prj = p[x][j], j++;
  }
  res[x1][y1] = mn;
  res[y1][x1] = mn;
}
int main() {
  scanf("%s", s);
  int n = strlen(s);
  int k = 0;
  for (int i = 0; i < n; i++) {
    string t = "";
    for (int j = 0; j < 4 && j + i < n; j++) {
      t.push_back(s[j + i]);
      if (st.count(t) == 0) {
        s1[k] = t;
        st[t] = k++;
      }
      p[st[t]].push_back(i);
    }
  }
  for (int i = 0; i < k; i++) {
    if (p[i].size() > sq) {
      h.push_back(i);
    }
  }
  for (int i = 0; i < h.size(); i++) {
    res[i][i] = s1[h[i]].size();
    for (int j = i + 1; j < h.size(); j++) {
      pro(i, j);
    }
  }
  int q;
  cin >> q;
  char s2[10];
  char s3[10];
  vector<int>::iterator it, it1;
  while (q--) {
    scanf("%s", s2);
    scanf("%s", s3);
    if (st.count(s2) == 0 || st.count(s3) == 0) {
      printf("-1\n");
      continue;
    }
    int x = st[s2];
    int y = st[s3];
    it = lower_bound(h.begin(), h.end(), x);
    it1 = lower_bound(h.begin(), h.end(), y);
    int mn = 1e9, r;
    if (it != h.end() && *it == x && it1 != h.end() && *it1 == y) {
      printf("%d\n", res[it - h.begin()][it1 - h.begin()]);
      continue;
    } else if (it == h.end() || *it != x) {
      for (int i = 0; i < p[x].size(); i++) {
        it = lower_bound(p[y].begin(), p[y].end(), p[x][i]);
        if (it != p[y].end()) {
          r = max(p[x][i] + s1[x].size(), *it + s1[y].size());
          r -= min(p[x][i], *it);
          mn = min(mn, r);
        }
        if (it != p[y].begin()) {
          it--;
          r = max(p[x][i] + s1[x].size(), *it + s1[y].size());
          r -= min(p[x][i], *it);
          mn = min(mn, r);
        }
      }
    } else {
      for (int i = 0; i < p[y].size(); i++) {
        it = lower_bound(p[x].begin(), p[x].end(), p[y][i]);
        if (it != p[x].end()) {
          r = max(p[y][i] + s1[y].size(), *it + s1[x].size());
          r -= min(p[y][i], *it);
          mn = min(mn, r);
        }
        if (it != p[x].begin()) {
          it--;
          r = max(p[y][i] + s1[y].size(), *it + s1[x].size());
          r -= min(p[y][i], *it);
          mn = min(mn, r);
        }
      }
    }
    printf("%d\n", mn);
  }
  return 0;
}
