Problem: Serega loves fun. However, everyone has fun in the unique manner. Serega has fun by solving query problems. One day Fedor came up with such a problem.

You are given an array a consisting of n positive integers and queries to it. The queries can be of two types:

  1. Make a unit cyclic shift to the right on the segment from l to r (both borders inclusive). That is rearrange elements of the array in the following manner:a[l], a[l + 1], ..., a[r - 1], a[r] → a[r], a[l], a[l + 1], ..., a[r - 1].
  2. Count how many numbers equal to k are on the segment from l to r (both borders inclusive). 



Fedor hurried to see Serega enjoy the problem and Serega solved it really quickly. Let's see, can you solve it?

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of elements of the array. The second line contains n integers a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ n).

The third line contains a single integer q (1 ≤ q ≤ 105) — the number of queries. The next q lines contain the queries.

As you need to respond to the queries online, the queries will be encoded. A query of the first type will be given in format: 1 l'i r'i. A query of the second type will be given in format: 2 l'i r'i k'i. All the number in input are integer. They satisfy the constraints: 1 ≤ l'i, r'i, k'i ≤ n.

To decode the queries from the data given in input, you need to perform the following transformations:

li = ((l'i + lastans - 1) mod n) + 1; ri = ((r'i + lastans - 1) mod n) + 1; ki = ((k'i + lastans - 1) mod n) + 1.

Where lastans is the last reply to the query of the 2-nd type (initially, lastans = 0). If after transformation li is greater than ri, you must swap these values.

Output

For each query of the 2-nd type print the answer on a single line.

Examples

Input

7
6 6 2 7 4 2 5
7
1 3 6
2 2 4 2
2 2 4 7
2 2 2 5
1 2 6
1 1 4
2 1 7 3


Output

2
1
0
0


Input

8
8 4 2 2 7 7 8 8
8
1 8 8
2 8 1 7
1 8 1
1 7 3
2 8 8 3
1 1 4
1 2 7
1 4 5


Output

2
0

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
template <typename Q>
Q &read(Q &x) {
  static char c, f;
  for (f = 0; c = getchar(), !isdigit(c);)
    if (c == '-') f = 1;
  for (x = 0; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  if (f) x = -x;
  return x;
}
template <typename Q>
Q read() {
  static Q x;
  read(x);
  return x;
}
const double alpha = 0.75;
using namespace std;
const int N = 200000 + 10;
template <typename Tp, int N>
struct holder {
  Tp da[N];
  int sz;
  void clear() { sz = 0; }
  holder() { clear(); }
  void push_back(const Tp &x) { da[sz++] = x; }
  Tp *begin() const { return da; }
  Tp *end() const { return da + sz; }
  const Tp &pop_back() { return da[--sz]; }
};
namespace sgt {
struct Node *null;
holder<int, N> rec_key;
holder<Node *, N> rec_mem;
struct Node {
  int key;
  Node *ch[2];
  int sz, nc;
  int exist;
  map<int, int> cnt;
  void maintain() {
    sz = ch[0]->sz + ch[1]->sz + exist;
    nc = ch[0]->nc + ch[1]->nc + 1;
  }
  void update() {
    cnt.clear();
    if (exist) cnt[key]++;
    for (int c = 0; c < 2; c++)
      if (ch[c] != null) {
        for (map<int, int>::iterator it = ch[c]->cnt.begin();
             it != ch[c]->cnt.end(); ++it) {
          if (it->second) cnt[it->first] += it->second;
        }
      }
  }
  int remove(int k) {
    int res, s = ch[0]->sz + exist;
    if (exist && s == k) {
      res = key;
      exist = 0;
    } else {
      if (k <= s)
        res = ch[0]->remove(k);
      else
        res = ch[1]->remove(k - s);
    }
    if (!--cnt[res]) cnt.erase(res);
    return maintain(), res;
  }
  int query(int l, int r, int x) {
    if (this == null) return 0;
    if (l <= 1 && sz <= r) return cnt.count(x) ? cnt[x] : 0;
    int res = 0;
    if (l <= ch[0]->sz) res += ch[0]->query(l, r, x);
    int s = ch[0]->sz + exist;
    if (r > s) res += ch[1]->query(l - s, r - s, x);
    if (exist && l <= s && r >= s) res += key == x;
    return res;
  }
  int query(int k, int x) {
    int res = 0;
    if (ch[0]->sz <= k)
      res += ch[0]->cnt.count(x) ? ch[0]->cnt[x] : 0, k -= ch[0]->sz;
    else
      res += ch[0]->query(k, x), k = 0;
    if (k && exist) res += (key == x), --k;
    if (k) res += ch[1]->query(k, x);
    return res;
  }
  bool bad() const { return ch[0]->nc > nc * alpha || ch[1]->nc > nc * alpha; }
  void print() {
    if (this == null) return;
    ch[0]->print();
    if (exist) rec_key.push_back(key);
    rec_mem.push_back(this);
    ch[1]->print();
  }
} pool[N], *pis, *root;
Node *newnode(int key) {
  Node *o = rec_mem.sz ? rec_mem.pop_back() : pis++;
  o->key = key;
  o->ch[0] = o->ch[1] = null;
  o->exist = o->sz = o->nc = 1;
  o->cnt.clear();
  if (key > 0) o->cnt[key] = 1;
  return o;
}
Node **insert(Node *&o, int k, int key) {
  if (o == null) return o = newnode(key), &null;
  o->cnt[key] += 1;
  Node **res;
  if (o->ch[0]->sz >= k)
    res = insert(o->ch[0], k, key);
  else
    res = insert(o->ch[1], k - o->ch[0]->sz - o->exist, key);
  return o->maintain(), o->bad() ? &o : res;
}
void init() {
  pis = pool;
  rec_mem.clear();
  null = newnode(0);
  null->ch[0] = null->ch[1] = 0;
  null->sz = null->nc = 0;
}
void divide(Node *&o, int *da, int l, int r) {
  if (l > r) return;
  int mid = (l + r) >> 1;
  o = newnode(da[mid]);
  divide(o->ch[0], da, l, mid - 1);
  divide(o->ch[1], da, mid + 1, r);
  o->maintain(), o->update();
}
void insert(int k, int key) {
  Node **o = insert(root, k, key);
  if (*o != null) {
    rec_key.clear();
    (*o)->print();
    divide(*o, rec_key.da, 0, rec_key.sz - 1);
  }
}
}  // namespace sgt
int a[N];
int main() {
  int n;
  while (scanf("%d", &n) == 1) {
    sgt::init();
    for (int i = 0; i < n; i++) {
      read(a[i]);
    }
    sgt::divide(sgt::root, a, 0, n - 1);
    int m, op, l, r, k;
    read(m);
    int last_ans = 0;
    while (m--) {
      read(op), read(l), read(r);
      l = (l + last_ans - 1) % n + 1;
      r = (r + last_ans - 1) % n + 1;
      if (l > r) swap(l, r);
      if (op == 1)
        sgt::insert(l - 1, sgt::root->remove(r));
      else {
        read(k);
        k = (k + last_ans - 1) % n + 1;
        printf("%d\n", last_ans = sgt::root->query(l, r, k));
      }
    }
  }
  return 0;
}