#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 5;
int dp[N][N];
int ispal[N][N];
string s;
int main() {
  cin >> s;
  for (int len = 1; len <= s.length(); len++)
    for (int i = 0, j = i + len - 1; j < s.length(); i++, j++) {
      if ((len < 3 && s[i] == s[j]) || (s[i] == s[j] && ispal[i + 1][j - 1])) {
        dp[i][j] += 1;
        ispal[i][j] = 1;
      }
      dp[i][j] += dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1];
    }
  int q;
  cin >> q;
  while (q--) {
    int a, b;
    scanf("%d%d", &a, &b);
    printf("%d\n", dp[a - 1][b - 1]);
  }
  return 0;
}
