#include <bits/stdc++.h>
using namespace std;
bool dp[5001][5001];
int mat[5001][5001];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, i, j, q;
  string s;
  cin >> s >> q;
  n = s.size();
  for (i = 1; i <= n; i++) {
    dp[i][i] = true;
    if (i != n && s[i - 1] == s[i]) dp[i][i + 1] = true;
  }
  for (int len = 3; len <= n; len++) {
    for (int start = 1; start + len - 1 <= n; start++)
      if (dp[start + 1][start + len - 2] && s[start - 1] == s[start + len - 2])
        dp[start][start + len - 1] = true;
  }
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++) {
      mat[i][j] = mat[i - 1][j] + mat[i][j - 1] + dp[i][j] - mat[i - 1][j - 1];
    }
  while (q--) {
    cin >> i >> j;
    cout << mat[j][j] - mat[i - 1][j] - mat[j][i - 1] + mat[i - 1][i - 1]
         << "\n";
  }
}
