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;
const int md = 1e9 + 19;
const int p = 37;
const int N = 5001;
string s, t;
int m, n;
long long hash1[N], hash2[N];
long long st[N];
int dp[N][N];
vector<int> a[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> s;
  n = s.size();
  st[0] = 1;
  for (int i = 1; i < n; ++i) {
    st[i] = (st[i - 1] * p) % md;
  }
  hash1[0] = s[0];
  for (int i = 1; i < n; ++i) {
    hash1[i] = (hash1[i - 1] + s[i] * st[i]) % md;
  }
  hash2[n - 1] = (s[n - 1]);
  for (int i = n - 2; i >= 0; i--) {
    hash2[i] = (hash2[i + 1] + s[i] * st[n - 1 - i]) % md;
  }
  long long len = 0, st1, st2;
  long long h1, h2;
  for (int i = 0; i < n; ++i) {
    for (int j = i; j < n; ++j) {
      st1 = i;
      st2 = n - 1 - j;
      h1 = hash1[j];
      if (i != 0) h1 -= hash1[i - 1];
      if (h1 < 0) h1 += md;
      h2 = hash2[i];
      h2 -= hash2[j + 1];
      if (h2 < 0) h2 += md;
      if (st1 < st2)
        h1 = (h1 * 1ll * st[st2 - st1]) % md;
      else
        h2 = (h2 * 1ll * st[st1 - st2]) % md;
      if (h1 == h2) {
        a[j].push_back(i);
      }
    }
  }
  int l, r, mid;
  for (int i = 0; i < n; ++i) {
    for (int j = i; j < n; ++j) {
      dp[i][j] = dp[i][j - 1];
      if (a[j].size() == 0) continue;
      l = 0;
      r = a[j].size() - 1;
      while (l < r - 1) {
        mid = (l + r) / 2;
        if (a[j][mid] < i)
          l = mid;
        else
          r = mid;
      }
      if (a[j][l] >= i)
        dp[i][j] += a[j].size() - l;
      else if (a[j][r] >= i)
        dp[i][j] += a[j].size() - r;
    }
  }
  cin >> m;
  for (int i = 1; i <= m; ++i) {
    cin >> l >> r;
    l--;
    r--;
    cout << dp[l][r] << '\n';
  }
}
