#include <bits/stdc++.h>
using namespace std;
bool ispal[5010][5010];
int dp[5010][5010];
char s[5010];
int main() {
  scanf("%s", s);
  memset(dp, 0, sizeof(dp));
  int len = strlen(s), i, start, l;
  for (i = 1; i <= len; ++i) {
    dp[i][i] = 1;
    ispal[i][i] = 1;
  }
  if (len >= 2) {
    for (start = 1; start + 1 <= len; ++start)
      if (s[start - 1] == s[start]) ispal[start][start + 1] = 1;
  }
  for (l = 3; l <= len; ++l)
    for (start = 1; start + l - 1 <= len; ++start) {
      ispal[start][start + l - 1] = (ispal[start + 1][start + l - 2] &
                                     (bool)(s[start - 1] == s[start + l - 2]));
    }
  for (l = 2; l <= len; ++l)
    for (start = 1; start + l - 1 <= len; ++start) {
      dp[start][start + l - 1] =
          dp[start + 1][start + l - 1] + dp[start][start + l - 2] -
          dp[start + 1][start + l - 2] + (int)(ispal[start][start + l - 1]);
    }
  int q, r;
  scanf("%d", &q);
  for (i = 0; i < q; ++i) {
    scanf("%d%d", &l, &r);
    printf("%d\n", dp[l][r]);
  }
  return 0;
}
