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;
using uint = unsigned;
using ll = long long;
using ull = unsigned long long;
template <class t>
void chmax(t& first, t second) {
  if (first < second) first = second;
}
template <class t>
void chmin(t& first, t second) {
  if (first > second) first = second;
}
template <class t>
using vc = vector<t>;
template <class t>
using vvc = vc<vc<t>>;
using pi = pair<ll, ll>;
using vi = vc<ll>;
template <class t, class u>
ostream& operator<<(ostream& os, const pair<t, u>& p) {
  return os << "{" << p.first << "," << p.second << "}";
}
template <class t>
ostream& operator<<(ostream& os, const vc<t>& v) {
  os << "{";
  for (auto e : v) os << e << ",";
  return os << "}";
}
constexpr ll ten(ll n) { return n == 0 ? 1 : ten(n - 1) * 10; }
template <class t>
void mkuni(vc<t>& v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
}
ll read() {
  ll i;
  cin >> i;
  return i;
}
template <ll i, class T>
void print_tuple(ostream&, const T&) {}
template <ll i, class T, class H, class... Args>
void print_tuple(ostream& os, const T& t) {
  if (i) os << ",";
  os << get<i>(t);
  print_tuple<i + 1, T, Args...>(os, t);
}
template <class... Args>
ostream& operator<<(ostream& os, const tuple<Args...>& t) {
  os << "{";
  print_tuple<0, tuple<Args...>, Args...>(os, t);
  return os << "}";
}
void print(ll x, ll suc = 1) {
  cout << x;
  if (suc == 1) cout << endl;
  if (suc == 2) cout << " ";
}
vi readvi(ll n, ll off = 0) {
  vi v(n);
  for (ll i = ll(0); i < ll(n); i++) v[i] = read() + off;
  return v;
}
template <class T>
void print(const vector<T>& v, ll suc = 1) {
  for (ll i = ll(0); i < ll(v.size()); i++)
    print(v[i], i == ll(v.size()) - 1 ? suc : 2);
}
string readString() {
  string s;
  cin >> s;
  return s;
}
template <class T>
T sq(const T& t) {
  return t * t;
}
void yes(bool ex = true) {
  cout << "Yes" << endl;
  if (ex) exit(0);
}
void no(bool ex = true) {
  cout << "No" << endl;
  if (ex) exit(0);
}
const ll infLL = LLONG_MAX / 3;
const ll inf = infLL;
ll topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
ll topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
ll popcount(signed t) { return __builtin_popcount(t); }
ll popcount(ll t) { return __builtin_popcountll(t); }
bool ispow2(ll i) { return i && (i & -i) == i; }
bool inc(ll first, ll second, ll c) { return first <= second && second <= c; }
template <class T, class F>
struct SegTree {
  vc<T> buf;
  ll s;
  const F f;
  const T g;
  SegTree(const vc<T>& d, F ff, T gg) : f(ff), g(gg) {
    ll n = d.size();
    s = 1;
    while (s < n) s *= 2;
    buf.resize(s * 2, g);
    for (ll i = ll(0); i < ll(n); i++) buf[i + s] = d[i];
    for (ll i = ll(s) - 1; i >= 1; i--) u(i);
  }
  void u(ll i) { buf[i] = f(buf[i * 2], buf[i * 2 + 1]); }
  void set(ll i, T t) {
    i += s;
    buf[i] = t;
    while (i >>= 1) u(i);
  }
  T get(ll second, ll e, ll l, ll r, ll i) {
    if (e <= l || r <= second) return g;
    if (second <= l && r <= e) return buf[i];
    ll m = (l + r) / 2;
    return f(get(second, e, l, m, i * 2), get(second, e, m, r, i * 2 + 1));
  }
  T get(ll second, ll e) { return get(second, e, 0, s, 1); }
};
template <class T, class F>
SegTree<T, F> segtree(const vc<T>& d, F f, T g) {
  return SegTree<T, F>(d, f, g);
}
struct W {
  vi x1, x2, y1, y2, as, hs, hxs;
  W() : as{0}, hs{0}, hxs{0} {}
  void push(ll first, ll second, ll c, ll d) {
    void(0);
    x1.push_back(first);
    x2.push_back(second);
    y1.push_back(c);
    y2.push_back(d);
    as.push_back(as.back() + (d - c) * (second - first));
    hs.push_back(hs.back() + (d - c));
    hxs.push_back(hxs.back() + (d - c) * second);
  }
  ll get(ll x, ll y) {
    ll s = upper_bound(y2.begin(), y2.end(), y) - y2.begin();
    ll res = 0;
    if (s < ll(x1.size()))
      res += max(y - y1[s], ll(0)) * max(x2[s] - max(x, x1[s]), ll(0));
    ll r = min(ll(lower_bound(x1.begin(), x1.end(), x) - x1.begin()), s);
    res += as[s] - as[r];
    ll q = min(ll(lower_bound(x2.begin(), x2.end(), x) - x2.begin()), s);
    res += hxs[r] - hxs[q] - x * (hs[r] - hs[q]);
    return res;
  }
};
signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  ll n = read();
  vc<pi> org(n + 2);
  for (ll i = ll(0); i < ll(n + 2); i++) org[i] = pi(-1 - i, i);
  auto seg = segtree(
      org, [](pi first, pi second) { return min(first, second); }, pi(inf, -1));
  vc<W> ws(n + 2);
  set<ll> vis;
  for (ll i = ll(0); i < ll(n + 2); i++) vis.insert(i);
  struct R {
    ll l, r, t;
  };
  vc<R> rs(n + 2, {-1, -1, -1});
  ll ans = 0;
  vi raw;
  for (ll i = ll(0); i < ll(n); i++) {
    ll first = (read() + ans) % (n + 1), l = (read() + ans) % (i + 1),
       r = (read() + ans) % (i + 1), k = (read() + ans) % (n + 1);
    if (l > r) swap(l, r);
    r++;
    void(0);
    void(0);
    void(0);
    void(0);
    raw.push_back(first);
    void(0);
    const auto cut = [&](ll x) {
      if (rs[x].l < rs[x].r) {
        void(0);
        ws[x].push(rs[x].l, rs[x].r, rs[x].t, i);
        rs[x] = {-1, -1, -1};
      }
    };
    auto itr = vis.find(first);
    if (itr != vis.end()) {
      cut(first);
      ll p = *vis.erase(itr);
      cut(p);
      org[first].first = i;
      seg.set(first, org[first]);
      while (p > first) {
        ll q = seg.get(0, p).second;
        void(0);
        vis.insert(q);
        rs[p] = {org[p].first + 1, org[q].first + 1, i};
        p = q;
      }
    } else {
      org[first].first = i;
      seg.set(first, org[first]);
    }
    vis.insert(0);
    cut(0);
    rs[0] = {org[0].first + 1, i + 1, i};
    ans = ws[k].get(l, r);
    ans += max(r - rs[k].t, ll(0)) * max(rs[k].r - max(rs[k].l, l), ll(0));
    print(ans);
  }
}