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 e[8100], st[8100], xx[1000010], yy[1000010], idx[1000010], res[1000010];
char p[5050][5050];
vector<pair<int, int> > v;
struct cmp {
  bool operator()(int i, int j) { return yy[i] < yy[j]; }
};
int main() {
  string s;
  cin >> s;
  int n = s.length(), x, y;
  memset(e, 0, sizeof(e));
  memset(st, 0, sizeof(st));
  for (int i = 0; i < n; i++) p[i][i] = '1';
  int c = 0;
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < n - i + 1; j++) {
      x = j;
      y = j + i - 1;
      p[x][y] = '0';
      if (s[x] == s[y] && (x + 1 > y - 1 || p[x + 1][y - 1] == '1'))
        p[x][y] = '1';
      if (p[x][y] == '1') {
        if (0) cout << x << " ", cout << y << endl;
        v.push_back(make_pair(y + 1, x + 1));
        c++;
      }
    }
  }
  int(q);
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int(x), (y);
    scanf("%d %d", &x, &y);
    idx[i] = i;
    xx[i] = x;
    yy[i] = y;
  }
  sort(idx, idx + q, cmp());
  sort(v.begin(), v.end());
  int j = 0;
  vector<pair<int, int> >::iterator it = v.begin();
  for (int i = 0; i < q; i++) {
    while (it != v.end() && it->first <= yy[idx[i]]) {
      x = it->second;
      y = it->first;
      for (int k = x + 1; k < 8100; k = (k | (k - 1)) + 1) st[k]++;
      for (int k = y; k < 8100; k = (k | (k - 1)) + 1) e[k]++;
      ++it;
    }
    x = xx[idx[i]];
    y = yy[idx[i]];
    int r = 0;
    for (int k = y; k; k &= (k - 1)) r += e[k];
    for (int k = x; k; k &= (k - 1)) r -= st[k];
    res[idx[i]] = (r + y - x + 1);
  }
  for (int i = 0; i < q; i++) cout << res[i] << endl;
  return 0;
}
