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.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.StringTokenizer;

public class p100
{
	static final int SIZE = 316;
	static int n, numBuckets;
	public static void main(String[] args) throws IOException
	{
		FastScanner in = new FastScanner();
		PrintWriter out = new PrintWriter(System.out);
		n = in.nextInt();
		numBuckets = (n+SIZE-1)/SIZE;
		ArrayDeque<Integer> buckets[] = new ArrayDeque[numBuckets];
		int cnts[][] = new int[numBuckets][n];
		for(int i = 0; i < n; i++)
		{
			int v = in.nextInt()-1;
			int idx = i/SIZE;
			if(buckets[idx] == null)
				buckets[idx] = new ArrayDeque<>();
			
			buckets[idx].add(v);
			cnts[idx][v]++;
		}
		
		int q = in.nextInt();
		int prev = 0;
		for(int m = 0; m < q; m++)
		{
			int type = in.nextInt();
			int l = (in.nextInt() + prev - 1) % n;
			int r = (in.nextInt() + prev - 1) % n;

			if(l > r)
				l ^= r ^ (r=l);
			
//			for(int i = 0; i < numBuckets; i++)
//				System.out.println(buckets[i]);
//			System.out.println();
//			System.out.println(l + " " + r);
			
			if(type == 1)
			{
				int lastBucket = r/SIZE;
				int lastIdx = r%SIZE;
				int lastVal = 0;
				int firstBucket = l/SIZE;
				int firstIdx = l%SIZE;
				
				ArrayDeque<Integer> temp = new ArrayDeque<>();
				for(int i = 0; !buckets[lastBucket].isEmpty(); i++)
				{
					if(i == lastIdx)
					{
						lastVal = buckets[lastBucket].poll();
						cnts[lastBucket][lastVal]--;
					}
					else
						temp.add(buckets[lastBucket].poll());
				}
				
				buckets[lastBucket] = temp;
				for(int j = lastBucket-1; j >= firstBucket; j--)
				{
					int v = buckets[j].pollLast();
					buckets[j+1].addFirst(v);
					cnts[j][v]--;
					cnts[j+1][v]++;
				}
				
				temp = new ArrayDeque<>();
				int i = 0;
				for(i = 0; !buckets[firstBucket].isEmpty(); i++)
				{
					if(i == firstIdx)
					{
						temp.addLast(lastVal);
						cnts[firstBucket][lastVal]++;
					}
					else
						temp.add(buckets[firstBucket].poll());
				}
				if(i == firstIdx)
				{
					temp.addLast(lastVal);
					cnts[firstBucket][lastVal]++;
				}
				buckets[firstBucket] = temp;
			}
			else
			{
				int ans = 0;
				int lastBucket = r/SIZE;
				int lastIdx = r%SIZE;
				int firstBucket = l/SIZE;
				int firstIdx = l%SIZE;
				int k = (in.nextInt() + prev - 1) % n;
//				System.out.println("FINDING : " + k);
				
				int i = 0;
				for(int v : buckets[firstBucket])
				{
					if(v == k)
					{
						if(lastBucket == firstBucket && i >= firstIdx && i <= lastIdx)
							ans++;
						else if(lastBucket != firstBucket && i >= firstIdx && v == k)
							ans++;
					}
					i++;
				}
				
				i = 0;
				for(int v : buckets[lastBucket])
				{
					if(lastBucket != firstBucket && i <= lastIdx && v == k)
						ans++;
					i++;
				}
				
				for(i = firstBucket+1; i < lastBucket; i++)
					ans += cnts[i][k];
				
				prev = ans;
				out.println(prev);
			}
		}
		
		out.close();
	}
	
	public static class FastScanner
	{
		BufferedReader br;
		StringTokenizer st;

		public FastScanner(String s)
		{
			try {
				br = new BufferedReader(new FileReader(s));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		public FastScanner()
		{
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String nextToken()
		{
			while (st == null || !st.hasMoreElements())
			{
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt()
		{
			return Integer.parseInt(nextToken());
		}

		long nextLong()
		{
			return Long.parseLong(nextToken());
		}

		double nextDouble()
		{
			return Double.parseDouble(nextToken());
		}
		
		String next()
		{
			return nextToken();
		}
		
		public boolean hasNext()
		{
			if(st != null && st.hasMoreElements())
				return true;
			
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				return false;
			}
			
			if(st == null) return false;
			return true;
		}
	}
}