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>
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 void chmin(T &x, const T y) {
  (x > y) && (x = y);
}
template <typename T>
inline void chmax(T &x, const T y) {
  (x < y) && (x = y);
}
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;
template <typename T = int>
inline T read() {
  T x = 0;
  int fu = 1;
  char c =
      (SS == TT && (TT = (SS = BB) + fread(BB, 1, DPAIRSIZ, stdin), SS == TT)
           ? EOF
           : *SS++);
  while (c > 57 || c < 48) {
    if (c == 45) fu = -1;
    c = (SS == TT && (TT = (SS = BB) + fread(BB, 1, DPAIRSIZ, stdin), SS == TT)
             ? EOF
             : *SS++);
  }
  while (c <= 57 && c >= 48) {
    x = x * 10 + c - 48;
    c = (SS == TT && (TT = (SS = BB) + fread(BB, 1, DPAIRSIZ, stdin), SS == TT)
             ? EOF
             : *SS++);
  }
  x *= fu;
  return x;
}
template <typename T>
inline void read(T &x) {
  x = 0;
  int fu = 1;
  char c =
      (SS == TT && (TT = (SS = BB) + fread(BB, 1, DPAIRSIZ, stdin), SS == TT)
           ? EOF
           : *SS++);
  while (c > 57 || c < 48) {
    if (c == 45) fu = -1;
    c = (SS == TT && (TT = (SS = BB) + fread(BB, 1, DPAIRSIZ, stdin), SS == TT)
             ? EOF
             : *SS++);
  }
  while (c <= 57 && c >= 48) {
    x = x * 10 + c - 48;
    c = (SS == TT && (TT = (SS = BB) + fread(BB, 1, DPAIRSIZ, stdin), SS == TT)
             ? EOF
             : *SS++);
  }
  x *= fu;
}
template <typename T>
inline void read(T *bg, T *ed) {
  while (bg != ed) read(*bg++);
}
inline void read(char &ch) {
  ch = (SS == TT && (TT = (SS = BB) + fread(BB, 1, DPAIRSIZ, stdin), SS == TT)
            ? EOF
            : *SS++);
  while (ch <= 32)
    ch = (SS == TT && (TT = (SS = BB) + fread(BB, 1, DPAIRSIZ, stdin), SS == TT)
              ? EOF
              : *SS++);
}
inline void read(char *s) {
  char ch =
      (SS == TT && (TT = (SS = BB) + fread(BB, 1, DPAIRSIZ, stdin), SS == TT)
           ? EOF
           : *SS++);
  while (ch <= 32)
    ch = (SS == TT && (TT = (SS = BB) + fread(BB, 1, DPAIRSIZ, stdin), SS == TT)
              ? EOF
              : *SS++);
  while (ch > 32)
    *s++ = ch,
    ch = (SS == TT && (TT = (SS = BB) + fread(BB, 1, DPAIRSIZ, stdin), SS == TT)
              ? EOF
              : *SS++);
  *s = '\0';
}
inline void sread(char *s) {
  char ch =
      (SS == TT && (TT = (SS = BB) + fread(BB, 1, DPAIRSIZ, stdin), SS == TT)
           ? EOF
           : *SS++);
  while (ch < 32)
    ch = (SS == TT && (TT = (SS = BB) + fread(BB, 1, DPAIRSIZ, stdin), SS == TT)
              ? EOF
              : *SS++);
  while (ch >= 32)
    *s++ = ch,
    ch = (SS == TT && (TT = (SS = BB) + fread(BB, 1, DPAIRSIZ, stdin), SS == TT)
              ? EOF
              : *SS++);
  *s = '\0';
}
inline void pread(char *&s) {
  char ch =
      (SS == TT && (TT = (SS = BB) + fread(BB, 1, DPAIRSIZ, stdin), SS == TT)
           ? EOF
           : *SS++);
  while (ch <= 32)
    ch = (SS == TT && (TT = (SS = BB) + fread(BB, 1, DPAIRSIZ, stdin), SS == TT)
              ? EOF
              : *SS++);
  while (ch > 32)
    *s++ = ch,
    ch = (SS == TT && (TT = (SS = BB) + fread(BB, 1, DPAIRSIZ, stdin), SS == TT)
              ? EOF
              : *SS++);
  *s = '\0';
}
inline void spread(char *&s) {
  char ch =
      (SS == TT && (TT = (SS = BB) + fread(BB, 1, DPAIRSIZ, stdin), SS == TT)
           ? EOF
           : *SS++);
  while (ch < 32)
    ch = (SS == TT && (TT = (SS = BB) + fread(BB, 1, DPAIRSIZ, stdin), SS == TT)
              ? EOF
              : *SS++);
  while (ch >= 32)
    *s++ = ch,
    ch = (SS == TT && (TT = (SS = BB) + fread(BB, 1, DPAIRSIZ, stdin), SS == TT)
              ? EOF
              : *SS++);
  *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);
}
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);
  print(args...);
}
template <typename... Args>
inline void printl(const char *ch, Args... args) {
  while (*ch != '\0') putcha(*(ch++));
  putcha(10);
  print(args...);
}
template <typename... Args>
inline void printl(char *ch, Args... args) {
  while (*ch != '\0') putcha(*(ch++));
  putcha(10);
  print(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;
int n, m, a[1000005];
long long s[1000005], p;
long long buf[1000005 << 4 | 5], *v[1000005 << 2],
    *ed = buf + sizeof(buf) / sizeof(long long),
    *fed = buf + (sizeof(buf) / sizeof(long long) >> 1);
inline long long *alloc(int siz) { return ed -= siz; }
inline long long *falloc(int siz) { return fed -= siz; }
const int alpha = 16;
inline void merge(long long *c, long long *x, long long *y, const long long w,
                  const int l1, const int l2) {
  for (int i = 0, j = 0; i < l1; ++i) {
    if (j == l2) --j;
    for (; j < l2; ++j) {
      if (w - p * i + x[i + 1] - 1 < y[j]) {
        if (j) --j;
        break;
      }
      c[i + j] = mx(x[i], y[j] - w + p * i);
    }
  }
}
void build(const int rt, const int l, const int r) {
  if (r - l + 1 <= alpha)
    v[rt] = falloc(r - l + 3);
  else
    v[rt] = alloc(r - l + 3);
  v[rt][0] = -0x3f3f3f3f3f3f3f3fll;
  v[rt][r - l + 2] = 0x3f3f3f3f3f3f3f3fll;
  if (l == r) return v[rt][1] = p - a[l], void();
  int mid = (l + r) >> 1;
  build(rt << 1, l, mid);
  build(rt << 1 | 1, mid + 1, r);
  merge(v[rt], v[rt << 1], v[rt << 1 | 1], s[mid] - s[l - 1], mid - l + 3,
        r - mid + 2);
}
inline void ADD(long long &x, const int y) {
  x += y;
  if (x >= p) x -= p;
}
long long query(const int rt, const int l, const int r, const int x,
                const int y, long long z) {
  if (x <= l && r <= y) {
    if (r - l + 1 <= alpha) {
      for (int i = (l); i <= (r); ++i) ADD(z, a[i]);
      return z;
    }
    return z + s[r] - s[l - 1] -
           p * (std ::upper_bound(v[rt], v[rt] + r - l + 3, z) - 1 - v[rt]);
  }
  int mid = (l + r) >> 1;
  if (y <= mid) return query(rt << 1, l, mid, x, y, z);
  if (x > mid) return query(rt << 1 | 1, mid + 1, r, x, y, z);
  return query(rt << 1 | 1, mid + 1, r, x, y, query(rt << 1, l, mid, x, y, z));
}
int main() {
  read(n, m, p);
  read(a + 1, a + n + 1);
  for (int i = (1); i <= (n); ++i) s[i] = s[i - 1] + a[i];
  build(1, 1, n);
  while (m--) {
    int l, r;
    read(l, r);
    print(query(1, 1, n, l, r, 0));
  }
}