#include <bits/stdc++.h>
using namespace std;
const long double eps = 1e-7;
const int inf = 1000000000;
const int mod = 1000000007;
const int MAXN = 5010;
long long n, m, q, l, r, ans, cnt;
string s;
long long dp[MAXN][MAXN];
bool pal[MAXN][MAXN];
bool check(int i, int j) {
  if (j < i) return true;
  return pal[i][j];
}
long long f(int i, int j) {
  if (j < i) return 1;
  if (i == j) return 1;
  if (i + 1 == j && s[i] != s[j]) return 2;
  if (i + 1 == j) return 3;
  if (dp[i][j] != -1) return dp[i][j];
  if (check(i, j))
    return dp[i][j] = f(i + 1, j) + f(i, j - 1) - f(i + 1, j - 1) + 1;
  return dp[i][j] = f(i + 1, j) + f(i, j - 1) - f(i + 1, j - 1);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> s >> q;
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < s.size(); i++) pal[i][i] = 1;
  for (int i = 0; i < s.size() - 1; i++) pal[i][i + 1] = s[i] == s[i + 1];
  for (int l = 2; l < s.size(); l++) {
    for (int i = 0; i < s.size() - l; i++) {
      int j = i + l;
      pal[i][j] = (s[i] == s[j]) && pal[i + 1][j - 1];
    }
  }
  while (q--) {
    cin >> l >> r;
    cout << f(l - 1, r - 1) << '\n';
  }
  cout << endl;
  return 0;
}
