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 JAVA):

Solution:
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Deque;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author beginner1010
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskD solver = new TaskD();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskD {
        final int SQRT = 330;

        int process(int a, int b, int mod) {
            long ans = a;
            ans += b - 1;
            ans %= mod;
            if (ans < 0) ans += mod;
            return (int) (ans + 1);
        }

        int blockID(int idx) {
            return idx / SQRT;
        }

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int[] a = new int[n];
            int[][] map = new int[SQRT][n];
            Deque<Integer>[] deq = new Deque[SQRT];
            for (int i = 0; i < SQRT; i++) {
                deq[i] = new ArrayDeque<>();
            }
            for (int i = 0; i < n; i++) {
                a[i] = in.nextInt() - 1;
                map[blockID(i)][a[i]]++;
                deq[blockID(i)].addLast(a[i]);
            }
            int q = in.nextInt();
            int lastAns = 0;
            for (int query = 0; query < q; query++) {
                int type = in.nextInt();
                int l = process(in.nextInt(), lastAns, n) - 1;
                int r = process(in.nextInt(), lastAns, n) - 1;
                if (l > r) {
                    int temp = l;
                    l = r;
                    r = temp;
                }

//            for(int i = 0;i < deq.length; i ++) {
//                for(int val: deq[i]) {
//                    System.out.print(val + " ");
//                }
//            }
//            System.out.println();
//            for(int i = 0; i < map.length; i ++) {
//                for(int j = 0; j < n; j ++) {
//                    System.out.print("(" + map[i][j] + ", " + j + "), ");
//                }
//                System.out.println();
//            }
//            System.out.println();
                if (type == 1) {
                    ArrayList<Integer> L = new ArrayList<>();
                    for (int val : deq[blockID(l)]) L.add(val);
                    ArrayList<Integer> R = new ArrayList<>();
                    for (int val : deq[blockID(r)]) R.add(val);
                    int idxInL = l % SQRT;
                    int idxInR = r % SQRT;
                    deq[blockID(l)].clear();
                    for (int j = 0; j < L.size(); j++) {
                        if (idxInL == j) deq[blockID(l)].addLast(R.get(idxInR));
                        if (blockID(l) == blockID(r) && idxInR == j) continue;
                        deq[blockID(l)].addLast(L.get(j));
                    }
                    if (blockID(r) != blockID(l)) {
                        map[blockID(l)][R.get(idxInR)]++;
                        deq[blockID(r)].clear();
                        for (int j = 0; j < R.size(); j++) {
                            if (idxInR == j) continue;
                            deq[blockID(r)].addLast(R.get(j));
                        }
                        map[blockID(r)][R.get(idxInR)]--;
                    }
                    for (int idx = blockID(l); idx < blockID(r); idx++) {
                        int x = deq[idx].pollLast();
                        deq[idx + 1].addFirst(x);
                        map[idx][x]--;
                        map[idx + 1][x]++;
                    }
                } else {
                    int k = process(in.nextInt(), lastAns, n) - 1;
                    int ans = 0;
                    ArrayList<Integer> arrL = new ArrayList<>();
                    for (int val : deq[blockID(l)]) arrL.add(val);
                    int begL = blockID(l) * SQRT;
                    for (int idx = 0; begL <= r && idx < arrL.size(); idx++, begL++) {
                        if (begL < l) {
                            continue;
                        }
                        if (arrL.get(idx) == k) ans++;
                    }
                    if (blockID(l) != blockID(r)) {
                        ArrayList<Integer> arrR = new ArrayList<>();
                        for (int val : deq[blockID(r)]) arrR.add(val);
                        int begR = blockID(r) * SQRT;
                        for (int idx = 0; idx < arrR.size() && begR <= r; idx++, begR++) {
                            if (arrR.get(idx) == k) ans++;
                        }
                    }
                    for (int idx = blockID(l) + 1; idx <= blockID(r) - 1; idx++) {
                        ans += map[idx][k];
                    }
                    out.println(ans);
                    lastAns = ans;
                }
            }
        }

    }

    static class InputReader {
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputStream stream;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        private boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isWhitespace(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isWhitespace(c));
            return res * sgn;
        }

    }
}