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 target("avx")
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
using namespace std;
const int N = 30000 + 10;
struct Seg {
  int l, r, id;
};
int n, m, q, a[N], B, fi[N];
Seg seg[N];
int A(int x, int y) { return ((x += y) >= m) ? (x - m) : x; }
bool cmp(const Seg& a, const Seg& b) {
  int t = a.l / B, t2 = b.l / B;
  if (t == t2)
    return (t & 1) ? (a.r < b.r) : (a.r > b.r);
  else
    return t < t2;
}
int cnt[N], tot, w[N];
void init() {
  for (int i = 1; i <= n; ++i) w[i] = a[i];
  sort(w + 1, w + n + 1);
  tot = unique(w + 1, w + n + 1) - w - 1;
  for (int i = 1; i <= n; ++i) a[i] = lower_bound(w + 1, w + tot + 1, a[i]) - w;
  for (int i = 1; i <= tot; ++i) w[i] %= m;
  fi[1] = fi[2] = 1 % m;
  for (int i = 3; i <= tot + 1; ++i) fi[i] = A(fi[i - 2], fi[i - 1]);
}
int s[N << 2], s2[N << 2], sz[N << 2];
void pull(int o) {
  int lc = o << 1, rc = lc ^ 1;
  sz[o] = sz[lc] + sz[rc];
  if (!sz[lc]) {
    s[o] = s[rc];
    s2[o] = s2[rc];
  } else if (!sz[rc]) {
    s[o] = s[lc];
    s2[o] = s2[lc];
  } else {
    s[o] = A(s[lc], A(s2[rc] * fi[sz[lc]] % m, s[rc] * fi[sz[lc] - 1] % m));
    s2[o] = A(s2[lc], A(s2[rc] * fi[sz[lc] + 1] % m, s[rc] * fi[sz[lc]] % m));
  }
}
void insert(int x, int o = 1, int l = 1, int r = tot) {
  if (l == r) {
    sz[o] = 1;
    s[o] = s2[o] = w[x];
    return;
  }
  int mid = (l + r) >> 1;
  if (x <= mid)
    insert(x, o << 1, l, mid);
  else
    insert(x, (o << 1) ^ 1, mid + 1, r);
  pull(o);
}
void erase(int x, int o = 1, int l = 1, int r = tot) {
  if (l == r) {
    sz[o] = 0;
    s[o] = s2[o] = 0;
    return;
  }
  int mid = (l + r) >> 1;
  if (x <= mid)
    erase(x, o << 1, l, mid);
  else
    erase(x, (o << 1) ^ 1, mid + 1, r);
  pull(o);
}
void add(int x) {
  if (!(cnt[x]++)) insert(x);
}
void del(int x) {
  if (!(--cnt[x])) erase(x);
}
int ans[N];
int main() {
  scanf("%d%d", &n, &m);
  B = sqrt(n);
  for (int i = 1; i <= n; ++i) scanf("%d", a + i);
  init();
  scanf("%d", &q);
  for (int i = 1; i <= q; ++i) {
    scanf("%d%d", &seg[i].l, &seg[i].r);
    seg[i].id = i;
  }
  sort(seg + 1, seg + q + 1, cmp);
  int l = 1, r = 0;
  for (int i = 1; i <= q; ++i) {
    while (r < seg[i].r) add(a[++r]);
    while (l > seg[i].l) add(a[--l]);
    while (r > seg[i].r) del(a[r--]);
    while (l < seg[i].l) del(a[l++]);
    ans[seg[i].id] = s[1];
  }
  for (int i = 1; i <= q; ++i) printf("%d\n", ans[i]);
  return 0;
}