#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000000;
const int MOD = 1000000007;
const double pi = 2 * acos(0.0);
bool p[5002][5002];
int cnt[5002][5005];
int main() {
  string s;
  getline(cin, s);
  s = " " + s;
  for (int len = 1; len <= (int)s.size(); ++len)
    for (int i = 1; i <= (int)s.size() - 1; ++i) {
      int j = i + len - 1;
      if (j >= (int)s.size()) continue;
      if (i == j) {
        p[i][j] = true;
        continue;
      }
      if (i + 1 == j) {
        p[i][j] = (s[i] == s[j]);
        continue;
      }
      p[i][j] = (p[i + 1][j - 1] && s[i] == s[j]);
    }
  for (int len = 1; len <= (int)s.size(); ++len)
    for (int i = 1; i <= (int)s.size(); ++i) {
      int j = i + len - 1;
      if (j >= (int)s.size()) continue;
      if (i == j) {
        cnt[i][j] = 1;
        continue;
      }
      if (i + 1 == j) {
        cnt[i][j] = 2 + (s[i] == s[j]);
        continue;
      }
      cnt[i][j] = cnt[i + 1][j] + cnt[i][j - 1] - cnt[i + 1][j - 1] + p[i][j];
    }
  int q;
  cin >> q;
  while (q--) {
    int l, r;
    scanf("%d %d", &l, &r);
    printf("%d\n", cnt[l][r]);
  }
}
