#include <bits/stdc++.h>
using namespace std;
const int N = 5000 + 7;
char str[N];
bool dp[N][N];
int f[N][N], cnt[N];
int main() {
  scanf("%s", str);
  int n = strlen(str);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dp[i][j] = 1;
    }
  }
  for (int i = 2; i <= n; i++) {
    for (int j = 1; j + i - 1 <= n; j++) {
      dp[j][j + i - 1] = (str[j - 1] == str[j + i - 2]) * dp[j + 1][j + i - 2];
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = i; j >= 1; j--) {
      cnt[j] += dp[j][i];
      f[j][i] = cnt[j] + f[j + 1][i];
    }
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int l, r;
    scanf("%d %d", &l, &r);
    printf("%d\n", f[l][r]);
  }
}
