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.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.ArrayDeque;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.util.List;
import java.util.StringTokenizer;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Deque;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author AlexFetisov
 */
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_SeregaFun solver = new TaskD_SeregaFun();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskD_SeregaFun {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int[] a = Utils.readIntArray(in, n);
            TaskD_SeregaFun.SqrtDec sqrtDec = new TaskD_SeregaFun.SqrtDec(a, n);
            int lastAns = 0;
            int countQ = in.nextInt();
            while (countQ-- > 0) {
                int type = in.nextInt();
                int left = (in.nextInt() + lastAns - 1) % n;
                int right = (in.nextInt() + lastAns - 1) % n;
                if (left > right) {
                    int t = left;
                    left = right;
                    right = t;
                }
                if (type == 1) {
                    sqrtDec.shift(left, right);
                } else {
                    int k = (in.nextInt() + lastAns - 1) % n + 1;
                    lastAns = sqrtDec.count(left, right, k);
                    out.println(lastAns);
                }
            }
        }

        class SqrtDec {
            int countBlocks;
            final int IN_BLOCK = 333;
            int[] a;
            int n;
            short[][] have;
            int[] blockStarts;
            int[] blockEnds;
            int[] inBlock;
            Deque<Integer>[] chain;

            public SqrtDec(int[] a, int n) {
                this.a = a;
                this.n = n;
                countBlocks = (n + IN_BLOCK - 1) / IN_BLOCK;
                // dict = new HashMap[countBlocks];
                have = new short[400][100001];
                chain = new ArrayDeque[countBlocks];
                for (int i = 0; i < countBlocks; ++i) {
                    //dict[i] = new HashMap<Integer, Integer>();
                    chain[i] = new ArrayDeque<Integer>();
                }

                blockStarts = new int[countBlocks];
                blockEnds = new int[countBlocks];
                inBlock = new int[n];

                for (int start = 0, blockId = 0; start < n; start += IN_BLOCK, ++blockId) {
                    int end = start + IN_BLOCK - 1;
                    blockStarts[blockId] = start;
                    blockEnds[blockId] = end;
                    for (int i = start; i <= end; ++i) {
                        if (i >= n) break;
                        inBlock[i] = blockId;
                        //CollectionUtils.addToMap(dict[blockId], a[i]);
                        ++have[blockId][a[i]];
                        chain[blockId].add(a[i]);
                    }
                }
            }

            void shift(int left, int right) {
                if (left == right) return;
                int start = inBlock[left];
                int end = inBlock[right];
                if (start == end) {
                    left -= blockStarts[start];
                    right -= blockStarts[start];
                    List<Integer> list = new ArrayList<Integer>(chain[start]);
                    int element = list.remove(right);
                    list.add(left, element);
                    chain[start] = new ArrayDeque<Integer>(list);
                    return;
                }

                left -= blockStarts[start];
                right -= blockStarts[end];

                List<Integer> list = new ArrayList<Integer>(chain[end]);
                int lastElement = list.remove(right);
                chain[end] = new ArrayDeque<Integer>(list);

//            int lastElement = removeFromDeque(chain[end], right);
                // int lastElement = chain[end].remove(right);
                //CollectionUtils.removeFromMap(dict[end], lastElement);
                --have[end][lastElement];

                // Do everything with the left block
                list = new ArrayList<Integer>(chain[start]);
                list.add(left, lastElement);
                chain[start] = new ArrayDeque<Integer>(list);
                //chain[start].add(left, lastElement);
                //CollectionUtils.addToMap(dict[start], lastElement);
                ++have[start][lastElement];
                int toRemove = chain[start].removeLast();
                //CollectionUtils.removeFromMap(dict[start], toRemove);
                --have[start][toRemove];

                // Now shift all the chains
                for (int i = start + 1; i < end; ++i) {
                    chain[i].addFirst(toRemove);
                    //CollectionUtils.addToMap(dict[i], toRemove);
                    ++have[i][toRemove];
                    toRemove = chain[i].removeLast();
//                CollectionUtils.removeFromMap(dict[i], toRemove);
                    --have[i][toRemove];
                }

                // Now finish with the last block
                chain[end].addFirst(toRemove);
                ++have[end][toRemove];
//            CollectionUtils.addToMap(dict[end], toRemove);
            }

            int count(int left, int right, int element) {
                int start = inBlock[left];
                int end = inBlock[right];
                int count = 0;
                if (start == end) {
                    left -= blockStarts[start];
                    right -= blockStarts[start];
                    List<Integer> clone = new ArrayList<Integer>(chain[start]);
                    for (int i = left; i <= right; ++i) {
                        //if (chain[start].get(i) == element) {
                        if (clone.get(i) == element) {
                            ++count;
                        }
                    }
                    return count;
                }

                left -= blockStarts[start];
                right -= blockStarts[end];
                List<Integer> clone = new ArrayList<Integer>(chain[start]);
                for (int i = left; i <= blockEnds[start] - blockStarts[start]; ++i) {
                    if (clone.get(i) == element) {
                        ++count;
                    }
                }
                clone = new ArrayList<Integer>(chain[end]);
                for (int i = 0; i <= right; ++i) {
                    if (clone.get(i) == element) {
                        ++count;
                    }
                }
                // Now shift all the chains
                for (int i = start + 1; i < end; ++i) {
                    count += have[i][element];
//                Integer c = dict[i].get(element);
//                if (c != null) {
//                    count += c;
//                }
                }
                return count;
            }

        }

    }

    static class InputReader {
        private BufferedReader reader;
        private StringTokenizer stt;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
        }

        public String nextLine() {
            try {
                return reader.readLine();
            } catch (IOException e) {
                return null;
            }
        }

        public String nextString() {
            while (stt == null || !stt.hasMoreTokens()) {
                stt = new StringTokenizer(nextLine());
            }
            return stt.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextString());
        }

    }

    static class Utils {
        public static int[] readIntArray(InputReader in, int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; ++i) {
                a[i] = in.nextInt();
            }
            return a;
        }

    }
}