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;
inline int Readint() {
  int ret = 0;
  int f = 1;
  char ch;
  do {
    ch = getchar();
    if (ch == '-') f *= -1;
  } while (ch >= 0 && (ch < '0' || ch > '9'));
  while ('0' <= ch && ch <= '9') {
    ret = ret * 10 + ch - '0';
    ch = getchar();
  }
  return ret * f;
}
void open() {
  freopen("D.in", "r", stdin);
  freopen("D.out", "w", stdout);
}
void close() {
  fclose(stdin);
  fclose(stdout);
}
const int MAXN = 1E5 + 10;
const int SIZE = 333;
const int LIM = 1000;
const int MAXQ = SIZE + LIM;
int v[SIZE][MAXQ];
int p[SIZE][MAXQ];
int f[SIZE][MAXN];
int sv[SIZE];
int sp[SIZE];
int n;
int a[MAXN];
int block, block_size;
int tot;
void build() {
  tot = 0;
  for (int i = 1, _END_ = n; i <= _END_; i++) {
    if (sv[tot] == block_size) tot++;
    sv[tot]++;
    v[tot][sv[tot]] = a[i];
    if (!f[tot][a[i]]) {
      sp[tot]++;
      p[tot][sp[tot]] = a[i];
    }
    f[tot][a[i]]++;
  }
}
void clear() {
  int k = 0;
  for (int i = 0, _END_ = tot; i <= _END_; i++) {
    for (int j = 1, _END_ = sv[i]; j <= _END_; j++)
      a[++k] = v[i][j], v[i][j] = 0;
    for (int j = 1, _END_ = sp[i]; j <= _END_; j++)
      f[i][p[i][j]] = 0, p[i][j] = 0;
    sv[i] = sp[i] = 0;
  }
  tot = 0;
}
void init() {
  n = Readint();
  block = (int)(sqrt(n) + 1.0);
  block_size = n / block + 1;
  for (int i = 1, _END_ = n; i <= _END_; i++) a[i] = Readint();
}
int Del(int rk) {
  int ret = 0;
  int now = 0;
  int sz = sv[now];
  while (rk > sz) {
    now++;
    sz += sv[now];
  }
  rk -= sz - sv[now];
  ret = v[now][rk];
  f[now][ret]--;
  for (int i = rk + 1, _END_ = sv[now]; i <= _END_; i++)
    v[now][i - 1] = v[now][i];
  v[now][sv[now]--] = 0;
  return ret;
}
void Ins(int rk, int val) {
  int now = 0;
  int sz = sv[now];
  while (rk > sz) {
    now++;
    sz += sv[now];
  }
  rk -= sz - sv[now];
  if (!f[now][val]) {
    sp[now]++;
    p[now][sp[now]] = val;
  }
  f[now][val]++;
  for (int i = sv[now], _END_ = rk + 1; i >= _END_; i--)
    v[now][i + 1] = v[now][i];
  v[now][rk + 1] = val;
  sv[now]++;
}
int sum(int l, int r, int val) {
  int nowl = 0, szl = sv[0];
  int nowr = 0, szr = sv[0];
  while (l > szl) {
    nowl++;
    szl += sv[nowl];
  }
  l -= szl - sv[nowl];
  while (r > szr) {
    nowr++;
    szr += sv[nowr];
  }
  r -= szr - sv[nowr];
  int ret = 0;
  if (nowl == nowr) {
    for (int i = l, _END_ = r; i <= _END_; i++) ret += v[nowl][i] == val;
    return ret;
  }
  for (int i = l, _END_ = sv[nowl]; i <= _END_; i++) ret += v[nowl][i] == val;
  for (int i = 1, _END_ = r; i <= _END_; i++) ret += v[nowr][i] == val;
  nowl++;
  nowr--;
  for (int i = nowl, _END_ = nowr; i <= _END_; i++) ret += f[i][val];
  return ret;
}
void work() {
  int _;
  int l, r;
  int kind;
  int num;
  int lans = 0;
  int ins = 0;
  _ = Readint();
  for (int __ = 1, _END_ = _; __ <= _END_; __++) {
    kind = Readint(), l = Readint(), r = Readint();
    l = (l + lans - 1) % n + 1;
    r = (r + lans - 1) % n + 1;
    if (l > r) swap(l, r);
    if (kind & 1) {
      if (l == r) continue;
      ins++;
      num = Del(r);
      Ins(l - 1, num);
    } else {
      num = Readint();
      num = (num + lans - 1) % n + 1;
      lans = sum(l, r, num);
      printf("%d\n", lans);
    }
    if (ins == 1000) {
      ins = 0;
      clear();
      build();
    }
  }
}
int main() {
  int _ = 0;
  init();
  build();
  work();
  close();
  return 0;
}
