#include <bits/stdc++.h>
using namespace std;
char s[5555];
int q;
int is[5555][5555] = {};
int main() {
  scanf("%s", s);
  int n = strlen(s);
  for (int len = 1; len < n + 1; len++) {
    for (int i = n - len; i >= 0; i--) {
      int j = i + len - 1;
      if (s[i] == s[j]) {
        if (len <= 3)
          is[i][j] = 1;
        else {
          is[i][j] = is[i + 1][j - 1];
        }
      }
    }
  }
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++) is[i][j] += is[i][j - 1];
  for (int i = 1; i < n; i++)
    for (int j = 0; j < n; j++) is[i][j] += is[i - 1][j];
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    l--;
    r--;
    printf("%d\n", is[r][r] - (l == 0 ? 0 : is[l - 1][r]));
  }
  return 0;
}
