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;
int set_bit(int mask, int c) { return mask |= (1UL << c); }
bool get_bit(int mask, int c) { return (mask >> c) & 1U; }
int fx[] = {1, -1, 0, 0};
int fy[] = {0, 0, 1, -1};
struct TYPE {
  int w = 0;
  bool operator<(const TYPE &a) const { return this->w < a.w; }
};
int KASE;
struct Hashing {
  long long *hash1, *hash2;
  long long *inv1, *inv2;
  int n;
  long long mod1 = (long long)1e9 + 97, mod2 = (long long)1e9 + 9;
  long long multiplier1 = 43, multiplier2 = 31;
  long long invMultiplier1 = 441860508, invMultiplier2 = 838709685;
  Hashing() {}
  Hashing(string &s) { build_Hash(s); }
  void build_Hash(string &s) {
    n = s.size();
    hash1 = new long long[n + 1];
    hash2 = new long long[n + 1];
    inv1 = new long long[n + 1];
    inv2 = new long long[n + 1];
    hash1[0] = hash2[0] = 0;
    inv1[0] = inv2[0] = 1;
    long long p1 = 1, p2 = 1;
    for (int i = 0; i < n; i++) {
      hash1[i + 1] = (hash1[i] + s[i] * p1) % mod1;
      p1 = (p1 * multiplier1) % mod1;
      inv1[i + 1] = inv1[i] * invMultiplier1 % mod1;
      hash2[i + 1] = (hash2[i] + s[i] * p2) % mod2;
      p2 = (p2 * multiplier2) % mod2;
      inv2[i + 1] = inv2[i] * invMultiplier2 % mod2;
    }
  }
  long long getHash(int i, int j) { return getHash_2(i, j - i + 1); }
  long long getHash_2(int i, int len) {
    return (((hash1[i + len] - hash1[i] + mod1) * inv1[i] % mod1) << 32) +
           (hash2[i + len] - hash2[i] + mod2) * inv2[i] % mod2;
  }
  long long revHash(int i, int j) { return getHash(n - j - 1, n - i - 1); }
  void clear() {
    delete hash1;
    delete hash2;
    delete inv1;
    delete inv2;
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  for (KASE = 1; KASE <= t; KASE++) {
    string s, r;
    cin >> s, r = s;
    reverse(r.begin(), r.end());
    Hashing hasher = Hashing(s);
    Hashing rhasher = Hashing(r);
    int n = s.size(), q, leftt, rightt;
    long long sum[n + 5][n + 5];
    memset(sum, 0, sizeof sum);
    for (int i = 1; i <= n; i++) {
      for (int j = i; j <= n; j++) {
        sum[i][j] = sum[i][j - 1] + (hasher.getHash(i - 1, j - 1) ==
                                     rhasher.revHash(i - 1, j - 1));
      }
    }
    for (int i = n; 0 < i; i--) {
      for (int j = i; j <= n; j++) {
        sum[i][j] += sum[i + 1][j];
      }
    }
    cin >> q;
    while (q--) {
      cin >> leftt >> rightt;
      cout << sum[leftt][rightt] << "\n";
    }
  }
  return 0;
}
