#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 2.1e5;
int N;
const int S = 1 << 18;
struct {
  int minVal = 0;
} seg[2 * S];
map<int, int> lows;
struct seg_node {
  seg_node* ch[2] = {nullptr, nullptr};
  ll a = 0, b = 0;
};
void init() {
  lows[-1] = int(1.1e9);
  lows[0] = 0;
  lows[S] = 0;
}
int queryPrefMin(int a) {
  int ans = seg[S + a].minVal;
  for (int l = S, r = S + a; l < r; l /= 2, r /= 2) {
    if (l & 1) {
      ans = min(ans, seg[l].minVal);
      l++;
    }
    if (r & 1) {
      r--;
      ans = min(ans, seg[r].minVal);
    }
  }
  return ans;
}
seg_node* update_seg(seg_node* n, int l, int r, ll a, ll b, int x = 0,
                     int y = S) {
  assert(l < r);
  if (n == nullptr) {
    n = new seg_node();
  } else {
    n = new seg_node(*n);
  }
  if (l <= x && y <= r) {
    n->a += a;
    n->b += b;
  } else {
    int z = (x + y) / 2;
    if (l < z) {
      n->ch[0] = update_seg(n->ch[0], l, r, a, b, x, z);
    }
    if (z < r) {
      n->ch[1] = update_seg(n->ch[1], l, r, a, b, z, y);
    }
  }
  return n;
}
seg_node* ROOTS[MAXN];
ll query_seg(seg_node* n, int p, ll fac, int x = 0, int y = S) {
  assert(x <= p && p < y);
  if (n == nullptr) return 0;
  ll res = n->a * fac + n->b;
  if (y - x > 1) {
    int z = (x + y) / 2;
    if (p < z) {
      res += query_seg(n->ch[0], p, fac, x, z);
    } else {
      res += query_seg(n->ch[1], p, fac, z, y);
    }
  }
  return res;
}
void doUpdate(int A) {
  N++;
  ROOTS[N] = ROOTS[N - 1];
  seg[S + A].minVal = N;
  for (int a = (S + A) / 2; a > 0; a /= 2) {
    seg[a].minVal = min(seg[2 * a].minVal, seg[2 * a + 1].minVal);
  }
  if (lows.count(A)) {
    auto pt = lows.find(A);
    --pt;
    auto jt = lows.find(A);
    ++jt;
    int oldV = lows[A];
    lows.erase(A);
    int x = A, y = jt->first;
    while (x < y) {
      int v = queryPrefMin(x);
      int mi = x, ma = y;
      while (ma - mi > 1) {
        int md = (mi + ma) / 2;
        if (queryPrefMin(md) == v) {
          mi = md;
        } else {
          ma = md;
        }
      }
      ROOTS[N] =
          update_seg(ROOTS[N], x, ma, (v - oldV), -ll(v - oldV) * ll(N - 1));
      if (v == pt->second) {
      } else {
        lows[x] = v;
      }
      x = ma;
    }
  }
}
ll doQuery(int L, int R, int K) {
  if (K < 0) {
    return ll(R - L + 2) * ll(R - L + 1) / 2;
  }
  auto queryPt = [&](int r) { return query_seg(ROOTS[r], K, r); };
  int mi = L - 1;
  int ma = R + 1;
  while (ma - mi > 1) {
    int md = (mi + ma) / 2;
    if (queryPt(md) - queryPt(md - 1) >= L) {
      ma = md;
    } else {
      mi = md;
    }
  }
  if (ma > R) return 0;
  return (queryPt(R) - queryPt(mi)) - ll(L - 1) * ll(R - mi);
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int Q;
  cin >> Q;
  ll lastAns = 0;
  init();
  for (int q = 1; q <= Q; q++) {
    ll A_, L_, R_, K_;
    cin >> A_ >> L_ >> R_ >> K_;
    int A = int((A_ + lastAns) % (Q + 1));
    int L = int((L_ + lastAns) % q + 1);
    int R = int((R_ + lastAns) % q + 1);
    if (L > R) swap(L, R);
    int K = int((K_ + lastAns) % (Q + 1));
    doUpdate(A);
    ll ans = doQuery(L, R, K - 1) - doQuery(L, R, K);
    cout << ans << '\n';
    lastAns = ans;
  }
  return 0;
}

### Prompt

Please formulate a cpp solution to 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>
using namespace std;
using ll = long long;
const int MAXN = 2.1e5;
int N;
const int S = 1 << 18;
struct {
  int minVal = 0;
} seg[2 * S];
map<int, int> lows;
struct seg_node {
  seg_node* ch[2] = {nullptr, nullptr};
  ll a = 0, b = 0;
};
void init() {
  lows[-1] = int(1.1e9);
  lows[0] = 0;
  lows[S] = 0;
}
int queryPrefMin(int a) {
  int ans = seg[S + a].minVal;
  for (int l = S, r = S + a; l < r; l /= 2, r /= 2) {
    if (l & 1) {
      ans = min(ans, seg[l].minVal);
      l++;
    }
    if (r & 1) {
      r--;
      ans = min(ans, seg[r].minVal);
    }
  }
  return ans;
}
seg_node* update_seg(seg_node* n, int l, int r, ll a, ll b, int x = 0,
                     int y = S) {
  assert(l < r);
  if (n == nullptr) {
    n = new seg_node();
  } else {
    n = new seg_node(*n);
  }
  if (l <= x && y <= r) {
    n->a += a;
    n->b += b;
  } else {
    int z = (x + y) / 2;
    if (l < z) {
      n->ch[0] = update_seg(n->ch[0], l, r, a, b, x, z);
    }
    if (z < r) {
      n->ch[1] = update_seg(n->ch[1], l, r, a, b, z, y);
    }
  }
  return n;
}
seg_node* ROOTS[MAXN];
ll query_seg(seg_node* n, int p, ll fac, int x = 0, int y = S) {
  assert(x <= p && p < y);
  if (n == nullptr) return 0;
  ll res = n->a * fac + n->b;
  if (y - x > 1) {
    int z = (x + y) / 2;
    if (p < z) {
      res += query_seg(n->ch[0], p, fac, x, z);
    } else {
      res += query_seg(n->ch[1], p, fac, z, y);
    }
  }
  return res;
}
void doUpdate(int A) {
  N++;
  ROOTS[N] = ROOTS[N - 1];
  seg[S + A].minVal = N;
  for (int a = (S + A) / 2; a > 0; a /= 2) {
    seg[a].minVal = min(seg[2 * a].minVal, seg[2 * a + 1].minVal);
  }
  if (lows.count(A)) {
    auto pt = lows.find(A);
    --pt;
    auto jt = lows.find(A);
    ++jt;
    int oldV = lows[A];
    lows.erase(A);
    int x = A, y = jt->first;
    while (x < y) {
      int v = queryPrefMin(x);
      int mi = x, ma = y;
      while (ma - mi > 1) {
        int md = (mi + ma) / 2;
        if (queryPrefMin(md) == v) {
          mi = md;
        } else {
          ma = md;
        }
      }
      ROOTS[N] =
          update_seg(ROOTS[N], x, ma, (v - oldV), -ll(v - oldV) * ll(N - 1));
      if (v == pt->second) {
      } else {
        lows[x] = v;
      }
      x = ma;
    }
  }
}
ll doQuery(int L, int R, int K) {
  if (K < 0) {
    return ll(R - L + 2) * ll(R - L + 1) / 2;
  }
  auto queryPt = [&](int r) { return query_seg(ROOTS[r], K, r); };
  int mi = L - 1;
  int ma = R + 1;
  while (ma - mi > 1) {
    int md = (mi + ma) / 2;
    if (queryPt(md) - queryPt(md - 1) >= L) {
      ma = md;
    } else {
      mi = md;
    }
  }
  if (ma > R) return 0;
  return (queryPt(R) - queryPt(mi)) - ll(L - 1) * ll(R - mi);
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int Q;
  cin >> Q;
  ll lastAns = 0;
  init();
  for (int q = 1; q <= Q; q++) {
    ll A_, L_, R_, K_;
    cin >> A_ >> L_ >> R_ >> K_;
    int A = int((A_ + lastAns) % (Q + 1));
    int L = int((L_ + lastAns) % q + 1);
    int R = int((R_ + lastAns) % q + 1);
    if (L > R) swap(L, R);
    int K = int((K_ + lastAns) % (Q + 1));
    doUpdate(A);
    ll ans = doQuery(L, R, K - 1) - doQuery(L, R, K);
    cout << ans << '\n';
    lastAns = ans;
  }
  return 0;
}

```