#include <bits/stdc++.h>
using namespace std;
map<string, int> mp;
char str[50010], s[100010][5], t[1000010][5];
int a[100010], b[100010];
vector<int> edge[200010], id[50010], pos[200010];
int times[200010], l[200010];
int ans[100010], f[200010];
int main() {
  int m;
  scanf("%s", str + 1);
  int n = strlen(str + 1);
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) ans[i] = n + 1;
  int num = 0;
  for (int i = 1; i <= m; i++) {
    scanf("%s%s", s[i], t[i]);
    if (mp.find(s[i]) == mp.end()) {
      mp[s[i]] = ++num;
      l[num] = strlen(s[i]);
    }
    if (mp.find(t[i]) == mp.end()) {
      mp[t[i]] = ++num;
      l[num] = strlen(t[i]);
    }
    a[i] = mp[s[i]];
    b[i] = mp[t[i]];
  }
  for (int i = 1; i <= n; i++) {
    string tmp = "";
    for (int j = i; j < i + 4 && j <= n; j++) {
      tmp += str[j];
      if (mp.find(tmp) != mp.end()) {
        id[i].push_back(mp[tmp]);
        times[mp[tmp]]++;
      }
    }
  }
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < id[i].size(); j++)
      if (times[id[i][j]] <= 310) pos[id[i][j]].push_back(i);
  for (int i = 1; i <= m; i++) {
    if (times[a[i]] <= 310 && times[b[i]] <= 310) {
      int k = 0;
      for (int j = 0; j < pos[a[i]].size(); j++) {
        while (k < pos[b[i]].size() && pos[b[i]][k] < pos[a[i]][j]) k++;
        if (k < pos[b[i]].size())
          ans[i] =
              min(ans[i], max(l[a[i]], pos[b[i]][k] + l[b[i]] - pos[a[i]][j]));
      }
      k = 0;
      for (int j = 0; j < pos[b[i]].size(); j++) {
        while (k < pos[a[i]].size() && pos[a[i]][k] < pos[b[i]][j]) k++;
        if (k < pos[a[i]].size())
          ans[i] =
              min(ans[i], max(l[b[i]], pos[a[i]][k] + l[a[i]] - pos[b[i]][j]));
      }
    }
    if (times[a[i]] > 310) edge[a[i]].push_back(i);
    if (times[b[i]] > 310) edge[b[i]].push_back(i);
  }
  for (int i = 1; i <= num; i++) {
    if (times[i] > 310) {
      for (int j = 1; j <= num; j++) f[j] = n + 1;
      int now = -1000000;
      for (int j = 1; j <= n; j++) {
        for (int k = 0; k < id[j].size(); k++) {
          if (id[j][k] == i) {
            now = j;
            break;
          }
        }
        for (int k = 0; k < id[j].size(); k++)
          f[id[j][k]] = min(f[id[j][k]], max(l[i], j + l[id[j][k]] - now));
      }
      now = 2000000;
      for (int j = n; j >= 1; j--) {
        for (int k = 0; k < id[j].size(); k++) {
          if (id[j][k] == i) {
            now = j;
            break;
          }
        }
        for (int k = 0; k < id[j].size(); k++)
          f[id[j][k]] = min(f[id[j][k]], max(l[id[j][k]], now + l[i] - j));
      }
      for (int j = 0; j < edge[i].size(); j++) {
        if (a[edge[i][j]] == i)
          ans[edge[i][j]] = min(ans[edge[i][j]], f[b[edge[i][j]]]);
        else
          ans[edge[i][j]] = min(ans[edge[i][j]], f[a[edge[i][j]]]);
      }
    }
  }
  for (int i = 1; i <= m; i++) {
    if (ans[i] > n)
      printf("-1\n");
    else
      printf("%d\n", ans[i]);
  }
  return 0;
}
