#include <bits/stdc++.h>
using namespace std;
const int maxn = 240000;
struct rec {
  int x1, x2, y1, y2;
  long long s;
  rec() { s = 0; }
  rec(int _x1, int _x2, int _y1, int _y2, long long _s) {
    x1 = _x1;
    x2 = _x2;
    y1 = _y1;
    y2 = _y2;
    s = _s;
  }
};
int a[maxn], L[maxn], R[maxn], T[maxn], p[maxn];
vector<rec> v[maxn];
void remove(int x, int t) {
  long long s = 0;
  if (not v[x].empty()) s = v[x].back().s;
  if (t > T[x])
    v[x].push_back(
        rec(T[x], t - 1, L[x], R[x], s + 1LL * (R[x] - L[x] + 1) * (t - T[x])));
  T[x] = 0;
}
void add(int x, int xL, int xR, int t) {
  if (T[x])
    remove(x, t);
  else
    L[x] = xL;
  R[x] = xR;
  T[x] = t;
}
long long query(int k, int kL, int kR, int t) {
  long long res = 0;
  int bL, bR, bM;
  if (not v[k].empty()) {
    int L, R;
    for (bL = 0, bR = v[k].size() - 1; bL < bR;) {
      bM = (bL + bR) >> 1;
      if (v[k][bM].x2 < kL or v[k][bM].y2 < kL)
        bL = bM + 1;
      else
        bR = bM;
    }
    L = bL;
    for (bL = 0, bR = v[k].size() - 1; bL < bR;) {
      bM = (bL + bR + 1) >> 1;
      if (v[k][bM].x1 > kR or v[k][bM].y1 > kR)
        bR = bM - 1;
      else
        bL = bM;
    }
    R = bL;
    if (L <= R and v[k][L].x2 >= kL and v[k][L].y2 >= kL and
        v[k][R].x1 <= kR and v[k][R].y1 <= kR) {
      res += v[k][R].s - (L ? v[k][L - 1].s : 0);
      if (kL > v[k][L].x1)
        res -= 1LL * (v[k][L].y2 - v[k][L].y1 + 1) * (kL - v[k][L].x1);
      if (kR < v[k][R].x2)
        res -= 1LL * (v[k][R].y2 - v[k][R].y1 + 1) * (v[k][R].x2 - kR);
      if (v[k][L].y1 < kL) {
        for (bL = L, bR = v[k].size() - 1; bL < bR;) {
          bM = (bL + bR + 1) >> 1;
          if (v[k][bM].y1 < kL)
            bL = bM;
          else
            bR = bM - 1;
        }
        res -= 1LL * (kL - v[k][L].y1) *
               (min(v[k][bL].x2, kR) - max(v[k][L].x1, kL) + 1);
      }
    }
  }
  if (T[k]) {
    int x1 = max(kL, T[k]);
    int x2 = min(kR, t);
    int y1 = max(kL, L[k]);
    int y2 = min(kR, R[k]);
    if (x1 <= x2 and y1 <= y2) res += 1LL * (x2 - x1 + 1) * (y2 - y1 + 1);
  }
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  long long lans = 0;
  for (int i = 1; i <= n; i += 1) {
    int l, r, k;
    cin >> a[i] >> l >> r >> k;
    a[i] = (a[i] + lans) % (n + 1);
    l = (l + lans) % i + 1;
    r = (r + lans) % i + 1;
    if (l > r) swap(l, r);
    k = (k + lans) % (n + 1);
    if (T[a[i]]) {
      int RR = R[a[i]];
      for (int x = a[i] + 1;; x += 1)
        if (p[x] < L[a[i]]) {
          add(x, L[a[i]], RR, i);
          break;
        } else if (p[x] + 1 <= RR) {
          add(x, p[x] + 1, RR, i);
          RR = p[x];
        }
      remove(a[i], i);
    }
    add(!a[i], i, i, i);
    p[a[i]] = i;
    cout << (lans = query(k, l, r, i)) << "\n";
  }
}

### Prompt

Generate 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 maxn = 240000;
struct rec {
  int x1, x2, y1, y2;
  long long s;
  rec() { s = 0; }
  rec(int _x1, int _x2, int _y1, int _y2, long long _s) {
    x1 = _x1;
    x2 = _x2;
    y1 = _y1;
    y2 = _y2;
    s = _s;
  }
};
int a[maxn], L[maxn], R[maxn], T[maxn], p[maxn];
vector<rec> v[maxn];
void remove(int x, int t) {
  long long s = 0;
  if (not v[x].empty()) s = v[x].back().s;
  if (t > T[x])
    v[x].push_back(
        rec(T[x], t - 1, L[x], R[x], s + 1LL * (R[x] - L[x] + 1) * (t - T[x])));
  T[x] = 0;
}
void add(int x, int xL, int xR, int t) {
  if (T[x])
    remove(x, t);
  else
    L[x] = xL;
  R[x] = xR;
  T[x] = t;
}
long long query(int k, int kL, int kR, int t) {
  long long res = 0;
  int bL, bR, bM;
  if (not v[k].empty()) {
    int L, R;
    for (bL = 0, bR = v[k].size() - 1; bL < bR;) {
      bM = (bL + bR) >> 1;
      if (v[k][bM].x2 < kL or v[k][bM].y2 < kL)
        bL = bM + 1;
      else
        bR = bM;
    }
    L = bL;
    for (bL = 0, bR = v[k].size() - 1; bL < bR;) {
      bM = (bL + bR + 1) >> 1;
      if (v[k][bM].x1 > kR or v[k][bM].y1 > kR)
        bR = bM - 1;
      else
        bL = bM;
    }
    R = bL;
    if (L <= R and v[k][L].x2 >= kL and v[k][L].y2 >= kL and
        v[k][R].x1 <= kR and v[k][R].y1 <= kR) {
      res += v[k][R].s - (L ? v[k][L - 1].s : 0);
      if (kL > v[k][L].x1)
        res -= 1LL * (v[k][L].y2 - v[k][L].y1 + 1) * (kL - v[k][L].x1);
      if (kR < v[k][R].x2)
        res -= 1LL * (v[k][R].y2 - v[k][R].y1 + 1) * (v[k][R].x2 - kR);
      if (v[k][L].y1 < kL) {
        for (bL = L, bR = v[k].size() - 1; bL < bR;) {
          bM = (bL + bR + 1) >> 1;
          if (v[k][bM].y1 < kL)
            bL = bM;
          else
            bR = bM - 1;
        }
        res -= 1LL * (kL - v[k][L].y1) *
               (min(v[k][bL].x2, kR) - max(v[k][L].x1, kL) + 1);
      }
    }
  }
  if (T[k]) {
    int x1 = max(kL, T[k]);
    int x2 = min(kR, t);
    int y1 = max(kL, L[k]);
    int y2 = min(kR, R[k]);
    if (x1 <= x2 and y1 <= y2) res += 1LL * (x2 - x1 + 1) * (y2 - y1 + 1);
  }
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  long long lans = 0;
  for (int i = 1; i <= n; i += 1) {
    int l, r, k;
    cin >> a[i] >> l >> r >> k;
    a[i] = (a[i] + lans) % (n + 1);
    l = (l + lans) % i + 1;
    r = (r + lans) % i + 1;
    if (l > r) swap(l, r);
    k = (k + lans) % (n + 1);
    if (T[a[i]]) {
      int RR = R[a[i]];
      for (int x = a[i] + 1;; x += 1)
        if (p[x] < L[a[i]]) {
          add(x, L[a[i]], RR, i);
          break;
        } else if (p[x] + 1 <= RR) {
          add(x, p[x] + 1, RR, i);
          RR = p[x];
        }
      remove(a[i], i);
    }
    add(!a[i], i, i, i);
    p[a[i]] = i;
    cout << (lans = query(k, l, r, i)) << "\n";
  }
}

```