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>
using namespace std;
struct segment_tree {
  segment_tree(int size) : n(size) { tree.resize(2 * n - 1); }
  void modify(int position, int value) { modify(0, 0, n - 1, position, value); }
  int query(int position) { return query(0, 0, n - 1, position); }

 private:
  vector<int> tree;
  int n;
  void pull(int u, int x, int y) { tree[u] = min(tree[x], tree[y]); }
  void modify(int u, int l, int r, int position, int value) {
    if (l == r) {
      tree[u] = value;
    } else {
      int mid = (l + r) / 2;
      int x = u + 1, y = u + (mid - l + 1) * 2;
      if (position <= mid) {
        modify(x, l, mid, position, value);
      } else {
        modify(y, mid + 1, r, position, value);
      }
      pull(u, x, y);
    }
  }
  int query(int u, int l, int r, int position) {
    if (l == r) {
      return tree[u];
    } else {
      int mid = (l + r) / 2;
      int x = u + 1, y = u + (mid - l + 1) * 2;
      if (position <= mid) {
        return query(x, l, mid, position);
      } else {
        return min(tree[x], query(y, mid + 1, r, position));
      }
    }
  }
};
struct persistent_segment_tree {
  persistent_segment_tree(int size) : n(size) { tree.push_back(nullptr); }
  void add() { tree.push_back(tree.back()); }
  void modify(int l, int r, long long k, long long b) {
    modify(tree.back(), 0, n - 1, l, r, k, b);
  }
  long long query(int position, int value) {
    return query(tree[value + 1], 0, n - 1, position, value + 1);
  }

 private:
  struct node {
    node *left, *right;
    long long k, b;
    node() : left(nullptr), right(nullptr), k(0), b(0) {}
  };
  vector<node *> tree;
  int n;
  void modify(node *&u, int l, int r, int ql, int qr, long long k,
              long long b) {
    if (u == nullptr) {
      u = new node();
    } else {
      u = new node(*u);
    }
    if (ql <= l && r <= qr) {
      u->k += k;
      u->b += b;
    } else {
      int mid = (l + r) / 2;
      if (ql <= mid) {
        modify(u->left, l, mid, ql, qr, k, b);
      }
      if (mid < qr) {
        modify(u->right, mid + 1, r, ql, qr, k, b);
      }
    }
  }
  long long query(node *u, int l, int r, int position, int value) {
    if (u == nullptr) {
      return 0;
    } else {
      long long result = u->k * value + u->b;
      if (l == r) {
        return result;
      } else {
        int mid = (l + r) / 2;
        if (position <= mid) {
          result += query(u->left, l, mid, position, value);
        } else {
          result += query(u->right, mid + 1, r, position, value);
        }
        return result;
      }
    }
  }
};
int main() {
  do {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
  } while (false);
  int n;
  cin >> n;
  segment_tree sgt(n);
  persistent_segment_tree persis(n);
  map<int, int> important;
  important[-1] = -1;
  important[0] = 0;
  important[n + 1] = 0;
  long long last = 0;
  for (int i = 0; i < n; i++) {
    int a, l, r, k;
    cin >> a >> l >> r >> k;
    a = (a + last) % (n + 1);
    l = (l + last) % (i + 1);
    r = (r + last) % (i + 1);
    if (l > r) {
      swap(l, r);
    }
    k = (k + last) % (n + 1);
    sgt.modify(a, i + 1);
    persis.add();
    if (important.count(a)) {
      int left = (--important.find(a))->second,
          right = (++important.find(a))->first, original = important[a];
      important.erase(a);
      int current = a;
      while (current < right) {
        int value = sgt.query(current), low = current, high = right - 1,
            result = -1;
        while (low <= high) {
          int mid = (low + high) / 2;
          if (sgt.query(mid) == value) {
            result = mid;
            low = mid + 1;
          } else {
            high = mid - 1;
          }
        }
        persis.modify(current, result, value - original,
                      -(long long)(value - original) * i);
        if (value != left) {
          important[current] = value;
        }
        current = result + 1;
      }
    }
    auto solve = [&](int ql, int qr, int limit) {
      if (limit < 0) {
        return (long long)(qr - ql + 2) * (qr - ql + 1) / 2;
      } else if (persis.query(limit, qr) - persis.query(limit, qr - 1) <= ql) {
        return 0ll;
      } else {
        int left = ql, right = qr, result = -1;
        while (left <= right) {
          int mid = (left + right) / 2;
          if (persis.query(limit, mid) - persis.query(limit, mid - 1) > ql) {
            result = mid;
            right = mid - 1;
          } else {
            left = mid + 1;
          }
        }
        return (persis.query(limit, qr) - persis.query(limit, result - 1)) -
               (long long)ql * (qr - result + 1);
      }
    };
    last = solve(l, r, k - 1) - solve(l, r, k);
    cout << last << "\n";
  }
  return 0;
}