#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e3 + 10, inf = 1e9, mod = 1e9 + 7;
string s;
int dp[MAXN][MAXN];
int a, b, q, d[MAXN][MAXN];
void Read_input() {
  cin >> s;
  int n = s.size();
  for (int i = 0; i < MAXN - 1; i++) {
    dp[i][i] = 1;
    d[i][i] = 1;
    dp[i + 1][i] = 1;
  }
  for (int j = 2; j <= n; j++) {
    for (int i = 0; i <= n - j; i++) {
      bool flag = 1;
      if (s[i] != s[i + j - 1]) flag = 0;
      if (!dp[i + 1][i + j - 2]) flag = 0;
      dp[i][i + j - 1] = flag;
      d[i][i + j - 1] = d[i + 1][j + i - 1] + d[i][j - 2 + i] -
                        d[i + 1][j - 2 + i] + dp[i][i + j - 1];
    }
  }
}
void Solve() {
  cin >> q;
  while (q-- > 0) {
    cin >> a >> b;
    cout << d[a - 1][b - 1] << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  Read_input(), Solve();
  return 0;
}
