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;
template <class T>
inline T bigmod(T p, T e, T M) {
  long long ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1) ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}
template <class T>
inline T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <class T>
inline T modinverse(T a, T M) {
  return bigmod(a, M - 2, M);
}
int seg[2000001][2], lzy[2000001], fib[200001], M;
void update(int idx, int s, int e) {
  if (s == e) {
    lzy[idx] = 0;
    return;
  }
  int v1 = (seg[idx * 2 + 1][0] * fib[lzy[idx] - 1] +
            seg[idx * 2 + 1][1] * fib[lzy[idx]]) %
           M;
  int v2 = (seg[idx * 2 + 1][0] * fib[lzy[idx]] +
            seg[idx * 2 + 1][1] * fib[lzy[idx] + 1]) %
           M;
  seg[idx * 2 + 1][0] = v1;
  seg[idx * 2 + 1][1] = v2;
  lzy[idx * 2 + 1] += lzy[idx];
  v1 = (seg[idx * 2 + 2][0] * fib[lzy[idx] - 1] +
        seg[idx * 2 + 2][1] * fib[lzy[idx]]) %
       M;
  v2 = (seg[idx * 2 + 2][0] * fib[lzy[idx]] +
        seg[idx * 2 + 2][1] * fib[lzy[idx] + 1]) %
       M;
  seg[idx * 2 + 2][0] = v1;
  seg[idx * 2 + 2][1] = v2;
  lzy[idx * 2 + 2] += lzy[idx];
  lzy[idx] = 0;
}
void insert1(int idx, int s, int e, int p, int v1, int v2) {
  if (lzy[idx]) {
    update(idx, s, e);
  }
  if (s == e) {
    seg[idx][0] = v1;
    seg[idx][1] = v2;
    return;
  }
  int mid = (s + e) / 2;
  if (p <= mid)
    insert1(idx * 2 + 1, s, mid, p, v1, v2);
  else
    insert1(idx * 2 + 2, mid + 1, e, p, v1, v2);
  for (int i = (0); i < (2); ++i)
    seg[idx][i] = (seg[idx * 2 + 1][i] + seg[idx * 2 + 2][i]) % M;
}
void insert2(int idx, int s, int e, int st, int ed) {
  if (st > ed) return;
  if (lzy[idx]) {
    update(idx, s, e);
  }
  if (s == st && e == ed) {
    int v = (seg[idx][0] + seg[idx][1]) % M;
    seg[idx][0] = seg[idx][1];
    seg[idx][1] = v;
    lzy[idx]++;
    return;
  }
  int mid = (s + e) / 2;
  if (ed <= mid)
    insert2(idx * 2 + 1, s, mid, st, ed);
  else if (st > mid)
    insert2(idx * 2 + 2, mid + 1, e, st, ed);
  else {
    insert2(idx * 2 + 1, s, mid, st, mid);
    insert2(idx * 2 + 2, mid + 1, e, mid + 1, ed);
  }
  for (int i = (0); i < (2); ++i)
    seg[idx][i] = (seg[idx * 2 + 1][i] + seg[idx * 2 + 2][i]) % M;
}
pair<int, int> query1(int idx, int s, int e, int st, int ed) {
  if (lzy[idx]) {
    update(idx, s, e);
  }
  if (st > ed) return make_pair(0, 0);
  if (s == st && e == ed) return make_pair(seg[idx][0], seg[idx][1]);
  int mid = (s + e) / 2;
  if (ed <= mid)
    return query1(idx * 2 + 1, s, mid, st, ed);
  else if (st > mid)
    return query1(idx * 2 + 2, mid + 1, e, st, ed);
  else {
    pair<int, int> pl = query1(idx * 2 + 1, s, mid, st, mid);
    pair<int, int> pr = query1(idx * 2 + 2, mid + 1, e, mid + 1, ed);
    return make_pair((pl.first + pr.first) % M, (pl.second + pr.second) % M);
  }
}
int seg2[2000001];
void insert(int idx, int s, int e, int p) {
  if (s == e) {
    seg2[idx]++;
    return;
  }
  int mid = (s + e) / 2;
  if (p <= mid)
    insert(idx * 2 + 1, s, mid, p);
  else
    insert(idx * 2 + 2, mid + 1, e, p);
  seg2[idx]++;
}
int query(int idx, int s, int e, int st, int ed) {
  if (st > ed) return 0;
  if (s == st && e == ed) return seg2[idx];
  int mid = (s + e) / 2;
  if (ed <= mid)
    return query(idx * 2 + 1, s, mid, st, ed);
  else if (st > mid)
    return query(idx * 2 + 2, mid + 1, e, st, ed);
  else {
    return query(idx * 2 + 1, s, mid, st, mid) +
           query(idx * 2 + 2, mid + 1, e, mid + 1, ed);
  }
}
pair<int, int> pp[300001];
vector<pair<int, int> > vc[300001];
int res[300001];
map<int, int> mm;
int ar[300001], val[300001];
int on[300001];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n >> M;
  for (int i = (0); i < (n); ++i) {
    cin >> ar[i];
    mm[ar[i]] = 1;
  }
  int cnt = 0;
  for (auto &v : mm) {
    v.second = ++cnt;
    val[v.second] = v.first % M;
  }
  for (int i = (0); i < (n); ++i) {
    ar[i] = mm[ar[i]];
  }
  fib[0] = 0;
  fib[1] = 1 % M;
  for (int i = 2; i <= 200000; i++) fib[i] = (fib[i - 1] + fib[i - 2]) % M;
  int SZ = 230;
  int q;
  cin >> q;
  for (int i = (0); i < (q); ++i) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    pp[i] = make_pair(l, r);
    if ((l / SZ) == (r / SZ)) {
      set<int> S;
      for (int j = l; j <= r; j++) S.insert(ar[j]);
      int cc = 0;
      for (auto &v : S) {
        res[i] = (res[i] + val[v] * fib[++cc]) % M;
      }
      continue;
    }
    vc[r].push_back(make_pair(l, i));
  }
  for (int i = 0; i < n; i += SZ) {
    memset(on, 0, sizeof on);
    memset(seg, 0, sizeof seg);
    memset(seg2, 0, sizeof seg2);
    for (int j = i + SZ; j < n; j++) {
      if (on[ar[j]] == 0) {
        on[ar[j]] = 1;
        int pre = query(0, 1, cnt, 1, ar[j] - 1) + 1;
        insert(0, 1, cnt, ar[j]);
        insert1(0, 1, cnt, ar[j], (val[ar[j]] * fib[pre - 1]) % M,
                (val[ar[j]] * fib[pre]) % M);
        insert2(0, 1, cnt, ar[j] + 1, cnt);
      }
      for (int k = (0); k < (vc[j].size()); ++k) {
        if (vc[j][k].first < i || vc[j][k].first >= i + SZ) continue;
        set<int> S;
        for (int l = i + SZ - 1; l >= vc[j][k].first; l--) {
          if (on[ar[l]] || S.count(ar[l])) continue;
          S.insert(ar[l]);
        }
        int cc = 0, pre = 0, tot = 0;
        for (auto &v : S) {
          pair<int, int> p = query1(0, 1, cnt, pre + 1, v);
          int pcnt = query(0, 1, cnt, pre + 1, v);
          int vv = (p.first * fib[cc] + p.second * fib[cc + 1]) % M;
          pre = v;
          tot += pcnt + 1;
          cc++;
          vv = (vv + val[v] * fib[tot]) % M;
          res[vc[j][k].second] = (res[vc[j][k].second] + vv) % M;
        }
        pair<int, int> p = query1(0, 1, cnt, pre + 1, cnt);
        int vv = (p.first * fib[cc] + p.second * fib[cc + 1]) % M;
        res[vc[j][k].second] = (res[vc[j][k].second] + vv) % M;
      }
    }
  }
  for (int i = (0); i < (q); ++i) cout << res[i] << "\n";
}