#include <bits/stdc++.h>
using namespace std;
string s;
int num;
bool ok[5005][5005];
int dp[5005][5005];
int main() {
  int left, right;
  cin >> s;
  int i, j;
  int len = s.length();
  for (i = 0; i < len; i++) {
    dp[i][i] = 1;
    ok[i][i] = true, ok[i + 1][i] = true;
  }
  for (i = 2; i <= len; i++) {
    for (j = 0; j <= len - i; j++) {
      ok[j][j + i - 1] = (ok[j + 1][j + i - 2] && s[j] == s[i + j - 1]);
      dp[j][j + i - 1] = dp[j][j + i - 2] + dp[j + 1][j + i - 1] -
                         dp[j + 1][j + i - 2] + ok[j][j + i - 1];
    }
  }
  cin >> num;
  for (i = 1; i <= num; i++) {
    cin >> left >> right;
    cout << dp[left - 1][right - 1] << endl;
  }
  return 0;
}
