Alice wants to send an important message to Bob. Message a = (a1, ..., an) is a sequence of positive integers (characters).

To compress the message Alice wants to use binary Huffman coding. We recall that binary Huffman code, or binary prefix code is a function f, that maps each letter that appears in the string to some binary string (that is, string consisting of characters '0' and '1' only) such that for each pair of different characters ai and aj string f(ai) is not a prefix of f(aj) (and vice versa). The result of the encoding of the message a1, a2, ..., an is the concatenation of the encoding of each character, that is the string f(a1)f(a2)... f(an). Huffman codes are very useful, as the compressed message can be easily and uniquely decompressed, if the function f is given. Code is usually chosen in order to minimize the total length of the compressed message, i.e. the length of the string f(a1)f(a2)... f(an).

Because of security issues Alice doesn't want to send the whole message. Instead, she picks some substrings of the message and wants to send them separately. For each of the given substrings ali... ari she wants to know the minimum possible length of the Huffman coding. Help her solve this problem.

Input

The first line of the input contains the single integer n (1 ≤ n ≤ 100 000) — the length of the initial message. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000) — characters of the message.

Next line contains the single integer q (1 ≤ q ≤ 100 000) — the number of queries.

Then follow q lines with queries descriptions. The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n) — the position of the left and right ends of the i-th substring respectively. Positions are numbered from 1. Substrings may overlap in any way. The same substring may appear in the input more than once.

Output

Print q lines. Each line should contain a single integer — the minimum possible length of the Huffman encoding of the substring ali... ari.

Example

Input

7
1 2 1 3 1 2 1
5
1 7
1 3
3 5
2 4
4 4


Output

10
3
3
5
0

Note

In the first query, one of the optimal ways to encode the substring is to map 1 to "0", 2 to "10" and 3 to "11".

Note that it is correct to map the letter to the empty substring (as in the fifth query from the sample).
#include <bits/stdc++.h>
using namespace std;
const int MaxN = 100010, Block = 500, TB = 300;
struct query {
  int l, r, id;
  bool operator<(const query &a) const {
    if (l / TB != a.l / TB) return l / TB < a.l / TB;
    return r < a.r;
  }
} q[MaxN];
long long ans[MaxN];
int cnt[MaxN], num[MaxN], a[MaxN];
vector<int> large;
bool vis[MaxN];
int n, m;
int tmp[MaxN];
long long get_ans() {
  for (int i = 1; i <= Block * 2; ++i) tmp[i] = num[i];
  long long ret = 0;
  int x = 1;
  for (int i = 1; i <= Block; ++i) {
    if (!tmp[i]) continue;
    if (tmp[i] >= 2) {
      ret += (tmp[i] / 2) * i * 2;
      tmp[i * 2] += tmp[i] / 2;
      tmp[i] &= 1;
    }
    if (tmp[i] & 1) {
      x = max(i + 1, x);
      while (tmp[x] == 0 && i + x <= Block * 2) ++x;
      if (i + x > Block * 2) break;
      ret += i + x;
      ++tmp[i + x];
      --tmp[x];
    }
    tmp[i] = 0;
  }
  priority_queue<int, vector<int>, greater<int> > Q;
  for (int i = 1; i <= Block * 2; ++i)
    for (int j = 1; j <= tmp[i]; ++j) Q.push(i);
  for (auto i : large)
    if (cnt[i] > Block * 2) Q.push(cnt[i]);
  while (Q.size() >= 2) {
    int x = Q.top();
    Q.pop();
    int y = Q.top();
    Q.pop();
    ret += x + y;
    Q.push(x + y);
  }
  return ret;
}
void reverse(int x) {
  int u = a[x];
  if (vis[x]) {
    --num[cnt[u]];
    --cnt[u];
    ++num[cnt[u]];
  } else {
    --num[cnt[u]];
    ++cnt[u];
    ++num[cnt[u]];
  }
  vis[x] ^= 1;
}
void move(int &x, int p) {
  while (x > p) {
    reverse(x);
    --x;
  }
  while (x < p) {
    ++x;
    reverse(x);
  }
}
int main() {
  scanf("%d", &n);
  int mx = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    ++cnt[a[i]];
    mx = max(mx, a[i]);
  }
  for (int i = 1; i <= mx; ++i)
    if (cnt[i] > Block * 2) large.push_back(i);
  memset(cnt, 0, sizeof(cnt));
  scanf("%d", &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d", &q[i].l, &q[i].r);
    q[i].id = i;
  }
  sort(q + 1, q + m + 1);
  int l = 0, r = 0;
  for (int i = 1; i <= m; ++i) {
    move(l, q[i].l - 1);
    move(r, q[i].r);
    if (q[i].l == q[i - 1].l && q[i].r == q[i - 1].r)
      ans[q[i].id] = ans[q[i - 1].id];
    else
      ans[q[i].id] = get_ans();
  }
  for (int i = 1; i <= m; ++i) printf("%I64d\n", ans[i]);
  return 0;
}
