#include <bits/stdc++.h>
using namespace std;
int xx[5003][5003];
int n, m, bit[5003][5003];
int query(int x, int y) {
  int res = 0;
  for (int i = x; i >= 0; i = (i & (i + 1)) - 1)
    for (int j = y; j >= 0; j = (j & (j + 1)) - 1) res += bit[i][j];
  return res;
}
void inc(int x, int y, int delta) {
  for (int i = x; i < n; i = (i | (i + 1)))
    for (int j = y; j < m; j = (j | (j + 1))) bit[i][j] += delta;
}
int main() {
  string s;
  int kk;
  while (cin >> s) {
    cin >> kk;
    int len = s.size(), inf, sup;
    n = m = len;
    memset(xx, 0, sizeof(xx));
    memset(bit, 0, sizeof(bit));
    for (int i = (int)0; i < (int)len; i++) {
      inf = i, sup = i;
      while (inf >= 0 && sup < len && s[inf] == s[sup])
        xx[inf][sup] = 1, inf--, sup++;
      inf = i - 1, sup = i;
      while (inf >= 0 && sup < len && s[inf] == s[sup])
        xx[inf][sup] = 1, inf--, sup++;
      inf = i, sup = i + 1;
      while (inf >= 0 && sup < len && s[inf] == s[sup])
        xx[inf][sup] = 1, inf--, sup++;
    }
    for (int i = (int)0; i < (int)len; i++) {
      for (int j = (int)0; j < (int)len; j++) inc(i, j, xx[i][j]);
    }
    while (kk--) {
      scanf("%d %d", &inf, &sup);
      inf--, sup--;
      int a = query(sup, sup), b = query(inf - 1, sup);
      printf("%d\n", a - b);
    }
  }
  return 0;
}
