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.OutputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Deque;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.ArrayDeque;
import java.io.InputStream;

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

    static class TaskD {
        static final int blockSize = 350;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.readInt();
            int[] a = IOUtils.readIntArray(in, n);
            Sqrt sqrt = new Sqrt(a);
            int q = in.readInt();
            for (int i = 0, lastAns = 0; i < q; i++) {
                int type = in.readInt();
                int l = ((in.readInt() + lastAns - 1) % n);
                int r = ((in.readInt() + lastAns - 1) % n);
                int x = type == 1 ? -1 : ((in.readInt() + lastAns - 1) % n) + 1;

                if (l > r) {
                    int t = l;
                    l = r;
                    r = t;
                }

                if (type == 1) {
                    sqrt.rotate(l, r);
                } else {
                    out.printLine(lastAns = sqrt.count(x, l, r));
                }
            }
        }

        class Sqrt {
            TaskD.Block[] blocks;

            Sqrt(int[] a) {
                int blockCount = (a.length + blockSize - 1) / blockSize;
                blocks = new TaskD.Block[blockCount];
                for (int i = 0; i < blockCount; i++) {
                    blocks[i] = new TaskD.Block(a, i * blockSize, Math.min(a.length - 1, (i + 1) * blockSize - 1));
                }
            }

            void rotate(int l, int r) {
                int removeBlock = r / blockSize;
                int removed = blocks[removeBlock].remove(r % blockSize);
                for (int i = removeBlock + 1; i < blocks.length; i++) {
                    blocks[i - 1].addLast(blocks[i].removeFirst());
                }

                int addBlock = l / blockSize;
                blocks[addBlock].add(l % blockSize, removed);
                for (int i = addBlock + 1; i < blocks.length; i++) {
                    blocks[i].addFirst(blocks[i - 1].removeLast());
                }
            }

            int count(int x, int l, int r) {
                int leftBlock = l / blockSize;
                int rightBlock = r / blockSize;
                if (leftBlock == rightBlock) {
                    return blocks[leftBlock].count(x, l % blockSize, r % blockSize);
                } else {
                    int countLeft = blocks[leftBlock].count(x, l % blockSize, Integer.MAX_VALUE);
                    int countRight = blocks[rightBlock].count(x, Integer.MIN_VALUE, r % blockSize);
                    int countBetween = 0;
                    for (int i = leftBlock + 1; i <= rightBlock - 1; i++) {
                        countBetween += blocks[i].count(x);
                    }
                    return countLeft + countBetween + countRight;
                }
            }

        }

        static class Block {
            int[] count;
            Deque<Integer> q;
            static int[] temp = new int[blockSize];

            Block(int[] a, int from, int to) {
                count = new int[a.length + 1];
                q = new ArrayDeque<>(to - from + 2);
                for (int at = from; at <= to; at++) {
                    count[a[at]]++;
                    q.addLast(a[at]);
                }
            }

            int count(int x) {
                return count[x];
            }

            int count(int x, int l, int r) {
                Iterator<Integer> iter = q.iterator();
                int count = 0;
                for (int i = 0; iter.hasNext() && i <= r; i++) {
                    if (iter.next() == x && l <= i) {
                        count++;
                    }
                }
                return count;
            }

            int remove(int at) {
                Iterator<Integer> it = q.iterator();
                for (int i = 0; i < at; i++) {
                    it.next();
                }
                int value = it.next();
                count[value]--;
                it.remove();
                return value;
            }

            int removeFirst() {
                int value = q.pollFirst();
                count[value]--;
                return value;
            }

            int removeLast() {
                int value = q.pollLast();
                count[value]--;
                return value;
            }

            void add(int at, int value) {
                for (int i = 0; i < at; i++) {
                    temp[i] = q.pollFirst();
                }
                addFirst(value);
                for (int i = at - 1; i >= 0; i--) {
                    q.addFirst(temp[i]);
                }
            }

            void addLast(int value) {
                q.addLast(value);
                count[value]++;
            }

            void addFirst(int value) {
                q.addFirst(value);
                count[value]++;
            }

        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void printLine(int i) {
            writer.println(i);
        }

    }

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

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

        public 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 readInt() {
            int c = read();
            while (isSpaceChar(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 (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

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

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }

    static class IOUtils {
        public static int[] readIntArray(InputReader in, int size) {
            int[] array = new int[size];
            for (int i = 0; i < size; i++) {
                array[i] = in.readInt();
            }
            return array;
        }

    }
}