#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
int upmin(T1 &x, T2 v) {
  if (x > v) {
    x = v;
    return 1;
  }
  return 0;
}
template <class T1, class T2>
int upmax(T1 &x, T2 v) {
  if (x < v) {
    x = v;
    return 1;
  }
  return 0;
}
const int INF = 0x3f3f3f3f;
string seq;
void init() { cin >> seq; }
bool is_pal[5000 + 1][5000 + 1];
int dp[5000 + 1][5000 + 1];
void preprocess() {
  for (int i = 0; i < seq.size(); ++i) {
    is_pal[i][i] = dp[i][i] = 1;
  }
  for (int i = 0; i + 1 < seq.size(); ++i) {
    if (seq[i] == seq[i + 1]) {
      is_pal[i][i + 1] = dp[i][i + 1] = 1;
    }
  }
  for (int len = 3; len <= seq.size(); ++len) {
    for (int i = 0; i + len <= seq.size(); ++i) {
      if (seq[i] == seq[i + len - 1]) {
        is_pal[i][i + len - 1] = is_pal[i + 1][i + len - 2];
      }
    }
  }
  for (int len = 2; len <= seq.size(); ++len) {
    for (int i = 0; i + len <= seq.size(); ++i) {
      dp[i][i + len - 1] = dp[i][i + len - 2] + dp[i + 1][i + len - 1] -
                           dp[i + 1][i + len - 2] + is_pal[i][i + len - 1];
    }
  }
}
void solve() {
  int Q;
  cin >> Q;
  for (int kase = 0; kase < Q; ++kase) {
    int ql, qr;
    scanf("%d%d", &ql, &qr);
    printf("%d\n", dp[ql - 1][qr - 1]);
  }
}
signed main() {
  init();
  preprocess();
  solve();
  return 0;
}
