#include <bits/stdc++.h>
using namespace std;
const int SZ = 5e3 + 10;
int dp[SZ][SZ], is[SZ][SZ];
string s;
int is_pal(int i, int j) {
  if (j < i) return 1;
  if (is[i][j]) return is[i][j];
  is[i][j] = is_pal(i + 1, j - 1);
  if (s[i] != s[j]) is[i][j] = 2;
  return is[i][j];
}
int ans(int i, int j) {
  if (j < i) return 0;
  if (dp[i][j]) return dp[i][j];
  dp[i][j] =
      ans(i, j - 1) + ans(i + 1, j) - ans(i + 1, j - 1) + (is_pal(i, j) % 2);
  return dp[i][j];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q, l, r;
  cin >> s >> q;
  while (q--) {
    cin >> l >> r;
    cout << ans(--l, --r) << '\n';
  }
  return 0;
}
