#include <bits/stdc++.h>
using namespace std;
int N, M;
int dpp[5005][5005];
int dp[5005][5005];
string A;
bool Dp(int x, int y) {
  if (x == y) return 1;
  if (x + 1 == y) return A[x] == A[y];
  int &ret = dp[x][y];
  if (ret != -1) return ret;
  return ret = Dp(x + 1, y - 1) && A[x] == A[y];
}
int Dp2(int x, int y) {
  if (x > y) return 0;
  int &ret = dpp[x][y];
  if (ret != -1) return ret;
  return ret = Dp2(x, y - 1) - Dp2(x + 1, y - 1) + Dp2(x + 1, y) + Dp(x, y);
}
int main() {
  int x, y;
  cin >> A >> N;
  memset(dp, -1, sizeof dp);
  memset(dpp, -1, sizeof dpp);
  for (int i = 1; i <= N; i++) {
    scanf("%d %d", &x, &y);
    printf("%d\n", Dp2(x - 1, y - 1));
  }
  return 0;
}
