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;
void dout() { cerr << endl; }
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
  cerr << H << ' ';
  dout(T...);
}
constexpr double pi{3.14159265358979323846264338327950288419716939937511};
constexpr double eps{1e-9};
char ch_ch_ch[1 << 20];
inline string gs() {
  scanf("%s", ch_ch_ch);
  return string(ch_ch_ch);
}
inline string gl() {
  gets(ch_ch_ch);
  return string(ch_ch_ch);
}
inline int gi() {
  int x;
  scanf("%d", &x);
  return x;
}
constexpr int inf = 1000000000;
struct block {
  int a[640];
  int cnt[100010];
  int length;
  block() { length = 0; }
};
block decomp[320];
int a[100100];
int n;
int num_blocks;
int block_size;
void rebuild_decomposition() {
  int ap = 0;
  for (auto i = 0; i < (num_blocks); ++i) {
    for (auto j = 0; j < (decomp[i].length); ++j) {
      int val = decomp[i].a[j];
      decomp[i].cnt[val]--;
      a[ap++] = val;
    }
  }
  int p = 0;
  int sum = 0;
  while (sum < n) {
    int cur_size = min(block_size, n - sum);
    sum += cur_size;
    decomp[p].length = cur_size;
    int pp = 0;
    for (auto i = (sum - cur_size); i < (sum); ++i) {
      decomp[p].a[pp++] = a[i];
      decomp[p].cnt[a[i]]++;
    }
    ++p;
  }
}
int del_element(int pos) {
  int block_ind = 0;
  while (pos >= decomp[block_ind].length) {
    pos -= decomp[block_ind].length;
    block_ind++;
  }
  block& cur = decomp[block_ind];
  int val = cur.a[pos];
  cur.cnt[val]--;
  for (auto i = (pos + 1); i < (cur.length); ++i) cur.a[i - 1] = cur.a[i];
  cur.length--;
  return val;
}
void ins_element(int pos, int val) {
  int block_ind = 0;
  while (pos >= decomp[block_ind].length) {
    pos -= decomp[block_ind].length;
    block_ind++;
  }
  block& cur = decomp[block_ind];
  for (int i = cur.length; i > pos; --i) cur.a[i] = cur.a[i - 1];
  cur.a[pos] = val;
  cur.length++;
  cur.cnt[val]++;
}
int cnt_element(int l, int r, int val) {
  int pos = l;
  int block_ind = 0;
  while (pos >= decomp[block_ind].length) {
    pos -= decomp[block_ind].length;
    block_ind++;
  }
  int left_block_ind = block_ind;
  l = pos;
  pos = r;
  block_ind = 0;
  while (pos >= decomp[block_ind].length) {
    pos -= decomp[block_ind].length;
    block_ind++;
  }
  int right_block_ind = block_ind;
  r = pos;
  if (left_block_ind == right_block_ind) {
    int cnt = 0;
    for (auto i = (l); i < (r + 1); ++i)
      if (decomp[left_block_ind].a[i] == val) cnt++;
    return cnt;
  } else {
    int cnt = 0;
    for (auto i = (l); i < (decomp[left_block_ind].length); ++i)
      if (decomp[left_block_ind].a[i] == val) cnt++;
    for (auto i = (0); i < (r + 1); ++i)
      if (decomp[right_block_ind].a[i] == val) cnt++;
    for (auto i = (left_block_ind + 1); i < (right_block_ind); ++i)
      cnt += decomp[i].cnt[val];
    return cnt;
  }
}
void solution() {
  n = gi();
  for (auto i = 0; i < (n); ++i) a[i] = gi();
  num_blocks = int(sqrt(n));
  block_size = (n + num_blocks - 1) / num_blocks;
  for (auto i = 0; i < (num_blocks); ++i)
    memset(decomp[i].cnt, 0, sizeof(decomp[i].cnt));
  rebuild_decomposition();
  int q = gi();
  int lastans = 0;
  int cnt_type1 = 0;
  for (auto it = 0; it < (q); ++it) {
    if (cnt_type1 == block_size) {
      rebuild_decomposition();
      cnt_type1 = 0;
    }
    int tp = gi();
    if (tp == 1) {
      int l = gi();
      int r = gi();
      l = (l + lastans - 1 + n) % n + 1;
      r = (r + lastans - 1 + n) % n + 1;
      if (l > r) swap(l, r);
      if (l == r) continue;
      int val = del_element(r - 1);
      ins_element(l - 1, val);
      cnt_type1++;
    } else {
      int l = gi();
      int r = gi();
      int k = gi();
      l = (l + lastans - 1 + n) % n + 1;
      r = (r + lastans - 1 + n) % n + 1;
      k = (k + lastans - 1 + n) % n + 1;
      if (l > r) swap(l, r);
      printf("%d\n", lastans = cnt_element(l - 1, r - 1, k));
    }
  }
}
int main(int argc, char** argv) {
  solution();
  return 0;
}
