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 <typename T>
inline bool chkmin(T &a, T b) {
  return b < a ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, T b) {
  return b > a ? a = b, 1 : 0;
}
inline int read() {
  int x(0), sgn(1);
  char ch(getchar());
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') sgn = -1;
  for (; isdigit(ch); ch = getchar()) x = (x * 10) + (ch ^ 48);
  return x * sgn;
}
void File() {
  freopen("queue.in", "r", stdin);
  freopen("queue.out", "w", stdout);
}
const int N = 1e5 + 1e3;
int cur, Data[N];
template <int Maxn>
struct SPLAY {
  int ch[Maxn][2], fa[Maxn], sz[Maxn], root[Maxn];
  inline bool get(int o) { return ch[fa[o]][1] == o; }
  inline void push_up(int o) { sz[o] = sz[ch[o][0]] + sz[ch[o][1]] + 1; }
  inline void rotate(int v) {
    int u = fa[v], t = fa[u], d = get(v);
    fa[ch[u][d] = ch[v][d ^ 1]] = u;
    fa[v] = t;
    if (t) ch[t][ch[t][1] == u] = v;
    fa[ch[v][d ^ 1] = u] = v;
    push_up(u);
    push_up(v);
  }
  inline void Splay(int o, int goal = 0) {
    for (; fa[o] != goal; rotate(o))
      if (fa[fa[o]] != goal) rotate(get(o) != get(fa[o]) ? o : fa[o]);
    if (!goal) root[cur] = o;
  }
  inline int Kth(int o, int k) {
    int res = k - sz[ch[o][0]];
    return res == 1 ? Splay(o),
           o        : (res <= 0 ? Kth(ch[o][0], k) : Kth(ch[o][1], res - 1));
  }
  int Rank(int o) {
    Splay(o);
    return sz[ch[o][0]] + 1;
  }
  void Delete(int o) {
    Splay(o);
    int L = ch[o][0], R = ch[o][1];
    if (!L) fa[root[cur] = R] = 0;
    if (!R) fa[root[cur] = L] = 0;
    if (L && R) {
      while (ch[L][1]) L = ch[L][1];
      while (ch[R][0]) R = ch[R][0];
      Splay(L, 0);
      Splay(R, L);
      ch[R][0] = 0;
      push_up(R);
      push_up(L);
      return;
    }
  }
};
SPLAY<N> S, T;
void InsertS(int &o, int from, int id, int val) {
  if (!o) {
    S.fa[o = id] = from;
    S.sz[o] = 1;
    S.ch[o][0] = S.ch[o][1] = 0;
    S.Splay(o);
    return;
  }
  int res = val - S.sz[S.ch[o][0]];
  res <= 0 ? InsertS(S.ch[o][0], o, id, val)
           : InsertS(S.ch[o][1], o, id, res - 1);
}
void InsertT(int &o, int from, int id, int val) {
  if (!o) {
    T.fa[o = id] = from;
    T.sz[o] = 1;
    T.ch[o][0] = T.ch[o][1] = 0;
    T.Splay(o);
    return;
  }
  int tmp = cur;
  cur = 1;
  int get_rank = S.Rank(o);
  cur = tmp;
  InsertT(val <= get_rank ? T.ch[o][0] : T.ch[o][1], o, id, val);
}
inline void Shift(int l, int r) {
  int o = S.Kth(S.root[cur = 1], r);
  S.Delete(o);
  InsertS(S.root[cur], 0, o, l - 1);
  cur = Data[o];
  T.Delete(o);
  InsertT(T.root[cur], 0, o, l);
}
inline int Query(int pos) {
  if (!pos) return 0;
  int o = T.root[cur], tmp = o, res = 0, other = cur;
  cur = 1;
  while (o) {
    tmp = o;
    if (pos < S.Rank(o))
      o = T.ch[o][0];
    else
      res += T.sz[T.ch[o][0]] + 1, o = T.ch[o][1];
  }
  cur = other;
  if (tmp) T.Splay(tmp);
  return res;
}
int main() {
  int n = read();
  for (register int i = (1), iend = (int)(n); i <= iend; ++i) {
    Data[i] = read();
    InsertS(S.root[cur = 1], 0, i, i - 1);
    InsertT(T.root[cur = Data[i]], 0, i, i);
  }
  int q = read(), ans = 0;
  for (register int i = (1), iend = (int)(q); i <= iend; ++i) {
    int type = read(), ql = (ans + read() - 1) % n + 1,
        qr = (ans + read() - 1) % n + 1;
    if (ql > qr) swap(ql, qr);
    if (type == 1)
      Shift(ql, qr);
    else {
      cur = (read() + ans - 1) % n + 1;
      printf("%d\n", ans = Query(qr) - Query(ql - 1));
    }
  }
  return 0;
}