#include <bits/stdc++.h>
using namespace std;
const int mm = 5005;
char s[mm];
int dp[mm][mm];
bool is_palin[mm][mm];
int main() {
  cin >> s;
  int len = strlen(s);
  memset(is_palin, 0, sizeof(is_palin));
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < len; i++) {
    dp[i][i] = 1;
    is_palin[i][i] = true;
    is_palin[i + 1][i] = true;
  }
  for (int i = 1; i <= len; i++)
    for (int j = 0; j <= len - i; j++) {
      is_palin[j][j + i] = is_palin[j + 1][j + i - 1] & s[j] == s[j + i];
      dp[j][j + i] = dp[j + 1][j + i] + dp[j][j + i - 1] -
                     dp[j + 1][i + j - 1] + is_palin[j][j + i];
    }
  int m, a, b;
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    cout << dp[a - 1][b - 1] << "\n";
  }
}
