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 <typename A, typename B>
ostream &operator<<(ostream &s, const pair<A, B> &p) {
  return s << "(" << p.first << "," << p.second << ")";
}
template <typename T>
ostream &operator<<(ostream &s, const vector<T> &c) {
  s << "[ ";
  for (auto it : c) s << it << " ";
  s << "]";
  return s;
}
const int MAXN = 32768;
const int BLK = 350;
pair<int, int> operator+(pair<int, int> a, pair<int, int> b) {
  return {a.first + b.first, a.second + b.second};
}
pair<int, int> operator%(pair<int, int> a, int b) {
  return {a.first % b, a.second % b};
}
int N, M, Q;
int arr[MAXN];
int _fib[MAXN], _mfib[MAXN];
int ql[MAXN], qr[MAXN];
int ans[MAXN];
int V;
vector<int> vec;
vector<int> qgrp[MAXN];
int BIT[MAXN];
int cnt[MAXN];
pair<int, int> seg[MAXN * 2];
int tag[MAXN * 2];
int fib(int x) { return x < 0 ? _mfib[-x] : _fib[x]; }
void addBIT(int pos, int val) {
  pos++;
  while (pos < MAXN) {
    BIT[pos] += val;
    pos += pos & (-pos);
  }
}
int qryBIT(int pos) {
  int a = 0;
  pos++;
  while (pos) {
    a += BIT[pos];
    pos -= pos & (-pos);
  }
  return a;
}
void set_tag(int s, int val) {
  tag[s] += val;
  int ff = (seg[s].first * fib(val + 1) + seg[s].second * fib(val)) % M;
  int ss = (seg[s].first * fib(val) + seg[s].second * fib(val - 1)) % M;
  seg[s] = {ff, ss};
}
void clear_tag(int s, int lb, int rb) {
  if (!tag[s]) return;
  if (rb - lb > 1) {
    set_tag(2 * s, tag[s]);
    set_tag(2 * s + 1, tag[s]);
  }
  tag[s] = 0;
}
void add_seg(int s, int lb, int rb, int pos, pair<int, int> val) {
  if (pos < lb || pos >= rb) return;
  clear_tag(s, lb, rb);
  if (rb - lb == 1) {
    seg[s] = (seg[s] + val) % M;
  } else {
    int mb = (lb + rb) >> 1;
    add_seg(2 * s, lb, mb, pos, val);
    add_seg(2 * s + 1, mb, rb, pos, val);
    seg[s] = (seg[2 * s] + seg[2 * s + 1]) % M;
  }
}
void update_seg(int s, int lb, int rb, int l, int r, int val) {
  if (rb <= l || r <= lb) return;
  if (l <= lb && rb <= r) {
    set_tag(s, val);
  } else {
    clear_tag(s, lb, rb);
    int mb = (lb + rb) >> 1;
    update_seg(2 * s, lb, mb, l, r, val);
    update_seg(2 * s + 1, mb, rb, l, r, val);
    seg[s] = (seg[2 * s] + seg[2 * s + 1]) % M;
  }
}
int qry_seg() { return seg[1].first; }
void add_num(int v) {
  int n = arr[v];
  if (cnt[n] == 0) {
    int ord = qryBIT(n);
    pair<int, int> pr;
    pr.first = (fib(ord + 1) * vec[n]) % M;
    pr.second = (fib(ord) * vec[n]) % M;
    add_seg(1, 0, N, n, pr);
    update_seg(1, 0, N, n + 1, N, 1);
    addBIT(n, 1);
  }
  cnt[n]++;
}
void del_num(int v) {
  int n = arr[v];
  cnt[n]--;
  if (cnt[n] == 0) {
    addBIT(n, -1);
    int ord = qryBIT(n);
    pair<int, int> pr;
    pr.first = (M - ((fib(ord + 1) * vec[n]) % M)) % M;
    pr.second = (M - ((fib(ord) * vec[n]) % M)) % M;
    add_seg(1, 0, N, n, pr);
    update_seg(1, 0, N, n + 1, N, -1);
  }
}
int qry_ans() { return qry_seg(); }
void make_fib() {
  _fib[0] = 0;
  _fib[1] = 1 % M;
  for (int i = 2; i < MAXN; i++) _fib[i] = (_fib[i - 2] + _fib[i - 1]) % M;
  for (int i = 0; i < MAXN; i++)
    _mfib[i] = (i % 2 ? _fib[i] : (M - _fib[i]) % M);
}
void make() {
  for (int i = 0; i < N; i++) vec.push_back(arr[i]);
  sort(begin(vec), end(vec));
  V = unique(begin(vec), end(vec)) - vec.begin();
  vec.resize(V);
  for (int i = 0; i < N; i++)
    arr[i] = lower_bound(begin(vec), end(vec), arr[i]) - vec.begin();
  for (int i = 0; i < V; i++) vec[i] %= M;
  for (int i = 0; i < Q; i++) qgrp[ql[i] / BLK].push_back(i);
  for (int i = 0; i < MAXN; i++)
    sort(begin(qgrp[i]), end(qgrp[i]),
         [](int a, int b) { return qr[a] < qr[b]; });
}
void calc() {
  int lb = 0;
  int rb = 0;
  for (int g = 0; g < MAXN; g++) {
    if (qgrp[g].empty()) continue;
    if (g % 2 == 1) reverse(begin(qgrp[g]), end(qgrp[g]));
    for (auto i : qgrp[g]) {
      while (lb > ql[i]) add_num(--lb);
      while (rb < qr[i]) add_num(rb++);
      while (rb > qr[i]) del_num(--rb);
      while (lb < ql[i]) del_num(lb++);
      ans[i] = qry_ans();
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> N >> M;
  make_fib();
  for (int i = 0; i < N; i++) cin >> arr[i];
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> ql[i] >> qr[i];
    ql[i]--;
  }
  make();
  calc();
  for (int i = 0; i < Q; i++) cout << ans[i] << "\n";
  return 0;
}