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 mx = (int)3e5 + 1000;
int rt[mx], lans, n, m;
int f[mx], ch[mx][2], sz[mx], bel[mx], key[mx], b[mx], cnt;
void up(int x) { sz[x] = sz[ch[x][1]] + sz[ch[x][0]] + 1; }
int newnode(int ff, int who) {
  sz[who] = 1;
  ch[who][1] = ch[who][0] = 0;
  f[who] = ff;
  return who;
}
void zig(int x, int p) {
  int y = f[x], z = f[y];
  ch[y][!p] = ch[x][p];
  f[ch[x][p]] = y;
  f[x] = z;
  if (z) ch[z][ch[z][1] == y] = x;
  ch[x][p] = y;
  f[y] = x;
  up(y);
  up(x);
}
void spaly(int x, int to) {
  while (f[x] != to) {
    if (f[f[x]] == to)
      zig(x, ch[f[x]][0] == x);
    else {
      int y = f[x], z = f[y];
      int p = (ch[z][0] == y);
      if (ch[y][p] == x)
        zig(x, !p);
      else
        zig(y, p);
      zig(x, p);
    }
  }
  if (to == 0) rt[bel[x]] = x;
}
void insert(int f, int p, int who) {
  if (!f)
    rt[bel[who]] = newnode(0, who);
  else {
    ch[f][p] = newnode(f, who);
    spaly(who, 0);
  }
}
int find(int x, int k) {
  if (!x || sz[x] < k) return 0;
  while (x) {
    if (k == sz[ch[x][0]] + 1) break;
    if (k > sz[ch[x][0]] + 1)
      k -= sz[ch[x][0]] + 1, x = ch[x][1];
    else
      x = ch[x][0];
  }
  return x;
}
int pre(int rr) {
  if (!rr || !ch[rr][0]) return 0;
  int x = ch[rr][0];
  while (ch[x][1]) x = ch[x][1];
  return x;
}
int suc(int rr) {
  if (!rr || !ch[rr][1]) return 0;
  int x = ch[rr][1];
  while (ch[x][0]) x = ch[x][0];
  return x;
}
void del(int &r, int x) {
  int q = pre(r), p = suc(r);
  if (!q && !p) {
    r = 0;
    return;
  }
  if (!q) {
    spaly(p, 0);
    ch[p][0] = 0;
    up(p);
    return;
  }
  if (!p) {
    spaly(q, 0);
    ch[q][1] = 0;
    up(q);
    return;
  }
  spaly(q, 0);
  spaly(p, q);
  ch[p][0] = 0;
  up(p);
  up(q);
}
int ans;
inline int Size(int x) {
  x -= n;
  spaly(x, 0);
  return sz[ch[x][0]] + 1;
}
void get(int x, int limi) {
  if (!x) return;
  while (x) {
    if (Size(x) >= limi)
      ans = x, x = ch[x][0];
    else
      x = ch[x][1];
  }
}
void get1(int x, int limi) {
  if (!x) return;
  while (x) {
    if (Size(x) <= limi)
      ans = x, x = ch[x][1];
    else
      x = ch[x][0];
  }
}
void sov(int k, int k1) {
  spaly(k, 0);
  del(rt[bel[k]], k);
  spaly(k1, 0);
  if (sz[ch[k1][0]] == 0)
    insert(k1, 0, k);
  else {
    int q = pre(rt[bel[k1]]);
    spaly(q, k1);
    insert(q, 1, k);
  }
}
void put(int t, int ps) {
  if (!t) return;
  put(ch[t][0], ps);
  cout << b[key[t] - 1] << ' ';
  put(ch[t][1], ps);
}
void solve1(int l, int r) {
  if (l == r) return;
  int k = find(rt[0], r);
  int k1 = find(rt[0], l);
  sov(k, k1);
  ans = 0;
  get(rt[key[k]], l);
  if (ans == k + n) return;
  sov(k + n, ans);
}
void solve2(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;
  get(rt[k], l), k1 = ans, ans = 0;
  get1(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 {
    spaly(k1, 0);
    spaly(k2, k1);
    printf("%d\n", lans = sz[ch[k2][0]] + 2);
  }
}
int re() {
  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 = re();
  for (int i = 1; i <= n; ++i) key[i] = re(), b[i - 1] = key[i];
  sort(b, b + n);
  cnt = unique(b, b + n) - b;
  for (int i = 1; i <= n; ++i)
    key[i] = (int)(lower_bound(b, b + cnt, key[i]) - 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] = key[i], insert(rt[key[i]], 1, i + n);
  m = re();
  int ty, l, r, k;
  for (int i = 1; i <= m; ++i) {
    ty = re(), l = re(), r = re();
    l = (l + lans - 1) % n + 1, r = (r + lans - 1) % n + 1;
    if (l > r) swap(l, r);
    if (ty == 1)
      solve1(l, r);
    else
      k = (re() + lans - 1) % n + 1, solve2(l, r, k);
  }
  return 0;
}