#include <bits/stdc++.h>
using namespace std;
int n;
const int maxn = 200100;
struct segment_node {
  int be, en;
  int minv;
};
struct segment_tree {
  int l;
  segment_node tree[200100 * 4];
  inline int gleft(int no) { return no << 1; }
  inline int gright(int no) { return (no << 1) + 1; }
  inline int gfa(int no) { return no >> 1; }
  inline segment_tree() { l = 0; }
  void build(int no, int l, int r, int orig = 0) {
    if (l > r) r = l;
    if (l == r) {
      tree[no].be = tree[no].en = l;
      tree[no].minv = orig;
      return;
    }
    tree[no].be = l;
    tree[no].en = r;
    int mid = (l + r) / 2;
    build(gleft(no), l, mid, orig);
    build(gright(no), mid + 1, r, orig);
    tree[no].minv = min(tree[gleft(no)].minv, tree[gright(no)].minv);
  }
  void upd(int loc, int no, int val) {
    if (loc <= tree[no].be && loc >= tree[no].en) {
      tree[no].minv = val;
      return;
    }
    int mid = (tree[no].be + tree[no].en) >> 1;
    if (loc <= mid)
      upd(loc, gleft(no), val);
    else
      upd(loc, gright(no), val);
    tree[no].minv = min(tree[gleft(no)].minv, tree[gright(no)].minv);
  }
  int getnxt(int l, int r, int no, int val) {
    if (l > r) return 0;
    if (l <= tree[no].be && r >= tree[no].en && tree[no].minv > val) return -1;
    if (tree[no].be == tree[no].en) return tree[no].be;
    int ans = -1;
    int mid = (tree[no].be + tree[no].en) >> 1;
    if (r >= tree[no].be && l <= mid) {
      ans = getnxt(l, r, gleft(no), val);
      if (ans != -1) return ans;
    }
    if (r >= mid + 1 && l <= tree[no].en) ans = getnxt(l, r, gright(no), val);
    return ans;
  }
} sgt1;
vector<int> mark_t[maxn];
vector<pair<int, int> > mark_range[maxn];
vector<long long> sum[maxn];
set<int> st;
int last[maxn];
int orig[maxn];
inline void UpdSum(int idx) {
  auto t2 = mark_t[idx][mark_t[idx].size() - 1],
       t1 = mark_t[idx][mark_t[idx].size() - 2];
  sum[idx].push_back(sum[idx].back());
  auto range = mark_range[idx][mark_range[idx].size() - 2];
  sum[idx][sum[idx].size() - 1] += (t2 - t1) * (range.second - range.first);
}
int main() {
  scanf("%d", &n);
  long long ans = 0;
  memset(last, 0, sizeof(last));
  sgt1.build(1, 0, n + 1);
  for (int i = 0; i <= n + 1; i++) {
    mark_t[i].push_back(0);
    mark_range[i].push_back(make_pair(0, 0));
    sum[i].push_back(0);
    sgt1.upd(i, 1, 0);
    st.insert(i);
  }
  for (int i = 1; i <= n; i++) {
    int a, l, r, k;
    scanf("%d%d%d%d", &a, &l, &r, &k);
    orig[i] = a;
    a = (a + ans) % (n + 1);
    l = (l + ans) % i + 1;
    r = (r + ans) % i + 1;
    k = (k + ans) % (n + 1);
    if (l > r) swap(l, r);
    sgt1.upd(a, 1, i);
    last[a] = i;
    if (a != 0) {
      mark_range[0].push_back(mark_range[0].back());
      mark_t[0].push_back(i);
      mark_range[0][mark_range[0].size() - 1].second = i;
      UpdSum(0);
    }
    mark_t[a].push_back(i);
    mark_range[a].push_back(make_pair(i, i));
    UpdSum(a);
    auto it = st.find(a);
    if (it != st.end()) {
      auto bef = it;
      if (it != st.begin()) {
        bef--;
        st.erase(it);
        it = bef;
        it++;
      } else {
        it++;
      }
      while (bef != it) {
        int nxt = sgt1.getnxt(*bef + 1, n + 1, 1, last[*bef]);
        st.insert(nxt);
        mark_t[nxt].push_back(i);
        mark_range[nxt].push_back(
            make_pair(mark_range[nxt].back().first, last[*bef]));
        UpdSum(nxt);
        bef++;
      }
    }
    auto st = lower_bound(
                  mark_range[k].begin(), mark_range[k].end(), make_pair(-1, l),
                  [&](const pair<int, int> &a, const pair<int, int> &b) {
                    if (a.second != b.second) return a.second < b.second;
                    return a.first < b.first;
                  }) -
              mark_range[k].begin();
    auto en =
        upper_bound(mark_t[k].begin(), mark_t[k].end(), r) - mark_t[k].begin();
    auto mid = lower_bound(mark_range[k].begin(), mark_range[k].end(),
                           make_pair(l - 1, l)) -
               mark_range[k].begin();
    ans = 0;
    if (en > st) {
      en--;
      mid = min(mid, en);
      if (en > st) {
        ans = sum[k][en] - sum[k][st];
        if (mark_range[k][st].first < l - 1) {
          ans -= 1ll * (mark_t[k][mid] - mark_t[k][st]) *
                 (l - 1 - mark_range[k][st].first);
        }
      }
      ans += 1ll * (r - mark_t[k][en] + 1) *
             (mark_range[k][en].second - max(l - 1, mark_range[k][en].first));
    }
    printf("%I64d\n", ans);
  }
  return 0;
}

### Prompt

Please provide a Cpp coded solution to the problem described below:

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;
int n;
const int maxn = 200100;
struct segment_node {
  int be, en;
  int minv;
};
struct segment_tree {
  int l;
  segment_node tree[200100 * 4];
  inline int gleft(int no) { return no << 1; }
  inline int gright(int no) { return (no << 1) + 1; }
  inline int gfa(int no) { return no >> 1; }
  inline segment_tree() { l = 0; }
  void build(int no, int l, int r, int orig = 0) {
    if (l > r) r = l;
    if (l == r) {
      tree[no].be = tree[no].en = l;
      tree[no].minv = orig;
      return;
    }
    tree[no].be = l;
    tree[no].en = r;
    int mid = (l + r) / 2;
    build(gleft(no), l, mid, orig);
    build(gright(no), mid + 1, r, orig);
    tree[no].minv = min(tree[gleft(no)].minv, tree[gright(no)].minv);
  }
  void upd(int loc, int no, int val) {
    if (loc <= tree[no].be && loc >= tree[no].en) {
      tree[no].minv = val;
      return;
    }
    int mid = (tree[no].be + tree[no].en) >> 1;
    if (loc <= mid)
      upd(loc, gleft(no), val);
    else
      upd(loc, gright(no), val);
    tree[no].minv = min(tree[gleft(no)].minv, tree[gright(no)].minv);
  }
  int getnxt(int l, int r, int no, int val) {
    if (l > r) return 0;
    if (l <= tree[no].be && r >= tree[no].en && tree[no].minv > val) return -1;
    if (tree[no].be == tree[no].en) return tree[no].be;
    int ans = -1;
    int mid = (tree[no].be + tree[no].en) >> 1;
    if (r >= tree[no].be && l <= mid) {
      ans = getnxt(l, r, gleft(no), val);
      if (ans != -1) return ans;
    }
    if (r >= mid + 1 && l <= tree[no].en) ans = getnxt(l, r, gright(no), val);
    return ans;
  }
} sgt1;
vector<int> mark_t[maxn];
vector<pair<int, int> > mark_range[maxn];
vector<long long> sum[maxn];
set<int> st;
int last[maxn];
int orig[maxn];
inline void UpdSum(int idx) {
  auto t2 = mark_t[idx][mark_t[idx].size() - 1],
       t1 = mark_t[idx][mark_t[idx].size() - 2];
  sum[idx].push_back(sum[idx].back());
  auto range = mark_range[idx][mark_range[idx].size() - 2];
  sum[idx][sum[idx].size() - 1] += (t2 - t1) * (range.second - range.first);
}
int main() {
  scanf("%d", &n);
  long long ans = 0;
  memset(last, 0, sizeof(last));
  sgt1.build(1, 0, n + 1);
  for (int i = 0; i <= n + 1; i++) {
    mark_t[i].push_back(0);
    mark_range[i].push_back(make_pair(0, 0));
    sum[i].push_back(0);
    sgt1.upd(i, 1, 0);
    st.insert(i);
  }
  for (int i = 1; i <= n; i++) {
    int a, l, r, k;
    scanf("%d%d%d%d", &a, &l, &r, &k);
    orig[i] = a;
    a = (a + ans) % (n + 1);
    l = (l + ans) % i + 1;
    r = (r + ans) % i + 1;
    k = (k + ans) % (n + 1);
    if (l > r) swap(l, r);
    sgt1.upd(a, 1, i);
    last[a] = i;
    if (a != 0) {
      mark_range[0].push_back(mark_range[0].back());
      mark_t[0].push_back(i);
      mark_range[0][mark_range[0].size() - 1].second = i;
      UpdSum(0);
    }
    mark_t[a].push_back(i);
    mark_range[a].push_back(make_pair(i, i));
    UpdSum(a);
    auto it = st.find(a);
    if (it != st.end()) {
      auto bef = it;
      if (it != st.begin()) {
        bef--;
        st.erase(it);
        it = bef;
        it++;
      } else {
        it++;
      }
      while (bef != it) {
        int nxt = sgt1.getnxt(*bef + 1, n + 1, 1, last[*bef]);
        st.insert(nxt);
        mark_t[nxt].push_back(i);
        mark_range[nxt].push_back(
            make_pair(mark_range[nxt].back().first, last[*bef]));
        UpdSum(nxt);
        bef++;
      }
    }
    auto st = lower_bound(
                  mark_range[k].begin(), mark_range[k].end(), make_pair(-1, l),
                  [&](const pair<int, int> &a, const pair<int, int> &b) {
                    if (a.second != b.second) return a.second < b.second;
                    return a.first < b.first;
                  }) -
              mark_range[k].begin();
    auto en =
        upper_bound(mark_t[k].begin(), mark_t[k].end(), r) - mark_t[k].begin();
    auto mid = lower_bound(mark_range[k].begin(), mark_range[k].end(),
                           make_pair(l - 1, l)) -
               mark_range[k].begin();
    ans = 0;
    if (en > st) {
      en--;
      mid = min(mid, en);
      if (en > st) {
        ans = sum[k][en] - sum[k][st];
        if (mark_range[k][st].first < l - 1) {
          ans -= 1ll * (mark_t[k][mid] - mark_t[k][st]) *
                 (l - 1 - mark_range[k][st].first);
        }
      }
      ans += 1ll * (r - mark_t[k][en] + 1) *
             (mark_range[k][en].second - max(l - 1, mark_range[k][en].first));
    }
    printf("%I64d\n", ans);
  }
  return 0;
}

```