#include <bits/stdc++.h>
using namespace std;
const int mx = 5000 + 1;
bool isPalindrome[mx][mx];
int dp[mx][mx];
char s[21000];
int main() {
  scanf("%s", s);
  int q;
  scanf("%d", &q);
  int l = strlen(s);
  for (int len = 0; len < l; ++len) {
    for (int i = 0; i < l; ++i) {
      if (i + len >= l) break;
      if (len == 0) isPalindrome[i][len] = 1;
      if (len == 1)
        isPalindrome[i][len] = (s[i] == s[i + len]) ? true : false;
      else if (s[i] == s[i + len] && isPalindrome[i + 1][len - 2])
        isPalindrome[i][len] = 1;
    }
  }
  for (int len = 0; len < l; ++len) {
    for (int i = 0; i < l; ++i) {
      if (i + len >= l) break;
      if (len == 0)
        dp[i][len] = 1;
      else if (len == 1)
        dp[i][len] = dp[i][0] + dp[i + 1][0] + isPalindrome[i][len];
      else
        dp[i][len] = dp[i][len - 1] + dp[i + 1][len - 1] +
                     isPalindrome[i][len] - dp[i + 1][len - 2];
    }
  }
  for (int i = 0; i < q; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    printf("%d\n", dp[a][b - a]);
  }
}
