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(2, 3, "Ofast")
#pragma GCC target( \
    "sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
const double pi = 3.141592653589793238462643383279;
namespace MyMinMax {
template <typename T>
inline T mn(const T x, const T y) {
  return x < y ? x : y;
}
template <typename T>
inline T mx(const T x, const T y) {
  return x > y ? x : y;
}
template <typename T>
inline bool chmin(T &x, const T y) {
  return (x > y) && ((x = y), 1);
}
template <typename T>
inline bool chmax(T &x, const T y) {
  return (x < y) && ((x = y), 1);
}
template <typename T, typename... Args>
inline T mx(const T x, const Args... args) {
  return mx(x, mx(args...));
}
template <typename T, typename... Args>
inline T mn(const T x, const Args... args) {
  return mn(x, mn(args...));
}
}  // namespace MyMinMax
using namespace MyMinMax;
namespace IO {
const int DPAIRSIZ = 1 << 18;
char BB[DPAIRSIZ], *SS = BB, *TT = BB;
inline char getcha() {
  return SS == TT && (TT = (SS = BB) + fread(BB, 1, DPAIRSIZ, stdin), SS == TT)
             ? EOF
             : *SS++;
}
template <typename T = int>
inline T read() {
  T x = 0;
  int fu = 1;
  char c = getcha();
  while (c > 57 || c < 48) {
    if (c == 45) fu = -1;
    c = getcha();
  }
  while (c <= 57 && c >= 48) {
    x = x * 10 + c - 48;
    c = getcha();
  }
  x *= fu;
  return x;
}
template <typename T>
inline void read(T &x) {
  x = 0;
  int fu = 1;
  char c = getcha();
  while (c > 57 || c < 48) {
    if (c == 45) fu = -1;
    c = getcha();
  }
  while (c <= 57 && c >= 48) {
    x = x * 10 + c - 48;
    c = getcha();
  }
  x *= fu;
}
template <typename T>
inline void read(T *bg, T *ed) {
  while (bg != ed) read(*bg++);
}
inline void read(char &ch) {
  ch = getcha();
  while (ch <= 32) ch = getcha();
}
inline void read(char *s) {
  char ch = getcha();
  while (ch <= 32) ch = getcha();
  while (ch > 32) *s++ = ch, ch = getcha();
  *s = '\0';
}
inline void sread(char *s) {
  char ch = getcha();
  while (ch < 32) ch = getcha();
  while (ch >= 32) *s++ = ch, ch = getcha();
  *s = '\0';
}
inline void pread(char *&s) {
  char ch = getcha();
  while (ch <= 32) ch = getcha();
  while (ch > 32) *s++ = ch, ch = getcha();
  *s = '\0';
}
inline void spread(char *&s) {
  char ch = getcha();
  while (ch < 32) ch = getcha();
  while (ch >= 32) *s++ = ch, ch = getcha();
  *s = '\0';
}
template <typename T, typename... Args>
inline void read(T &x, Args &...args) {
  read(x);
  read(args...);
}
char out[DPAIRSIZ], *Out = out;
inline void putcha(char x) {
  *Out++ = x;
  if (Out - out >= (DPAIRSIZ)) fwrite(out, 1, Out - out, stdout), Out = out;
}
template <typename T>
inline void fprint(T x) {
  if (x < 0) putcha(45), x = -x;
  if (x > 9) fprint(x / 10);
  putcha(x % 10 + 48);
}
inline void print() { putcha(10); }
template <typename T>
inline void print(T x) {
  fprint(x);
  putcha(10);
}
inline void print(char *ch) {
  while (*ch != '\0') putcha(*(ch++));
  putcha(10);
}
inline void put(char *ch) {
  while (*ch != '\0') putcha(*(ch++));
}
inline void print(const char *ch) {
  while (*ch != '\0') putcha(*(ch++));
  putcha(10);
}
inline void put(const char *ch) {
  while (*ch != '\0') putcha(*(ch++));
}
template <typename T, typename... Args>
inline void print(T x, Args... args) {
  fprint(x);
  putcha(32);
  print(args...);
}
template <typename... Args>
inline void print(const char *ch, Args... args) {
  while (*ch != '\0') putcha(*(ch++));
  putcha(32);
  print(args...);
}
template <typename... Args>
inline void print(char *ch, Args... args) {
  while (*ch != '\0') putcha(*(ch++));
  putcha(32);
  print(args...);
}
template <typename T, typename... Args>
inline void printl(T x, Args... args) {
  fprint(x);
  putcha(10);
  printl(args...);
}
template <typename... Args>
inline void printl(const char *ch, Args... args) {
  while (*ch != '\0') putcha(*(ch++));
  putcha(10);
  printl(args...);
}
template <typename... Args>
inline void printl(char *ch, Args... args) {
  while (*ch != '\0') putcha(*(ch++));
  putcha(10);
  printl(args...);
}
template <typename T>
inline void sprint(T x) {
  fprint(x);
  putcha(32);
}
template <typename T, typename... Args>
inline void sprint(T x, Args... args) {
  fprint(x);
  putcha(32);
  sprint(args...);
}
template <typename T>
inline void sprint(T *bg, T *ed) {
  while (bg != ed) sprint(*bg++);
}
template <typename T>
inline void print(T *bg, T *ed) {
  while (bg != ed) sprint(*bg++);
  putcha(10);
}
template <typename T>
inline void printl(T *bg, T *ed) {
  while (bg != ed) print(*bg++);
}
class AutoFlush {
 public:
  ~AutoFlush() { fwrite(out, 1, Out - out, stdout); }
} __AutoFlush;
}  // namespace IO
using namespace IO;
const int INF = 0x3f3f3f3f;
const long long INFll = 0x3f3f3f3f3f3f3f3fll;
const int MAXN = 3e4 + 5;
int f[MAXN], m, a[MAXN], n, q;
inline void init() {
  f[0] = f[1] = 1;
  for (int i = (2); i <= (n); ++i)
    ((f[i] = f[i - 1] + f[i - 2]) >= m) && (f[i] -= m);
}
int b[MAXN];
namespace DPair {
int cnt1[256], cnt2[256], cnt3[256], cnt4[256], A[MAXN];
inline void RadixSort(int *a, const int N) {
  for (int i = 0; i < N - 3; i += 4) {
    const int x[4] = {a[i], a[i + 1], a[i + 2], a[i + 3]};
    ++cnt1[x[0] & 255];
    ++cnt1[x[1] & 255];
    ++cnt1[x[2] & 255];
    ++cnt1[x[3] & 255];
    ++cnt2[x[0] >> 8 & 255];
    ++cnt2[x[1] >> 8 & 255];
    ++cnt2[x[2] >> 8 & 255];
    ++cnt2[x[3] >> 8 & 255];
    ++cnt3[x[0] >> 16 & 255];
    ++cnt3[x[1] >> 16 & 255];
    ++cnt3[x[2] >> 16 & 255];
    ++cnt3[x[3] >> 16 & 255];
    ++cnt4[x[0] >> 24 & 255];
    ++cnt4[x[1] >> 24 & 255];
    ++cnt4[x[2] >> 24 & 255];
    ++cnt4[x[3] >> 24 & 255];
  }
  for (int i = (1); i < (256); ++i) {
    cnt1[i] += cnt1[i - 1];
    cnt2[i] += cnt2[i - 1];
    cnt3[i] += cnt3[i - 1];
    cnt4[i] += cnt4[i - 1];
  }
  for (int i = N - 1; i >= 3; i -= 4) {
    const int x[4] = {a[i], a[i - 1], a[i - 2], a[i - 3]};
    A[--cnt1[x[0] & 255]] = x[0];
    A[--cnt1[x[1] & 255]] = x[1];
    A[--cnt1[x[2] & 255]] = x[2];
    A[--cnt1[x[3] & 255]] = x[3];
  }
  for (int i = N - 1; i >= 3; i -= 4) {
    const int x[4] = {A[i], A[i - 1], A[i - 2], A[i - 3]};
    a[--cnt2[x[0] >> 8 & 255]] = x[0];
    a[--cnt2[x[1] >> 8 & 255]] = x[1];
    a[--cnt2[x[2] >> 8 & 255]] = x[2];
    a[--cnt2[x[3] >> 8 & 255]] = x[3];
  }
  for (int i = N - 1; i >= 3; i -= 4) {
    const int x[4] = {a[i], a[i - 1], a[i - 2], a[i - 3]};
    A[--cnt3[x[0] >> 16 & 255]] = x[0];
    A[--cnt3[x[1] >> 16 & 255]] = x[1];
    A[--cnt3[x[2] >> 16 & 255]] = x[2];
    A[--cnt3[x[3] >> 16 & 255]] = x[3];
  }
  for (int i = N - 1; i >= 3; i -= 4) {
    const int x[4] = {A[i], A[i - 1], A[i - 2], A[i - 3]};
    a[--cnt4[x[0] >> 24 & 255]] = x[0];
    a[--cnt4[x[1] >> 24 & 255]] = x[1];
    a[--cnt4[x[2] >> 24 & 255]] = x[2];
    a[--cnt4[x[3] >> 24 & 255]] = x[3];
  }
  memset(cnt1, 0, sizeof(cnt1));
  memset(cnt2, 0, sizeof(cnt2));
  memset(cnt3, 0, sizeof(cnt3));
  memset(cnt4, 0, sizeof(cnt4));
}
inline void RadixSort(int *a, int *_a) {
  int N = _a - a;
  while (N & 3) a[N++] = INF;
  RadixSort(a, N);
}
}  // namespace DPair
signed main() {
  read(n, m);
  read(a + 1, a + n + 1);
  init();
  read(q);
  while (q--) {
    int l, r;
    read(l, r);
    const int len = (r - l + 1);
    memcpy(b, a + l, len * sizeof(int));
    DPair ::RadixSort(b, b + len);
    const int p = std ::unique(b, b + len) - b;
    int ans[4] = {0, 0, 0, 0};
    for (int i = 0; i < p - 3; i += 4) {
      ans[0] = (1ll * b[i] * f[i] + ans[0]) % m;
      ans[1] = (1ll * b[i + 1] * f[i + 1] + ans[1]) % m;
      ans[2] = (1ll * b[i + 2] * f[i + 2] + ans[2]) % m;
      ans[3] = (1ll * b[i + 3] * f[i + 3] + ans[3]) % m;
    }
    switch (p & 3) {
      case 3:
        ans[0] = (1ll * b[p - 3] * f[p - 3] + ans[0]) % m;
      case 2:
        ans[0] = (1ll * b[p - 2] * f[p - 2] + ans[0]) % m;
      case 1:
        ans[0] = (1ll * b[p - 1] * f[p - 1] + ans[0]) % m;
    }
    print((ans[0] + ans[1] + ans[2] + ans[3]) % m);
  }
}