#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-9, PI = acos(-1.);
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const int N = 5e3 + 5;
int n, q, l, r, p[N][N], dp[N][N];
char s[N];
int gop(int i, int j) {
  if (i >= j) return 1;
  if (p[i][j] >= 0) return p[i][j];
  return p[i][j] = s[i] == s[j] ? gop(i + 1, j - 1) : 0;
}
int go(int i, int j) {
  if (i == j) return 1;
  if (i > j) return 0;
  if (dp[i][j] >= 0) return dp[i][j];
  return dp[i][j] = gop(i, j) + go(i + 1, j) + go(i, j - 1) - go(i + 1, j - 1);
}
int main() {
  memset((dp), (-1), sizeof(dp));
  memset((p), (-1), sizeof(p));
  scanf("%s", s);
  n = strlen(s);
  scanf("%d", &q);
  for (int i = 0; i < q; ++i) {
    scanf("%d%d", &l, &r);
    printf("%d\n", go(l - 1, r - 1));
  }
  return 0;
}
