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 solve(int test_number);
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout.setf(ios::fixed);
  cout.precision(9);
  cerr.setf(ios::fixed);
  cerr.precision(3);
  int n = 1;
  for (int i = 0; i < n; i++) {
    solve(i + 1);
  }
  return 0;
}
const int MAXN = 100100;
const int BLOCK = 350;
const int BLOCK_SIZE = MAXN / BLOCK + 10;
int n;
int a[MAXN];
int cnt[BLOCK][MAXN];
int bl[BLOCK][BLOCK_SIZE * 2];
int sz[BLOCK];
int lastans = 0;
inline pair<int, int> get_block(int pos) {
  int cur = 0;
  int lst = -1;
  for (int i = 0; i < BLOCK; i++) {
    cur += sz[i];
    if (sz[i] != 0) {
      lst = i;
    }
    if (cur > pos) {
      cur -= sz[i];
      return {i, pos - cur};
    }
  }
  return {lst, sz[lst]};
}
inline void rebuild() {
  int i = 0;
  int cur_bl = 0;
  while (i < n) {
    for (int j = 0; j < sz[cur_bl]; j++) {
      a[i++] = bl[cur_bl][j];
      cnt[cur_bl][a[i - 1]]--;
    }
    cur_bl++;
  }
  memset(sz, 0, sizeof(sz));
  int cur = 0;
  cur_bl = 0;
  for (int i = 0; i < n; i++) {
    bl[cur_bl][cur++] = a[i];
    cnt[cur_bl][a[i]]++;
    sz[cur_bl]++;
    if (cur == BLOCK_SIZE) {
      cur_bl++;
      cur = 0;
    }
  }
}
inline void recalc(int &l, int &r, int &k) {
  l = (l + lastans - 1) % n;
  r = (r + lastans - 1) % n;
  k = (k + lastans - 1) % n + 1;
  if (l > r) {
    swap(l, r);
  }
}
void print(int nn = n) {
  int n = nn;
  int i = 0;
  int b = 0;
  while (i < n) {
    for (int j = 0; j < sz[b]; j++) {
      i++;
      cout << bl[b][j] << ' ';
    }
    b++;
  }
  cout << endl;
}
inline void shift(int l, int r) {
  auto coord = get_block(r);
  int b = coord.first;
  int pos = coord.second;
  int val = bl[b][pos];
  sz[b]--;
  cnt[b][val]--;
  for (int i = pos; i < sz[b]; i++) {
    bl[b][i] = bl[b][i + 1];
  }
  coord = get_block(l);
  b = coord.first;
  pos = coord.second;
  sz[b]++;
  cnt[b][val]++;
  for (int i = sz[b] - 1; i > pos; i--) {
    bl[b][i] = bl[b][i - 1];
  }
  bl[b][pos] = val;
}
int count(int l, int r, int k) {
  auto coordl = get_block(l);
  auto coordr = get_block(r);
  int bll = coordl.first, posl = coordl.second;
  int blr = coordr.first, posr = coordr.second;
  int res = 0;
  if (bll == blr) {
    for (int i = posl; i <= posr; i++) {
      if (bl[bll][i] == k) {
        res++;
      }
    }
  } else {
    for (int i = posl; i < sz[bll]; i++) {
      if (bl[bll][i] == k) {
        res++;
      }
    }
    for (int i = 0; i <= posr; i++) {
      if (bl[blr][i] == k) {
        res++;
      }
    }
    for (int i = bll + 1; i < blr; i++) {
      res += cnt[i][k];
    }
  }
  return res;
}
int arr[MAXN];
void naive_shift(int l, int r) {
  int buf = arr[r];
  for (int i = r; i >= l + 1; i--) {
    arr[i] = arr[i - 1];
  }
  arr[l] = buf;
}
int naive_count(int l, int r, int k) {
  int res = 0;
  for (int i = l; i <= r; i++) {
    if (arr[i] == k) res++;
  }
  return res;
}
void solve(int test_number) {
  cin >> n;
  int cur = 0;
  int cur_bl = 0;
  memset(sz, 0, sizeof(sz));
  memset(bl, 0, sizeof(bl));
  memset(cnt, 0, sizeof(cnt));
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    arr[i] = a[i];
    bl[cur_bl][cur++] = a[i];
    cnt[cur_bl][a[i]]++;
    sz[cur_bl]++;
    if (cur == BLOCK_SIZE) {
      cur_bl++;
      cur = 0;
    }
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    if (i % BLOCK == 0) {
      rebuild();
    }
    int l, r, k, t;
    cin >> t;
    if (t == 1) {
      l = rand() % n + 1;
      r = rand() % n + 1;
      cin >> l >> r;
    } else {
      l = rand() % n + 1;
      r = rand() % n + 1;
      k = rand() % n + 1;
      cin >> l >> r >> k;
    }
    recalc(l, r, k);
    if (t == 1) {
      shift(l, r);
    } else {
      int res = count(l, r, k);
      lastans = res;
      cout << res << "\n";
    }
  }
}
