#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n;
map<int, int> order;
int minimum[2 * 200000 + 1];
struct node {
  node* left_child = nullptr;
  node* right_child = nullptr;
  ll length, area = 0;
};
node* root[200000 + 1];
void update_segment_tree(int a, int i) {
  minimum[n + 1 + a] = i;
  for (int j = (n + 1 + a) / 2; j > 0; j /= 2) {
    minimum[j] = min(minimum[2 * j], minimum[2 * j + 1]);
  }
}
int query_segment_tree(int a) {
  int minimum_position = minimum[n + 1 + a];
  for (int left = n + 1, right = n + 1 + a; left < right;
       left /= 2, right /= 2) {
    if (left & 1) {
      minimum_position = min(minimum_position, minimum[left]);
      left++;
    }
    if (right & 1) {
      right--;
      minimum_position = min(minimum_position, minimum[right]);
    }
  }
  return minimum_position;
}
node* update_persistent_segment_tree(node* current_node, int sub_start,
                                     int sub_end, ll length, ll area,
                                     int low = 0, int high = n - 1) {
  if (current_node == nullptr) {
    current_node = new node();
  } else {
    current_node = new node(*current_node);
  }
  if (sub_start <= low && high <= sub_end) {
    current_node->length += length;
    current_node->area += area;
  } else {
    int mid = (low + high) / 2;
    if (sub_start <= mid) {
      current_node->left_child = update_persistent_segment_tree(
          current_node->left_child, sub_start, sub_end, length, area, low, mid);
    }
    if (mid + 1 <= sub_end) {
      current_node->right_child =
          update_persistent_segment_tree(current_node->right_child, sub_start,
                                         sub_end, length, area, mid + 1, high);
    }
  }
  return current_node;
}
ll query_persistent_segment_tree(node* current_node, int k, int r, int low = 0,
                                 int high = n - 1) {
  if (current_node == nullptr) {
    return 0;
  } else {
    ll total_area = current_node->length * r + current_node->area;
    if (low == high) {
      return total_area;
    } else {
      int mid = (low + high) / 2;
      if (k <= mid) {
        total_area += query_persistent_segment_tree(current_node->left_child, k,
                                                    r, low, mid);
      } else {
        total_area += query_persistent_segment_tree(current_node->right_child,
                                                    k, r, mid + 1, high);
      }
      return total_area;
    }
  }
}
void update(int a, int i) {
  update_segment_tree(a, i);
  root[i] = root[i - 1];
  if (order.count(a)) {
    int sub_start = a;
    int end = (++order.find(a))->first;
    int previous_order_position = (--order.find(a))->second;
    int previous_position = order[a];
    order.erase(a);
    while (sub_start < end) {
      int current_position = query_segment_tree(sub_start);
      int low = sub_start, high = end - 1, sub_end = -1;
      while (low <= high) {
        int mid = (low + high) / 2;
        if (query_segment_tree(mid) == current_position) {
          sub_end = mid;
          low = mid + 1;
        } else {
          high = mid - 1;
        }
      }
      root[i] = update_persistent_segment_tree(
          root[i], sub_start, sub_end, current_position - previous_position,
          -ll(current_position - previous_position) * (i - 1));
      if (current_position != previous_order_position) {
        order[sub_start] = current_position;
      }
      sub_start = sub_end + 1;
    }
  }
}
ll query(int l, int r, int k) {
  if (k < 0) {
    return ll(r - l + 2) * ll(r - l + 1) / 2;
  } else if (query_persistent_segment_tree(root[r], k, r) -
                 query_persistent_segment_tree(root[r - 1], k, r - 1) <
             l) {
    return 0;
  } else {
    int low = l - 1, high = r - 1, sub_end = -1;
    while (low <= high) {
      int mid = (low + high) / 2;
      if (query_persistent_segment_tree(root[mid + 1], k, mid + 1) -
              query_persistent_segment_tree(root[mid], k, mid) >=
          l) {
        sub_end = mid;
        high = mid - 1;
      } else {
        low = mid + 1;
      }
    }
    return query_persistent_segment_tree(root[r], k, r) -
           query_persistent_segment_tree(root[sub_end], k, sub_end) -
           ll(l - 1) * ll(r - sub_end);
  }
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  int a, l, r, k;
  ll lans = 0;
  order[-1] = -1, order[0] = 0, order[n + 1] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a >> l >> r >> k;
    a = (a + lans) % (n + 1);
    l = (l + lans) % i + 1;
    r = (r + lans) % i + 1;
    if (l > r) {
      swap(l, r);
    }
    k = (k + lans) % (n + 1);
    update(a, i);
    lans = query(l, r, k - 1) - query(l, r, k);
    cout << lans << endl;
  }
  return 0;
}

### Prompt

Please create a solution in CPP 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;
int n;
map<int, int> order;
int minimum[2 * 200000 + 1];
struct node {
  node* left_child = nullptr;
  node* right_child = nullptr;
  ll length, area = 0;
};
node* root[200000 + 1];
void update_segment_tree(int a, int i) {
  minimum[n + 1 + a] = i;
  for (int j = (n + 1 + a) / 2; j > 0; j /= 2) {
    minimum[j] = min(minimum[2 * j], minimum[2 * j + 1]);
  }
}
int query_segment_tree(int a) {
  int minimum_position = minimum[n + 1 + a];
  for (int left = n + 1, right = n + 1 + a; left < right;
       left /= 2, right /= 2) {
    if (left & 1) {
      minimum_position = min(minimum_position, minimum[left]);
      left++;
    }
    if (right & 1) {
      right--;
      minimum_position = min(minimum_position, minimum[right]);
    }
  }
  return minimum_position;
}
node* update_persistent_segment_tree(node* current_node, int sub_start,
                                     int sub_end, ll length, ll area,
                                     int low = 0, int high = n - 1) {
  if (current_node == nullptr) {
    current_node = new node();
  } else {
    current_node = new node(*current_node);
  }
  if (sub_start <= low && high <= sub_end) {
    current_node->length += length;
    current_node->area += area;
  } else {
    int mid = (low + high) / 2;
    if (sub_start <= mid) {
      current_node->left_child = update_persistent_segment_tree(
          current_node->left_child, sub_start, sub_end, length, area, low, mid);
    }
    if (mid + 1 <= sub_end) {
      current_node->right_child =
          update_persistent_segment_tree(current_node->right_child, sub_start,
                                         sub_end, length, area, mid + 1, high);
    }
  }
  return current_node;
}
ll query_persistent_segment_tree(node* current_node, int k, int r, int low = 0,
                                 int high = n - 1) {
  if (current_node == nullptr) {
    return 0;
  } else {
    ll total_area = current_node->length * r + current_node->area;
    if (low == high) {
      return total_area;
    } else {
      int mid = (low + high) / 2;
      if (k <= mid) {
        total_area += query_persistent_segment_tree(current_node->left_child, k,
                                                    r, low, mid);
      } else {
        total_area += query_persistent_segment_tree(current_node->right_child,
                                                    k, r, mid + 1, high);
      }
      return total_area;
    }
  }
}
void update(int a, int i) {
  update_segment_tree(a, i);
  root[i] = root[i - 1];
  if (order.count(a)) {
    int sub_start = a;
    int end = (++order.find(a))->first;
    int previous_order_position = (--order.find(a))->second;
    int previous_position = order[a];
    order.erase(a);
    while (sub_start < end) {
      int current_position = query_segment_tree(sub_start);
      int low = sub_start, high = end - 1, sub_end = -1;
      while (low <= high) {
        int mid = (low + high) / 2;
        if (query_segment_tree(mid) == current_position) {
          sub_end = mid;
          low = mid + 1;
        } else {
          high = mid - 1;
        }
      }
      root[i] = update_persistent_segment_tree(
          root[i], sub_start, sub_end, current_position - previous_position,
          -ll(current_position - previous_position) * (i - 1));
      if (current_position != previous_order_position) {
        order[sub_start] = current_position;
      }
      sub_start = sub_end + 1;
    }
  }
}
ll query(int l, int r, int k) {
  if (k < 0) {
    return ll(r - l + 2) * ll(r - l + 1) / 2;
  } else if (query_persistent_segment_tree(root[r], k, r) -
                 query_persistent_segment_tree(root[r - 1], k, r - 1) <
             l) {
    return 0;
  } else {
    int low = l - 1, high = r - 1, sub_end = -1;
    while (low <= high) {
      int mid = (low + high) / 2;
      if (query_persistent_segment_tree(root[mid + 1], k, mid + 1) -
              query_persistent_segment_tree(root[mid], k, mid) >=
          l) {
        sub_end = mid;
        high = mid - 1;
      } else {
        low = mid + 1;
      }
    }
    return query_persistent_segment_tree(root[r], k, r) -
           query_persistent_segment_tree(root[sub_end], k, sub_end) -
           ll(l - 1) * ll(r - sub_end);
  }
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  int a, l, r, k;
  ll lans = 0;
  order[-1] = -1, order[0] = 0, order[n + 1] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a >> l >> r >> k;
    a = (a + lans) % (n + 1);
    l = (l + lans) % i + 1;
    r = (r + lans) % i + 1;
    if (l > r) {
      swap(l, r);
    }
    k = (k + lans) % (n + 1);
    update(a, i);
    lans = query(l, r, k - 1) - query(l, r, k);
    cout << lans << endl;
  }
  return 0;
}

```