#include <bits/stdc++.h>
using namespace std;
int ceil(int a, int b) {
  if (a % b == 0)
    return a / b;
  else
    return a / b + 1;
}
bool checkbit(int pos, int mask) { return mask & (1 << pos); }
int turnon(int pos, int mask) { return mask | (1 << pos); }
const int N = 5005;
string s;
int dp[N][N], pref[N][N];
int fun(int i, int j) {
  if (i > j) return 1;
  if (dp[i][j] != -1) return dp[i][j];
  int ans = 0;
  if (s[i] == s[j]) ans |= fun(i + 1, j - 1);
  return dp[i][j] = ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  memset(dp, -1, sizeof(dp));
  cin >> s;
  s = " " + s;
  int n = s.size();
  for (int i = 1; i < n; i++) {
    for (int j = i; j < n; j++) {
      int tmp = fun(i, j);
    }
  }
  for (int i = 1; i < n; i++) {
    pref[i][0] = 0;
    for (int j = i; j < n; j++) {
      pref[i][j] = pref[i][j - 1] + dp[i][j];
    }
  }
  for (int i = 1; i < n; i++) {
    for (int j = i - 1; j >= 1; j--) {
      pref[j][i] += pref[j + 1][i];
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int x, y;
    cin >> x >> y;
    cout << pref[x][y] << "\n";
  }
}
