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;
int ROW[] = {+1, -1, +0, +0};
int COL[] = {+0, +0, +1, -1};
int X[] = {+0, +0, +1, -1, -1, +1, -1, +1};
int Y[] = {-1, +1, +0, +0, +1, +1, -1, -1};
int KX[] = {-2, -2, -1, -1, 1, 1, 2, 2};
int KY[] = {-1, 1, -2, 2, -2, 2, -1, 1};
int basePrime[] = {1009, 1013, 1019, 1021, 1031, 1223, 1229, 1231, 1237, 1249,
                   1289, 1291, 1297, 1301, 1303, 353,  359,  367,  373,  379,
                   859,  863,  877,  881,  883,  1931, 1933, 1949, 1951, 1973,
                   401,  409,  419,  421,  431,  1709, 1721, 1723, 1733, 1741,
                   3499, 3511, 3517, 3527, 3529, 929,  937,  941,  947,  953};
template <class XXX>
XXX GCD(XXX a, XXX b) {
  return b == 0 ? a : GCD(b, a % b);
}
template <class XXX>
XXX LCM(XXX a, XXX b) {
  return a * (b / GCD(a, b));
}
bool CMP(long long int a, long long int b) { return a > b; }
template <class XXX>
void fastread(XXX &number) {
  bool negative = false;
  register XXX c;
  number = 0;
  c = getchar();
  if (c == '-') {
    negative = true;
    c = getchar();
  }
  for (; (c > 47 && c < 58); c = getchar()) number = number * 10 + c - 48;
  if (negative) number *= -1;
}
template <class XXX>
inline XXX moduler(XXX num, XXX mod) {
  return (num >= mod ? num -= mod : num);
}
inline int Set(int pos, int N) { return N = N | (1 << pos); }
inline int Reset(int pos, int N) { return N = N & ~(1 << pos); }
inline bool Check(int pos, int N) { return (bool)(N & (1 << pos)); }
void OPFILE() {}
struct decomp {
  deque<int> dq;
  int mp[100007];
};
vector<decomp> bucket(320);
int bucketSize;
int whichBucket(int i) { return i / bucketSize; }
void sameBucket(int l, int r, int x) {
  int cnt = x * bucketSize - 1;
  vector<int> a;
  while (++cnt <= r) {
    int k = bucket[x].dq.front();
    if (cnt == l) a.push_back(-1);
    a.push_back(k);
    bucket[x].dq.pop_front();
  }
  for (int i = a.size() - 2; i >= 0; i--) {
    if (a[i] == -1)
      bucket[x].dq.push_front(a.back());
    else
      bucket[x].dq.push_front(a[i]);
  }
}
void differentBucket(int l, int r, int x, int y) {
  for (int i = x; i < y; i++) {
    int k = bucket[i].dq.back();
    bucket[i].dq.pop_back();
    bucket[i].mp[k]--;
    bucket[i + 1].mp[k]++;
    bucket[i + 1].dq.push_front(k);
  }
  int cnt = y * bucketSize - 1;
  vector<int> a;
  while (++cnt <= r + 1) {
    int k = bucket[y].dq.front();
    a.push_back(k);
    bucket[y].dq.pop_front();
  }
  for (int i = a.size() - 2; i >= 0; i--) bucket[y].dq.push_front(a[i]);
  int d = a.back();
  a.clear();
  bucket[y].mp[d]--;
  cnt = x * bucketSize - 1;
  while (++cnt < l) {
    int k = bucket[x].dq.front();
    a.push_back(k);
    bucket[x].dq.pop_front();
  }
  bucket[x].dq.push_front(d);
  bucket[x].mp[d]++;
  for (int i = a.size() - 1; i >= 0; i--) bucket[x].dq.push_front(a[i]);
}
void update(int l, int r) {
  int x = whichBucket(l), y = whichBucket(r);
  if (x == y)
    sameBucket(l, r, x);
  else
    differentBucket(l, r, x, y);
}
int query(int l, int r, int v) {
  int x = whichBucket(l), y = whichBucket(r);
  int ans = 0;
  for (int i = x + 1; i < y; i++) ans += bucket[i].mp[v];
  int cnt = x * bucketSize;
  for (int i = 0; i < bucket[x].dq.size() and cnt <= r; i++, cnt++)
    ans += (bucket[x].dq[i] == v and cnt >= l and cnt <= r);
  if (x == y) return ans;
  cnt = y * bucketSize;
  for (int i = 0; i < bucket[y].dq.size() and cnt <= r; i++, cnt++)
    ans += (bucket[y].dq[i] == v and cnt >= l and cnt <= r);
  return ans;
}
int main() {
  OPFILE();
  int n;
  scanf("%d", &n);
  bucketSize = sqrt(n);
  for (int i = 0; i < n; i++) {
    int a;
    scanf("%d", &a);
    int k = whichBucket(i);
    bucket[k].dq.push_back(a);
    bucket[k].mp[a]++;
  }
  int q;
  scanf("%d", &q);
  int last = 0;
  while (q--) {
    int tp, i, j;
    scanf("%d %d %d", &tp, &i, &j);
    i = ((i + last - 1) % n) + 1;
    j = ((j + last - 1) % n) + 1;
    if (i > j) swap(i, j);
    if (tp == 1)
      update(i - 1, j - 1);
    else {
      int v;
      scanf("%d", &v);
      v = ((v + last - 1) % n) + 1;
      last = query(i - 1, j - 1, v);
      printf("%d\n", last);
    }
  }
  return 0;
}