#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  int n, m, q, l, r;
  cin >> n >> m;
  vector<pair<int, int> > a(n);
  for (int i = 0; i < (n); ++i) {
    cin >> a[i].first;
    a[i].second = i;
  }
  sort(a.begin(), a.end());
  vector<int> s(n), bt(n);
  int idx = 0, uniq = 0, last = -1;
  while (idx < n) {
    if (last != a[idx].first) ++uniq;
    last = a[idx].first;
    bt[a[idx].second] = uniq - 1;
    s[uniq - 1] = a[idx].first % m;
    ++idx;
  }
  vector<long long> fib(uniq);
  fib[0] = 1;
  if (uniq > 1) fib[1] = 1;
  for (int i = 2; i < (uniq); ++i) fib[i] = (fib[i - 1] + fib[i - 2]) % m;
  cin >> q;
  vector<int> inc(uniq);
  for (int qq = 0; qq < (q); ++qq) {
    cin >> l >> r;
    fill(inc.begin(), inc.end(), 0);
    for (int i = l - 1; i < (r); ++i) inc[bt[i]] = 1;
    long long sum = 0;
    int idxf = 0;
    for (int i = 0; i < (uniq); ++i) {
      sum += inc[i] * s[i] * fib[idxf];
      idxf += inc[i];
    }
    cout << sum % m << '\n';
  }
}
