#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5 + 5;
struct Trie {
  static int nodes;
  int nodeInd, strInd;
  char lastChar;
  Trie *failLink, *successLink;
  Trie *parent, *root;
  Trie *children[30];
  Trie() {
    this->nodeInd = this->nodes;
    this->nodes++;
    this->strInd = -1;
    this->lastChar = '#';
    this->root = nullptr;
    this->parent = nullptr;
    this->failLink = this->successLink = nullptr;
    for (char c = 'a'; c <= 'z'; c++) {
      this->children[c - 'a'] = nullptr;
    }
  }
  void addString(string s, int strInd, int ind = 0) {
    if (ind == s.size()) {
      this->strInd = strInd;
      return;
    }
    if (this->children[s[ind] - 'a'] == nullptr) {
      this->children[s[ind] - 'a'] = new Trie();
      this->children[s[ind] - 'a']->parent = this;
      this->children[s[ind] - 'a']->lastChar = s[ind];
      this->children[s[ind] - 'a']->root = this->root;
    }
    this->children[s[ind] - 'a']->addString(s, strInd, ind + 1);
  }
  int getString(string s, int ind = 0) {
    if (ind == s.size()) return this->strInd;
    return this->children[s[ind] - 'a']->getString(s, ind + 1);
  }
  void doLinks() {
    queue<Trie *> q;
    q.push(this);
    this->failLink = this;
    this->successLink = nullptr;
    while (q.empty() == false) {
      Trie *curr = q.front();
      q.pop();
      for (int c = 'a'; c <= 'z'; c++) {
        if (curr->children[c - 'a'] != nullptr) {
          q.push(curr->children[c - 'a']);
        }
      }
      if (curr == curr->root) continue;
      if (curr->parent == curr->root) {
        curr->failLink = curr->root;
        curr->successLink = nullptr;
        continue;
      }
      curr->failLink = curr->parent->failLink;
      while (curr->failLink->children[curr->lastChar - 'a'] == nullptr &&
             curr->failLink != curr->root)
        curr->failLink = curr->failLink->failLink;
      if (curr->failLink->children[curr->lastChar - 'a'] == nullptr)
        curr->failLink = curr->root;
      else
        curr->failLink = curr->failLink->children[curr->lastChar - 'a'];
      if (curr->failLink->strInd != -1)
        curr->successLink = curr->failLink;
      else
        curr->successLink = curr->failLink->successLink;
    }
  }
  void printTrie() {
    cout << this << " " << this->lastChar << " | " << this->failLink << " "
         << this->successLink << '\n';
    for (char c = 'a'; c <= 'z'; c++) {
      if (this->children[c - 'a'] != nullptr) {
        this->children[c - 'a']->printTrie();
      }
    }
  }
};
int Trie::nodes = 0;
struct Query {
  string a, b;
  Query() {}
  Query(string a, string b) {
    this->a = a;
    this->b = b;
  }
};
int n;
string s;
int Q;
Query q[MAXN];
Trie *T;
string ind2Str[MAXN];
vector<int> str2Inds[MAXN];
map<pair<int, int>, int> memo;
void Init() {
  T = new Trie();
  T->root = T;
  set<string> allStrings;
  for (int i = 0; i < Q; i++) {
    allStrings.insert(q[i].a);
    allStrings.insert(q[i].b);
  }
  int ind = 0;
  for (string x : allStrings) {
    ind2Str[ind] = x;
    T->addString(x, ind);
    ind++;
  }
  T->doLinks();
  Trie *curr = T;
  for (int i = 0; i < n; i++) {
    while (curr != curr->root && curr->children[s[i] - 'a'] == nullptr)
      curr = curr->failLink;
    if (curr->children[s[i] - 'a'] != nullptr)
      curr = curr->children[s[i] - 'a'];
    Trie *succ = curr;
    while (succ != nullptr) {
      if (succ->strInd != -1) {
        str2Inds[succ->strInd].push_back(i - ind2Str[succ->strInd].size() + 1);
      }
      succ = succ->successLink;
    }
  }
  for (int i = 0; i < ind; i++) {
  }
}
pair<int, int> findInds(int x, vector<int> &v) {
  if (x <= v[0]) return {-1, v[0]};
  if (v[v.size() - 1] <= x) return {v[v.size() - 1], n};
  int l = 0, r = v.size() - 1, mid;
  while (l + 1 < r) {
    mid = (l + r) / 2;
    if (v[mid] >= x)
      r = mid;
    else
      l = mid + 1;
  }
  int ind = -1;
  if (v[l] >= x)
    ind = l;
  else
    ind = r;
  return {v[ind - 1], v[ind]};
}
int calcLen(int pos1, int pos2, int sz1, int sz2) {
  int l = min(pos1, pos2);
  int r = max(pos1 + sz1 - 1, pos2 + sz2 - 1);
  return r - l + 1;
}
void answerQuery(int aInd, int bInd) {
  if (str2Inds[aInd].size() > str2Inds[bInd].size()) swap(aInd, bInd);
  int answer = MAXN;
  if (memo.count({aInd, bInd}) == false) {
    for (int ind : str2Inds[aInd]) {
      pair<int, int> sol = findInds(ind, str2Inds[bInd]);
      if (sol.first != -1)
        answer = min(answer, calcLen(sol.first, ind, ind2Str[bInd].size(),
                                     ind2Str[aInd].size()));
      if (sol.second != n)
        answer = min(answer, calcLen(ind, sol.second, ind2Str[aInd].size(),
                                     ind2Str[bInd].size()));
    }
  } else {
    answer = memo[{aInd, bInd}];
  }
  memo[{aInd, bInd}] = answer;
  cout << ((answer == MAXN) ? -1 : answer) << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> s;
  n = s.size();
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    string a, b;
    cin >> a >> b;
    q[i] = Query(a, b);
  }
  Init();
  for (int i = 0; i < Q; i++) {
    answerQuery(T->getString(q[i].a), T->getString(q[i].b));
  }
}
