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>
using namespace std;
const int Maxn = 200005;
int n, q, Node_ct, ans, lastans, bel[Maxn], cnt[300][Maxn];
struct Node {
  Node *son[2], *fa;
  int siz, val;
} * root, pool[Maxn];
void create(Node *&t) {
  t = &pool[++Node_ct];
  t->siz = 1;
}
bool get_son(Node *t) { return t->fa->son[1] == t; }
void update(Node *t) {
  t->siz = 1;
  if (t->son[0] != NULL) t->siz += t->son[0]->siz;
  if (t->son[1] != NULL) t->siz += t->son[1]->siz;
}
void rotate(Node *t) {
  Node *fa = t->fa, *gf = fa->fa;
  if (gf != NULL) {
    bool ss = get_son(fa);
    gf->son[ss] = t;
  } else
    root = t;
  bool s = get_son(t);
  fa->son[s] = t->son[!s];
  if (fa->son[s] != NULL) fa->son[s]->fa = fa;
  t->son[!s] = fa;
  fa->fa = t;
  t->fa = gf;
  update(fa), update(t);
}
void splay(Node *t, Node *goal = NULL) {
  while (t->fa != goal) {
    Node *fa = t->fa, *gf = fa->fa;
    if (gf == goal)
      rotate(t);
    else {
      if (get_son(t) ^ get_son(fa))
        rotate(t), rotate(t);
      else
        rotate(fa), rotate(t);
    }
  }
}
Node *find_kth(int k) {
  k++;
  for (Node *t = root; t != NULL;) {
    if (t->son[0] == NULL) {
      if (k == 1) {
        splay(t);
        return t;
      }
      k--, t = t->son[1];
    } else {
      if (k == t->son[0]->siz + 1) {
        splay(t);
        return t;
      }
      if (k <= t->son[0]->siz)
        t = t->son[0];
      else
        k -= t->son[0]->siz + 1, t = t->son[1];
    }
  }
}
void insert(int val) {
  if (root == NULL) {
    create(root);
    root->val = val;
  } else {
    Node *tmp = root;
    while (tmp->son[1] != NULL) tmp = tmp->son[1];
    splay(tmp);
    create(tmp->son[1]);
    tmp->son[1]->val = val;
    tmp->son[1]->fa = tmp;
    splay(tmp->son[1]);
  }
}
void insert(Node *t, Node *now) {
  splay(t);
  if (t->son[0] == NULL)
    t->son[0] = now, now->fa = t, update(t);
  else {
    Node *tmp = t->son[0];
    while (tmp->son[1] != NULL) tmp = tmp->son[1];
    splay(tmp, t);
    t->son[0]->son[1] = now;
    now->fa = t->son[0];
  }
  splay(now);
}
Node *erase(Node *t) {
  splay(t);
  if (t->son[0] == NULL)
    root = t->son[1], root->fa = NULL;
  else {
    Node *tmp = t->son[0];
    while (tmp->son[1] != NULL) tmp = tmp->son[1];
    splay(tmp, root);
    tmp->fa = NULL;
    tmp->son[1] = root->son[1];
    if (tmp->son[1] != NULL) tmp->son[1]->fa = tmp;
    root = root->son[0];
    update(tmp);
  }
  t->son[0] = t->son[1] = NULL;
  update(t);
  return t;
}
void dfs(Node *t, int k) {
  if (t == NULL) return;
  if (t->val == k) ans++;
  if (t->son[0] != NULL) dfs(t->son[0], k);
  if (t->son[1] != NULL) dfs(t->son[1], k);
}
void work(int lt, int rt, int k) {
  Node *tmp1 = find_kth(lt - 1), *tmp2 = find_kth(rt + 1);
  splay(tmp1), splay(tmp2, tmp1);
  dfs(tmp2->son[0], k);
}
int main() {
  scanf("%d", &n);
  int bloc = sqrt(n / log2(n) / 1.8), per = n / bloc;
  for (int i = 1; i <= n; i++) bel[i] = (i - 1) / per + 1;
  insert(-0x3f3f3f3f);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    insert(x);
    cnt[bel[i]][x]++;
  }
  insert(0x3f3f3f3f);
  scanf("%d", &q);
  while (q--) {
    int opt, l, r;
    scanf("%d", &opt);
    if (opt == 1) {
      scanf("%d%d", &l, &r);
      l = (l + lastans - 1) % n + 1;
      r = (r + lastans - 1) % n + 1;
      if (l == r) continue;
      if (l > r) swap(l, r);
      if (bel[l] == bel[r]) goto A;
      cnt[bel[r]][find_kth(r)->val]--;
      cnt[bel[l]][find_kth(r)->val]++;
      for (int i = bel[l]; i <= bel[r] - 1; i++)
        cnt[i][find_kth(i * per)->val]--, cnt[i + 1][find_kth(i * per)->val]++;
    A:
      Node *tmp = find_kth(l);
      insert(tmp, erase(find_kth(r)));
    } else {
      ans = 0;
      int k;
      scanf("%d%d%d", &l, &r, &k);
      l = (l + lastans - 1) % n + 1;
      r = (r + lastans - 1) % n + 1;
      k = (k + lastans - 1) % n + 1;
      if (l > r) swap(l, r);
      if (bel[l] == bel[r])
        work(l, r, k);
      else {
        work(l, per * bel[l], k);
        for (int i = bel[l] + 1; i <= bel[r] - 1; i++) ans += cnt[i][k];
        work(per * (bel[r] - 1) + 1, r, k);
      }
      printf("%d\n", lastans = ans);
    }
  }
  return 0;
}