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;
const int N = 1e5 + 6;
const int T = (1 << 18) + 3;
const int K = 1111;
vector<int> t[T];
int d;
int rcq(int l, int r, int x) {
  int res = 0;
  vector<int>::iterator it;
  for (l += d, r += d; l <= r; ++l >>= 1, --r >>= 1) {
    if (l & 1)
      res += upper_bound(t[l].begin(), t[l].end(), x) -
             lower_bound(t[l].begin(), t[l].end(), x);
    if (~r & 1)
      res += upper_bound(t[r].begin(), t[r].end(), x) -
             lower_bound(t[r].begin(), t[r].end(), x);
  }
  return res;
}
int st[K * 2 + 10][3], sn;
int a[N];
int f[N], sf[N];
void add(int i, int x) {
  sf[i] += x;
  for (; i < N; i |= i + 1) f[i] += x;
}
int get(int i) {
  int s = 0;
  for (; i >= 0; i = (i & (i + 1)) - 1) s += f[i];
  return s;
}
int index(int i, int n) {
  int l = 0, r = n;
  while (l < r) {
    int m = (l + r) >> 1;
    int k = get(m);
    if (k >= i)
      r = m;
    else
      l = m + 1;
  }
  return r;
}
void build(int *a, int n, bool first) {
  memset(sf, 0, sizeof sf);
  memset(f, 0, sizeof f);
  for (d = 2; d < n; d <<= 1)
    ;
  for (int i = 0; i < n; ++i) {
    if (first) t[i + d].resize(1);
    t[i + d][0] = a[i];
    add(i + 1, 1);
  }
  for (int i = d - 1; i; --i) {
    if (first) t[i].resize(t[i * 2].size() + t[i * 2 + 1].size());
    merge(t[i * 2].begin(), t[i * 2].end(), t[i * 2 + 1].begin(),
          t[i * 2 + 1].end(), t[i].begin());
  }
}
int b[N];
void rere(int n) {
  for (int i = 0; i < n; ++i) {
    sf[i + 1] += sf[i];
    b[i] = a[i];
    a[i] = -1;
  }
  for (int i = n - 1; i >= 0; --i) a[sf[i]] = b[i];
  for (int k = 0; k < sn; ++k) a[st[k][0]] = st[k][1];
  sn = 0;
  build(a, n, false);
}
int z[N];
void br1(int i, int j) { rotate(z + i, z + j, z + j + 1); }
int br2(int i, int j, int x) {
  int r = 0;
  for (int k = i; k <= j; ++k)
    if (z[k] == x) ++r;
  return r;
}
int main() {
  int n, m;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    z[i] = a[i];
  }
  build(a, n, true);
  int lans = 0;
  bool SUBMIT = 1;
  int qn;
  scanf("%d", &qn);
  while (qn--) {
    int tp, l, r, x;
    scanf("%d%d%d", &tp, &l, &r);
    if (SUBMIT) {
      l = (l + lans - 1) % n + 1;
      r = (r + lans - 1) % n + 1;
    }
    if (l > r) swap(l, r);
    l--;
    r--;
    int i = index(l, n);
    int j = index(r + 1, n);
    int j2 = index(r, n);
    if (tp == 1) {
      if (!SUBMIT) br1(l, r);
      if (l < r) {
        bool fl = false;
        for (int k = 0; k < sn; ++k) {
          if (l <= st[k][0] && st[k][0] < r)
            ++st[k][0];
          else if (st[k][0] == r) {
            st[k][0] = l;
            fl = true;
          }
        }
        if (!fl && i <= j2 && get(j2) == r) {
          int x = a[j2];
          st[sn][0] = l;
          st[sn][1] = x;
          st[sn][2] = j2;
          ++sn;
        }
        if (i < j) {
          add(i, +1);
          add(j2, -1);
        }
        if (sn >= K) rere(n);
      }
    } else if (tp == 2) {
      int x;
      scanf("%d", &x);
      if (SUBMIT) x = (x + lans - 1) % n + 1;
      int ans;
      if (!SUBMIT) ans = br2(l, r, x);
      int res = 0;
      if (i < j) {
        res += rcq(i, j - 1, x);
      }
      for (int k = 0; k < sn; ++k) {
        if (l <= st[k][0] && st[k][0] <= r && st[k][1] == x) ++res;
        if (i <= st[k][2] && st[k][2] <= j - 1 && st[k][1] == x) {
          --res;
        }
      }
      printf("%d\n", res);
      if (!SUBMIT) assert(ans == res);
      lans = res;
    }
  }
  return 0;
}