#include <bits/stdc++.h>
using namespace std;
char s[200010];
int L[200010], R[200010];
int n, q;
vector<int> from[400010];
int pos[200010];
int fa[400010], len[400010];
int dep[400010], son[400010], id[400010], sz[400010], TOT, tp[400010],
    seq[400010];
long long ans[200010];
vector<pair<int, int> > qry[400010], mdy[400010];
struct SAM {
  int son[400010][26], TOT, lst;
  void clear() {
    memset(son, 0, sizeof(son)), len[1] = 0, fa[1] = 0, TOT = lst = 1;
  }
  int append(int x) {
    int p = lst, np = ++TOT;
    len[np] = len[p] + 1;
    while (p && !son[p][x]) son[p][x] = np, p = fa[p];
    if (p) {
      int q = son[p][x];
      if (len[q] == len[p] + 1)
        fa[np] = q;
      else {
        int nq = ++TOT;
        memcpy(son[nq], son[q], sizeof(son[q])), len[nq] = len[p] + 1;
        fa[nq] = fa[q], fa[q] = fa[np] = nq;
        while (p && son[p][x] == q) son[p][x] = nq, p = fa[p];
      }
    } else
      fa[np] = 1;
    return lst = np;
  }
  void BuildT() {
    for (int i = 2; i <= TOT; i++) from[fa[i]].push_back(i);
  }
} sam;
void dfs1(int x, int last) {
  sz[x] = 1, son[x] = -1;
  for (auto v : from[x])
    if (v != last) {
      dep[v] = dep[x] + 1, dfs1(v, x), sz[x] += sz[v];
      if (son[x] == -1 || sz[v] > sz[son[x]]) son[x] = v;
    }
}
void dfs2(int x, int t) {
  id[x] = ++TOT, seq[TOT] = x, tp[x] = t;
  if (son[x] == -1) return;
  dfs2(son[x], t);
  for (auto v : from[x])
    if (v != fa[x] && v != son[x]) dfs2(v, v);
}
void AddQry(int id) {
  int x = pos[L[id]], l = R[id] - L[id] + 1;
  while (tp[x] != 1) {
    if (len[fa[tp[x]]] < l)
      qry[tp[x]].push_back(make_pair(min(l, len[x]) - len[fa[tp[x]]], id));
    x = fa[tp[x]];
  }
  qry[tp[x]].push_back(make_pair(min(l, len[x]), id));
}
void AddMdy(int id) {
  int x = pos[id];
  while (tp[x] != 1)
    mdy[tp[x]].push_back(make_pair(len[x] - len[fa[tp[x]]], id)), x = fa[tp[x]];
  mdy[tp[x]].push_back(make_pair(len[x] - len[fa[tp[x]]], id));
}
bool cmpm(pair<int, int> x, pair<int, int> y) {
  return x.first + x.second < y.first + y.second;
}
bool cmpq1(pair<int, int> x, pair<int, int> y) {
  return R[x.second] < R[y.second];
}
bool cmpq2(pair<int, int> x, pair<int, int> y) {
  return R[x.second] - x.first < R[y.second] - y.first;
}
struct BIT {
  long long T[200010][2];
  int vis[200010], VIS;
  BIT() { memset(vis, 0, sizeof(vis)), VIS = 2333; }
  void clear() { VIS++; }
  void Flush(int x) {
    if (vis[x] != VIS) vis[x] = VIS, T[x][0] = T[x][1] = 0;
  }
  void insert(int x, int v) {
    while (x <= 200005) Flush(x), T[x][0]++, T[x][1] += v, x += x & -x;
  }
  void erase(int x, int v) {
    while (x <= 200005) Flush(x), T[x][0]--, T[x][1] -= v, x += x & -x;
  }
  long long query0(int x) {
    long long res = 0;
    while (x) Flush(x), res += T[x][0], x -= x & -x;
    return res;
  }
  long long query1(int x) {
    long long res = 0;
    while (x) Flush(x), res += T[x][1], x -= x & -x;
    return res;
  }
} up, dw;
void Solve(int x) {
  sort(mdy[x].rbegin(), mdy[x].rend()), sort(qry[x].rbegin(), qry[x].rend());
  int pos = 0;
  up.clear(), dw.clear();
  for (auto it : mdy[x]) up.insert(it.second, it.first);
  for (auto it : qry[x]) {
    int lim = R[it.second] + 1 - len[fa[x]];
    while (pos < mdy[x].size() && mdy[x][pos].first >= it.first) {
      up.erase(mdy[x][pos].second, mdy[x][pos].first);
      dw.insert(mdy[x][pos].second, mdy[x][pos].first);
      pos++;
    }
    int tmp = lim - it.first;
    ans[it.second] -= up.query1(L[it.second] - 1);
    ans[it.second] -= dw.query0(L[it.second] - 1) * it.first;
    int old = ans[it.second];
    ans[it.second] -= it.first * up.query0(tmp) - up.query1(tmp);
  }
  sort(mdy[x].begin(), mdy[x].end(), cmpm);
  sort(qry[x].begin(), qry[x].end(), cmpq1);
  up.clear(), dw.clear(), pos = 0;
  for (auto it : mdy[x]) up.insert(it.second, it.second);
  for (auto it : qry[x]) {
    int lim = R[it.second] + 1 - len[fa[x]];
    while (pos < mdy[x].size() &&
           mdy[x][pos].first + mdy[x][pos].second <= lim) {
      up.erase(mdy[x][pos].second, mdy[x][pos].second);
      dw.insert(mdy[x][pos].second, mdy[x][pos].first);
      pos++;
    }
    ans[it.second] += lim * up.query0(lim) - up.query1(lim);
    ans[it.second] += dw.query1(lim);
    int tmp = lim - it.first;
    ans[it.second] -= tmp * up.query0(tmp) - up.query1(tmp);
    ans[it.second] -= dw.query1(tmp) - dw.query0(tmp) * it.first;
  }
}
int main() {
  scanf("%s", s + 1), n = strlen(s + 1);
  sam.clear();
  for (int i = n; i >= 1; i--) pos[i] = sam.append(s[i] - 'a');
  sam.BuildT(), dfs1(1, 1), dfs2(1, 1);
  scanf("%d", &q);
  for (int i = 0; i < q; i++) scanf("%d%d", &L[i], &R[i]), AddQry(i);
  for (int i = 1; i <= n; i++) AddMdy(i);
  for (int i = 1; i <= TOT; i++)
    if (tp[i] == i) Solve(i);
  for (int i = 0; i < q; i++) printf("%lld\n", ans[i]);
  return 0;
}
