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;
template <class T>
inline T bigmod(T p, T e, T M) {
  long long ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1) ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}
template <class T>
inline T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <class T>
inline T modinverse(T a, T M) {
  return bigmod(a, M - 2, M);
}
template <class T>
inline T lcm(T a, T b) {
  a = abs(a);
  b = abs(b);
  return (a / gcd(a, b)) * b;
}
template <class T, class X>
inline bool getbit(T a, X i) {
  T t = 1;
  return ((a & (t << i)) > 0);
}
template <class T, class X>
inline T setbit(T a, X i) {
  T t = 1;
  return (a | (t << i));
}
template <class T, class X>
inline T resetbit(T a, X i) {
  T t = 1;
  return (a & (~(t << i)));
}
inline long long getnum() {
  char c = getchar();
  long long num, sign = 1;
  for (; c < '0' || c > '9'; c = getchar())
    if (c == '-') sign = -1;
  for (num = 0; c >= '0' && c <= '9';) {
    c -= '0';
    num = num * 10 + c;
    c = getchar();
  }
  return num * sign;
}
inline long long power(long long a, long long b) {
  long long multiply = 1;
  for (int i = (0); i < (b); i++) {
    multiply *= a;
  }
  return multiply;
}
int n, q, type, l, r, k, a[100002], blockSZ;
vector<int> v[318];
int sz, cnt[100002][318];
void Shift(int to, int from) {
  int b1 = -1, b2 = -1;
  int s = 0, idx1, idx2;
  if (to == from) return;
  for (int i = (0); i < (sz + 1); i++) {
    if (b1 == -1 && s + (int)v[i].size() > from) {
      b1 = from - s;
      idx1 = i;
    }
    if (b2 == -1 && s + (int)v[i].size() > to) {
      b2 = to - s;
      idx2 = i;
    }
    s += v[i].size();
  }
  int val = v[idx1][b1];
  v[idx1].erase(v[idx1].begin() + b1);
  v[idx2].insert(v[idx2].begin() + b2, val);
  cnt[val][idx1]--;
  cnt[val][idx2]++;
}
int Query(int from, int to, int x) {
  int s = 0, idx1 = -1, idx2 = -1, b1 = -1, b2 = -1;
  for (int i = (0); i < (sz + 1); i++) {
    if (idx1 == -1 && s + (int)v[i].size() > from) {
      idx1 = i;
      b1 = from - s;
    }
    if (idx2 == -1 && s + (int)v[i].size() > to) {
      idx2 = i;
      b2 = to - s;
    }
    s += v[i].size();
  }
  int ret = 0;
  for (int i = (idx1 + 1); i < (idx2); i++) {
    ret += cnt[x][i];
  }
  if (idx1 != idx2) {
    for (int i = (b1); i < (v[idx1].size()); i++) {
      if (v[idx1][i] == x) ret++;
    }
    for (int i = (0); i < (b2 + 1); i++) {
      if (v[idx2][i] == x) ret++;
    }
  } else {
    for (int i = (b1); i < (b2 + 1); i++) {
      if (v[idx1][i] == x) ret++;
    }
  }
  return ret;
}
void Rebuild() {
  vector<int> temp;
  for (int i = (0); i < (sz + 1); i++) {
    for (int j = (0); j < (v[i].size()); j++) {
      temp.push_back(v[i][j]);
      cnt[v[i][j]][i]--;
    }
    v[i].clear();
  }
  sz = 0;
  for (int i = (0); i < (n); i++) {
    v[sz].push_back(temp[i]);
    cnt[temp[i]][sz]++;
    if (i + 1 < n && (i + 1) % blockSZ == 0) sz++;
  }
}
void Print() {
  cout << "All numbers: ";
  for (int i = (0); i < (sz + 1); i++) {
    for (int j = (0); j < (v[i].size()); j++) cout << v[i][j] << " ";
  }
  cout << "\n";
  cout << "All vectors: "
       << "\n";
  for (int i = (0); i < (sz + 1); i++) {
    for (int J = (0); J < (v[i].size()); J++) cout << v[i][J] << " ";
    cout << "\n";
  }
}
int main() {
  int test, cases = 1;
  scanf("%d", &n);
  blockSZ = 700;
  for (int i = (0); i < (n); i++) {
    scanf("%d", &a[i]);
  }
  for (int i = (0); i < (n); i++) {
    v[sz].push_back(a[i]);
    cnt[a[i]][sz]++;
    if (i + 1 < n && (i + 1) % blockSZ == 0) sz++;
  }
  scanf("%d", &q);
  int last = 0, shift = 0;
  while (q--) {
    scanf("%d", &type);
    if (type == 1) {
      scanf("%d%d", &l, &r);
      l = ((l + last - 1) % n) + 1;
      r = ((r + last - 1) % n) + 1;
      l--, r--;
      if (l > r) swap(l, r);
      Shift(l, r);
      shift++;
    } else {
      scanf("%d%d%d", &l, &r, &k);
      l = ((l + last - 1) % n) + 1;
      r = ((r + last - 1) % n) + 1;
      k = ((k + last - 1) % n) + 1;
      l--, r--;
      if (l > r) swap(l, r);
      last = Query(l, r, k);
      printf("%d\n", last);
    }
    if (shift == 1200) {
      Rebuild();
      shift = 0;
    }
  }
  return 0;
}