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
#include <bits/stdc++.h>
using namespace std;
const int N = (int)3e5 + 1000;
int rt[N], lans, n, m;
int bel[N], b[N], cnt;
struct node {
  int fa, siz, val;
  int son[2];
} tr[N];
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;
}
void rotate(int x) {
  int y = tr[x].fa, z = tr[y].fa;
  bool w = which_son(x);
  tr[y].son[w] = tr[x].son[w ^ 1];
  tr[tr[x].son[w ^ 1]].fa = y;
  tr[x].fa = z;
  if (z) tr[z].son[tr[z].son[1] == y] = x;
  tr[x].son[w ^ 1] = y;
  tr[y].fa = x;
  pushup(y);
  pushup(x);
}
void splay(int x, int goal) {
  for (int f; (f = tr[x].fa) != goal;) {
    if (tr[f].fa == goal)
      rotate(x);
    else {
      int z = tr[f].fa;
      int p = !which_son(f);
      if (tr[f].son[p] == x)
        rotate(x);
      else
        rotate(f);
      rotate(x);
    }
  }
  if (goal == 0) rt[bel[x]] = x;
}
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;
}
int pre(int rr) {
  if (!rr || !tr[rr].son[0]) return 0;
  int x = tr[rr].son[0];
  while (tr[x].son[1]) x = tr[x].son[1];
  return x;
}
int nxt(int rr) {
  if (!rr || !tr[rr].son[1]) return 0;
  int x = tr[rr].son[1];
  while (tr[x].son[0]) x = tr[x].son[0];
  return x;
}
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 Size(int x) {
  x -= n;
  splay(x, 0);
  return tr[tr[x].son[0]].siz + 1;
}
void _pre(int x, int limi) {
  if (!x) return;
  while (x) {
    if (Size(x) >= limi)
      ans = x, x = tr[x].son[0];
    else
      x = tr[x].son[1];
  }
}
void _nxt(int x, int limi) {
  if (!x) return;
  while (x) {
    if (Size(x) <= limi)
      ans = x, x = tr[x].son[1];
    else
      x = tr[x].son[0];
  }
}
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);
  }
}
void moving(int l, int r) {
  if (l == r) return;
  int k = find(rt[0], r);
  int 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);
}
void query(int l, int r, int k) {
  int t = (int)(lower_bound(b, b + cnt, k) - b) + 1;
  if (!rt[t] || b[t - 1] != 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 || Size(k1) > Size(k2)) {
    puts("0");
    lans = 0;
  } else {
    splay(k1, 0);
    splay(k2, k1);
    printf("%d\n", lans = tr[tr[k2].son[0]].siz + 2);
  }
}
int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
  return x * f;
}
int main() {
  n = read();
  for (int i = 1; i <= n; ++i) tr[i].val = read(), b[i - 1] = tr[i].val;
  sort(b, b + n);
  cnt = unique(b, b + n) - b;
  for (int i = 1; i <= n; ++i)
    tr[i].val = (int)(lower_bound(b, b + cnt, tr[i].val) - b) + 1;
  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();
  int ty, l, r, k;
  for (int i = 1; i <= m; ++i) {
    ty = read(), l = read(), r = read();
    l = (l + lans - 1) % n + 1, r = (r + lans - 1) % n + 1;
    if (l > r) swap(l, r);
    if (ty == 1)
      moving(l, r);
    else
      k = (read() + lans - 1) % n + 1, query(l, r, k);
  }
  return 0;
}
