Problem: — Willem...

— What's the matter?

— It seems that there's something wrong with Seniorious...

— I'll have a look...

<image>

Seniorious is made by linking special talismans in particular order.

After over 500 years, the carillon is now in bad condition, so Willem decides to examine it thoroughly.

Seniorious has n pieces of talisman. Willem puts them in a line, the i-th of which is an integer ai.

In order to maintain it, Willem needs to perform m operations.

There are four types of operations:

  * 1 l r x: For each i such that l ≤ i ≤ r, assign ai + x to ai.
  * 2 l r x: For each i such that l ≤ i ≤ r, assign x to ai.
  * 3 l r x: Print the x-th smallest number in the index range [l, r], i.e. the element at the x-th position if all the elements ai such that l ≤ i ≤ r are taken and sorted into an array of non-decreasing integers. It's guaranteed that 1 ≤ x ≤ r - l + 1.
  * 4 l r x y: Print the sum of the x-th power of ai such that l ≤ i ≤ r, modulo y, i.e. <image>.

Input

The only line contains four integers n, m, seed, vmax (1 ≤ n, m ≤ 105, 0 ≤ seed < 109 + 7, 1 ≤ vmax ≤ 109).

The initial values and operations are generated using following pseudo code:
    
    
      
    def rnd():  
      
        ret = seed  
        seed = (seed * 7 + 13) mod 1000000007  
        return ret  
      
    for i = 1 to n:  
      
        a[i] = (rnd() mod vmax) + 1  
      
    for i = 1 to m:  
      
        op = (rnd() mod 4) + 1  
        l = (rnd() mod n) + 1  
        r = (rnd() mod n) + 1  
      
        if (l > r):   
             swap(l, r)  
      
        if (op == 3):  
            x = (rnd() mod (r - l + 1)) + 1  
        else:  
            x = (rnd() mod vmax) + 1  
      
        if (op == 4):  
            y = (rnd() mod vmax) + 1  
      
    

Here op is the type of the operation mentioned in the legend.

Output

For each operation of types 3 or 4, output a line containing the answer.

Examples

Input

10 10 7 9


Output

2
1
0
3


Input

10 10 9 9


Output

1
1
3
3

Note

In the first example, the initial array is {8, 9, 7, 2, 3, 1, 5, 6, 4, 8}.

The operations are:

  * 2 6 7 9
  * 1 3 10 8
  * 4 4 6 2 4
  * 1 4 5 8
  * 2 1 7 1
  * 4 7 9 4 4
  * 1 2 7 9
  * 4 5 8 1 1
  * 2 5 7 5
  * 4 3 10 8 5

Solution (in JAVA):

Solution:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class Div1_449C {

	public static void main(String[] args) throws IOException {
		new Div1_449C().execute();
	}

	void execute() throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter printer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		StringTokenizer inputData = new StringTokenizer(reader.readLine());
		nE = Integer.parseInt(inputData.nextToken());
		int nQ = Integer.parseInt(inputData.nextToken());
		seed = Integer.parseInt(inputData.nextToken());
		vmax = Integer.parseInt(inputData.nextToken());

		vals = new TreeSet<>();

		//array = new long[nE];
		int first = nxt() % vmax + 1;
		vals.add(new Range(0, first));
		//array[0] = first;
		for (int i = 1; i < nE; i++) {
			int cur = nxt() % vmax + 1;
			Range pR = vals.last();
			if (pR.v != cur) {
				vals.add(new Range(i, cur));
			}
			//array[i] = cur;
		}

		for (int i = 1; i <= nQ; i++) {
			int op = nxt() % 4 + 1;
			int l = nxt() % nE;
			int r = nxt() % nE;
			if (l > r) {
				int temp = l;
				l = r;
				r = temp;
			}
			if (op == 1) {
				op1(l, r, nxt() % vmax + 1);
				//check();
			} else if (op == 2) {
				op2(l, r, nxt() % vmax + 1);
				//check();
			} else if (op == 3) {
				printer.println(op3(l, r, nxt() % (r - l + 1) + 1));
			} else {
				printer.println(op4(l, r, nxt() % vmax + 1, nxt() % vmax + 1));
			}
		}
		printer.close();
	}
	/*
	long[] array;

	void check() {
		for (int i = 0; i < nE; i++) {
			Range tRange = vals.floor(new Range(i));
			assert (tRange.v == array[i]);
			if (tRange.v != array[i]) {
				System.out.println("Strange");
			}
		}
	}*/

	int seed;
	int vmax;

	int nxt() {
		int ret = seed;
		seed = (int) (((long) seed * 7 + 13) % 1000000007);
		return ret;
	}

	int nE;
	TreeSet<Range> vals;

	void op1(int uL, int uR, int d) {
		Range cR = vals.floor(new Range(uL));
		if (cR.l < uL) {
			cR = new Range(uL, cR.v);
			vals.add(cR);
		}

		while (cR != null && cR.l <= uR) {
			Range higher = vals.higher(cR);
			if (higher == null ? uR < nE - 1 : uR < higher.l - 1) {
				higher = new Range(uR + 1, cR.v);
				vals.add(higher);
			}

			cR.v += d;
			cR = higher;
		}

		Range lMost = vals.floor(new Range(uL));
		Range lower = vals.lower(lMost);
		if (lower != null && lower.v == lMost.v) {
			vals.remove(lMost);
		}

		Range tRight = vals.ceiling(new Range(uR + 1));
		if (tRight != null) {
			lower = vals.lower(tRight);
			if (lower != null && lower.v == tRight.v) {
				vals.remove(tRight);
			}
		}
		/*for (int i = uL; i <= uR; i++) {
			array[i] += d;
		}*/
	}

	void op2(int uL, int uR, int nV) {
		/*for (int i = uL; i <= uR; i++) {
			array[i] = nV;
		}*/
		Range cR = vals.floor(new Range(uL));
		if (cR.l < uL) {
			cR = new Range(uL, cR.v);
			vals.add(cR);
		}

		while (cR != null && cR.l <= uR) {
			Range higher = vals.higher(cR);
			if (higher == null ? uR < nE - 1 : uR < higher.l - 1) {
				higher = new Range(uR + 1, cR.v);
				vals.add(higher);
			}

			vals.remove(cR);
			cR = higher;
		}
		vals.add(new Range(uL, nV));

		Range lMost = vals.floor(new Range(uL));
		Range lower = vals.lower(lMost);
		if (lower != null && lower.v == lMost.v) {
			vals.remove(lMost);
		}

		Range tRight = vals.ceiling(new Range(uR + 1));
		if (tRight != null) {
			lower = vals.lower(tRight);
			if (lower != null && lower.v == tRight.v) {
				vals.remove(tRight);
			}
		}
	}

	long op3(int qL, int qR, int qI) {
		ArrayList<Num> numbers = new ArrayList<>();
		Range cR = vals.floor(new Range(qL));
		Range higher = vals.higher(cR);
		numbers.add(new Num(cR.v, Math.min(qR, higher != null ? higher.l - 1 : nE - 1) - qL + 1));

		cR = higher;

		while (cR != null && cR.l <= qR) {
			higher = vals.higher(cR);
			numbers.add(new Num(cR.v, Math.min(qR, higher != null ? higher.l - 1 : nE - 1) - cR.l + 1));
			cR = higher;
		}
		Collections.sort(numbers);
		for (Num cNum : numbers) {
			if (cNum.c >= qI) {
				return cNum.n;
			}
			qI -= cNum.c;
		}
		return -1;
	}

	long exp(long b, int p, int m) {
		b %= m;
		long c = 1;
		while (p > 0) {
			if ((p & 1) != 0) {
				c = c * b % m;
			}
			b = b * b % m;
			p >>= 1;
		}
		return c;
	}

	long op4(int qL, int qR, int qP, int qM) {
		long sum = 0;
		Range cR = vals.floor(new Range(qL));
		Range higher = vals.higher(cR);
		sum = exp(cR.v, qP, qM) * (Math.min(qR, higher != null ? higher.l - 1 : nE - 1) - qL + 1) % qM;

		cR = higher;

		while (cR != null && cR.l <= qR) {
			higher = vals.higher(cR);
			sum = (sum + exp(cR.v, qP, qM) * (Math.min(qR, higher != null ? higher.l - 1 : nE - 1) - cR.l + 1)) % qM;
			cR = higher;
		}
		return sum;
	}

	class Num implements Comparable<Num> {
		long n;
		int c;

		Num(long n, int c) {
			this.n = n;
			this.c = c;
		}

		public int compareTo(Num o) {
			return Long.compare(n, o.n);
		}
	}

	class Range implements Comparable<Range> {
		int l;
		long v;

		Range(int l) {
			this.l = l;
		}

		Range(int l, long v) {
			this.l = l;
			this.v = v;
		}

		public int compareTo(Range o) {
			return Integer.compare(l, o.l);
		}
	}
}