#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool ylmin(T &a, T b) {
  return a < b ? 0 : (a = b, 1);
}
template <class T>
inline bool ylmax(T &a, T b) {
  return a > b ? 0 : (a = b, 1);
}
template <class T>
inline T abs(T x) {
  return x < 0 ? -x : x;
}
inline char read() {
  static const long long IO_LEN = 1024 * 1024;
  static char buf[IO_LEN], *ioh, *iot;
  if (ioh == iot) {
    iot = (ioh = buf) + fread(buf, 1, IO_LEN, stdin);
    if (ioh == iot) return -1;
  }
  return *ioh++;
}
template <class T>
inline void read(T &x) {
  static long long iosig;
  static char ioc;
  for (iosig = 0, ioc = read(); !isdigit(ioc); ioc = read())
    if (ioc == '-') iosig = 1;
  for (x = 0; isdigit(ioc); ioc = read()) x = (x << 1) + (x << 3) + (ioc ^ '0');
  if (iosig) x = -x;
}
const long long MAXN = 2e5 + 10;
long long n, pos[MAXN];
long long lastans, a, l, r, k;
struct Node {
  long long l1, l2, r1, r2;
  long long sum;
};
struct lastNode {
  long long up, down, first;
} t[MAXN];
vector<Node> s[MAXN];
inline long long query(long long u, long long x, long long y) {
  long long l = 0, r = ((long long)s[u].size()) - 1;
  long long lans = -1, rans = -1;
  while (l <= r) {
    long long mid = l + r >> 1;
    if (s[u][mid].l2 >= x && s[u][mid].r2 >= x)
      lans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  l = 0, r = ((long long)s[u].size()) - 1;
  while (l <= r) {
    long long mid = l + r >> 1;
    if (s[u][mid].l1 <= y && s[u][mid].r1 <= y)
      rans = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  long long res = 0;
  if (lans <= rans && lans != -1 && rans != -1) {
    res = s[u][rans].sum - (lans ? s[u][lans - 1].sum : 0);
    if (s[u][lans].l1 < x)
      res -= 1ll * (x - s[u][lans].l1) * (s[u][lans].r2 - s[u][lans].r1 + 1);
    if (s[u][rans].l2 > y)
      res -= 1ll * (s[u][rans].l2 - y) * (s[u][rans].r2 - s[u][rans].r1 + 1);
    if (s[u][lans].r1 < x) {
      long long ans = -1;
      l = 0, r = ((long long)s[u].size()) - 1;
      while (l <= r) {
        long long mid = l + r >> 1;
        if (s[u][mid].r1 < x)
          ans = mid, l = mid + 1;
        else
          r = mid - 1;
      }
      if (ans != -1)
        res -= 1ll * (min(s[u][ans].l2, y) - max(s[u][lans].l1, x) + 1) *
               (x - s[u][lans].r1);
    }
  }
  if (t[u].first) {
    long long l1 = max(t[u].first, x), l2 = y;
    long long r1 = max(t[u].down, x), r2 = min(t[u].up, y);
    if (l1 <= l2 && r1 <= r2) res += 1ll * (l2 - l1 + 1) * (r2 - r1 + 1);
  }
  return res;
}
inline void end(long long u, long long x) {
  long long sum = (s[u].empty() ? 0 : s[u].back().sum);
  s[u].push_back(
      (Node){t[u].first, x - 1, t[u].down, t[u].up,
             sum + 1ll * (x - t[u].first) * (t[u].up - t[u].down + 1)});
  t[u].first = 0;
}
inline void add(long long x, long long down, long long up, long long u) {
  if (t[u].first)
    end(u, x);
  else
    t[u].down = down;
  t[u].up = up, t[u].first = x;
}
signed main() {
  read(n);
  for (long long i = (1); i <= (n); i++) {
    read(a), read(l), read(r), read(k);
    a = (a + lastans) % (n + 1);
    k = (k + lastans) % (n + 1);
    l = (l + lastans) % i + 1, r = (r + lastans) % i + 1;
    if (l > r) swap(l, r);
    if (t[a].first) {
      long long down = t[a].down, up = t[a].up;
      for (long long x = (a + 1); x <= (n); x++) {
        if (pos[x] < down) {
          add(i, down, up, x);
          break;
        } else if (pos[x] < up) {
          add(i, pos[x] + 1, up, x);
          up = pos[x];
        }
      }
      end(a, i);
    }
    add(i, i, i, !a);
    pos[a] = i;
    printf("%lld\n", lastans = query(k, l, r));
  }
  return 0;
}

### Prompt

Your challenge is to write 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;
template <class T>
inline bool ylmin(T &a, T b) {
  return a < b ? 0 : (a = b, 1);
}
template <class T>
inline bool ylmax(T &a, T b) {
  return a > b ? 0 : (a = b, 1);
}
template <class T>
inline T abs(T x) {
  return x < 0 ? -x : x;
}
inline char read() {
  static const long long IO_LEN = 1024 * 1024;
  static char buf[IO_LEN], *ioh, *iot;
  if (ioh == iot) {
    iot = (ioh = buf) + fread(buf, 1, IO_LEN, stdin);
    if (ioh == iot) return -1;
  }
  return *ioh++;
}
template <class T>
inline void read(T &x) {
  static long long iosig;
  static char ioc;
  for (iosig = 0, ioc = read(); !isdigit(ioc); ioc = read())
    if (ioc == '-') iosig = 1;
  for (x = 0; isdigit(ioc); ioc = read()) x = (x << 1) + (x << 3) + (ioc ^ '0');
  if (iosig) x = -x;
}
const long long MAXN = 2e5 + 10;
long long n, pos[MAXN];
long long lastans, a, l, r, k;
struct Node {
  long long l1, l2, r1, r2;
  long long sum;
};
struct lastNode {
  long long up, down, first;
} t[MAXN];
vector<Node> s[MAXN];
inline long long query(long long u, long long x, long long y) {
  long long l = 0, r = ((long long)s[u].size()) - 1;
  long long lans = -1, rans = -1;
  while (l <= r) {
    long long mid = l + r >> 1;
    if (s[u][mid].l2 >= x && s[u][mid].r2 >= x)
      lans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  l = 0, r = ((long long)s[u].size()) - 1;
  while (l <= r) {
    long long mid = l + r >> 1;
    if (s[u][mid].l1 <= y && s[u][mid].r1 <= y)
      rans = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  long long res = 0;
  if (lans <= rans && lans != -1 && rans != -1) {
    res = s[u][rans].sum - (lans ? s[u][lans - 1].sum : 0);
    if (s[u][lans].l1 < x)
      res -= 1ll * (x - s[u][lans].l1) * (s[u][lans].r2 - s[u][lans].r1 + 1);
    if (s[u][rans].l2 > y)
      res -= 1ll * (s[u][rans].l2 - y) * (s[u][rans].r2 - s[u][rans].r1 + 1);
    if (s[u][lans].r1 < x) {
      long long ans = -1;
      l = 0, r = ((long long)s[u].size()) - 1;
      while (l <= r) {
        long long mid = l + r >> 1;
        if (s[u][mid].r1 < x)
          ans = mid, l = mid + 1;
        else
          r = mid - 1;
      }
      if (ans != -1)
        res -= 1ll * (min(s[u][ans].l2, y) - max(s[u][lans].l1, x) + 1) *
               (x - s[u][lans].r1);
    }
  }
  if (t[u].first) {
    long long l1 = max(t[u].first, x), l2 = y;
    long long r1 = max(t[u].down, x), r2 = min(t[u].up, y);
    if (l1 <= l2 && r1 <= r2) res += 1ll * (l2 - l1 + 1) * (r2 - r1 + 1);
  }
  return res;
}
inline void end(long long u, long long x) {
  long long sum = (s[u].empty() ? 0 : s[u].back().sum);
  s[u].push_back(
      (Node){t[u].first, x - 1, t[u].down, t[u].up,
             sum + 1ll * (x - t[u].first) * (t[u].up - t[u].down + 1)});
  t[u].first = 0;
}
inline void add(long long x, long long down, long long up, long long u) {
  if (t[u].first)
    end(u, x);
  else
    t[u].down = down;
  t[u].up = up, t[u].first = x;
}
signed main() {
  read(n);
  for (long long i = (1); i <= (n); i++) {
    read(a), read(l), read(r), read(k);
    a = (a + lastans) % (n + 1);
    k = (k + lastans) % (n + 1);
    l = (l + lastans) % i + 1, r = (r + lastans) % i + 1;
    if (l > r) swap(l, r);
    if (t[a].first) {
      long long down = t[a].down, up = t[a].up;
      for (long long x = (a + 1); x <= (n); x++) {
        if (pos[x] < down) {
          add(i, down, up, x);
          break;
        } else if (pos[x] < up) {
          add(i, pos[x] + 1, up, x);
          up = pos[x];
        }
      }
      end(a, i);
    }
    add(i, i, i, !a);
    pos[a] = i;
    printf("%lld\n", lastans = query(k, l, r));
  }
  return 0;
}

```