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>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
const long double pi = acos(-1);
bool debug = false;
long long int mod = 1e9 + 7;
const long long int mxm = 5e3 + 9;
mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
long long int power(long long int x, long long int y) {
  long long int res = 1;
  x = x % mod;
  if (x == 0) return 0;
  while (y > 0) {
    if (y & 1) res = (res * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return res;
}
long long int inverse(long long int n) { return power(n, mod - 2); }
long long int kk[10] = {89,
                        101,
                        189,
                        94,
                        621,
                        (long long int)rng() % 1000 + 1051,
                        (long long int)rng() % 2000 + 2761,
                        (long long int)rng() % 4000 + 4441,
                        (long long int)rng() % 8000 + 8111,
                        (long long int)rng() % 16000 + 18883};
long long int primes[10] = {533000401,  735632791, 776531419, 797003413,
                            817504243,  920419813, 961748941, 982451653,
                            1000000007, 1000000009};
long long int dp1[mxm];
long long int dp2[mxm];
long long int base1;
long long int base2;
long long int m1;
long long int m2;
long long int inv1[mxm];
long long int inv2[mxm];
void assign_constants() {
  base1 = kk[rng() % 10];
  long long int y = rng() % 10;
  base2 = kk[y];
  if (base1 == base2) {
    y++;
    base2 = kk[y % 10];
  }
  m1 = primes[rng() % 10];
  y = rng() % 10;
  m2 = primes[y];
  if (m1 == m2) {
    y++;
    m2 = primes[y % 10];
  }
}
string s;
void precompute() {
  long long int tmp, i, x, x2, tmp2;
  tmp = 1;
  inv1[0] = 1;
  mod = m1;
  x = inverse(base1);
  dp1[0] = s[0] - 'a' + 1;
  tmp2 = 1;
  inv2[0] = 1;
  mod = m2;
  x2 = inverse(base2);
  dp2[0] = s[0] - 'a' + 1;
  for (i = 1; i <= (long long int)(s.size()) - 1; i++) {
    tmp = (tmp * base1) % m1;
    inv1[i] = (inv1[i - 1] * x) % m1;
    dp1[i] = (dp1[i - 1] + (s[i] - 'a' + 1) * tmp) % m1;
    tmp2 = (tmp2 * base2) % m2;
    inv2[i] = (inv2[i - 1] * x2) % m2;
    dp2[i] = (dp2[i - 1] + (s[i] - 'a' + 1) * tmp2) % m2;
  }
}
long long int query1(long long int l, long long int r) {
  long long int ans1 = dp1[r];
  if (l) {
    ans1 -= dp1[l - 1];
  }
  ans1 = (ans1 + m1 + m1) % m1;
  ans1 *= inv1[l];
  ans1 %= m1;
  return ans1;
}
long long int query2(long long int l, long long int r) {
  long long int ans2 = dp2[r];
  if (l) {
    ans2 -= dp2[l - 1];
  }
  ans2 = (ans2 + m2 + m2) % m2;
  ans2 *= inv2[l];
  ans2 %= m2;
  return ans2;
}
long long int hashing_is_cool(string const& str, long long int b,
                              long long int m) {
  long long int pow_p = 1, p = b, ans = 0, i;
  for (i = 0; i < str.size(); i++) {
    ans += ((str[i] - 'a' + 1) * pow_p) % m;
    ans %= m;
    pow_p *= p;
    pow_p %= m;
  }
  return ans;
}
long long int dp1r[mxm];
long long int dp2r[mxm];
void precompute_reverse() {
  long long int tmp, i, x, x2, tmp2;
  tmp = 1;
  dp1r[(long long int)(s.size()) - 1] =
      s[(long long int)(s.size()) - 1] - 'a' + 1;
  tmp2 = 1;
  dp2r[(long long int)(s.size()) - 1] =
      s[(long long int)(s.size()) - 1] - 'a' + 1;
  for (i = (long long int)(s.size()) - 1 - 1; i >= 0; i--) {
    tmp = (tmp * base1) % m1;
    dp1r[i] = (dp1r[i + 1] + (s[i] - 'a' + 1) * tmp) % m1;
    tmp2 = (tmp2 * base2) % m2;
    dp2r[i] = (dp2r[i + 1] + (s[i] - 'a' + 1) * tmp2) % m2;
  }
}
long long int query1r(long long int l, long long int r) {
  long long int ans1r = dp1r[l];
  if (r < (long long int)(s.size()) - 1) {
    ans1r -= dp1r[r + 1];
  }
  ans1r = (ans1r + m1 + m1) % m1;
  ans1r *= inv1[(long long int)(s.size()) - r - 1];
  ans1r %= m1;
  return ans1r;
}
long long int query2r(long long int l, long long int r) {
  long long int ans2r = dp2r[l];
  if (r < (long long int)(s.size()) - 1) {
    ans2r -= dp2r[r + 1];
  }
  ans2r = (ans2r + m2 + m2) % m2;
  ans2r *= inv2[(long long int)(s.size()) - r - 1];
  ans2r %= m2;
  return ans2r;
}
long long int cache[mxm][mxm];
long long int memoization(long long int l, long long int r) {
  if (l < 0 or r >= (long long int)(s.size())) return 0;
  if (l > r) return 0;
  if (cache[l][r] != -1) return cache[l][r];
  long long int ans = 0;
  if (query1(l, r) == query1r(l, r) and query2(l, r) == query2r(l, r)) ans++;
  ans +=
      memoization(l + 1, r) + memoization(l, r - 1) - memoization(l + 1, r - 1);
  cache[l][r] = ans;
  return cache[l][r];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long int t = 1, n, i, j, k, len, x, y, z, c, f, flag, p, q, mx, mn, l, r,
                sum, ans, tmp, it, pos, avg, m, cnt;
  char ch;
  vector<long long int> v;
  vector<pair<long long int, long long int>> vec;
  unordered_map<long long int, long long int> mappu;
  pair<long long int, long long int> pr;
  while (t--) {
    f = 0;
    sum = 0;
    flag = 0;
    ans = 0;
    cnt = 0;
    v.clear();
    mappu.clear();
    vec.clear();
    assign_constants();
    cin >> s;
    memset(cache, -1, sizeof(cache));
    precompute();
    precompute_reverse();
    cin >> q;
    while (q--) {
      cin >> l >> r;
      l--;
      r--;
      ans = memoization(l, r);
      cout << ans << "\n";
    }
  }
  return 0;
}
