#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, a[N], l, r, k, i, x, lst[N], L[N], R[N], va[N];
long long la;
inline long long C2(int x) { return x > 0 ? 1ll * (x + 1) * x >> 1 : 0; }
inline long long F(int a, int b, int c, int d) {
  return C2(min(d, b) - max(c, a) + 1);
}
struct atom {
  int x1, x2, y1, y2, s3;
  long long s1, s2;
  inline void setv(const atom& pre) {
    x1 = max(x1, ++y1);
    y2 = min(y2 + 1, x2);
    s1 = pre.s1 + 1ll * (x2 - x1 + 1) * (y2 - y1 + 1) - C2(y2 - x1);
    s2 = pre.s2 + 1ll * (y1 - 1) * (x2 - x1 + 1);
    s3 = pre.s3 + x2 - x1 + 1;
  }
  inline long long calcv(int l, int r) const {
    int x3 = x2, y3 = y2, x4 = x1, y4 = y1;
    if (x2 == -1) x3 = i, y3 += y2 != -1, x4 = max(x4, y4 = y1 + 1);
    if (y2 == -1) y3 = min(i, x3), x4 = max(x4, y4 = y1 + 1);
    int a = max(l, x4), b = min(r, x3), c = max(l, y4), d = min(r, y3);
    return a <= b && c <= d
               ? 1ll * (b - a + 1) * (d - c + 1) - F(x4 + 1, y3, l + 1, r)
               : 0;
  }
};
vector<atom> v[N];
int T[N << 2], M;
bool bbb[N];
int ppp;
inline void mdy(int x, int v) {
  for (T[x += M] = v; x >>= 1; T[x] = min(T[x << 1], T[x << 1 | 1]))
    ;
}
inline int ask(int x) {
  int v = T[x += M];
  for (; x; x >>= 1)
    if (!(x & 1) && T[x ^ 1] < v) break;
  if (!x) return N;
  for (x ^= 1; x < M;) x = x << 1 | (T[x << 1] > v);
  return x - M;
}
inline void lk(int x, int y) {
  R[x] = y;
  L[x] = L[y];
  R[L[y]] = x;
  L[y] = x;
}
inline void ope(int x) {
  v[a[x]].push_back((atom){i, -1, x, R[x] == n + 1 ? -1 : R[x] - 1});
}
inline void clos(int u) {
  v[a[u]].back().x2 = i - 1;
  if (-1 == v[a[u]].back().y2) v[a[u]].back().y2 = i - 1;
  v[a[u]].back().setv(v[a[u]].size() == 1 ? (atom){} : *-- --v[a[u]].end());
}
inline void ins(int u) {
  int x = u, en = R[u], y;
  for (;; lk(x = y, en))
    if (y = ask(x), y > i || y == en) break;
  for (x = R[u]; x != en; x = R[x]) ope(x);
}
inline void del(int u) {
  mdy(u, N);
  R[L[u]] = R[u];
  L[R[u]] = L[u];
  clos(u);
  ins(L[u]);
}
inline long long query(const vector<atom>& v, int x, int y) {
  if (v.empty()) return 0;
  int l, r, m, p3, p4;
  long long ass;
  for (l = 0, r = v.size() - 1; l < r;) {
    m = l + r + 1 >> 1;
    if (v[m].x1 <= y)
      l = m;
    else
      r = m - 1;
  }
  p4 = l;
  ass = v[l].calcv(x, y);
  if (l == 0) return ass;
  for (l = 0, r = p4 - 1; l < r;) {
    m = l + r >> 1;
    if (x - 1 <= v[m].y2)
      r = m;
    else
      l = m + 1;
  }
  p3 = l;
  ass += v[l].calcv(x, y);
  if (l + 1 == p4 || v[l].y2 < x - 1) return ass;
  for (l = p3, r = p4 - 1; l < r;) {
    m = l + r + 1 >> 1;
    if (v[m].y1 < x)
      l = m;
    else
      r = m - 1;
  }
  ass += v[p4 - 1].s1 - v[l].s1;
  ass += v[l].s1 - v[p3].s1 + v[l].s2 - v[p3].s2 -
         1ll * (v[l].s3 - v[p3].s3) * (x - 1);
  return ass;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  ppp = 0;
  for (M = 1; M < n + 2; M <<= 1)
    ;
  R[0] = n + 1;
  int mn = N;
  ope(0);
  for (i = 1; i <= n; ++i) {
    bool fl = 0;
    cin >> a[i] >> l >> r >> k;
    a[i] = (a[i] + la) % (n + 1);
    l = (l + la) % i + 1;
    r = (r + la) % i + 1;
    if (l > r) swap(l, r);
    k = (k + la) % (n + 1);
    mdy(i, a[i]);
    bbb[a[i]] = 1;
    if (a[i] == ppp) {
      for (; bbb[ppp]; ++ppp)
        ;
      mdy(0, ppp);
      fl = 1;
    }
    if (x = lst[a[i]]) {
      mdy(x, N);
      if (R[x])
        L[R[x]] = L[x], R[L[x]] = R[x], clos(x), ins(L[x]), clos(L[x]),
        ope(L[x]), fl &= L[x];
    }
    if (fl) clos(0), ins(0), a[0] = ppp, ope(0);
    mn = min(mn, a[i]);
    if (a[i] == mn) ins(L[n + 1]);
    lst[a[i]] = i;
    cout << (la = query(v[k], l, r)) << '\n';
  }
  return 0;
}

### Prompt

Please formulate a CPP solution to the following problem:

You are given an array which is initially empty. You need to perform n operations of the given format: 

  * "a l r k": append a to the end of the array. After that count the number of integer pairs x, y such that l ≤ x ≤ y ≤ r and \operatorname{mex}(a_{x}, a_{x+1}, …, a_{y}) = k. 



The elements of the array are numerated from 1 in the order they are added to the array.

To make this problem more tricky we don't say your real parameters of the queries. Instead your are given a', l', r', k'. To get a, l, r, k on the i-th operation you need to perform the following: 

  * a := (a' + lans) mod(n + 1), 
  * l := (l' + lans) mod{i} + 1, 
  * r := (r' + lans) mod{i} + 1, 
  * if l > r swap l and r, 
  * k := (k' + lans) mod(n + 1), 

where lans is the answer to the previous operation, initially lans is equal to zero. i is the id of the operation, operations are numbered from 1.

The \operatorname{mex}(S), where S is a multiset of non-negative integers, is the smallest non-negative integer which does not appear in the set. For example, \operatorname{mex}(\{2, 2, 3\}) = 0 and \operatorname{mex} (\{0, 1, 4, 1, 6\}) = 2. 

Input

The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the array.

The next n lines contain the description of queries.

Each of them n lines contains four non-negative integers a', l', r', k' (0, ≤ a', l', r', k' ≤ 10^9), describing one operation.

Output

For each query print a single integer — the answer to this query.

Examples

Input


5
0 0 0 1
0 1 0 5
5 2 1 0
5 2 1 0
2 4 3 3


Output


1
1
2
6
3


Input


5
2 0 0 2
2 0 1 1
0 0 2 0
3 2 2 0
0 2 3 0


Output


0
0
3
0
0

Note

For the first example the decoded values of a, l, r, k are the following:

a_1=0,l_1=1,r_1=1,k_1=1

a_2=1,l_2=1,r_2=2,k_2=0

a_3=0,l_3=1,r_3=3,k_3=1

a_4=1,l_4=1,r_4=4,k_4=2

a_5=2,l_5=1,r_5=5,k_5=3

For the second example the decoded values of a, l, r, k are the following:

a_1=2,l_1=1,r_1=1,k_1=2

a_2=2,l_2=1,r_2=2,k_2=1

a_3=0,l_3=1,r_3=3,k_3=0

a_4=0,l_4=2,r_4=2,k_4=3

a_5=0,l_5=3,r_5=4,k_5=0

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, a[N], l, r, k, i, x, lst[N], L[N], R[N], va[N];
long long la;
inline long long C2(int x) { return x > 0 ? 1ll * (x + 1) * x >> 1 : 0; }
inline long long F(int a, int b, int c, int d) {
  return C2(min(d, b) - max(c, a) + 1);
}
struct atom {
  int x1, x2, y1, y2, s3;
  long long s1, s2;
  inline void setv(const atom& pre) {
    x1 = max(x1, ++y1);
    y2 = min(y2 + 1, x2);
    s1 = pre.s1 + 1ll * (x2 - x1 + 1) * (y2 - y1 + 1) - C2(y2 - x1);
    s2 = pre.s2 + 1ll * (y1 - 1) * (x2 - x1 + 1);
    s3 = pre.s3 + x2 - x1 + 1;
  }
  inline long long calcv(int l, int r) const {
    int x3 = x2, y3 = y2, x4 = x1, y4 = y1;
    if (x2 == -1) x3 = i, y3 += y2 != -1, x4 = max(x4, y4 = y1 + 1);
    if (y2 == -1) y3 = min(i, x3), x4 = max(x4, y4 = y1 + 1);
    int a = max(l, x4), b = min(r, x3), c = max(l, y4), d = min(r, y3);
    return a <= b && c <= d
               ? 1ll * (b - a + 1) * (d - c + 1) - F(x4 + 1, y3, l + 1, r)
               : 0;
  }
};
vector<atom> v[N];
int T[N << 2], M;
bool bbb[N];
int ppp;
inline void mdy(int x, int v) {
  for (T[x += M] = v; x >>= 1; T[x] = min(T[x << 1], T[x << 1 | 1]))
    ;
}
inline int ask(int x) {
  int v = T[x += M];
  for (; x; x >>= 1)
    if (!(x & 1) && T[x ^ 1] < v) break;
  if (!x) return N;
  for (x ^= 1; x < M;) x = x << 1 | (T[x << 1] > v);
  return x - M;
}
inline void lk(int x, int y) {
  R[x] = y;
  L[x] = L[y];
  R[L[y]] = x;
  L[y] = x;
}
inline void ope(int x) {
  v[a[x]].push_back((atom){i, -1, x, R[x] == n + 1 ? -1 : R[x] - 1});
}
inline void clos(int u) {
  v[a[u]].back().x2 = i - 1;
  if (-1 == v[a[u]].back().y2) v[a[u]].back().y2 = i - 1;
  v[a[u]].back().setv(v[a[u]].size() == 1 ? (atom){} : *-- --v[a[u]].end());
}
inline void ins(int u) {
  int x = u, en = R[u], y;
  for (;; lk(x = y, en))
    if (y = ask(x), y > i || y == en) break;
  for (x = R[u]; x != en; x = R[x]) ope(x);
}
inline void del(int u) {
  mdy(u, N);
  R[L[u]] = R[u];
  L[R[u]] = L[u];
  clos(u);
  ins(L[u]);
}
inline long long query(const vector<atom>& v, int x, int y) {
  if (v.empty()) return 0;
  int l, r, m, p3, p4;
  long long ass;
  for (l = 0, r = v.size() - 1; l < r;) {
    m = l + r + 1 >> 1;
    if (v[m].x1 <= y)
      l = m;
    else
      r = m - 1;
  }
  p4 = l;
  ass = v[l].calcv(x, y);
  if (l == 0) return ass;
  for (l = 0, r = p4 - 1; l < r;) {
    m = l + r >> 1;
    if (x - 1 <= v[m].y2)
      r = m;
    else
      l = m + 1;
  }
  p3 = l;
  ass += v[l].calcv(x, y);
  if (l + 1 == p4 || v[l].y2 < x - 1) return ass;
  for (l = p3, r = p4 - 1; l < r;) {
    m = l + r + 1 >> 1;
    if (v[m].y1 < x)
      l = m;
    else
      r = m - 1;
  }
  ass += v[p4 - 1].s1 - v[l].s1;
  ass += v[l].s1 - v[p3].s1 + v[l].s2 - v[p3].s2 -
         1ll * (v[l].s3 - v[p3].s3) * (x - 1);
  return ass;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  ppp = 0;
  for (M = 1; M < n + 2; M <<= 1)
    ;
  R[0] = n + 1;
  int mn = N;
  ope(0);
  for (i = 1; i <= n; ++i) {
    bool fl = 0;
    cin >> a[i] >> l >> r >> k;
    a[i] = (a[i] + la) % (n + 1);
    l = (l + la) % i + 1;
    r = (r + la) % i + 1;
    if (l > r) swap(l, r);
    k = (k + la) % (n + 1);
    mdy(i, a[i]);
    bbb[a[i]] = 1;
    if (a[i] == ppp) {
      for (; bbb[ppp]; ++ppp)
        ;
      mdy(0, ppp);
      fl = 1;
    }
    if (x = lst[a[i]]) {
      mdy(x, N);
      if (R[x])
        L[R[x]] = L[x], R[L[x]] = R[x], clos(x), ins(L[x]), clos(L[x]),
        ope(L[x]), fl &= L[x];
    }
    if (fl) clos(0), ins(0), a[0] = ppp, ope(0);
    mn = min(mn, a[i]);
    if (a[i] == mn) ins(L[n + 1]);
    lst[a[i]] = i;
    cout << (la = query(v[k], l, r)) << '\n';
  }
  return 0;
}

```