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>
inline void err() { std::cerr << "\033[0m\n"; }
template <class T, class... U>
inline void err(const T &x, const U &...a) {
  std::cerr << x << ' ';
  err(a...);
}
template <class T>
inline void readInt(T &w) {
  char c, p = 0;
  while (!isdigit(c = getchar())) p = c == '-';
  for (w = c & 15; isdigit(c = getchar());) w = w * 10 + (c & 15);
  if (p) w = -w;
}
template <class T, class... U>
inline void readInt(T &w, U &...a) {
  readInt(w), readInt(a...);
}
template <class T, class U>
inline bool smin(T &x, const U &y) {
  return y < x ? x = y, 1 : 0;
}
template <class T, class U>
inline bool smax(T &x, const U &y) {
  return x < y ? x = y, 1 : 0;
}
constexpr int N(2e5 + 5);
int n;
struct Rect {
  int t, b, l, r;
  Rect(int t, int b, int l, int r) : t(t), b(b), l(l), r(r) {}
  inline Rect operator&(const Rect &rhs) const {
    return Rect(std::max(t, rhs.t), std::min(b, rhs.b), std::max(l, rhs.l),
                std::min(r, rhs.r));
  }
  inline long long area() const {
    return 1LL * std::max(0, b - t + 1) * std::max(0, r - l + 1);
  }
};
int pos[N];
std::vector<Rect> rect[N];
std::vector<long long> sum[N];
namespace SegTree {
int min[N << 2];
void update(int o, int l, int r, int x, int y) {
  if (l == r) {
    min[o] = y;
    return;
  }
  int m = l + r >> 1;
  x <= m ? update(o << 1, l, m, x, y) : update(o << 1 | 1, m + 1, r, x, y);
  min[o] = std::min(min[o << 1], min[o << 1 | 1]);
}
int ask(int o, int l, int r, int x) {
  if (x < l) return n;
  if (r <= x) return min[o];
  int m = l + r >> 1;
  return std::min(ask(o << 1, l, m, x), ask(o << 1 | 1, m + 1, r, x));
}
}  // namespace SegTree
bool vis[N];
std::set<int> s;
std::pair<int, int> now[N];
int top[N];
void insert(int i, int v, int l, int r) {
  if (s.count(v)) {
    if (top[v] == i) {
      assert(l == now[v].second + 1);
      now[v].second = r;
      return;
    }
    l = now[v].first;
    rect[v].emplace_back(top[v], i - 1, now[v].first, now[v].second);
    sum[v].push_back(sum[v].back() + rect[v].back().area());
  } else {
    s.insert(v);
  }
  top[v] = i, now[v].first = l, now[v].second = r;
}
int main() {
  readInt(n);
  for (int i = 0; i <= n; i++)
    rect[i].emplace_back(0, -1, 0, -1), sum[i].push_back(0);
  long long ans = 0;
  int mex = 0;
  for (int i = 1; i <= n; i++) {
    int x, l, r, k;
    readInt(x, l, r, k);
    x = (x + ans) % (n + 1);
    l = (l + ans) % i + 1;
    r = (r + ans) % i + 1;
    k = (k + ans) % (n + 1);
    if (l > r) std::swap(l, r);
    ;
    for (vis[x] = 1; vis[mex]; mex++)
      ;
    SegTree::update(1, 1, n, i, x);
    if (pos[x]) SegTree::update(1, 1, n, pos[x], n);
    pos[x] = i;
    auto it = s.find(x);
    if (it != s.end()) {
      s.erase(it);
      auto [p, q] = now[x];
      rect[x].emplace_back(top[x], i - 1, p, q);
      sum[x].push_back(sum[x].back() + rect[x].back().area());
      while (p <= q) {
        int v = SegTree::ask(1, 1, n, q - 1), idx = smin(v, mex) ? 0 : pos[v];
        insert(i, v, idx + 1, q);
        q = idx;
      }
    }
    insert(i, !x, i, i);
    auto &a = rect[k];
    auto &b = sum[k];
    auto qrect = Rect(l, r, l, r);
    ans = 0;
    int p = std::partition_point(
                a.begin(), a.end(),
                [&](const Rect &v) { return v.b < l || v.r < l; }) -
            a.begin();
    int mp = std::partition_point(a.begin(), a.end(),
                                  [&](const Rect &v) { return v.l < l; }) -
             a.begin() - 1;
    int mq = std::partition_point(a.begin(), a.end(),
                                  [&](const Rect &v) { return v.l <= r; }) -
             a.begin() - 1;
    int q = std::partition_point(
                a.begin(), a.end(),
                [&](const Rect &v) { return v.t <= r && v.l <= r; }) -
            a.begin() - 1;
    if (p > q) {
      ans = 0;
    } else if (p == q) {
      ans = (a[p] & qrect).area();
    } else {
      ans = b[q] - b[p - 1];
      ans -= a[p].area() - (a[p] & qrect).area();
      ans -= a[q].area() - (a[q] & qrect).area();
      p++, q--;
      smin(mp, q), smax(mq, mp + 1);
      if (p <= mp)
        assert(a[p].l == a[mp].l),
            ans -= Rect(a[p].t, a[mp].b, a[p].l, l - 1).area();
      if (mq <= q)
        assert(a[mq].l == a[q].l),
            ans -= b[q] - b[mq - 1] - Rect(a[mq].t, a[q].b, a[q].l, r).area();
    }
    if (s.count(k))
      ans += (Rect(top[k], i, now[k].first, now[k].second) & qrect).area();
    printf("%lld\n", ans);
    if (ans == 110) {
      for (auto u : a)
        ;
      ;
    }
  }
  return 0;
}