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>
using namespace std;
int n, m;
int a[30005], tmp[30005];
int B;
struct query {
  int l, r, id;
  bool operator<(const query &x) const {
    if (l / B != x.l / B) return l < x.l;
    return (l / B) & 1 ? r < x.r : r > x.r;
  }
} q[30005];
struct matrix {
  int a, b, c, d;
  void set() {
    a = d = 1;
    b = c = 0;
  }
  void operator*(const matrix &w) {
    matrix s;
    s.a = (a * w.a + b * w.c) % m;
    s.b = (a * w.b + b * w.d) % m;
    s.c = (c * w.a + d * w.c) % m;
    s.d = (c * w.b + d * w.d) % m;
    a = s.a;
    b = s.b;
    c = s.c;
    d = s.d;
  }
} zy, nzy, ad[30005 << 2], sum[30005 << 2];
int f[30005], vis[30005 << 2];
int ss[30005];
int lb(int x) { return x & (-x); }
void adds(int x, int y) {
  for (; x <= n; x += lb(x)) ss[x] += y;
}
int ask(int x) {
  int ans = 0;
  for (; x; x -= lb(x)) ans += ss[x];
  return ans;
}
void build(int k, int l, int r) {
  ad[k].set();
  if (l == r) return;
  int mid = l + r >> 1;
  build(k << 1, l, mid);
  build(k << 1 | 1, mid + 1, r);
}
void add(int k, const matrix &w) {
  ad[k] * w;
  sum[k] * w;
  vis[k] = 1;
}
void pd(int k) {
  if (!vis[k]) return;
  add(k << 1, ad[k]);
  add(k << 1 | 1, ad[k]);
  ad[k].set();
  vis[k] = 0;
}
void update(int k) {
  sum[k].a = (sum[k << 1].a + sum[k << 1 | 1].a) % m;
  sum[k].b = (sum[k << 1].b + sum[k << 1 | 1].b) % m;
}
void change(int k, int l, int r, int a, int x, int y, int op) {
  sum[k].a += op * x + m;
  sum[k].b += op * y + m;
  sum[k].a %= m;
  sum[k].b %= m;
  if (l == r) return;
  pd(k);
  int mid = l + r >> 1;
  if (a <= mid)
    change(k << 1, l, mid, a, x, y, op);
  else
    change(k << 1 | 1, mid + 1, r, a, x, y, op);
}
void change(int k, int l, int r, int x, int y, const matrix &a) {
  if (x > y) return;
  if (x <= l && r <= y) return add(k, a);
  pd(k);
  int mid = l + r >> 1;
  if (x <= mid) change(k << 1, l, mid, x, y, a);
  if (mid < y) change(k << 1 | 1, mid + 1, r, x, y, a);
  update(k);
}
int t[30005];
void add(int x) {
  t[a[x]]++;
  if (t[a[x]] == 1) {
    adds(a[x], 1);
    int s = ask(a[x]);
    change(1, 1, n, a[x], f[s - 1] * tmp[a[x]] % m, f[s] * tmp[a[x]] % m, 1);
    change(1, 1, n, a[x] + 1, n, zy);
  }
}
void erase(int x) {
  t[a[x]]--;
  if (t[a[x]] == 0) {
    change(1, 1, n, a[x] + 1, n, nzy);
    int s = ask(a[x]);
    change(1, 1, n, a[x], f[s - 1] * tmp[a[x]] % m, f[s] * tmp[a[x]] % m, -1);
    adds(a[x], -1);
  }
}
int res[30005];
signed main() {
  zy.b = zy.c = zy.d = 1;
  scanf("%d%d", &n, &m);
  nzy.b = nzy.c = 1;
  nzy.a = m - 1;
  f[1] = 1;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), tmp[i] = a[i];
  for (int i = 2; i <= n; i++) f[i] = (f[i - 1] + f[i - 2]) % m;
  sort(tmp + 1, tmp + n + 1);
  int sss = unique(tmp + 1, tmp + n + 1) - tmp - 1;
  for (int i = 1; i <= n; i++)
    a[i] = lower_bound(tmp + 1, tmp + sss + 1, a[i]) - tmp;
  for (int i = 1; i <= n; i++) tmp[i] %= m;
  int Q;
  scanf("%d", &Q);
  for (int i = 1; i <= Q; i++) {
    scanf("%d%d", &q[i].l, &q[i].r);
    q[i].id = i;
  }
  B = sqrt(n);
  sort(q + 1, q + Q + 1);
  int l = 1, r = 0;
  build(1, 1, n);
  for (int i = 1; i <= Q; i++) {
    while (r < q[i].r) add(++r);
    while (l > q[i].l) add(--l);
    while (r > q[i].r) erase(r--);
    while (l < q[i].l) erase(l++);
    res[q[i].id] = sum[1].b;
  }
  for (int i = 1; i <= Q; i++) cout << res[i] << "\n";
  return 0;
}