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 maxarr(vector<int> &arr);
int minarr(vector<int> &arr);
long long modulo(long long base, long long exponent, long long modulus);
long long choose(long long n, long long k);
bool isprime(long long n);
long long inverse(long long a, long long m);
long long fastmult(long long a, long long b, long long c);
vector<long long> prime;
long long sum[100100];
vector<bool> aprime(100100);
void setprime();
long long divCeil(long long a, long long b);
long long arrceil(vector<long long> a, long long k);
long long n;
vector<long long> a(100100), b(100100);
long long block = 500;
long long cblock = 201;
short met[201][100100];
vector<long long> val[201];
void build() {
  for (int i = 1; i < cblock; i++) {
    long long r = min((long long)block * i, n);
    for (int j = (i - 1) * block + 1; j < r + 1; j++) {
      val[i].push_back(a[j]);
      met[i][a[j]]++;
    }
  }
}
long long erase(long long x) {
  long long sum = 0;
  long long ele;
  for (int i = 1; i < cblock; i++) {
    sum += val[i].size();
    if (sum >= x) {
      long long pos = val[i].size() - (sum - x) - 1;
      ele = val[i][pos];
      met[i][ele]--;
      val[i].erase(val[i].begin() + pos);
      break;
    }
  }
  return ele;
}
void insert(long long x, long long ele) {
  long long sum = 0;
  if (x == 0) {
    met[1][ele]++;
    val[1].insert(val[1].begin(), ele);
    return;
  }
  for (int i = 1; i < cblock; i++) {
    sum += val[i].size();
    if (sum >= x) {
      long long pos = val[i].size() - (sum - x) - 1;
      met[i][ele]++;
      val[i].insert(val[i].begin() + pos, ele);
      break;
    }
  }
}
void shiftquery(long long l, long long r) {
  if (l == r) return;
  long long ele = erase(r);
  insert(l, ele);
}
long long querycount(long long x, long long k) {
  long long sum = 0;
  long long ans = 0;
  for (int i = 1; i < cblock; i++) {
    sum += val[i].size();
    if (sum <= x)
      ans += met[i][k];
    else {
      sum -= val[i].size();
      sum++;
      long long j = 0;
      while (sum <= x) {
        if (val[i][j] == k) ans++;
        j++;
        sum++;
      }
      break;
    }
  }
  return ans;
}
void rebuild() {
  long long it = 1;
  for (int i = 1; i < cblock; i++) {
    for (int j = 0; j < val[i].size(); j++) {
      b[it++] = val[i][j];
    }
    val[i].resize(0);
  }
  memset(met, 0, sizeof(met));
  for (int i = 1; i < n + 1; i++) a[i] = b[i];
  build();
}
int main() {
  long long q;
  cin >> n;
  for (int i = 1; i < n + 1; i++) cin >> a[i];
  build();
  cin >> q;
  long long lastans = 0;
  long long count = 0;
  while (q--) {
    long long t;
    cin >> t;
    if (t == 1) {
      long long l, r;
      cin >> l >> r;
      l = (l + lastans - 1) % n + 1;
      r = (r + lastans - 1) % n + 1;
      if (l > r) swap(l, r);
      shiftquery(l, r);
      count++;
    } else {
      long long l, r, k;
      cin >> l >> r >> k;
      l = (l + lastans - 1) % n + 1;
      r = (r + lastans - 1) % n + 1;
      k = (k + lastans - 1) % n + 1;
      if (l > r) swap(l, r);
      long long ans = querycount(r, k);
      ans -= querycount(l - 1, k);
      lastans = ans;
      printf("%lld\n", ans);
    }
    if (count > 500) {
      rebuild();
      count = 0;
    }
  }
  return 0;
}
int maxarr(vector<int> &arr) {
  int maxi = INT_MIN;
  for (int i = 0; i < arr.size(); i++) maxi = max(arr[i], maxi);
  return maxi;
}
int minarr(vector<int> &arr) {
  int mini = INT_MAX;
  for (int i = 0; i < arr.size(); i++) mini = min(arr[i], mini);
  return mini;
}
long long modulo(long long base, long long exponent, long long modulus) {
  if (modulus == 1) return 0;
  long long result = 1;
  base = base % modulus;
  while (exponent > 0) {
    if (exponent % 2 == 1) {
      result = (result * base) % modulus;
    }
    exponent = exponent >> 1;
    base = (base * base) % modulus;
  }
  return result;
}
long long choose(long long n, long long k) {
  if (k == 0) return 1;
  return (n * choose(n - 1, k - 1)) / k;
}
bool isprime(long long n) {
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) return false;
  }
  return true;
}
void EE(long long a, long long b, long long &co1, long long &co2) {
  if (a % b == 0) {
    co1 = 0;
    co2 = 1;
    return;
  }
  EE(b, a % b, co1, co2);
  long long temp = co1;
  co1 = co2;
  co2 = temp - co2 * (a / b);
}
long long inverse(long long a, long long m) {
  long long x, y;
  EE(a, m, x, y);
  if (x < 0) x += m;
  return x;
}
long long fastmult(long long a, long long b, long long c) {
  if (b == 0) return 0;
  long long res = fastmult(a, b >> 1, c);
  res = (res + res) % c;
  if (b % 2 == 1) {
    res = (res + a) % c;
  }
  return res;
}
void setprime() {
  aprime[0] = true;
  for (int i = 2; i * i <= 100100; i++) {
    if (aprime[i - 1] == false) {
      for (int j = i * 2; j <= 100100; j += i) {
        aprime[j - 1] = true;
      }
    }
  }
}
long long divCeil(long long a, long long b) { return (a + b - 1) / b; }
long long arrceil(vector<long long> a, long long k) {
  long long n = a.size();
  long long low = 0;
  long long high = n - 1;
  if (a[0] > k) return -1;
  while (low < high) {
    long long mid = (low + high + 1) / 2;
    if (a[mid] <= k)
      low = mid;
    else
      high = mid - 1;
  }
  return low;
}