#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;
  int n = s.size();
  vector<vector<int>> P(n + 1, vector<int>(n + 1, 0));
  for (int len = 1; len <= n; len++) {
    for (int start = 1; start <= n - len + 1; start++) {
      if (len == 1 || len == 2) {
        P[start][start + len - 1] = (s[start - 1] == s[start + len - 2]);
      } else {
        P[start][start + len - 1] = (s[start - 1] == s[start + len - 2] &&
                                     P[start + 1][start + len - 2]);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      P[i][j] = P[i][j] + P[i][j - 1] + P[i - 1][j] - P[i - 1][j - 1];
    }
  }
  int q;
  cin >> q;
  for (int i = 0; i < (q); i++) {
    int lo, hi;
    scanf("%d %d", &lo, &hi);
    cout << (P[hi][hi] - P[lo - 1][hi] - P[hi][lo - 1] + P[lo - 1][lo - 1])
         << endl;
  }
}
