#include <bits/stdc++.h>
using namespace std;
const int maxn = 5050;
int dp[maxn][maxn];
bool ha[maxn][maxn];
char str[maxn];
void DP(void) {
  int n = strlen(str + 1);
  for (int i = 1; i <= n; i++) dp[i][i] = 1, ha[i][i] = true;
  for (int i = 2; i <= n; i++)
    if (str[i] == str[i - 1]) ha[i - 1][i] = true;
  for (int len = 2; len <= n; len++) {
    for (int l = 1; l <= n - len + 1; l++) {
      int r = l + len - 1;
      if (str[l] == str[r] && ha[l + 1][r - 1]) ha[l][r] = true;
      dp[l][r] = dp[l][r - 1] + dp[l + 1][r] - dp[l + 1][r - 1] + ha[l][r];
    }
  }
  return;
}
int main(void) {
  scanf("%s", str + 1);
  DP();
  int n;
  scanf("%d", &n);
  int x, y;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &x, &y);
    printf("%d\n", dp[x][y]);
  }
}
