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 N = 3e5 + 10;
int rt[N], cnt, b[N], bel[N], lans, n, m;
struct node {
  int fa, siz, val;
  int son[2];
} tr[N];
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
inline bool which_son(int x) { return x == tr[tr[x].fa].son[1]; }
inline void pushup(int x) {
  tr[x].siz = tr[tr[x].son[0]].siz + tr[tr[x].son[1]].siz + 1;
}
int newnode(int f, int x) {
  tr[x].siz = 1;
  tr[x].son[0] = tr[x].son[1] = 0;
  tr[x].fa = f;
  return x;
}
inline void rotate(int x) {
  int f = tr[x].fa, ff = tr[f].fa;
  bool w = which_son(x);
  tr[f].son[w] = tr[x].son[w ^ 1];
  tr[tr[x].son[w ^ 1]].fa = f;
  tr[x].fa = ff;
  tr[x].son[w ^ 1] = f;
  tr[f].fa = x;
  if (ff) {
    tr[ff].son[tr[ff].son[1] == f] = x;
  }
  pushup(f);
  pushup(x);
}
inline void splay(int x, int goal) {
  for (int f; (f = tr[x].fa) != goal;) {
    if (tr[f].fa != goal) {
      rotate(which_son(x) == which_son(f) ? f : x);
    }
    rotate(x);
  }
  if (goal == 0) {
    rt[bel[x]] = x;
  }
}
inline void insert(int f, int p, int x) {
  if (!f)
    rt[bel[x]] = newnode(0, x);
  else {
    tr[f].son[p] = newnode(f, x);
    splay(x, 0);
  }
}
int find(int x, int k) {
  if (!x || tr[x].siz < k) return 0;
  while (x) {
    if (k == tr[tr[x].son[0]].siz + 1) break;
    if (k > tr[tr[x].son[0]].siz + 1)
      k -= tr[tr[x].son[0]].siz + 1, x = tr[x].son[1];
    else
      x = tr[x].son[0];
  }
  return x;
}
inline int pre(int x) {
  int now = tr[x].son[0];
  while (tr[now].son[1]) now = tr[now].son[1];
  return now;
}
inline int nxt(int x) {
  int now = tr[x].son[1];
  while (tr[now].son[0]) now = tr[now].son[0];
  return now;
}
inline void del(int &r, int x) {
  int q = pre(r), p = nxt(r);
  if (!q && !p) {
    r = 0;
    return;
  }
  if (!q) {
    splay(p, 0);
    tr[p].son[0] = 0;
    pushup(p);
    return;
  }
  if (!p) {
    splay(q, 0);
    tr[q].son[1] = 0;
    pushup(q);
    return;
  }
  splay(q, 0);
  splay(p, q);
  tr[p].son[0] = 0;
  pushup(p);
  pushup(q);
}
int ans;
inline int s_ize(int x) {
  x -= n;
  splay(x, 0);
  return tr[tr[x].son[0]].siz + 1;
}
inline void _pre(int x, int lim) {
  if (!x) return;
  while (x) {
    if (s_ize(x) >= lim)
      ans = x, x = tr[x].son[0];
    else
      x = tr[x].son[1];
  }
}
inline void _nxt(int x, int lim) {
  if (!x) return;
  while (x) {
    if (s_ize(x) <= lim)
      ans = x, x = tr[x].son[1];
    else
      x = tr[x].son[0];
  }
}
inline void change(int k, int k1) {
  splay(k, 0);
  del(rt[bel[k]], k);
  splay(k1, 0);
  if (tr[tr[k1].son[0]].siz == 0)
    insert(k1, 0, k);
  else {
    int q = pre(rt[bel[k1]]);
    splay(q, k1);
    insert(q, 1, k);
  }
}
inline void moving(int l, int r) {
  if (l == r) return;
  int k = find(rt[0], r), k1 = find(rt[0], l);
  change(k, k1);
  ans = 0;
  _pre(rt[tr[k].val], l);
  if (ans == k + n) return;
  change(k + n, ans);
}
inline void query(int l, int r, int k) {
  int t = lower_bound(b + 1, b + n + 1, k) - b;
  if (!rt[t] || b[t] != k) {
    printf("%d\n", lans = 0);
    return;
  }
  k = t;
  int k1, k2;
  ans = 0;
  _pre(rt[k], l), k1 = ans, ans = 0;
  _nxt(rt[k], r), k2 = ans, ans = 0;
  if (k1 == k2 && k1) {
    puts("1");
    lans = 1;
  } else if (!k1 || !k2 || s_ize(k1) > s_ize(k2)) {
    puts("0");
    lans = 0;
  } else {
    splay(k1, 0);
    splay(k2, k1);
    printf("%d\n", lans = tr[tr[k2].son[0]].siz + 2);
  }
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) tr[i].val = read(), b[i] = tr[i].val;
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; i++)
    tr[i].val = lower_bound(b + 1, b + n + 1, tr[i].val) - b;
  for (int i = 1; i <= n; i++) bel[i] = 0, insert(rt[0], 1, i);
  for (int i = 1; i <= n; i++)
    bel[i + n] = tr[i].val, insert(rt[tr[i].val], 1, i + n);
  m = read();
  for (int i = 1, op, l, r, k; i <= m; i++) {
    op = read(), l = read(), r = read();
    l = (l + lans - 1) % n + 1, r = (r + lans - 1) % n + 1;
    if (l > r) swap(l, r);
    if (op == 1)
      moving(l, r);
    else
      k = (read() + lans - 1) % n + 1, query(l, r, k);
  }
  return 0;
}