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

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const int N = 200100;
struct data {
  int x1, x2, y1, y2;
  long long sum;
  data(int x1 = 0, int x2 = 0, int y1 = 0, int y2 = 0, long long sum = 0)
      : x1(x1), x2(x2), y1(y1), y2(y2), sum(sum) {}
};
int a[N], le[N], ri[N], tag[N], lst[N];
vector<data> vec[N];
void del(int x, int y) {
  if (y > tag[x])
    vec[x].emplace_back(tag[x], y - 1, le[x], ri[x],
                        (vec[x].empty() ? 0ll : vec[x].back().sum) +
                            (long long)(ri[x] - le[x] + 1) * (y - tag[x]));
  tag[x] = 0;
}
void mdf(int x, int l, int r, int y) {
  if (tag[x])
    del(x, y);
  else
    le[x] = l;
  ri[x] = r;
  tag[x] = y;
}
long long query(int K, int l, int r) {
  long long ret = 0;
  if (!vec[K].empty()) {
    int L = 0, R = vec[K].size(), tL, tR;
    while (L < R) {
      int mid = (L + R) / 2;
      if (vec[K][mid].x2 >= l && vec[K][mid].y2 >= l)
        R = mid;
      else
        L = mid + 1;
    }
    tL = L;
    L = -1;
    R = vec[K].size() - 1;
    while (L < R) {
      int mid = (L + R + 1) / 2;
      if (vec[K][mid].x1 <= r && vec[K][mid].y1 <= r)
        L = mid;
      else
        R = mid - 1;
    }
    tR = L;
    if (tL <= tR) {
      assert(vec[K][tL].x2 >= l && vec[K][tL].y2 >= l && vec[K][tR].x1 <= r &&
             vec[K][tR].y1 <= r);
      ret += vec[K][tR].sum - (tL ? vec[K][tL - 1].sum : 0);
      if (l > vec[K][tL].x1)
        ret -= (long long)(vec[K][tL].y2 - vec[K][tL].y1 + 1) *
               (l - vec[K][tL].x1);
      if (r < vec[K][tR].x2)
        ret -= (long long)(vec[K][tR].y2 - vec[K][tR].y1 + 1) *
               (vec[K][tR].x2 - r);
      if (vec[K][tL].y1 < l) {
        L = tL;
        R = vec[K].size() - 1;
        while (L < R) {
          int mid = (L + R + 1) / 2;
          if (vec[K][mid].y1 < l)
            L = mid;
          else
            R = mid - 1;
        }
        ret -= (long long)(l - vec[K][tL].y1) *
               (min(vec[K][L].x2, r) - max(vec[K][tL].x1, l) + 1);
      }
    }
  }
  if (tag[K]) {
    int l1 = max(l, tag[K]), r1 = r, l2 = max(l, le[K]), r2 = min(r, ri[K]);
    if (l1 <= r1 && l2 <= r2) ret += (long long)(r1 - l1 + 1) * (r2 - l2 + 1);
  }
  return ret;
}
int n;
long long last_ans;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    int l, r, K;
    cin >> a[i] >> l >> r >> K;
    a[i] = (a[i] + last_ans) % (n + 1);
    l = (l + last_ans) % i + 1;
    r = (r + last_ans) % i + 1;
    if (l > r) swap(l, r);
    K = (K + last_ans) % (n + 1);
    if (tag[a[i]]) {
      for (int j = a[i] + 1, r = ri[a[i]];; ++j)
        if (lst[j] < le[a[i]]) {
          mdf(j, le[a[i]], r, i);
          break;
        } else if (lst[j] < r) {
          mdf(j, lst[j] + 1, r, i);
          r = lst[j];
        }
      del(a[i], i);
    }
    mdf(!a[i], i, i, i);
    lst[a[i]] = i;
    cout << (last_ans = query(K, l, r)) << '\n';
  }
  return 0;
}