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.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.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[] L = new int[SQRT + 1];
        int[] R = new int[SQRT + 1];

        int process(int a, int b, int mod) {
            int ans = a;
            ans += b - 1;
            if (ans >= mod) 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[i / SQRT][a[i]]++;
                deq[i / SQRT].addLast(a[i]);
            }
            int q = in.nextInt();
            int lastAns = 0;
            int sizeL = 0;
            int sizeR = 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;
                }
                int blockL = blockID(l);
                int blockR = blockID(r);
                if (type == 1) {
                    sizeL = 0;
                    for (int val : deq[blockL]) L[sizeL++] = val;
                    sizeR = 0;
                    for (int val : deq[blockR]) R[sizeR++] = val;
                    int idxInL = l % SQRT;
                    int idxInR = r % SQRT;
                    deq[blockL].clear();
                    for (int j = 0; j < sizeL; j++) {
                        if (idxInL == j) deq[blockL].addLast(R[idxInR]);
                        if (blockL == blockR && idxInR == j) continue;
                        deq[blockL].addLast(L[j]);
                    }
                    if (blockR != blockL) {
                        map[blockL][R[idxInR]]++;
                        deq[blockR].clear();
                        for (int j = 0; j < sizeR; j++) {
                            if (idxInR == j) continue;
                            deq[blockR].addLast(R[j]);
                        }
                        map[blockR][R[idxInR]]--;
                    }
                    for (int idx = blockL; idx < blockR; 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;
                    sizeL = 0;
                    for (int val : deq[blockL]) L[sizeL++] = val;
                    int begL = blockL * SQRT;
                    for (int idx = 0; begL <= r && idx < sizeL; idx++, begL++) {
                        if (begL < l) {
                            continue;
                        }
                        if (L[idx] == k) ans++;
                    }
                    if (blockL != blockR) {
                        sizeR = 0;
                        for (int val : deq[blockR]) R[sizeR++] = val;
                        int begR = blockR * SQRT;
                        for (int idx = 0; idx < sizeR && begR <= r; idx++, begR++) {
                            if (R[idx] == k) ans++;
                        }
                    }
                    for (int idx = blockL + 1; idx <= blockR - 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;
        }

    }
}

