#include <bits/stdc++.h>
using namespace std;
inline void EnableFileIO(const string &fileName, bool local = 0) {
  if (fileName.empty()) return;
  if (local) {
    freopen((fileName + ".in").c_str(), "r", stdin);
    freopen((fileName + ".out").c_str(), "w", stdout);
    return;
  }
  freopen((fileName + ".in").c_str(), "r", stdin);
  freopen((fileName + ".out").c_str(), "w", stdout);
}
const int INF = (1 << 30) - 1;
const long long LINF = (1LL << 61) - 1;
const double EPS = 1e-10, PI = acos(-1);
const int N = 2e5 + 100, MAGIC = 500;
char buf[N];
int n;
string s;
int q;
string a[N], b[N];
int cnt = -1;
map<string, int> rev;
int aid[N], bid[N], ap[N];
vector<int> pos[N], has[N], hasbig[N];
int len[N];
void getstr(string &s) {
  scanf("%s", buf);
  s = buf;
}
int getid(const string &s) {
  if (rev.count(s)) return rev[s];
  rev[s] = ++cnt;
  return cnt;
}
int nxt[N / 4][MAGIC];
int pre[N / 4][MAGIC];
vector<int> big;
int isbig[N], bigid[N], biglen[N];
int ansbig[MAGIC][MAGIC];
void updmin(int &a, int b) {
  if (b < a) a = b;
}
int ask(int aid, int bid) {
  int ans = INF;
  if (isbig[aid] && isbig[bid]) {
    ans = ansbig[bigid[aid]][bigid[bid]];
  } else if (!isbig[aid] && !isbig[bid]) {
    int i = 0, j = 0;
    for (; i < pos[aid].size(); i++) {
      while (j < pos[bid].size() && pos[bid][j] < pos[aid][i]) j++;
      if (j == pos[bid].size()) break;
      int val = max(pos[bid][j] - pos[aid][i] + len[bid], len[aid]);
      updmin(ans, val);
    }
  } else {
    if (isbig[aid]) swap(aid, bid);
    int bb = bigid[bid];
    assert(isbig[bid]);
    for (int i : pos[aid]) {
      if (nxt[i][bb] >= 0) {
        int val = max(nxt[i][bb] - i + len[bid], len[aid]);
        updmin(ans, val);
      }
      if (pre[i][bb] >= 0) {
        int val = max(i - pre[i][bb] + len[aid], len[bid]);
        updmin(ans, val);
      }
    }
  }
  return ans;
}
int main() {
  getstr(s);
  n = (int)s.length();
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    getstr(a[i]);
    getstr(b[i]);
    aid[i] = getid(a[i]);
    bid[i] = getid(b[i]);
  }
  for (auto i : rev) {
    len[i.second] = (int)i.first.length();
  }
  assert(rev.size() < N);
  for (int i = 0; i < n; i++) {
    string t = "";
    for (int j = i; j < i + 4 && j < n; j++) {
      t = t + s[j];
      if (rev.count(t)) {
        int k = rev[t];
        ++ap[k];
        pos[k].push_back(i);
      }
    }
  }
  for (int i = 0; i <= cnt; i++) {
    if (ap[i] > MAGIC) {
      isbig[i] = 1;
      bigid[i] = (int)big.size();
      biglen[big.size()] = len[i];
      big.push_back(i);
      for (int j : pos[i]) hasbig[j].push_back(bigid[i]);
    }
  }
  for (int i = 0; i < MAGIC; i++) nxt[n][i] = -1;
  for (int i = n - 1; i >= 0; i--) {
    memcpy(nxt[i], nxt[i + 1], sizeof nxt[i]);
    string t = "";
    for (int j = i; j < i + 4 && j < n; j++) {
      t = t + s[j];
      if (rev.count(t)) {
        int k = rev[t];
        if (isbig[k]) {
          nxt[i][bigid[k]] = i;
        }
      }
    }
  }
  for (int i = 0; i < MAGIC; i++) pre[0][i] = -1;
  for (int i = 0; i < n; i++) {
    if (i) memcpy(pre[i], pre[i - 1], sizeof pre[i]);
    string t = "";
    for (int j = i; j < i + 4 && j < n; j++) {
      t = t + s[j];
      if (rev.count(t)) {
        int k = rev[t];
        if (isbig[k]) {
          pre[i][bigid[k]] = i;
        }
      }
    }
  }
  assert(big.size() <= MAGIC);
  for (int i = 0; i < big.size(); i++) {
    for (int k = 0; k < big.size(); k++) ansbig[i][k] = INF;
  }
  for (int i = 0; i < n; i++) {
    for (int id : hasbig[i]) {
      for (int k = 0; k < big.size(); k++) {
        if (nxt[i][k] >= 0) {
          int val = max(nxt[i][k] - i + biglen[k], biglen[id]);
          updmin(ansbig[id][k], val);
          updmin(ansbig[k][id], val);
        }
        if (pre[i][k] >= 0) {
          int val = max(i - pre[i][k] + biglen[id], biglen[k]);
          updmin(ansbig[id][k], val);
          updmin(ansbig[k][id], val);
        }
      }
    }
  }
  for (int i = 0; i < q; i++) {
    int ans = min(ask(aid[i], bid[i]), ask(bid[i], aid[i]));
    printf("%d\n", ans == INF ? -1 : ans);
  }
  return 0;
}
