Problem: Nauuo is a girl who loves coding.

One day she was solving a problem which requires to calculate a sum of some numbers modulo p.

She wrote the following code and got the verdict "Wrong answer".

<image>

She soon discovered the bug — the ModAdd function only worked for numbers in the range [0,p), but the numbers in the problem may be out of the range. She was curious about the wrong function, so she wanted to know the result of it.

However, the original code worked too slow, so she asked you to help her.

You are given an array a_1,a_2,…,a_n and a number p. Nauuo will make m queries, in each query, you are given l and r, and you have to calculate the results of Sum(a,l,r,p). You can see the definition of the Sum function in the pseudocode above.

Note that the integers won't overflow in the code above.

Input

The first line contains three integers n, m, p (1 ≤ n ≤ 10^6, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ p ≤ 10^9) — the length of the given array, the number of queries and the modulus. Note that the modulus is used only in the ModAdd function.

The second line contains n integers a_1,a_2,…,a_n (-10^9≤ a_i≤10^9) — the given array.

In the following m lines, each line contains two integers l, r (1≤ l≤ r≤ n) — you have to calculate the result of Sum(a,l,r,p).

Output

The output contains m integers to answer the queries in the given order.

Example

Input


4 5 6
7 2 -3 17
2 3
1 3
1 2
2 4
4 4


Output


-1
0
3
10
11

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
clock_t t = clock();
namespace my_std {
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
inline T rnd(T l, T r) {
  return uniform_int_distribution<T>(l, r)(rng);
}
template <typename T>
inline bool chkmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}
template <typename T>
inline bool chkmin(T &x, T y) {
  return x > y ? x = y, 1 : 0;
}
template <typename T>
inline void read(T &t) {
  t = 0;
  char f = 0, ch = getchar();
  double d = 0.1;
  while (ch > '9' || ch < '0') f |= (ch == '-'), ch = getchar();
  while (ch <= '9' && ch >= '0') t = t * 10 + ch - 48, ch = getchar();
  if (ch == '.') {
    ch = getchar();
    while (ch <= '9' && ch >= '0') t += d * (ch ^ 48), d *= 0.1, ch = getchar();
  }
  t = (f ? -t : t);
}
template <typename T, typename... Args>
inline void read(T &t, Args &...args) {
  read(t);
  read(args...);
}
char __sr[1 << 21], __z[20];
int __C = -1, __zz = 0;
inline void Ot() { fwrite(__sr, 1, __C + 1, stdout), __C = -1; }
inline void print(register int x) {
  if (__C > 1 << 20) Ot();
  if (x < 0) __sr[++__C] = '-', x = -x;
  while (__z[++__zz] = x % 10 + 48, x /= 10)
    ;
  while (__sr[++__C] = __z[__zz], --__zz)
    ;
  __sr[++__C] = '\n';
}
void file() {}
inline void chktime() {}
long long ksm(long long x, int y) {
  long long ret = 1;
  for (; y; y >>= 1, x = x * x)
    if (y & 1) ret = ret * x;
  return ret;
}
}  // namespace my_std
using namespace my_std;
int n;
long long P;
long long a[1110002];
struct hh {
  long long l, b;
};
vector<hh> tr[1110002 << 1];
long long calc(const vector<hh> &V, long long x) {
  int l = 0, r = (int)V.size() - 1, pos = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (x >= V[mid].l)
      pos = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  return x + V[pos].b;
}
vector<hh> merge(vector<hh> A, const vector<hh> &B) {
  int p = 0;
  vector<hh> ret;
  int aa = A.size(), bb = B.size();
  for (int i = 0; i <= (int)A.size() - 1;) {
    long long yl = A[i].l + A[i].b, yr;
    if (A[i].l == -1e17) yl = -1e17;
    if (i == aa - 1)
      yr = 1e17;
    else
      yr = A[i + 1].l - 1 + A[i].b;
    while (p && B[p].l > yl) --p;
    while (p < bb - 1 && B[p + 1].l - 1 < yl) ++p;
    long long r;
    if (p == bb - 1)
      r = 1e17;
    else
      r = B[p + 1].l - 1;
    if (r < yr)
      ((ret.empty() || ret.back().b != A[i].b + B[p].b)
       ? ret.push_back((hh){A[i].l, A[i].b + B[p].b}),
       0 : 0),
          A[i].l = r - A[i].b + 1;
    else
      ((ret.empty() || ret.back().b != A[i].b + B[p].b)
       ? ret.push_back((hh){A[i].l, A[i].b + B[p].b}),
       0 : 0),
          ++i;
  }
  return ret;
}
void build(int k, int l, int r) {
  if (l == r) {
    tr[k].push_back((hh){-(long long)1e17, a[l]}),
        tr[k].push_back((hh){P - a[l], a[l] - P});
    return;
  }
  int mid = (l + r) >> 1;
  build(k << 1, l, mid), build(k << 1 | 1, mid + 1, r);
  tr[k] = merge(tr[k << 1], tr[k << 1 | 1]);
}
long long query(int k, int l, int r, int x, int y, long long w) {
  if (x <= l && r <= y) return calc(tr[k], w);
  int mid = (l + r) >> 1;
  if (x <= mid) w = query(k << 1, l, mid, x, y, w);
  if (y > mid) w = query(k << 1 | 1, mid + 1, r, x, y, w);
  return w;
}
int main() {
  file();
  int Q;
  read(n, Q, P);
  for (int i = (1); i <= (n); i++) read(a[i]);
  build(1, 1, n);
  int l, r;
  while (Q--) read(l, r), printf("%I64d\n", (long long)query(1, 1, n, l, r, 0));
  return 0;
}