#include <bits/stdc++.h>
const int MAXN = 200010;
const int INF = 0x3f3f3f3f;
int Q, T;
class seg {
  int at, v, A[MAXN], tree[MAXN << 2];
  void mdf(int u, int l, int r) {
    if (l == r) return (void)(tree[u] = v);
    int mid = l + r >> 1;
    if (at <= mid)
      mdf(u << 1, l, mid);
    else
      mdf(u << 1 | 1, mid + 1, r);
    tree[u] = std::min(tree[u << 1], tree[u << 1 | 1]);
  }
  int _right(int u, int l, int r) {
    if (at <= l) {
      if (tree[u] > v) return INF;
      while (l != r) {
        if (tree[u << 1] <= v)
          u <<= 1, r = (l + r) / 2;
        else
          u = u << 1 | 1, l = (l + r) / 2 + 1;
      }
      return l;
    }
    int mid = l + r >> 1, t;
    if (at <= mid && (t = _right(u << 1, l, mid)) != INF) return t;
    if ((t = _right(u << 1 | 1, mid + 1, r)) != INF) return t;
    return INF;
  }

 public:
  void mdf(int x, int y) { at = x, v = y, A[at] = v, mdf(1, 0, Q + 1); }
  int right(int x, int z) { return at = x, v = z, _right(1, 0, Q + 1); }
  inline int& operator[](int x) { return A[x]; }
} lst;
struct itv {
  int l, r, x, y;
  bool operator<(const itv& b) const { return y < b.y; }
  long long val() { return (r - l + 1ll) * (y - x + 1); }
};
std::set<itv> S;
std::vector<itv> hav[MAXN];
std::vector<long long> pre[MAXN];
void del(itv x, int at) {
  S.erase(x);
  if (x.x > at) return;
  int k = x.y;
  x.y = at;
  hav[k].push_back(x);
  long long v = pre[k].empty() ? 0 : pre[k].back();
  pre[k].push_back(v + x.val());
}
void append(int at, int v) {
  lst.mdf(v, at);
  itv tp0 = *S.begin();
  if (tp0.y == 0)
    del(tp0, at - 1), tp0.r = tp0.x = at;
  else
    tp0 = (itv){at, at, at, 0};
  S.insert(tp0);
  auto it = S.lower_bound((itv){0, 0, 0, v});
  if (it == S.end() || it->y != v) return;
  itv now = *it, nt = *(++it);
  del(now, at - 1);
  int R = now.r;
  while (R >= now.l) {
    int u = lst.right(v, R - 1);
    int L = std::max(lst[u], now.l - 1);
    itv t = (itv){L + 1, R, at, u};
    if (u == nt.y) {
      del(nt, at - 1);
      assert(L == nt.r);
      t.l = nt.l;
    }
    S.insert(t);
    R = L;
  }
}
typedef const itv& cir;
inline itv red(itv x, int l, int r) {
  x.l = std::max(x.l, l);
  x.y = std::min(x.y, r);
  if (x.l > x.r || x.x > x.y) x.r = x.l - 1;
  return x;
}
auto cmpl = [](cir a, cir b) { return a.r < b.r; };
auto cmpr = [](cir a, cir b) { return a.x < b.x; };
auto cmpm = [](cir a, cir b) { return a.l < b.l; };
long long qry(int at, int K, int l, int r) {
  std::vector<itv>& v = hav[K];
  itv t = (itv){0, l, 0, 0};
  int a = (std::lower_bound(v.begin(), v.end(), t, cmpl) - v.begin());
  t = (itv){0, 0, r + 1, 0};
  int b = (std::lower_bound(v.begin(), v.end(), t, cmpr) - v.begin());
  long long res = 0;
  if (a < b) {
    res += red(v[a], l, r).val();
    if (a != b - 1) res += red(v[b - 1], l, r).val();
    ++a, b -= 2;
    if (a <= b) {
      res += pre[K][b] - pre[K][a - 1];
      if (hav[K][a].l < l) {
        t = (itv){hav[K][a].l + 1, 0, 0, 0};
        int c = std::min(
            (int)((std::lower_bound(v.begin(), v.end(), t, cmpm) - v.begin()) -
                  1),
            b);
        if (a <= c)
          res -= (l - hav[K][a].l) * (hav[K][c].y - hav[K][a].x + 1ll);
      }
    }
  }
  auto it = S.lower_bound((itv){0, 0, 0, K});
  if (it != S.end() && it->y == K) t = *it, t.y = at, res += red(t, l, r).val();
  return res;
}
int main() {
  std::ios_base::sync_with_stdio(false), std::cin.tie(0);
  std::cin >> Q;
  S.insert((itv){0, 0, 0, Q + 12243});
  long long lans = 0;
  for (int opt = 1; opt <= Q; ++opt) {
    int x, l, r, K;
    std::cin >> x >> l >> r >> K;
    x = (x + lans) % (Q + 1);
    K = (K + lans) % (Q + 1);
    l = (l + lans) % opt + 1;
    r = (r + lans) % opt + 1;
    if (l > r) std::swap(l, r);
    append(opt, x);
    std::cout << (lans = qry(opt, K, l, r)) << '\n';
  }
  return 0;
}

### Prompt

In Cpp, your task is to solve the following 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

### Response

```cpp
#include <bits/stdc++.h>
const int MAXN = 200010;
const int INF = 0x3f3f3f3f;
int Q, T;
class seg {
  int at, v, A[MAXN], tree[MAXN << 2];
  void mdf(int u, int l, int r) {
    if (l == r) return (void)(tree[u] = v);
    int mid = l + r >> 1;
    if (at <= mid)
      mdf(u << 1, l, mid);
    else
      mdf(u << 1 | 1, mid + 1, r);
    tree[u] = std::min(tree[u << 1], tree[u << 1 | 1]);
  }
  int _right(int u, int l, int r) {
    if (at <= l) {
      if (tree[u] > v) return INF;
      while (l != r) {
        if (tree[u << 1] <= v)
          u <<= 1, r = (l + r) / 2;
        else
          u = u << 1 | 1, l = (l + r) / 2 + 1;
      }
      return l;
    }
    int mid = l + r >> 1, t;
    if (at <= mid && (t = _right(u << 1, l, mid)) != INF) return t;
    if ((t = _right(u << 1 | 1, mid + 1, r)) != INF) return t;
    return INF;
  }

 public:
  void mdf(int x, int y) { at = x, v = y, A[at] = v, mdf(1, 0, Q + 1); }
  int right(int x, int z) { return at = x, v = z, _right(1, 0, Q + 1); }
  inline int& operator[](int x) { return A[x]; }
} lst;
struct itv {
  int l, r, x, y;
  bool operator<(const itv& b) const { return y < b.y; }
  long long val() { return (r - l + 1ll) * (y - x + 1); }
};
std::set<itv> S;
std::vector<itv> hav[MAXN];
std::vector<long long> pre[MAXN];
void del(itv x, int at) {
  S.erase(x);
  if (x.x > at) return;
  int k = x.y;
  x.y = at;
  hav[k].push_back(x);
  long long v = pre[k].empty() ? 0 : pre[k].back();
  pre[k].push_back(v + x.val());
}
void append(int at, int v) {
  lst.mdf(v, at);
  itv tp0 = *S.begin();
  if (tp0.y == 0)
    del(tp0, at - 1), tp0.r = tp0.x = at;
  else
    tp0 = (itv){at, at, at, 0};
  S.insert(tp0);
  auto it = S.lower_bound((itv){0, 0, 0, v});
  if (it == S.end() || it->y != v) return;
  itv now = *it, nt = *(++it);
  del(now, at - 1);
  int R = now.r;
  while (R >= now.l) {
    int u = lst.right(v, R - 1);
    int L = std::max(lst[u], now.l - 1);
    itv t = (itv){L + 1, R, at, u};
    if (u == nt.y) {
      del(nt, at - 1);
      assert(L == nt.r);
      t.l = nt.l;
    }
    S.insert(t);
    R = L;
  }
}
typedef const itv& cir;
inline itv red(itv x, int l, int r) {
  x.l = std::max(x.l, l);
  x.y = std::min(x.y, r);
  if (x.l > x.r || x.x > x.y) x.r = x.l - 1;
  return x;
}
auto cmpl = [](cir a, cir b) { return a.r < b.r; };
auto cmpr = [](cir a, cir b) { return a.x < b.x; };
auto cmpm = [](cir a, cir b) { return a.l < b.l; };
long long qry(int at, int K, int l, int r) {
  std::vector<itv>& v = hav[K];
  itv t = (itv){0, l, 0, 0};
  int a = (std::lower_bound(v.begin(), v.end(), t, cmpl) - v.begin());
  t = (itv){0, 0, r + 1, 0};
  int b = (std::lower_bound(v.begin(), v.end(), t, cmpr) - v.begin());
  long long res = 0;
  if (a < b) {
    res += red(v[a], l, r).val();
    if (a != b - 1) res += red(v[b - 1], l, r).val();
    ++a, b -= 2;
    if (a <= b) {
      res += pre[K][b] - pre[K][a - 1];
      if (hav[K][a].l < l) {
        t = (itv){hav[K][a].l + 1, 0, 0, 0};
        int c = std::min(
            (int)((std::lower_bound(v.begin(), v.end(), t, cmpm) - v.begin()) -
                  1),
            b);
        if (a <= c)
          res -= (l - hav[K][a].l) * (hav[K][c].y - hav[K][a].x + 1ll);
      }
    }
  }
  auto it = S.lower_bound((itv){0, 0, 0, K});
  if (it != S.end() && it->y == K) t = *it, t.y = at, res += red(t, l, r).val();
  return res;
}
int main() {
  std::ios_base::sync_with_stdio(false), std::cin.tie(0);
  std::cin >> Q;
  S.insert((itv){0, 0, 0, Q + 12243});
  long long lans = 0;
  for (int opt = 1; opt <= Q; ++opt) {
    int x, l, r, K;
    std::cin >> x >> l >> r >> K;
    x = (x + lans) % (Q + 1);
    K = (K + lans) % (Q + 1);
    l = (l + lans) % opt + 1;
    r = (r + lans) % opt + 1;
    if (l > r) std::swap(l, r);
    append(opt, x);
    std::cout << (lans = qry(opt, K, l, r)) << '\n';
  }
  return 0;
}

```