#include <bits/stdc++.h>
using namespace std;
char s[5003], M1[5003][5003];
int M2[5003][5003];
bool DP1(int i, int j) {
  if (i > j) return true;
  if (M1[i][j] != -1) return M1[i][j];
  return (M1[i][j] = (s[i] == s[j] && DP1(i + 1, j - 1)));
}
int DP2(int i, int j) {
  if (i == j) return 1;
  if (i > j) return 0;
  if (M2[i][j] != -1) return M2[i][j];
  return M2[i][j] =
             DP2(i + 1, j) + DP2(i, j - 1) - DP2(i + 1, j - 1) + DP1(i, j);
}
void solve(int q) {
  int n = strlen(s), l, r, i, j;
  for (i = 0; i < n; ++i)
    for (j = i; j < n; ++j) M1[i][j] = -1, M2[i][j] = -1;
  while (q--) {
    scanf("%d %d", &l, &r);
    printf("%d\n", DP2(l - 1, r - 1));
  }
}
int main() {
  int q;
  scanf("%s %d", s, &q);
  solve(q);
  return 0;
}
