#include <bits/stdc++.h>
const int MAXN = 5000 + 10;
char s[MAXN];
int f[MAXN][MAXN];
bool isP[MAXN][MAXN];
int main() {
  scanf("%s", s + 1);
  int len = strlen(s + 1);
  for (int i = 1; i <= len + 1; i++) {
    f[i][i + 1] = 1;
    isP[i][i + 1] = isP[i][i] = true;
  }
  for (int l = 2; l <= len; l++) {
    for (int i = 1; i + l <= len + 1; i++) {
      int j = i + l;
      isP[i][j] = (isP[i + 1][j - 1] && s[i] == s[j - 1]);
    }
  }
  for (int l = 2; l <= len; l++) {
    for (int i = 1; i + l <= len + 1; i++) {
      int j = i + l;
      f[i][j] = f[i][j - 1] + f[i + 1][j] - f[i + 1][j - 1] + isP[i][j];
    }
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%d\n", f[l][r + 1]);
  }
  return 0;
}
