Problem: Yash is finally tired of computing the length of the longest Fibonacci-ish sequence. He now plays around with more complex things such as Fibonacci-ish potentials. 

Fibonacci-ish potential of an array ai is computed as follows: 

  1. Remove all elements j if there exists i < j such that ai = aj. 
  2. Sort the remaining elements in ascending order, i.e. a1 < a2 < ... < an. 
  3. Compute the potential as P(a) = a1·F1 + a2·F2 + ... + an·Fn, where Fi is the i-th Fibonacci number (see notes for clarification). 



You are given an array ai of length n and q ranges from lj to rj. For each range j you have to compute the Fibonacci-ish potential of the array bi, composed using all elements of ai from lj to rj inclusive. Find these potentials modulo m.

Input

The first line of the input contains integers of n and m (1 ≤ n, m ≤ 30 000) — the length of the initial array and the modulo, respectively.

The next line contains n integers ai (0 ≤ ai ≤ 109) — elements of the array.

Then follow the number of ranges q (1 ≤ q ≤ 30 000).

Last q lines contain pairs of indices li and ri (1 ≤ li ≤ ri ≤ n) — ranges to compute Fibonacci-ish potentials.

Output

Print q lines, i-th of them must contain the Fibonacci-ish potential of the i-th range modulo m.

Example

Input

5 10
2 1 2 1 2
2
2 4
4 5


Output

3
3

Note

For the purpose of this problem define Fibonacci numbers as follows: 

  1. F1 = F2 = 1. 
  2. Fn = Fn - 1 + Fn - 2 for each n > 2. 



In the first query, the subarray [1,2,1] can be formed using the minimal set {1,2}. Thus, the potential of this subarray is 1*1+2*1=3.

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
template <typename T>
T min(T x, T y) {
  return x < y ? x : y;
}
template <typename T>
T max(T x, T y) {
  return x > y ? x : y;
};
const int MAXN = 100005;
const int INF = 2000000005;
const int block = 200;
int mod, *fib, pool[MAXN];
int rmove(int sum, int sum1, int k) {
  return (fib[k + 1] * sum + fib[k] * sum1) % mod;
}
struct tree {
  int l;
  int r;
  int sum;
  int sum1;
  int shift;
  tree *lson;
  tree *rson;
  tree() {
    l = r = sum = sum1 = shift = 0;
    lson = rson = NULL;
  }
  void right_move(int k) {
    int t = (fib[k + 1] * sum + fib[k] * sum1) % mod;
    int t1 = (fib[k] * sum + fib[k - 1] * sum1) % mod;
    sum = t;
    sum1 = t1;
    shift += k;
  }
  void fixup() {
    sum = (lson->sum + rson->sum) % mod;
    sum1 = (lson->sum1 + rson->sum1) % mod;
    int t = rmove(sum, sum1, shift);
    int t1 = rmove(sum, sum1, shift - 1);
    sum = t;
    sum1 = t1;
  }
  void build(int L, int R) {
    l = L;
    r = R;
    if (l == r) return;
    int mid = (l + r) / 2;
    lson = new tree;
    rson = new tree;
    lson->build(l, mid);
    rson->build(mid + 1, r);
  }
  void modify(int id, int W, int S, int rs) {
    if (l == r) {
      sum = W * fib[S] % mod;
      sum1 = W * fib[S - 1] % mod;
      return;
    }
    int mid = (l + r) / 2;
    if (id <= mid) {
      rson->right_move(rs);
      lson->modify(id, W, S - shift, rs);
    } else
      rson->modify(id, W, S - shift, rs);
    fixup();
  }
  void move(int L, int R, int k) {
    if (l >= L && r <= R) {
      right_move(k);
      return;
    }
    int mid = (l + r) / 2;
    if (L <= mid) lson->move(L, R, k);
    if (R > mid) rson->move(L, R, k);
    fixup();
  }
  void print() {
    if (lson) lson->print();
    printf("seg_tree l %d r %d sum %d sum1 %d\n", l, r, sum, sum1);
    if (rson) rson->print();
  }
};
struct Ftree {
  int bit[MAXN];
  Ftree() { memset(bit, 0, sizeof(bit)); }
  void add(int pos, int k) {
    for (int i = pos; i < MAXN; i += ((i) & -(i))) bit[i] += k;
  }
  int sum(int pos) {
    int ret = 0;
    for (int i = pos; i; i -= ((i) & -(i))) ret += bit[i];
    return ret;
  }
  int query(int l, int r) { return sum(r) - sum(l - 1); }
};
struct data {
  int id;
  int w;
  bool operator<(data t) const { return w < t.w; }
};
struct QUERY {
  int id;
  int l;
  int r;
  bool operator<(QUERY t) const {
    if (l / block != t.l / block) return l < t.l;
    return r < t.r;
  }
};
int N, Q;
int a[MAXN], rank[MAXN];
QUERY q[MAXN];
int L, R, cnt[MAXN];
tree T;
Ftree FT;
void Rr_move() {
  R++;
  cnt[rank[R]]++;
  if (cnt[rank[R]] == 1) {
    FT.add(rank[R], 1);
    int s = FT.sum(rank[R]);
    T.modify(rank[R], a[R], s, 1);
  }
}
void Rl_move() {
  cnt[rank[R]]--;
  if (cnt[rank[R]] == 0) {
    FT.add(rank[R], -1);
    T.modify(rank[R], 0, 0, -1);
  }
  R--;
}
void Ll_move() {
  L--;
  cnt[rank[L]]++;
  if (cnt[rank[L]] == 1) {
    FT.add(rank[L], 1);
    int s = FT.sum(rank[L]);
    T.modify(rank[L], a[L], s, 1);
  }
}
void Lr_move() {
  cnt[rank[L]]--;
  if (cnt[rank[L]] == 0) {
    FT.add(rank[L], -1);
    T.modify(rank[L], 0, 0, -1);
  }
  L++;
}
data tmp[MAXN];
void init() {
  scanf("%d %d", &N, &mod);
  fib = pool + MAXN / 2;
  fib[1] = fib[2] = 1;
  for (int i = 3; i < 40000; i++) fib[i] = (fib[i - 1] + fib[i - 2]) % mod;
  for (int i = 0; i > -40000; i--) fib[i] = (fib[i + 2] - fib[i + 1]) % mod;
  for (int i = 1; i <= N; i++) {
    scanf("%d", a + i);
    tmp[i].id = i;
    tmp[i].w = a[i];
  }
  std::sort(tmp + 1, tmp + N + 1);
  int cur = 0;
  for (int i = 1; i <= N; i++) {
    if (i == 1 || tmp[i].w != tmp[i - 1].w) cur++;
    rank[tmp[i].id] = cur;
  }
  scanf("%d", &Q);
  for (int i = 1; i <= Q; i++) {
    scanf("%d %d", &q[i].l, &q[i].r);
    q[i].id = i;
  }
  std::sort(q + 1, q + Q + 1);
  T.build(1, N);
  L = R = 1;
  T.modify(rank[1], a[1], 1, 1);
  cnt[rank[1]] = 1;
  FT.add(rank[1], 1);
}
void solve() {
  static int ans[MAXN];
  for (int i = 1; i <= Q; i++) {
    int l = q[i].l, r = q[i].r;
    while (R < r) Rr_move();
    while (L < l) Lr_move();
    while (L > l) Ll_move();
    while (R > r) Rl_move();
    ans[q[i].id] = (T.sum % mod + mod) % mod;
  }
  for (int i = 1; i <= Q; i++) printf("%d\n", ans[i]);
}
int main() {
  init();
  solve();
  return 0;
}