#include <bits/stdc++.h>
using namespace std;
const int HMAX = 27 * 27 * 27 * 27;
vector<int> pos[HMAX];
int mp[HMAX];
int dis[290][200001];
string S;
int hashmap[HMAX];
const int SIZ = 700;
int getlen(int hash) {
  int cnt = 0;
  while (hash) {
    cnt++;
    hash /= 27;
  }
  return cnt;
}
int gethash(string s) {
  int len = s.size();
  int hashnow = 0;
  for (int i = 0; i < len; i++) hashnow = hashnow * 27 + s[i] - 'a' + 1;
  return hashnow;
}
int getdis(int pos1, int pos2, int len1, int len2) {
  int st = min(pos1, pos2);
  int end = max(pos1 + len1, pos2 + len2);
  return end - st;
}
int hashcnt = 0;
int main() {
  cin >> S;
  int len = S.size();
  for (int i = 0; i < len; i++) {
    int hashnow = 0;
    for (int j = i; j < i + 4 && j < len; j++) {
      hashnow = hashnow * 27 + S[j] - 'a' + 1;
      if (hashmap[hashnow] == 0) hashcnt++, hashmap[hashnow] = hashcnt;
      pos[hashnow].push_back(i);
    }
  }
  memset(dis, 0x3f, sizeof(dis));
  int ct = 0;
  for (int x = 0; x < HMAX; x++)
    if (pos[x].size() > SIZ) {
      ct++;
      mp[x] = ct;
      for (int olen = 0; olen < 4; olen++) {
        int l = 0;
        for (int i = 0; i < len; i++) {
          int hashnow = 0;
          for (int j = i; j <= i + olen && j < len; j++)
            hashnow = hashnow * 27 + S[j] - 'a' + 1;
          int id = hashmap[hashnow];
          dis[ct][id] = min(dis[ct][id],
                            min(getdis(pos[x][l + 1], i, getlen(x), olen + 1),
                                getdis(pos[x][l], i, getlen(x), olen + 1)));
          if (i == pos[x][l + 1] && l < pos[x].size() - 2) {
            l++;
          }
        }
      }
    }
  int Q;
  scanf("%d", &Q);
  while (Q--) {
    string s1, s2;
    cin >> s1 >> s2;
    int h1 = gethash(s1);
    int h2 = gethash(s2);
    if (pos[h1].size() > SIZ) {
      if (dis[mp[h1]][hashmap[h2]] == 0x3f3f3f3f)
        puts("-1");
      else
        printf("%d\n", dis[mp[h1]][hashmap[h2]]);
    } else if (pos[h2].size() > SIZ) {
      if (dis[mp[h2]][hashmap[h1]] == 0x3f3f3f3f)
        puts("-1");
      else
        printf("%d\n", dis[mp[h2]][hashmap[h1]]);
    } else {
      if (pos[h1].size() == 0 || pos[h2].size() == 0)
        puts("-1");
      else {
        if (pos[h1].size() == 1) pos[h1].push_back(pos[h1][0]);
        int l = 0;
        int ll = pos[h2].size();
        int res = 0x3f3f3f3f;
        for (int i = 0; i < ll; i++) {
          while (l < pos[h1].size() - 2 && pos[h2][i] > pos[h1][l + 1]) {
            l++;
          }
          res = min(
              res,
              min(getdis(pos[h1][l], pos[h2][i], s1.size(), s2.size()),
                  getdis(pos[h1][l + 1], pos[h2][i], s1.size(), s2.size())));
        }
        printf("%d\n", res);
      }
    }
  }
}
