#include <bits/stdc++.h>
struct TSubStr {
  TSubStr(const std::string& text, size_t begin, size_t end)
      : Text(text), Begin(begin), End(end) {}
  const std::string& Text;
  size_t Begin;
  size_t End;
  static inline size_t getId(const std::string& text, size_t begin,
                             size_t end) {
    size_t id = 0;
    size_t power = 1;
    for (size_t i = begin; i < end; ++i) {
      id += (text[i] - 'a' + 1) * power;
      power *= 27;
    }
    return id;
  }
  static inline size_t getId(const std::string& str) {
    size_t id = 0;
    size_t power = 1;
    for (size_t i = 0; i < str.size(); ++i) {
      id += (str[i] - 'a' + 1) * power;
      power *= 27;
    }
    return id;
  }
  inline size_t getId() const {
    size_t id = 0;
    size_t power = 1;
    for (size_t i = Begin; i < End; ++i) {
      id += (Text[i] - 'a' + 1) * power;
      power *= 27;
    }
    return id;
  }
};
void FillIndex(const std::string& text,
               std::vector<std::vector<size_t> >& index) {
  index.clear();
  index.resize(27 * 27 * 27 * 27);
  for (size_t i = 0; i < text.size(); ++i) {
    index[TSubStr::getId(text, i, i + 1)].push_back(i);
    if (i + 1 < text.size()) index[TSubStr::getId(text, i, i + 2)].push_back(i);
    if (i + 2 < text.size()) index[TSubStr::getId(text, i, i + 3)].push_back(i);
    if (i + 3 < text.size()) index[TSubStr::getId(text, i, i + 4)].push_back(i);
  }
}
std::string ReverseStr(const std::string& str) {
  std::string res(str);
  std::reverse(res.begin(), res.end());
  return res;
}
struct TProblemQuery {
  TProblemQuery(const std::string& firstStr, const std::string& secondStr) {
    if (firstStr < secondStr) {
      FirstStr = firstStr;
      SecondStr = secondStr;
    } else {
      FirstStr = secondStr;
      SecondStr = firstStr;
    }
  }
  TProblemQuery(const TProblemQuery& rhs)
      : FirstStr(rhs.FirstStr), SecondStr(rhs.SecondStr) {}
  TProblemQuery() {}
  bool operator==(const TProblemQuery& rhs) const {
    return FirstStr == rhs.FirstStr && SecondStr == rhs.SecondStr;
  }
  std::string FirstStr;
  std::string SecondStr;
};
struct TProblemQueryHash {
  size_t operator()(const TProblemQuery& query) const {
    size_t x = HashFn(query.FirstStr);
    size_t y = HashFn(query.SecondStr);
    return (x * 1000000009) ^ y;
  }
  std::hash<std::string> HashFn;
};
inline void TryImprove(
    const std::string& freqStr, const std::string& text,
    const std::vector<size_t>& freqStrOccurencies,
    std::unordered_map<TProblemQuery, size_t, TProblemQueryHash>& results,
    int freqStrI, int textI, int len) {
  if (textI + len > text.size()) {
    return;
  }
  TProblemQuery query(freqStr, text.substr(textI, len));
  if (results.count(query)) {
    size_t& res = results[query];
    size_t begI = std::min<size_t>(textI, freqStrOccurencies[freqStrI]);
    size_t endI = std::max<size_t>(
        textI + len, freqStrOccurencies[freqStrI] + freqStr.size());
    size_t potentialRes = endI - begI;
    if (freqStrI > 0) {
      begI = std::min<size_t>(textI, freqStrOccurencies[freqStrI - 1]);
      endI = std::max<size_t>(
          textI + len, freqStrOccurencies[freqStrI - 1] + freqStr.size());
      potentialRes = std::min<size_t>(potentialRes, endI - begI);
    }
    res = std::min<size_t>(res, potentialRes);
  }
}
void ProcessFreq(
    const std::string& freqStr, const std::string& text,
    const std::vector<std::vector<size_t> >& index,
    std::unordered_map<TProblemQuery, size_t, TProblemQueryHash>& results,
    std::vector<bool>& processedAsFreq) {
  const std::vector<size_t>& freqStrOccurencies =
      index[TSubStr::getId(freqStr)];
  int freqStrI = 0;
  int textI = 0;
  while (textI < text.size()) {
    if (freqStrI < freqStrOccurencies.size() - 1 &&
        textI > freqStrOccurencies[freqStrI]) {
      ++freqStrI;
      continue;
    }
    TryImprove(freqStr, text, freqStrOccurencies, results, freqStrI, textI, 1);
    TryImprove(freqStr, text, freqStrOccurencies, results, freqStrI, textI, 2);
    TryImprove(freqStr, text, freqStrOccurencies, results, freqStrI, textI, 3);
    TryImprove(freqStr, text, freqStrOccurencies, results, freqStrI, textI, 4);
    ++textI;
  }
  processedAsFreq[TSubStr::getId(freqStr)] = true;
}
void ProcessQuery(
    const TProblemQuery& query, const std::string& text,
    const std::vector<std::vector<size_t> >& index,
    std::unordered_map<TProblemQuery, size_t, TProblemQueryHash>& results,
    std::vector<bool>& processedAsFreq) {
  if (index[TSubStr::getId(query.FirstStr)].empty() ||
      index[TSubStr::getId(query.SecondStr)].empty()) {
    return;
  }
  if (processedAsFreq[TSubStr::getId(query.FirstStr)] ||
      processedAsFreq[TSubStr::getId(query.SecondStr)]) {
    return;
  }
  if (index[TSubStr::getId(query.FirstStr)].size() > 800) {
    ProcessFreq(query.FirstStr, text, index, results, processedAsFreq);
    return;
  }
  if (index[TSubStr::getId(query.SecondStr)].size() > 800) {
    ProcessFreq(query.SecondStr, text, index, results, processedAsFreq);
    return;
  }
  const std::vector<size_t>& firstStrOccurencies =
      index[TSubStr::getId(query.FirstStr)];
  const std::vector<size_t>& secondStrOccurencies =
      index[TSubStr::getId(query.SecondStr)];
  size_t& res = results[query];
  int firstI = 0;
  int secondI = 0;
  int potentialSubstrBegin =
      std::min(firstStrOccurencies[firstI], secondStrOccurencies[secondI]);
  int potentialSubstrEnd =
      std::max(firstStrOccurencies[firstI] + query.FirstStr.size(),
               secondStrOccurencies[secondI] + query.SecondStr.size());
  size_t potentialRes = potentialSubstrEnd - potentialSubstrBegin;
  res = std::min(res, potentialRes);
  while (secondI < secondStrOccurencies.size() &&
         secondStrOccurencies[secondI] < firstStrOccurencies[firstI]) {
    potentialSubstrBegin =
        std::min(firstStrOccurencies[firstI], secondStrOccurencies[secondI]);
    potentialSubstrEnd =
        std::max(firstStrOccurencies[firstI] + query.FirstStr.size(),
                 secondStrOccurencies[secondI] + query.SecondStr.size());
    potentialRes = potentialSubstrEnd - potentialSubstrBegin;
    res = std::min(res, potentialRes);
    ++secondI;
  }
  while (secondI < secondStrOccurencies.size()) {
    if (firstI < firstStrOccurencies.size() - 1 &&
        firstStrOccurencies[firstI + 1] < secondStrOccurencies[secondI]) {
      ++firstI;
      continue;
    }
    potentialSubstrBegin =
        std::min(firstStrOccurencies[firstI], secondStrOccurencies[secondI]);
    potentialSubstrEnd =
        std::max(firstStrOccurencies[firstI] + query.FirstStr.size(),
                 secondStrOccurencies[secondI] + query.SecondStr.size());
    potentialRes = potentialSubstrEnd - potentialSubstrBegin;
    res = std::min(res, potentialRes);
    if (firstI < firstStrOccurencies.size() - 1) {
      potentialSubstrBegin = std::min(firstStrOccurencies[firstI + 1],
                                      secondStrOccurencies[secondI]);
      potentialSubstrEnd =
          std::max(firstStrOccurencies[firstI + 1] + query.FirstStr.size(),
                   secondStrOccurencies[secondI] + query.SecondStr.size());
      potentialRes = potentialSubstrEnd - potentialSubstrBegin;
      res = std::min(res, potentialRes);
    }
    ++secondI;
  }
}
int main() {
  std::string text;
  std::cin >> text;
  std::vector<std::vector<size_t> > index;
  FillIndex(text, index);
  std::vector<bool> processedAsFreq(27 * 27 * 27 * 27, false);
  std::unordered_map<TProblemQuery, size_t, TProblemQueryHash> results;
  results.max_load_factor(3.0);
  results.reserve(300000);
  std::vector<TProblemQuery> queries;
  size_t m;
  std::cin >> m;
  size_t NO_ANSWER = 1000000009;
  for (size_t i = 0; i < m; ++i) {
    std::string strFirst;
    std::string strSecond;
    std::cin >> strFirst;
    std::cin >> strSecond;
    TProblemQuery query(strFirst, strSecond);
    queries.push_back(query);
    results[query] = NO_ANSWER;
  }
  for (size_t i = 0; i < m; ++i) {
    ProcessQuery(queries[i], text, index, results, processedAsFreq);
    size_t res = results[queries[i]];
    std::cout << (res == NO_ANSWER ? -1 : (int)res) << "\n";
  }
  return 0;
}
