#include <bits/stdc++.h>
using namespace std;
int dp[5050][5050];
string s;
bool is[5050][5050];
int main() {
  cin >> s;
  int n = s.size();
  for (int i = 0; i < n; i++) {
    is[i][i] = true;
    dp[i][i] = 1;
  }
  for (int len = 2; len <= n; len++) {
    for (int i = 0; i <= n - len; i++) {
      int l = i, r = i + len - 1;
      if (r - l == 1) {
        is[l][r] = (s[l] == s[r]);
      } else
        is[l][r] = (is[l + 1][r - 1] && s[l] == s[r]);
    }
  }
  for (int len = 2; len <= n; len++) {
    for (int i = 0; i <= n - len; i++) {
      int l = i, r = i + len - 1;
      dp[l][r] = dp[l][r - 1] + dp[l + 1][r] - dp[l + 1][r - 1] + is[l][r];
    }
  }
  int m;
  cin >> m;
  while (m--) {
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%d\n", dp[l - 1][r - 1]);
  }
}
