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 GCC optimize("O3")
#pragma GCC optimize("-Ofast", "-funroll-all-loops", "-ffast-math")
#pragma GCC target("avx2,fma")
#pragma GCC optimize("unroll-loops")
using namespace std;
const long long Linf = 0x7f7f7f7f7f7f7f7f;
const int Inf = 0x3f3f3f3f;
const int MAXN = 2e5;
char buf[100000], *p1 = buf, *p2 = buf;
inline char nc() {
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline int read() {
  int w = 1, data = 0;
  char ch = 0;
  while (ch != '-' && (ch < '0' || ch > '9')) ch = nc();
  if (ch == '-') w = -1, ch = nc();
  while (ch >= '0' && ch <= '9') data = data * 10 + ch - '0', ch = nc();
  return w * data;
}
int blLen, P, bas = 1e5;
struct Que {
  int l, r, id;
  bool operator<(const Que& oth) const {
    return ((l / blLen) ^ (oth.l / blLen)) ? l / blLen < oth.l / blLen
           : ((l / blLen) & 1)             ? r < oth.r
                                           : r > oth.r;
  }
} que[MAXN];
int n, q, a[MAXN], b[MAXN], coN, fib[MAXN];
struct Node {
  int lVal, Val, addv;
};
struct Segment_Tree {
  Node v[MAXN << 2];
  void PushUp(int rt) {
    v[rt].lVal = (v[rt << 1].lVal + v[rt << 1 | 1].lVal) % P;
    v[rt].Val = (v[rt << 1].Val + v[rt << 1 | 1].Val) % P;
  }
  void Add(int l, int r, int C, int rt) {
    long long lVal = ((1ll * v[rt].lVal * fib[bas + C - 1]) +
                      (1ll * v[rt].Val * fib[bas + C])) %
                     P;
    long long Val = ((1ll * v[rt].lVal * fib[bas + C]) +
                     (1ll * v[rt].Val * fib[bas + C + 1])) %
                    P;
    v[rt].lVal = lVal;
    v[rt].Val = Val;
    v[rt].addv += C;
  }
  void PushDown(int l, int r, int rt) {
    if (v[rt].addv) {
      int mid = (l + r) >> 1;
      Add(l, mid, v[rt].addv, rt << 1);
      Add(mid + 1, r, v[rt].addv, rt << 1 | 1);
      v[rt].addv = 0;
    }
  }
  void UpdateAdd(int L, int C, int l, int r, int rt) {
    if (l == r) {
      v[rt].lVal = 1ll * b[l] % P * fib[bas + v[rt].addv] % P;
      v[rt].Val = 1ll * b[l] % P * fib[bas + v[rt].addv + 1] % P;
      return;
    }
    int mid = (l + r) >> 1;
    PushDown(l, r, rt);
    if (L <= mid)
      UpdateAdd(L, C, l, mid, rt << 1), Add(mid + 1, r, C, rt << 1 | 1);
    else
      UpdateAdd(L, C, mid + 1, r, rt << 1 | 1);
    PushUp(rt);
  }
  void UpdateRemove(int L, int C, int l, int r, int rt) {
    if (l == r) {
      v[rt].lVal = v[rt].Val = 0;
      return;
    }
    int mid = (l + r) >> 1;
    PushDown(l, r, rt);
    if (L <= mid)
      UpdateRemove(L, C, l, mid, rt << 1), Add(mid + 1, r, C, rt << 1 | 1);
    else
      UpdateRemove(L, C, mid + 1, r, rt << 1 | 1);
    PushUp(rt);
  }
} t;
int cnt[MAXN];
void add(int x) {
  if (!cnt[x]) t.UpdateAdd(x, 1, 1, coN, 1);
  cnt[x]++;
}
void remove(int x) {
  --cnt[x];
  if (!cnt[x]) t.UpdateRemove(x, -1, 1, coN, 1);
}
int ans[MAXN];
int32_t main() {
  n = read();
  P = read();
  blLen = sqrt(n);
  for (int i = 1; i <= n; i++) b[i] = a[i] = read();
  q = read();
  for (int i = 1; i <= q; i++) que[que[i].id = i].l = read(), que[i].r = read();
  sort(que + 1, que + q + 1);
  sort(b + 1, b + n + 1);
  coN = unique(b + 1, b + n + 1) - b - 1;
  for (int i = 1; i <= n; i++) a[i] = lower_bound(b + 1, b + coN + 1, a[i]) - b;
  fib[bas] = 0;
  fib[bas + 1] = 1;
  for (int i = 2; bas + i < MAXN; i++)
    fib[bas + i] = (1ll * fib[bas + i - 1] + fib[bas + i - 2]) % P;
  for (int i = 1; bas - i >= 0; i++)
    fib[bas - i] = (1ll * fib[bas - i + 2] - fib[bas - i + 1] + P) % P;
  int l = 1, r = 0;
  for (int i = 1; i <= q; i++) {
    while (l < que[i].l) remove(a[l++]);
    while (l > que[i].l) add(a[--l]);
    while (r < que[i].r) add(a[++r]);
    while (r > que[i].r) remove(a[r--]);
    ans[que[i].id] = t.v[1].Val;
  }
  for (int i = 1; i <= q; i++) printf("%d\n", ans[i]);
  return 0;
}