#include <bits/stdc++.h>
using namespace std;
map<long long, int> mp;
bool ck[600000];
int len[600000];
char s[50005];
int sz, q;
vector<int> pos[600000];
vector<int> E1, E2;
int getId(char* str, int sz) {
  int ret = 0;
  for (int i = 0; i < sz; ++i) ret *= 27, ret += str[i] - 'a' + 1;
  return ret;
}
int getAns(int i1, int l1, int i2, int l2) {
  return max(i1 + l1 - 1, i2 + l2 - 1) - min(i1, i2) + 1;
}
int main() {
  scanf("%s", s), sz = strlen(s);
  scanf("%d", &q);
  for (int i = 0; i < q; ++i) {
    char s1[10], s2[10];
    scanf("%s%s", s1, s2);
    int u = getId(s1, strlen(s1));
    int v = getId(s2, strlen(s2));
    E1.push_back(u), E2.push_back(v);
    ck[u] = ck[v] = 1;
    len[u] = strlen(s1);
    len[v] = strlen(s2);
  }
  for (int i = 0; i < sz; ++i) {
    for (int j = i; j < sz; ++j) {
      if (j - i + 1 > 4) break;
      int u = getId(s + i, j - i + 1);
      if (!ck[u]) continue;
      pos[u].push_back(i);
    }
  }
  for (int i = 0; i < q; ++i) {
    int u = E1[i], v = E2[i];
    if (!ck[u] || !ck[v] || pos[u].size() == 0 || pos[v].size() == 0) {
      printf("-1\n");
      continue;
    }
    if (pos[u].size() > pos[v].size() ||
        (pos[u].size() == pos[v].size() && u > v))
      swap(u, v);
    if (u == v)
      printf("%d\n", len[u]);
    else if (mp.count(u * 1000000ll + v))
      printf("%d\n",
             mp[u * 1000000ll + v] == 1000000 ? -1 : mp[u * 1000000ll + v]);
    else {
      int& ret = mp[u * 1000000ll + v];
      ret = 1000000;
      for (int j = 0; j < pos[u].size(); ++j) {
        int x = pos[u][j], y, idx;
        idx = lower_bound(pos[v].begin(), pos[v].end(), x) - pos[v].begin();
        if (idx == pos[v].size())
          idx--;
        else if (pos[v][idx] > x)
          idx--;
        if (idx >= 0) {
          y = pos[v][idx];
          ret = min(ret, getAns(x, len[u], y, len[v]));
        }
        idx = upper_bound(pos[v].begin(), pos[v].end(), x) - pos[v].begin();
        if (idx == pos[v].size()) idx--;
        if (idx >= 0) {
          y = pos[v][idx];
          ret = min(ret, getAns(x, len[u], y, len[v]));
        }
      }
      printf("%d\n", ret == 1000000 ? -1 : ret);
    }
  }
}
