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 char IO_MODE = 3;
inline long long ReadInt() {
  long long x = 0, s = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') s = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return s * x;
}
inline void WriteInt(long long x) {
  char c[20];
  if (!x) {
    putchar('0');
    return;
  }
  if (x < 0) putchar('-'), x = -x;
  int i = 0;
  while (x > 0) c[++i] = x % 10, x /= 10;
  while (i) putchar(c[i--] + 48);
}
template <typename T>
inline void out(T x) {
  if (IO_MODE & 1)
    WriteInt(x);
  else if (typeid(x) == typeid(int))
    printf("%i", x);
  else
    printf("%lld", (long long)x);
}
template <typename T, typename... Args>
inline void out(T x, Args... args) {
  out(x);
  putc(' ', stdout);
  out(args...);
}
template <typename T>
inline void in(T &x) {
  if (IO_MODE & 2)
    x = ReadInt();
  else if (typeid(x) == typeid(int))
    scanf("%i", &x);
  else if (typeid(x) == typeid(long long))
    scanf("%lld", &x);
}
template <typename T, typename... Args>
inline void in(T &x, Args &...args) {
  in(x);
  in(args...);
}
int aa, bb, cc, dd, ee, ff, gg, hh, ii, jj, kk, mm, nn, oo, pp, qq, rr, ss, tt,
    uu, vv, ww, xx, yy, zz;
int tc;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c *x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug &operator<<(const c &) {
    return *this;
  }
};
struct node {
  int pr, sz;
  int val;
  node *l, *r, *link, *par;
  node(int v) {
    val = v;
    pr = rand();
    sz = 1;
    l = r = par = NULL;
  }
} * root, *H[1001000];
struct ImplicitTreap {
  int szOf(node *p) { return p ? p->sz : 0; }
  void upd_sz(node *p) {
    if (p) p->sz = szOf(p->l) + szOf(p->r) + 1;
  }
  void upd_par(node *p) {
    if (!p) return;
    if (p->l) p->l->par = p;
    if (p->r) p->r->par = p;
    p->par = NULL;
  }
  int idxOf(node *p) {
    if (!p) return 0;
    int idx = szOf(p->l);
    while (p->par) {
      node *par = p->par;
      if (par->r == p) idx += szOf(par->l) + 1;
      p = par;
    }
    return idx;
  }
  node *merge(node *a, node *b) {
    if (!a || !b) return a ? a : b;
    if (a->pr > b->pr) {
      a->r = merge(a->r, b);
      upd_sz(a);
      upd_par(a);
      return a;
    } else {
      b->l = merge(a, b->l);
      upd_sz(b);
      upd_par(b);
      return b;
    }
  }
  pair<node *, node *> split(node *p, int idx) {
    if (!p) return {NULL, NULL};
    int cur = szOf(p->l);
    if (idx >= cur) {
      node *a = p, *b;
      tie(a->r, b) = split(p->r, idx - cur - 1);
      upd_sz(a);
      upd_par(a);
      return {a, b};
    } else {
      node *a, *b = p;
      tie(a, b->l) = split(p->l, idx);
      upd_sz(b);
      upd_par(b);
      return {a, b};
    }
  }
  node *insert(node *p, int idx, node *v) {
    node *l, *r;
    tie(l, r) = split(p, idx - 1);
    return merge(l, merge(v, r));
  }
  node *getNode(node *p, int idx) {
    if (!p) return NULL;
    int cur = szOf(p->l);
    if (idx == cur) return p;
    if (idx > cur)
      return getNode(p->r, idx - szOf(p->l) - 1);
    else
      return getNode(p->l, idx);
  }
} tr;
void lower_bound(node *p, int val, int &res, int idx = 0) {
  if (!p) return;
  int cur = tr.idxOf(p->link);
  if (cur >= val) {
    res = idx + tr.szOf(p->l);
    lower_bound(p->l, val, res, idx);
  } else {
    lower_bound(p->r, val, res, idx + tr.szOf(p->l) + 1);
  }
}
int n, q;
int main() {
  in(n);
  for (int i = 0; i < n; i++) {
    int val = ReadInt();
    node *a = new node(val);
    node *b = new node(i);
    a->link = b;
    b->link = a;
    root = tr.insert(root, i, a);
    H[val] = tr.insert(H[val], tr.szOf(H[val]), b);
  }
  int last = 0;
  in(q);
  while (q--) {
    if (ReadInt() == 1) {
      int l = (ReadInt() + last - 1) % n;
      int r = (ReadInt() + last - 1) % n;
      if (l > r) swap(l, r);
      node *lft, *mid, *ele, *rgt;
      int val = tr.getNode(root, r)->val;
      int a, b;
      lower_bound(H[val], l, a);
      lower_bound(H[val], r, b);
      tie(lft, rgt) = tr.split(root, l - 1);
      tie(mid, rgt) = tr.split(rgt, r - l);
      tie(mid, ele) = tr.split(mid, r - l - 1);
      root = tr.merge(lft, tr.merge(ele, tr.merge(mid, rgt)));
      tie(lft, rgt) = tr.split(H[val], a - 1);
      tie(mid, rgt) = tr.split(rgt, b - a);
      tie(mid, ele) = tr.split(mid, b - a - 1);
      H[val] = tr.merge(lft, tr.merge(ele, tr.merge(mid, rgt)));
    } else {
      int l = (ReadInt() + last - 1) % n;
      int r = (ReadInt() + last - 1) % n;
      int k = (ReadInt() + last - 1) % n + 1;
      if (l > r) swap(l, r);
      int a, b;
      a = b = tr.szOf(H[k]);
      lower_bound(H[k], l, a);
      lower_bound(H[k], r + 1, b);
      last = b - a;
      printf("%d\n", last);
    }
  }
}