1148_H. Holy Diver 

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

Example input:
5
2 0 0 2
2 0 1 1
0 0 2 0
3 2 2 0
0 2 3 0

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


Example output:
                                                               0
                                                               0
                                                               3
                                                               0
                                                               0

                                                               1
                                                               1
                                                               2
                                                               6
                                                               3


Tags: data structures
Difficulty rating: 3500

Solution:
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
using ll = long long;
struct Node {
  int x1, x2, y1, y2;
  ll sum;
};
int a[N], lp[N], rp[N], tag[N], lst[N];
vector<Node> v[N];
void del(int x, int y) {
  if (y > tag[x])
    v[x].push_back({tag[x], y - 1, lp[x], rp[x],
                    (v[x].empty() ? 0ll : v[x].back().sum) +
                        (rp[x] - lp[x] + 1ll) * (y - tag[x])});
  tag[x] = 0;
}
void modify(int x, int l, int r, int y) {
  if (tag[x])
    del(x, y);
  else
    lp[x] = l;
  rp[x] = r;
  tag[x] = y;
}
ll query(int kk, int l, int r) {
  ll ret = 0;
  if (!v[kk].empty()) {
    int lt = 0, rt = v[kk].size(), tl, tr;
    while (lt < rt) {
      int mid = (lt + rt) >> 1;
      if (v[kk][mid].x2 >= l && v[kk][mid].y2 >= l)
        rt = mid;
      else
        lt = mid + 1;
    }
    tl = lt;
    lt = -1;
    rt = v[kk].size() - 1;
    while (lt < rt) {
      int mid = (lt + rt + 1) >> 1;
      if (v[kk][mid].x1 <= r && v[kk][mid].y1 <= r)
        lt = mid;
      else
        rt = mid - 1;
    }
    tr = lt;
    if (tl <= tr) {
      ret += v[kk][tr].sum - (tl ? v[kk][tl - 1].sum : 0);
      if (l > v[kk][tl].x1)
        ret -= ll(v[kk][tl].y2 - v[kk][tl].y1 + 1) * (l - v[kk][tl].x1);
      if (r < v[kk][tr].x2)
        ret -= ll(v[kk][tr].y2 - v[kk][tr].y1 + 1) * (v[kk][tr].x2 - r);
      if (v[kk][tl].y1 < l) {
        lt = tl;
        rt = v[kk].size() - 1;
        while (lt < rt) {
          int mid = (lt + rt + 1) >> 1;
          if (v[kk][mid].y1 < l)
            lt = mid;
          else
            rt = mid - 1;
        }
        ret -= ll(l - v[kk][tl].y1) *
               (min(v[kk][lt].x2, r) - max(v[kk][tl].x1, l) + 1);
      }
    }
  }
  if (tag[kk]) {
    int l1 = max(l, tag[kk]), r1 = r, l2 = max(l, lp[kk]), r2 = min(r, rp[kk]);
    if (l1 <= r1 && l2 <= r2) ret += ll(r1 - l1 + 1) * (r2 - l2 + 1);
  }
  return ret;
}
int n;
ll lst_ans;
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int l, r, kk;
    cin >> a[i] >> l >> r >> kk;
    a[i] = (a[i] + lst_ans) % (n + 1);
    l = (l + lst_ans) % i + 1;
    r = (r + lst_ans) % i + 1;
    if (l > r) swap(l, r);
    kk = (kk + lst_ans) % (n + 1);
    if (tag[a[i]]) {
      for (int j = a[i] + 1, r = rp[a[i]];; j++) {
        if (lst[j] < lp[a[i]]) {
          modify(j, lp[a[i]], r, i);
          break;
        } else if (lst[j] < r)
          modify(j, lst[j] + 1, r, i), r = lst[j];
      }
      del(a[i], i);
    }
    modify(!a[i], i, i, i);
    lst[a[i]] = i;
    cout << (lst_ans = query(kk, l, r)) << endl;
  }
  return 0;
}