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.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

@SuppressWarnings("unchecked")
public class D455 implements Runnable {
	
	int n, q;
	int sqrt;
	int[] input;
	ArrayList<Integer>[] a;
	int[][] freq;
	
	void shift(int l, int r) {
		//find buckets for l and r
		int bucketL = -1, indexL = -1;
		int bucketR = -1, indexR = -1;
		for(int i = 0; i < sqrt; ++i) {
			if(indexL == -1 && l >= a[i].size()) {
				l -= a[i].size();
			} else if(indexL == -1) {
				bucketL = i;
				indexL = l;
			}
			if(indexR == -1 && r >= a[i].size()) {
				r -= a[i].size();
			} else if(indexR == -1) {
				bucketR = i;
				indexR = r;
			}
		}
		//move r'th element to be before the l'th element
		int x = a[bucketR].get(indexR);
		freq[bucketR][x]--;
		freq[bucketL][x]++;
		a[bucketR].remove(indexR);
		a[bucketL].add(indexL, x);
	}
	
	void rebuild() {
		int idx = 0, bucketId = 0;
		for(ArrayList<Integer> bucket : a) {
			for(int cur : bucket) {
				input[idx++] = cur;
				freq[bucketId][cur]--;
			}
			++bucketId;
		}
		for(int i = 0, bucket = 0; i < n; ++bucket, i += sqrt) {
			while(a[bucket].size() > sqrt) a[bucket].remove(a[bucket].size() - 1);
			for(int j = i, k = 0; j < i + sqrt && j < n; ++j, ++k) {
				if(a[bucket].size() <= k) a[bucket].add(0);
				a[bucket].set(k, input[j]);
				freq[bucket][input[j]]++;
			}
		}
	}
	
	int go(int l, int r, int want) {
		int behind = 0;
		int ans = 0;
		for(int bucket = 0; bucket < sqrt; ++bucket) {
			int cl = behind, cr = behind + a[bucket].size() - 1;
			behind += a[bucket].size();
			if(r < cl || cr < l) continue; //no cover
			if(l <= cl && cr <= r) { //full cover
				ans += freq[bucket][want];
				continue;
			}
			//partial cover
			for(int i = cl, j = 0; j < a[bucket].size(); ++i, ++j) {
				if(i < l) continue;
				if(i > r) break;
				if(a[bucket].get(j) == want) {
					++ans;
				}
			}
		}
		return ans;
	}
	
	public void solve(FS in, PrintWriter out) {
		n = in.nextInt();
		input = new int[n];
		for(int i = 0; i < n; ++i) input[i] = in.nextInt() - 1;
		sqrt = (int) Math.sqrt(n) + 1;
		a = new ArrayList[sqrt];
		freq = new int[sqrt][n];
		for(int i = 0; i < sqrt; ++i) a[i] = new ArrayList<>();
		for(int i = 0, bucket = 0; i < n; ++bucket, i += sqrt) {
			for(int j = i; j < i + sqrt && j < n; ++j) {
				a[bucket].add(input[j]);
				freq[bucket][input[j]]++;
			}
		}
		q = in.nextInt();
		int lastans = 0;
		for(int qq = 0; qq < q; ++qq) {
			int type = in.nextInt();
			int l = in.nextInt(), r = in.nextInt();
			l = (l + lastans - 1) % n;
			r = (r + lastans - 1) % n;
			if(l > r) { int tmp = l; l = r; r = tmp; }
			if(type == 1) {
				shift(l, r);
			} else if(type == 2) {
				int k = in.nextInt();
				k = (k + lastans - 1) % n;
				lastans = go(l, r, k);
				out.println(lastans);
			} else throw null;
			if((qq + 1) % sqrt == 0) rebuild();
		}
	}
	
	public void run() {
		System.err.println("go!");
		FS in = new FS();
		PrintWriter out = new PrintWriter(System.out);
		solve(in, out);
		out.close();
	}
	
	public static void main(String[] args) {
		new Thread(null, new D455(), "lmao", 1L<<28).start();
	}
	static class FS {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer("");
		String next() {
			while(!st.hasMoreElements()) {
				try { st = new StringTokenizer(br.readLine()); }
				catch(Exception e) {}
			}
			return st.nextToken();
		}
		int nextInt() {
			return Integer.parseInt(next());
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}
}