#include <bits/stdc++.h>
using namespace std;
const int md = 1e9 + 19;
const int p = 37;
const int N = 5001;
string s, t;
int m, n;
long long hash1[N], hash2[N];
long long st[N];
int dp[N][N];
vector<int> a[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> s;
  n = s.size();
  st[0] = 1;
  for (int i = 1; i < n; ++i) {
    st[i] = (st[i - 1] * p) % md;
  }
  hash1[0] = s[0];
  for (int i = 1; i < n; ++i) {
    hash1[i] = (hash1[i - 1] + s[i] * st[i]) % md;
  }
  hash2[n - 1] = (s[n - 1]);
  for (int i = n - 2; i >= 0; i--) {
    hash2[i] = (hash2[i + 1] + s[i] * st[n - 1 - i]) % md;
  }
  long long len = 0, st1, st2;
  long long h1, h2;
  for (int i = 0; i < n; ++i) {
    for (int j = i; j < n; ++j) {
      st1 = i;
      st2 = n - 1 - j;
      h1 = hash1[j];
      if (i != 0) h1 -= hash1[i - 1];
      if (h1 < 0) h1 += md;
      h2 = hash2[i];
      h2 -= hash2[j + 1];
      if (h2 < 0) h2 += md;
      if (st1 < st2)
        h1 = (h1 * 1ll * st[st2 - st1]) % md;
      else
        h2 = (h2 * 1ll * st[st1 - st2]) % md;
      if (h1 == h2) {
        a[j].push_back(i);
      }
    }
  }
  int l, r, mid;
  for (int i = 0; i < n; ++i) {
    for (int j = i; j < n; ++j) {
      dp[i][j] = dp[i][j - 1];
      if (a[j].size() == 0) continue;
      l = 0;
      r = a[j].size() - 1;
      while (l < r - 1) {
        mid = (l + r) / 2;
        if (a[j][mid] < i)
          l = mid;
        else
          r = mid;
      }
      if (a[j][l] >= i)
        dp[i][j] += a[j].size() - l;
      else if (a[j][r] >= i)
        dp[i][j] += a[j].size() - r;
    }
  }
  cin >> m;
  for (int i = 1; i <= m; ++i) {
    cin >> l >> r;
    l--;
    r--;
    cout << dp[l][r] << '\n';
  }
}
