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>
#pragma GCC optimize("O2,Ofast,inline,unroll-all-loops,-ffast-math")
using namespace std;
template <class T>
inline void in(T &x) {
  x = 0;
  char c = getchar();
  bool f = 0;
  while (!isdigit(c)) f |= (c == '-'), c = getchar();
  while (isdigit(c)) x = x * 10 + (c ^ '0'), c = getchar();
  f ? x = -x : 0;
}
template <class T>
inline void out(T x, const char c = '\n') {
  static short st[40];
  short m = 0;
  if (x < 0) putchar('-'), x = -x;
  do st[++m] = x % 10, x /= 10;
  while (x);
  while (m) putchar(st[m--] | '0');
  putchar(c);
}
template <class T>
inline void err(const T &x, const char c = '\n') {
  cerr << x << c;
}
template <class T, class... Args>
inline void in(T &x, Args &...args) {
  in(x);
  in(args...);
}
template <class T, class... Args>
inline void out(const T &x, const Args &...args) {
  out(x, ' ');
  out(args...);
}
template <class T, class... Args>
inline void err(const T &x, const Args &...args) {
  err(x, ' ');
  err(args...);
}
template <class T>
inline void prt(T a, int n) {
  for (register int i = 0; i < n; ++i) out(a[i], i == n - 1 ? '\n' : ' ');
}
template <class T>
inline void clr(T a[], int n) {
  memset(a, 0, sizeof(T) * n);
}
template <class T>
inline void clr(T *a, T *b) {
  memset(a, 0, sizeof(T) * (b - a));
}
template <class T>
inline bool ckmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T>
inline bool ckmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
namespace i207M {
const int rt = 1;
const long long Inf = 1e16;
int n, cq;
long long P;
int a[1000005];
struct Node {
  long long l, r;
  int k;
  Node() {}
  Node(const long long _l, const long long _r, const int _k) {
    l = _l, r = _r, k = _k;
  }
};
auto cmpl = [](const Node &a, const Node &b) { return a.l < b.l; };
vector<Node> duan[1000005 * 4];
long long sum[1000005 * 4];
void build(int x, int l, int r) {
  if (l == r) {
    sum[x] = a[l];
    duan[x].emplace_back(Node(-Inf, P - sum[x] - 1, 0)),
        duan[x].emplace_back(Node(P - sum[x], Inf, 1));
    return;
  }
  int mid((l + r) / 2);
  build((x << 1), l, mid), build((x << 1 | 1), mid + 1, r);
  sum[x] = sum[(x << 1)] + sum[(x << 1 | 1)];
  const auto &L = duan[(x << 1)], R = duan[(x << 1 | 1)];
  static Node st[1000005];
  int tp = 0, p = 0;
  for (const auto &it : L) {
    long long mn = it.l + sum[(x << 1)] - it.k * P, mx = mn + it.r - it.l;
    while (p == ((int)R.size()) || (p && mn < R[p].l)) --p;
    while (p < ((int)R.size()) && R[p].l <= mx) {
      long long nl = max(R[p].l, mn), nr = min(R[p].r, mx);
      nl = nl - sum[(x << 1)] + it.k * P, nr = nr - sum[(x << 1)] + it.k * P;
      if (tp && st[tp].k == it.k + R[p].k)
        st[tp].r = nr;
      else
        st[++tp] = Node(nl, nr, it.k + R[p].k);
      ++p;
    }
  }
  duan[x].reserve(tp);
  for (register int i = 1; i <= tp; ++i) duan[x].emplace_back(st[i]);
  duan[x][0].l = -Inf, duan[x].back().r = Inf;
}
long long ans;
void ask(int x, int l, int r, int ql, int qr) {
  if (ql <= l && r <= qr) {
    const auto &it = *(
        upper_bound(duan[x].begin(), duan[x].end(), Node(ans, 0, 0), cmpl) - 1);
    ans = ans + sum[x] - it.k * P;
    return;
  }
  int mid((l + r) / 2);
  if (ql <= mid) ask((x << 1), l, mid, ql, qr);
  if (qr > mid) ask((x << 1 | 1), mid + 1, r, ql, qr);
}
signed main() {
  in(n, cq, P);
  for (register int i = 1; i <= n; ++i) in(a[i]);
  build(rt, 1, n);
  int l, r;
  while (cq--) {
    in(l, r);
    ans = 0;
    ask(rt, 1, n, l, r);
    out(ans);
  }
  return 0;
}
}  // namespace i207M
signed main() {
  i207M::main();
  return 0;
}