#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  string s;
  cin >> s;
  long long n = s.size();
  vector<vector<bool> > isPal(n, vector<bool>(n, false));
  vector<vector<long long> > dp(n, vector<long long>(n, 0));
  for (long long len = 1; len <= n; len++)
    for (long long start = 0; start + len - 1 < n; start++)
      if (len == 1)
        isPal[start][start + len - 1] = true;
      else
        isPal[start][start + len - 1] =
            (s[start] == s[start + len - 1]) &&
            (len == 2 ? true : isPal[start + 1][start + len - 2]);
  for (long long len = 1; len <= n; len++)
    for (long long start = 0; start + len - 1 < n; start++)
      if (len == 1)
        dp[start][start + len - 1] = 1LL;
      else
        dp[start][start + len - 1] =
            (dp[start + 1][start + len - 1] + dp[start][start + len - 2] -
             dp[start + 1][start + len - 2]) +
            (isPal[start][start + len - 1] ? 1LL : 0LL);
  long long q;
  cin >> q;
  while (q--) {
    long long x, y;
    cin >> x >> y;
    cout << dp[--x][--y] << '\n';
  }
  return 0;
}
