#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long add(long long a, long long b) { return (a % mod + b % mod) % mod; }
long long mult(long long a, long long b) { return (a % mod * b % mod) % mod; }
long long sub(long long a, long long b) {
  return (a % mod - b % mod + mod) % mod;
}
long long dp[5000][5000];
bool palin[5000][5000];
void solve() {
  string s;
  cin >> s;
  long long n = s.length();
  for (long long i = 0; i < n; i++) palin[i][i] = true;
  for (long long i = 0; i + 1 < n; i++)
    if (s[i] == s[i + 1]) palin[i][i + 1] = true;
  for (long long i = 3; i <= n; i++)
    for (long long j = 0; j + i - 1 < n; j++)
      palin[j][j + i - 1] = (palin[j + 1][j + i - 2] && s[j] == s[j + i - 1]);
  for (long long i = 0; i < n; i++) dp[i][i] = 1;
  for (long long i = 0; i + 1 < n; i++) dp[i][i + 1] = 2 + (s[i] == s[i + 1]);
  for (long long i = 3; i <= n; i++)
    for (long long j = 0; j + i - 1 < n; j++)
      dp[j][j + i - 1] = palin[j][j + i - 1] + dp[j][j + i - 2] +
                         dp[j + 1][j + i - 1] - dp[j + 1][j + i - 2];
  long long q;
  cin >> q;
  while (q--) {
    long long l, r;
    cin >> l >> r;
    l--, r--;
    cout << dp[l][r] << "\n";
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long tc = 1;
  for (long long i = 1; i <= tc; i++) {
    solve();
  }
}
