#include <bits/stdc++.h>
using ll = long long;
const int MN = 2e5 + 100;
int N;
struct node {
 public:
  node *l, *r;
  ll v, z;
  node() : l(nullptr), r(nullptr), v(0LL), z(0LL){};
};
const int pool_sz = 50000;
node *pool;
int pool_ctr;
node *nn(node *p = nullptr) {
  if (!pool_ctr) pool = new node[pool_ctr = pool_sz];
  if (p) pool[pool_ctr - 1] = *p;
  return pool + --pool_ctr;
}
void upd(node *&n, int l, int r, int ql, int qr, ll q) {
  n = nn(n);
  n->v += (qr - ql) * q;
  if (ql == l && r == qr)
    n->z += q;
  else {
    int m = l + (r - l) / 2;
    if (ql < m) upd(n->l, l, m, ql, std::min(qr, m), q);
    if (m < qr) upd(n->r, m, r, std::max(m, ql), qr, q);
  }
}
ll qry(node *n, int l, int r, int ql, int qr) {
  if (!n) return 0LL;
  ll f = 0LL;
  if (ql == l && r == qr)
    return n->v;
  else {
    f = n->z * (qr - ql);
    int m = l + (r - l) / 2;
    if (ql < m) f += qry(n->l, l, m, ql, std::min(qr, m));
    if (m < qr) f += qry(n->r, m, r, std::max(m, ql), qr);
    return f;
  }
}
struct S {
 public:
  node *n;
  int t;
};
std::vector<S> m[MN];
int lb(int k, int v) {
  int l = 0, r = m[k].size();
  for (; r - l > 1;) {
    int x = l + (r - l) / 2;
    if (m[k][x].t < v)
      l = x;
    else
      r = x;
  }
  return r;
}
struct R {
 public:
  int m;
  mutable int t;
  R(int m = -1, int t = 0) : m(m), t(t) {}
  bool operator<(R o) const { return m < o.m; }
};
std::set<R> r;
void fix(std::set<R>::iterator it, int i, bool over = 0) {
  int v = it->m;
  S x = m[v].back();
  if (over && x.t == i) return;
  if (!over) assert(x.t != i);
  node *n = x.n;
  upd(n, 0, N, std::next(it)->t, it->t, i - x.t);
  m[v].push_back(S{n, i});
}
void fix(int v, int i, bool over = 0) {
  if (over && m[v].back().t == i) return;
  if (!over) assert(m[v].back().t < i);
  m[v].push_back(S{m[v].back().n, i});
}
int p[MN];
ll getper(int k, int i, int l, int r) {
  if (i == m[k].size()) {
    std::set<R>::iterator it = ::r.lower_bound({k, 0});
    return it->m == k
               ? std::max(0LL,
                          static_cast<ll>(std::min(r, it->t) -
                                          std::max(l - 1, std::next(it)->t)))
               : 0LL;
  }
  return (qry(m[k][i].n, 0, N, l - 1, r) - qry(m[k][i - 1].n, 0, N, l - 1, r)) /
         (m[k][i].t - m[k][i - 1].t);
}
ll P;
void convert(int i, int *a, int *l, int *r, int *k) {
  *a = (*a + P) % (N + 1);
  *l = (*l + P) % i + 1;
  *r = (*r + P) % i + 1;
  if (*r < *l) *l ^= *r ^= *l ^= *r;
  *k = (*k + P) % (N + 1);
}
void ans_q(int i) {
  int a, l, r, k;
  scanf("%d%d%d%d", &a, &l, &r, &k);
  convert(i, &a, &l, &r, &k);
  p[a] = i;
  std::set<R>::iterator it;
  it = ::r.lower_bound({a});
  if (it->m == a) fix(it, i);
  for (int j = a, k = i; it->m == j; ++j) {
    k = std::min(k, p[j]);
    if (k >= it->t)
      k = it->t, it = ::r.erase(it);
    else
      ++it;
    if (k <= it->t) break;
    if (it->m == j + 1)
      fix(it, i), it->t = k;
    else
      fix(j + 1, i), it = ::r.insert(R{j + 1, k}).first;
  }
  if (a)
    if ((it = ::r.begin())->m)
      fix(0, i), ::r.insert(R{0, i});
    else
      fix(it, i), ++it->t;
  else if ((it = ::r.begin())->m == 1)
    fix(it, i, 1), ++it->t;
  else if (it->m > 1)
    fix(1, i, 1), ::r.insert(R{1, i});
  P = 0LL;
  int u, v;
  u = lb(k, l);
  v = lb(k, r + 1) - 1;
  assert(v + 1 >= u);
  if (u > v) return printf("%lld\n", P = getper(k, u, l, r) * (u - v)), void();
  if (u < v)
    P = qry(m[k][v].n, 0, N, l - 1, r) - qry(m[k][u].n, 0, N, l - 1, r);
  if (l < m[k][u].t) P += getper(k, u, l, r) * (m[k][u].t - l);
  P += getper(k, v + 1, l, r) * (r - m[k][v].t + 1);
  printf("%lld\n", P);
}
int main(void) {
  scanf("%d", &N);
  for (int i = 0; i <= N; i++) m[i].assign(1, S{nullptr, 0});
  r.clear();
  r.insert(R{MN, 0});
  for (int i = 1; i <= N; i++) ans_q(i);
  return 0;
}

### Prompt

Develop a solution in CPP 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 ll = long long;
const int MN = 2e5 + 100;
int N;
struct node {
 public:
  node *l, *r;
  ll v, z;
  node() : l(nullptr), r(nullptr), v(0LL), z(0LL){};
};
const int pool_sz = 50000;
node *pool;
int pool_ctr;
node *nn(node *p = nullptr) {
  if (!pool_ctr) pool = new node[pool_ctr = pool_sz];
  if (p) pool[pool_ctr - 1] = *p;
  return pool + --pool_ctr;
}
void upd(node *&n, int l, int r, int ql, int qr, ll q) {
  n = nn(n);
  n->v += (qr - ql) * q;
  if (ql == l && r == qr)
    n->z += q;
  else {
    int m = l + (r - l) / 2;
    if (ql < m) upd(n->l, l, m, ql, std::min(qr, m), q);
    if (m < qr) upd(n->r, m, r, std::max(m, ql), qr, q);
  }
}
ll qry(node *n, int l, int r, int ql, int qr) {
  if (!n) return 0LL;
  ll f = 0LL;
  if (ql == l && r == qr)
    return n->v;
  else {
    f = n->z * (qr - ql);
    int m = l + (r - l) / 2;
    if (ql < m) f += qry(n->l, l, m, ql, std::min(qr, m));
    if (m < qr) f += qry(n->r, m, r, std::max(m, ql), qr);
    return f;
  }
}
struct S {
 public:
  node *n;
  int t;
};
std::vector<S> m[MN];
int lb(int k, int v) {
  int l = 0, r = m[k].size();
  for (; r - l > 1;) {
    int x = l + (r - l) / 2;
    if (m[k][x].t < v)
      l = x;
    else
      r = x;
  }
  return r;
}
struct R {
 public:
  int m;
  mutable int t;
  R(int m = -1, int t = 0) : m(m), t(t) {}
  bool operator<(R o) const { return m < o.m; }
};
std::set<R> r;
void fix(std::set<R>::iterator it, int i, bool over = 0) {
  int v = it->m;
  S x = m[v].back();
  if (over && x.t == i) return;
  if (!over) assert(x.t != i);
  node *n = x.n;
  upd(n, 0, N, std::next(it)->t, it->t, i - x.t);
  m[v].push_back(S{n, i});
}
void fix(int v, int i, bool over = 0) {
  if (over && m[v].back().t == i) return;
  if (!over) assert(m[v].back().t < i);
  m[v].push_back(S{m[v].back().n, i});
}
int p[MN];
ll getper(int k, int i, int l, int r) {
  if (i == m[k].size()) {
    std::set<R>::iterator it = ::r.lower_bound({k, 0});
    return it->m == k
               ? std::max(0LL,
                          static_cast<ll>(std::min(r, it->t) -
                                          std::max(l - 1, std::next(it)->t)))
               : 0LL;
  }
  return (qry(m[k][i].n, 0, N, l - 1, r) - qry(m[k][i - 1].n, 0, N, l - 1, r)) /
         (m[k][i].t - m[k][i - 1].t);
}
ll P;
void convert(int i, int *a, int *l, int *r, int *k) {
  *a = (*a + P) % (N + 1);
  *l = (*l + P) % i + 1;
  *r = (*r + P) % i + 1;
  if (*r < *l) *l ^= *r ^= *l ^= *r;
  *k = (*k + P) % (N + 1);
}
void ans_q(int i) {
  int a, l, r, k;
  scanf("%d%d%d%d", &a, &l, &r, &k);
  convert(i, &a, &l, &r, &k);
  p[a] = i;
  std::set<R>::iterator it;
  it = ::r.lower_bound({a});
  if (it->m == a) fix(it, i);
  for (int j = a, k = i; it->m == j; ++j) {
    k = std::min(k, p[j]);
    if (k >= it->t)
      k = it->t, it = ::r.erase(it);
    else
      ++it;
    if (k <= it->t) break;
    if (it->m == j + 1)
      fix(it, i), it->t = k;
    else
      fix(j + 1, i), it = ::r.insert(R{j + 1, k}).first;
  }
  if (a)
    if ((it = ::r.begin())->m)
      fix(0, i), ::r.insert(R{0, i});
    else
      fix(it, i), ++it->t;
  else if ((it = ::r.begin())->m == 1)
    fix(it, i, 1), ++it->t;
  else if (it->m > 1)
    fix(1, i, 1), ::r.insert(R{1, i});
  P = 0LL;
  int u, v;
  u = lb(k, l);
  v = lb(k, r + 1) - 1;
  assert(v + 1 >= u);
  if (u > v) return printf("%lld\n", P = getper(k, u, l, r) * (u - v)), void();
  if (u < v)
    P = qry(m[k][v].n, 0, N, l - 1, r) - qry(m[k][u].n, 0, N, l - 1, r);
  if (l < m[k][u].t) P += getper(k, u, l, r) * (m[k][u].t - l);
  P += getper(k, v + 1, l, r) * (r - m[k][v].t + 1);
  printf("%lld\n", P);
}
int main(void) {
  scanf("%d", &N);
  for (int i = 0; i <= N; i++) m[i].assign(1, S{nullptr, 0});
  r.clear();
  r.insert(R{MN, 0});
  for (int i = 1; i <= N; i++) ans_q(i);
  return 0;
}

```