#include <bits/stdc++.h>
using namespace std;
bool f[5010][5010];
int dp[5010][5010];
char s[5010];
int main() {
  int i, j, l, n, u, v;
  scanf("%s", s);
  n = strlen(s);
  for (i = 0; i < n; i++) f[i][i] = f[i + 1][i] = dp[i][i] = 1;
  for (l = 2; l <= n; l++)
    for (i = 0; i < n; i++) {
      j = i + l - 1;
      f[i][j] = f[i + 1][j - 1] && (s[i] == s[j]);
      dp[i][j] = dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1] + f[i][j];
    }
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d%d", &u, &v);
    printf("%d\n", dp[--u][--v]);
  }
  return 0;
}
