#include <bits/stdc++.h>
using namespace std;
const int maxN = 5005;
int q;
int f[maxN][maxN];
bool isPal[maxN][maxN];
string s;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  string s;
  cin >> s;
  cin >> q;
  int n = s.size();
  s = ' ' + s;
  for (int i = 1; i <= n; ++i) {
    isPal[i][i] = 1;
    f[i][i] = 1;
    isPal[i + 1][i] = 1;
  }
  for (int len = 2; len <= n; ++len)
    for (int i = 1; i <= n - len + 1; ++i) {
      isPal[i][i + len - 1] =
          (isPal[i + 1][i + len - 2] && (s[i] == s[i + len - 1]));
      f[i][i + len - 1] = f[i][i + len - 2] + f[i + 1][i + len - 1] -
                          f[i + 1][i + len - 2] + isPal[i][i + len - 1];
    }
  while (q--) {
    int x, y;
    cin >> x >> y;
    cout << f[x][y] << '\n';
  }
  return 0;
}
