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 comment(linker, "/STACK:256000000")
using namespace std;
const int INF = 1000 * 1000 * 1000;
const double EPS = 1e-9;
bool p[5555][5555];
int tree[5555][5555];
int len;
void upd(int x, int y) {
  for (int i = x; i < len; i = i | (i + 1))
    for (int j = y; j < len; j = j | (j + 1)) {
      ++tree[i][j];
    }
}
int sum(int x, int y) {
  int res = 0;
  for (int i = x; i >= 0; i = (i & (i + 1)) - 1)
    for (int j = y; j >= 0; j = (j & (j + 1)) - 1) {
      res += tree[i][j];
    }
  return res;
}
int sum(int x1, int y1, int x2, int y2) {
  int res = sum(x2, y2);
  if (x1 > 0) {
    res -= sum(x1 - 1, y2);
  }
  if (y1 > 0) {
    res -= sum(x2, y1 - 1);
  }
  if (x1 > 0 && y1 > 0) {
    res += sum(x1 - 1, y1 - 1);
  }
  return res;
}
int main() {
  string s;
  cin >> s;
  len = (int)s.size();
  for (int c = 0; c < len; ++c) {
    int l = c, r = c;
    while (l >= 0 && r < len && s[l] == s[r]) {
      p[l][r] = true;
      --l;
      ++r;
    }
    l = c, r = c + 1;
    while (l >= 0 && r < len && s[l] == s[r]) {
      p[l][r] = true;
      --l;
      ++r;
    }
  }
  for (int i = 0; i < len; ++i)
    for (int j = i; j < len; ++j)
      if (p[i][j]) {
        upd(i, j);
      }
  int Q;
  scanf("%d", &Q);
  while (Q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%d\n", sum(l - 1, l - 1, r - 1, r - 1));
  }
  return 0;
}
