#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 50;
int n, m;
char str[N];
vector<int> G[N];
namespace SAM {
int lk[N], len[N], nd = 1, las = 1, ch[N][26];
int extend(int c) {
  int u = ++nd, p = las;
  las = u;
  len[u] = len[p] + 1;
  for (; p && !ch[p][c]; p = lk[p]) ch[p][c] = u;
  if (!p) return lk[u] = 1, u;
  int q = ch[p][c];
  if (len[q] == len[p] + 1) return lk[u] = q, u;
  int cl = ++nd;
  len[cl] = len[p] + 1;
  memcpy(ch[cl], ch[q], 26 << 2);
  lk[cl] = lk[q], lk[q] = lk[u] = cl;
  for (; p && ch[p][c] == q; p = lk[p]) ch[p][c] = cl;
  return u;
}
void init() {
  for (int i = 2; i <= nd; i++) G[lk[i]].push_back(i);
}
}  // namespace SAM
using SAM ::len;
int pt[N], son[N], sz[N], fa[N];
int f[N][20], top[N];
void pre_dfs(int u) {
  sz[u] = 1;
  for (int i = 1; i <= 18; i++) f[u][i] = f[f[u][i - 1]][i - 1];
  for (int v : G[u]) {
    f[v][0] = fa[v] = u;
    pre_dfs(v);
    sz[u] += sz[v];
    if (sz[v] > sz[son[u]]) son[u] = v;
  }
}
void sub_dfs(int u, int tp) {
  top[u] = tp;
  if (son[u]) sub_dfs(son[u], tp);
  for (int v : G[u])
    if (v != son[u]) sub_dfs(v, v);
}
int jmp(int x, int l) {
  for (int i = 18; ~i; i--)
    if (len[f[x][i]] >= l) x = f[x][i];
  return x;
}
struct lll {
  int c, p, x, y;
  bool operator<(const lll &z) const {
    return p < z.p || (p == z.p && c < z.c);
  }
};
vector<lll> S[N], _S[N];
long long ans[N];
void work(int x, int R, int Len, int c) {
  while (x > 1) {
    int z = top[x];
    int P = R + 1 - Len;
    S[z].push_back((lll){c, P, Len, R + 1});
    Len = len[x = fa[z]];
  }
}
void _work(int x, int R, int Len, int c) {
  while (x > 1) {
    int z = top[x];
    _S[z].push_back((lll){c, R, Len});
    Len = len[x = fa[z]];
  }
}
void ins(int x, int c, int Len) {
  while (x > 1) {
    int z = top[x];
    int l = len[fa[z]] + 1;
    int r = Len + 1;
    S[z].push_back((lll){0, c, l, l + c});
    S[z].push_back((lll){-1, c, r, r + c});
    Len = len[x = fa[z]];
  }
}
void _ins(int x, int c, int Len) {
  while (x > 1) {
    int z = top[x];
    int l = len[fa[z]] + 1;
    int r = Len + 1;
    _S[z].push_back((lll){0, c, l});
    _S[z].push_back((lll){-1, c, r});
    Len = len[x = fa[z]];
  }
}
struct bit {
  long long c[N];
  void ins(int x, long long v) {
    for (; x <= n + 1; x += x & -x) c[x] += v;
  }
  long long ask(int x) {
    long long ans = 0;
    for (; x; x -= x & -x) ans += c[x];
    return ans;
  }
} P, Q;
void cope(vector<lll> &z) {
  sort(z.begin(), z.end());
  for (lll t : z) {
    if (t.c == -1)
      P.ins(t.x, -1), Q.ins(t.x, -t.x);
    else if (t.c == 0)
      P.ins(t.x, 1), Q.ins(t.x, t.x);
    else
      ans[t.c] += (t.x + 1) * P.ask(t.x) - Q.ask(t.x);
  }
  for (lll t : z) {
    if (t.c == -1)
      P.ins(t.x, 1), Q.ins(t.x, t.x);
    else if (t.c == 0)
      P.ins(t.x, -1), Q.ins(t.x, -t.x);
  }
  reverse(z.begin(), z.end());
  for (lll t : z) {
    if (t.c == -1)
      P.ins(t.y, -1), Q.ins(t.y, -t.y);
    else if (t.c == 0)
      P.ins(t.y, 1), Q.ins(t.y, t.y);
    else
      ans[t.c] += (t.y + 1) * P.ask(t.y) - Q.ask(t.y);
  }
  for (lll t : z) {
    if (t.c == -1)
      P.ins(t.y, 1), Q.ins(t.y, t.y);
    else if (t.c == 0)
      P.ins(t.y, -1), Q.ins(t.y, -t.y);
  }
}
void _cope(vector<lll> &z) {
  sort(z.begin(), z.end());
  for (lll t : z) {
    if (t.c == -1)
      P.ins(t.x, -1), Q.ins(t.x, -t.x);
    else if (t.c == 0)
      P.ins(t.x, 1), Q.ins(t.x, t.x);
    else
      ans[t.c] -= (t.x + 1) * P.ask(t.x) - Q.ask(t.x);
  }
  for (lll t : z) {
    if (t.c == -1)
      P.ins(t.x, 1), Q.ins(t.x, t.x);
    else if (t.c == 0)
      P.ins(t.x, -1), Q.ins(t.x, -t.x);
  }
}
int main() {
  scanf("%s%d", str + 1, &m);
  n = strlen(str + 1);
  for (int i = n; i; i--) pt[i] = SAM ::extend(str[i] - 'a');
  SAM ::init();
  pre_dfs(1), sub_dfs(1, 1);
  for (int i = 1, l, r; i <= m; i++) {
    scanf("%d%d", &l, &r);
    int len = r - l + 1;
    int x = jmp(pt[l], len);
    work(x, r, len, i);
    _work(x, l - 1, len, i);
  }
  for (int i = 1; i <= n; i++)
    ins(pt[i], i, len[pt[i]]), _ins(pt[i], i, len[pt[i]]);
  for (int i = 1; i <= SAM ::nd; i++)
    if (S[i].size()) cope(S[i]), _cope(_S[i]);
  for (int i = 1; i <= m; i++) cout << ans[i] << '\n';
  return 0;
}
