You've got a string s = s1s2... s|s| of length |s|, consisting of lowercase English letters. There also are q queries, each query is described by two integers li, ri (1 ≤ li ≤ ri ≤ |s|). The answer to the query is the number of substrings of string s[li... ri], which are palindromes.

String s[l... r] = slsl + 1... sr (1 ≤ l ≤ r ≤ |s|) is a substring of string s = s1s2... s|s|.

String t is called a palindrome, if it reads the same from left to right and from right to left. Formally, if t = t1t2... t|t| = t|t|t|t| - 1... t1.

Input

The first line contains string s (1 ≤ |s| ≤ 5000). The second line contains a single integer q (1 ≤ q ≤ 106) — the number of queries. Next q lines contain the queries. The i-th of these lines contains two space-separated integers li, ri (1 ≤ li ≤ ri ≤ |s|) — the description of the i-th query.

It is guaranteed that the given string consists only of lowercase English letters.

Output

Print q integers — the answers to the queries. Print the answers in the order, in which the queries are given in the input. Separate the printed numbers by whitespaces.

Examples

Input

caaaba
5
1 1
1 4
2 3
4 6
4 5


Output

1
7
3
4
2

Note

Consider the fourth query in the first test case. String s[4... 6] = «aba». Its palindrome substrings are: «a», «b», «a», «aba».
#include <bits/stdc++.h>
using namespace std;
long long base = 31, M = 1000000007, base1 = 29, M1 = 1000000009;
long long fpow(long long x, long long y) {
  if (y == 0) return 1;
  if (y == 1) return x;
  long long p = fpow(x, y / 2) % M;
  p *= p;
  p %= M;
  if (y & 1) p *= x;
  return p % M;
}
long long po1[5005], pr[5005], po[5005], pr1[5005], suf[5005], suf1[5005];
int ans[5005][5005], dp[5005][5005];
pair<long long, long long> gethash(int i, int j) {
  long long has = pr[j];
  if (i != 0) has -= (pr[i - 1] * po[j - i + 1]) % M;
  has += M;
  has %= M;
  long long has1 = pr1[j];
  if (i != 0) has1 -= (pr1[i - 1] * po1[j - i + 1]) % M1;
  has1 += M1;
  has1 %= M1;
  return {has, has1};
}
pair<long long, long long> gethash1(int i, int j) {
  long long has = suf[j];
  if (i != 0) has -= (suf[i - 1] * po[j - i + 1]) % M;
  has += M;
  has %= M;
  long long has1 = suf1[j];
  if (i != 0) has1 -= (suf1[i - 1] * po1[j - i + 1]) % M1;
  has1 += M1;
  has1 %= M1;
  return {has, has1};
}
int main() {
  po[0] = 1;
  po1[0] = 1;
  for (int i = 1; i <= 5001; i++) {
    po[i] = po[i - 1] * base;
    po1[i] = po1[i - 1] * base1;
    po[i] %= M;
    po1[i] %= M1;
  }
  string s, t;
  char c[100005];
  scanf("%s", &c);
  s = c;
  int n = s.size();
  t = s;
  reverse(t.begin(), t.end());
  int q;
  scanf("%d", &q);
  pr[0] = s[0] - 'a' + 1;
  pr1[0] = s[0] - 'a' + 1;
  suf[0] = t[0] - 'a' + 1;
  suf1[0] = t[0] - 'a' + 1;
  for (int i = 1; i < s.size(); i++) {
    long long x = s[i] - 'a' + 1;
    long long y = t[i] - 'a' + 1;
    pr[i] = (pr[i - 1] * base + x) % M;
    pr1[i] = (pr1[i - 1] * base1 + x) % M1;
    suf[i] = (suf[i - 1] * base + y) % M;
    suf1[i] = (suf1[i - 1] * base1 + y) % M1;
  }
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      pair<long long, long long> o = gethash(i, j);
      pair<long long, long long> os = gethash1(n - 1 - j, n - 1 - i);
      if (o.first == os.first && o.second == os.second) ans[i][j]++;
      if (j != i) ans[i][j] += ans[i][j - 1];
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    for (int j = i; j >= 0; j--) {
      dp[j][i] = dp[j + 1][i] + ans[j][i];
    }
  }
  int l, r;
  while (q--) {
    scanf("%d%d", &l, &r);
    l--;
    r--;
    printf("%d\n", dp[l][r]);
  }
  return 0;
}
