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
import java.io.*;
import java.util.*;

/*
polyakoff
*/

public class Main {

    static FastReader in;
    static PrintWriter out;
    static Random rand = new Random();
    static final int oo = (int) 2e9;
    static final long OO = (long) 4e18;
    static final int MOD = (int) 1e9 + 7;

    static Node[] t;

    static class Node {
        int y, cnt, val;
        Node par, left, right, corr;

        Node(int y, int val) {
            this.y = y;
            cnt = 1;
            this.val = val;
        }
    }

    static int cnt(Node t) {
        return (t == null) ? 0 : t.cnt;
    }

    static void pull(Node t) {
        t.cnt = 1 + cnt(t.left) + cnt(t.right);
        if (t.left != null)
            t.left.par = t;
        if (t.right != null)
            t.right.par = t;
    }

    static int order(Node t) {
        int res = 0;
        boolean right = true;
        while (t != null) {
            if (right)
                res += cnt(t.left) + 1;
            if (t.par != null)
                right = t.par.right == t;
            t = t.par;
        }
        return res - 1;
    }

    static Node merge(Node t1, Node t2) {
        if (t1 == null)
            return t2;
        if (t2 == null)
            return t1;
        if (t1.y >= t2.y) {
            t1.right = merge(t1.right, t2);
            pull(t1);
            return t1;
        } else {
            t2.left = merge(t1, t2.left);
            pull(t2);
            return t2;
        }
    }

    static Node[] split(Node t, int k, int type) {
        if (t == null)
            return new Node[]{null, null};
        int curr_k = (type == 0) ? cnt(t.left) : order(t.corr);
        if (curr_k < k) {
            Node[] tt = split(t.right, (type == 0) ? k - curr_k - 1 : k, type);
            t.right = tt[0];
            pull(t);
            return new Node[]{t, tt[1]};
        } else {
            Node[] tt = split(t.left, k, type);
            t.left = tt[1];
            pull(t);
            return new Node[]{tt[0], t};
        }
    }

    static void insert(int pos, int val) {
        Node t1 = new Node(rand.nextInt(), val);
        Node t2 = new Node(t1.y, val);
        t2.corr = t1;

        Node[] tt1 = split(t[0], pos, 0);
        t[0] = merge(merge(tt1[0], t1), tt1[1]);

        Node[] tt2 = split(t[val], pos, 1);
        t[val] = merge(merge(tt2[0], t2), tt2[1]);
    }

    static void erase(int pos, int val) {
        Node[] tt3 = split(t[val], pos, 1);
        Node[] tt4 = split(tt3[1], 1, 0);
        t[val] = merge(tt3[0], tt4[1]);

        Node[] tt1 = split(t[0], pos, 0);
        Node[] tt2 = split(tt1[1], 1, 0);
        t[0] = merge(tt1[0], tt2[1]);
    }

    static int kth(Node t, int k) {
        int curr_k = cnt(t.left);
        if (k == curr_k)
            return t.val;
        else if (k < curr_k)
            return kth(t.left, k);
        else
            return kth(t.right, k - curr_k - 1);
    }

    static void shift(int l, int r) {
        int val = kth(t[0], r);
        erase(r, val);
        insert(l, val);
    }

    static int count(int l, int r, int k) {
        Node[] tt1 = split(t[k], l, 1);
        Node[] tt2 = split(tt1[1], r + 1, 1);
        int res = cnt(tt2[0]);
        t[k] = merge(merge(tt1[0], tt2[0]), tt2[1]);

        return res;
    }


    static void solve() {
        int n = in.nextInt();

        t = new Node[n + 1];

        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = in.nextInt();
            insert(i, a[i]);
        }

        int q = in.nextInt();
        int ans = 0;
        for (int i = 0; i < q; i++) {
            int type = in.nextInt();
            int l = (in.nextInt() - 1 + ans) % n;
            int r = (in.nextInt() - 1 + ans) % n;
            if (l > r) {
                l ^= r; r ^= l; l ^= r;
            }
            if (type == 1) {
                shift(l, r);
            } else {
                int k = (in.nextInt() - 1 + ans) % n + 1;
                ans = count(l, r, k);
                out.println(ans);
            }
        }


    }

    public static void main(String[] args) throws FileNotFoundException {
        in = new FastReader(System.in);
//        in = new FastReader(new FileInputStream("sometext.txt"));
        out = new PrintWriter(System.out);
//        out = new PrintWriter(new FileOutputStream("output.txt"));

        int T = 1;
//        T = in.nextInt();
        while (T-- > 0)
            solve();

//        Thread thread = new Thread(null, () -> {
//            solve();
//        }, "Main", 1 << 28);
//        thread.start();
//        try {
//            thread.join();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

//        out.flush();
        out.close();
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        FastReader(InputStream is) {
            br = new BufferedReader(new InputStreamReader(is));
        }

        Integer nextInt() {
            return Integer.parseInt(next());
        }

        Long nextLong() {
            return Long.parseLong(next());
        }

        Double nextDouble() {
            return Double.parseDouble(next());
        }

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                st = new StringTokenizer(nextLine());
            }
            return st.nextToken();
        }

        String nextLine() {
            String s = "";
            try {
                s = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return s;
        }
    }

    static void dbg(Object... args) {
        for (Object arg : args)
            out.print(" " + arg);
        out.flush();
    }

}