package main

import (
	"bufio"
	"fmt"
	"os"
)

var stdin *bufio.Reader
var stdout *bufio.Writer

func init() {
	stdin = bufio.NewReader(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func printf(format string, arg ...interface{}) {
	fmt.Fprintf(stdout, format, arg...)
}

func scan(arg ...interface{}) (int, error) {
	return fmt.Fscan(stdin, arg...)
}

func main() {
	for true {
		var n, k int
		if _, err := scan(&n, &k); err != nil {
			break
		}
		a := make([]int, n)
		for i := 0; i < n; i++ {
			scan(&a[i])
		}
		if k == 1 {
			min := a[0]
			for i := 0; i < n; i++ {
				if a[i] < min {
					min = a[i]
				}
			}
			printf("%d\n", min)
		} else if k == 2 {
			max := a[0]
			if a[n-1] > max {
				max = a[n-1]
			}
			printf("%d\n", max)
		} else {
			max := a[0]
			for i := 0; i < n; i++ {
				if max < a[i] {
					max = a[i]
				}
			}
			printf("%d\n", max)
		}
	}
	stdout.Flush()
}

// Codeforces helper functions
// 20 Jan 18
// samadhi

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
	"strings"
	"unicode"
)

const debug = true

func main() {
	reader := bufio.NewReader(os.Stdin)

	in1 := gsli(reader)
	k := in1[1]

	in := gsli(reader)

	min := in[0]
	max := in[0]

	for _, e := range in {
		if e > max {
			max = e
		}
		if e < min {
			min = e
		}
	}

	if k == 1 {
		fmt.Println(min)
	} else if k == 2 {
		if in[0] >= in[len(in)-1] {
			fmt.Println(in[0])
		} else {
			fmt.Println(in[len(in)-1])
		}
	} else {
		fmt.Println(max)
	}
}

// Input helpers

// If you don't pass in a reference to the reader, and just instantiate a new
// one every time, it will work locally, with IO typed at human-speed, but not
// on Codeforces.

// Get Space-separated Line as Strings
//
// Example input:
// 1 test 382.0
// Example output:
// ["1", "test", "382.0"]

func gsls(reader *bufio.Reader) (arr []string) {
	// Get a full line
	input, _ := reader.ReadString('\n')

	//Strip newline and return chars, if any
	input = strings.TrimSuffix(input, "\n")
	input = strings.TrimSuffix(input, "\r")

	// Split at the spaces into an array
	arr = strings.Split(input, " ")

	return
}

// Get Space-separated Line as Integers
// Any non-valid integers will be 0
//
// Example input:
// test 1 12.3 -117
// Example output:
// [0, 1, 0, -117]

func gsli(reader *bufio.Reader) (arr []int) {
	stringArr := gsls(reader)

	for _, e := range stringArr {
		cur, _ := strconv.Atoi(e)
		arr = append(arr, cur)
	}

	return
}

// Get Space-separated Line as 64-bit Floats
// Any non-valid floats will be 0
//
// Example input:
// test 128.328 12 -11.0
// Example output:
// [0, 128.328, 12, -11]

func gslf(reader *bufio.Reader) (arr []float64) {
	stringArr := gsls(reader)

	for _, e := range stringArr {
		cur, _ := strconv.ParseFloat(e, 64)
		arr = append(arr, cur)
	}

	return
}

// String helpers

// Is Lowercase
// Given some string[i], return whether it is lower case or not.

func isl(b byte) bool {
	var r rune
	r = rune(b)
	return unicode.IsLower(r)
}

// Math helpers

// Positve Modulo
// Returns x mod y, but always positive

func posMod(x, y int) int {
	return (x%y + y) % y
}

// Max Int
// Returns the max of two ints

func maxInt(x, y int) int {
	if x > y {
		return x
	}
	return y
}

// Distance formule
// Calculates the distance to the origin

func dist(x, y float64) float64 {
	return math.Sqrt(math.Pow(x, 2) + math.Pow(y, 2))
}

// Output helpers

func yes() {
	fmt.Println("Yes")
}

func no() {
	fmt.Println("No")
}

// Debug helpers

// From https://groups.google.com/forum/#!topic/golang-nuts/Qlxs3V77nss

func dbg(s string, a ...interface{}) {
	if debug {
		fmt.Printf(s, a...)
	}
}

package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
	"strings"
)

func main() {
	data, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		fmt.Println(err.Error())
	}
	lines := strings.Split(string(data), "\r\n")
	parts := strings.Split(lines[0], " ")
	n, _ := strconv.Atoi(parts[0])
	k, _ := strconv.Atoi(parts[1])
	parts = strings.Split(lines[1], " ")
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i], err = strconv.Atoi(parts[i])
		if err != nil {
			fmt.Println(parts[i], err.Error())
		}
	}
	fmt.Println(solve(n, k, arr))
}

func solve(n, k int, arr []int) int {
	st := NewSparseTable(arr, func(d ...int) int {
		res := d[0]
		for _, v := range d {
			if v < res {
				res = v
			}
		}
		return res
	})
	if k == 1 {
		return st.QueryRangeFast(0, len(arr)-1)
	}
	if k >= 3 {
		res := arr[0]
		for i := 1; i < len(arr); i++ {
			if arr[i] > res {
				res = arr[i]
			}
		}
		return res
	}
	m := st.QueryRangeFast(0, 0)
	for i := 0; i < len(arr)-1; i++ {
		m = max(m, max(st.QueryRangeFast(0, i), st.QueryRangeFast(i+1, len(arr)-1)))
	}
	return m
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

// SparseTable is fast in multiple range maximum/minimum queryies in immutable array.
//
// Ref: https://cp-algorithms.com/data_structures/sparse-table.html .
type SparseTable struct {
	t  [][]int
	f  func(d ...int) int
	n  int
	lg []int
}

// NewSparseTable creates a sparse table with the range rule l. Time complexity: O(nlgn).
//
// NOTICE: Parameter f must be an [idempotent function](https://en.wikipedia.org/wiki/Idempotence) if you want to do range queries in O(1). If not, wrong query results will be returned.
func NewSparseTable(data []int, f func(d ...int) int) *SparseTable {
	n := len(data)
	t := make([][]int, n+1)
	for i := 0; i < n; i++ {
		t[i] = append(t[i], data[i])
	}
	for i := 1; 1<<i <= n && 1<<i > 0; i++ {
		for j := 0; j+(1<<i) <= n && j+(1<<i) > 0; j++ {
			t[j] = append(t[j], f(t[j][i-1], t[j+(1<<(i-1))][i-1]))
		}
	}
	lg := make([]int, n+1)
	lg[1] = 0
	for i := 2; i <= n; i++ {
		lg[i] = lg[i/2] + 1
	}
	return &SparseTable{
		t:  t,
		f:  f,
		n:  n,
		lg: lg,
	}
}

// QueryRangeFast queries the range [left, right] with function f.
//
// NOTICE: Only available if f is an [idempotent function](https://en.wikipedia.org/wiki/Idempotence).
//
// Time complexity: O(1).
func (s *SparseTable) QueryRangeFast(left, right int) int {
	l := right - left + 1
	return s.f(s.t[left][s.lg[l]], s.t[right-(1<<s.lg[l])+1][s.lg[l]])
}

// QueryRange queries the range [left, right] with function f.
//
// Time complexity: O(lgn).
func (s *SparseTable) QueryRange(left, right int) int {
	l := right - left + 1
	res := s.t[left][s.lg[l]]
	for left += 1 << s.lg[l]; left <= right; left += 1 << s.lg[l] {
		l = right - left + 1
		res = s.f(res, s.t[left][s.lg[l]])
	}
	return res
}

// Codeforces helper functions
// 20 Jan 18
// samadhi

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
	"strings"
	"unicode"
)

const debug = true

func main() {
	reader := bufio.NewReader(os.Stdin)

	in1 := gsli(reader)
	k := in1[1]

	in := gsli(reader)

	min := in[0]
	max := in[0]

	for _, e := range in {
		if e > max {
			max = e
		}
		if e < min {
			min = e
		}
	}

	if k == 1 {
		fmt.Println(min)
	} else {
		fmt.Println(max)
	}
}

// Input helpers

// If you don't pass in a reference to the reader, and just instantiate a new
// one every time, it will work locally, with IO typed at human-speed, but not
// on Codeforces.

// Get Space-separated Line as Strings
//
// Example input:
// 1 test 382.0
// Example output:
// ["1", "test", "382.0"]

func gsls(reader *bufio.Reader) (arr []string) {
	// Get a full line
	input, _ := reader.ReadString('\n')

	//Strip newline and return chars, if any
	input = strings.TrimSuffix(input, "\n")
	input = strings.TrimSuffix(input, "\r")

	// Split at the spaces into an array
	arr = strings.Split(input, " ")

	return
}

// Get Space-separated Line as Integers
// Any non-valid integers will be 0
//
// Example input:
// test 1 12.3 -117
// Example output:
// [0, 1, 0, -117]

func gsli(reader *bufio.Reader) (arr []int) {
	stringArr := gsls(reader)

	for _, e := range stringArr {
		cur, _ := strconv.Atoi(e)
		arr = append(arr, cur)
	}

	return
}

// Get Space-separated Line as 64-bit Floats
// Any non-valid floats will be 0
//
// Example input:
// test 128.328 12 -11.0
// Example output:
// [0, 128.328, 12, -11]

func gslf(reader *bufio.Reader) (arr []float64) {
	stringArr := gsls(reader)

	for _, e := range stringArr {
		cur, _ := strconv.ParseFloat(e, 64)
		arr = append(arr, cur)
	}

	return
}

// String helpers

// Is Lowercase
// Given some string[i], return whether it is lower case or not.

func isl(b byte) bool {
	var r rune
	r = rune(b)
	return unicode.IsLower(r)
}

// Math helpers

// Positve Modulo
// Returns x mod y, but always positive

func posMod(x, y int) int {
	return (x%y + y) % y
}

// Max Int
// Returns the max of two ints

func maxInt(x, y int) int {
	if x > y {
		return x
	}
	return y
}

// Distance formule
// Calculates the distance to the origin

func dist(x, y float64) float64 {
	return math.Sqrt(math.Pow(x, 2) + math.Pow(y, 2))
}

// Output helpers

func yes() {
	fmt.Println("Yes")
}

func no() {
	fmt.Println("No")
}

// Debug helpers

// From https://groups.google.com/forum/#!topic/golang-nuts/Qlxs3V77nss

func dbg(s string, a ...interface{}) {
	if debug {
		fmt.Printf(s, a...)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	line := scanner.Text()
	parts := strings.Split(line, " ")
	n, _ := strconv.ParseInt(parts[0], 10, 64)
	k, _ := strconv.ParseInt(parts[1], 10, 64)
	scanner.Scan()
	line = scanner.Text()
	parts = strings.Split(line, " ")
	arr := make([]int, int(n))
	for i := 0; i < int(n); i++ {
		a, _ := strconv.ParseInt(parts[i], 10, 64)
		arr[i] = int(a)
	}
	fmt.Println(solve(int(n), int(k), arr))
}

func solve(n, k int, arr []int) int {
	st := NewSparseTable(arr, func(d ...int) int {
		res := d[0]
		for _, v := range d {
			if v < res {
				res = v
			}
		}
		return res
	})
	if k == 1 {
		return st.QueryRangeFast(0, len(arr)-1)
	}
	if k > 3 {
		res := arr[0]
		for i := 1; i < len(arr); i++ {
			if arr[i] > res {
				res = arr[i]
			}
		}
		return res
	}
	m := st.QueryRangeFast(0, 0)
	for i := 0; i < len(arr)-1; i++ {
		m = max(st.QueryRangeFast(0, i), st.QueryRangeFast(i+1, len(arr)-1))
	}
	return m
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

// SparseTable is fast in multiple range maximum/minimum queryies in immutable array.
//
// Ref: https://cp-algorithms.com/data_structures/sparse-table.html .
type SparseTable struct {
	t  [][]int
	f  func(d ...int) int
	n  int
	lg []int
}

// NewSparseTable creates a sparse table with the range rule l. Time complexity: O(nlgn).
//
// NOTICE: Parameter f must be an [idempotent function](https://en.wikipedia.org/wiki/Idempotence) if you want to do range queries in O(1). If not, wrong query results will be returned.
func NewSparseTable(data []int, f func(d ...int) int) *SparseTable {
	n := len(data)
	t := make([][]int, n+1)
	for i := 0; i < n; i++ {
		t[i] = append(t[i], data[i])
	}
	for i := 1; 1<<i <= n && 1<<i > 0; i++ {
		for j := 0; j+(1<<i) <= n && j+(1<<i) > 0; j++ {
			t[j] = append(t[j], f(t[j][i-1], t[j+(1<<(i-1))][i-1]))
		}
	}
	lg := make([]int, n+1)
	lg[1] = 0
	for i := 2; i <= n; i++ {
		lg[i] = lg[i/2] + 1
	}
	return &SparseTable{
		t:  t,
		f:  f,
		n:  n,
		lg: lg,
	}
}

// QueryRangeFast queries the range [left, right] with function f.
//
// NOTICE: Only available if f is an [idempotent function](https://en.wikipedia.org/wiki/Idempotence).
//
// Time complexity: O(1).
func (s *SparseTable) QueryRangeFast(left, right int) int {
	l := right - left + 1
	return s.f(s.t[left][s.lg[l]], s.t[right-(1<<s.lg[l])+1][s.lg[l]])
}

// QueryRange queries the range [left, right] with function f.
//
// Time complexity: O(lgn).
func (s *SparseTable) QueryRange(left, right int) int {
	l := right - left + 1
	res := s.t[left][s.lg[l]]
	for left += 1 << s.lg[l]; left <= right; left += 1 << s.lg[l] {
		l = right - left + 1
		res = s.f(res, s.t[left][s.lg[l]])
	}
	return res
}

package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
	"strings"
)

func main() {
	data, _ := ioutil.ReadAll(os.Stdin)
	lines := strings.Split(string(data), "\n")
	parts := strings.Split(lines[0], " ")
	n, _ := strconv.Atoi(parts[0])
	k, _ := strconv.Atoi(parts[1])
	parts = strings.Split(lines[1], " ")
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i], _ = strconv.Atoi(parts[i])
	}
	fmt.Println(solve(n, k, arr))
}

func solve(n, k int, arr []int) int {
	st := NewSparseTable(arr, func(d ...int) int {
		res := d[0]
		for _, v := range d {
			if v < res {
				res = v
			}
		}
		return res
	})
	if k == 1 {
		return st.QueryRangeFast(0, len(arr)-1)
	}
	if k > 3 {
		res := arr[0]
		for i := 1; i < len(arr); i++ {
			if arr[i] > res {
				res = arr[i]
			}
		}
		return res
	}
	m := st.QueryRangeFast(0, 0)
	for i := 0; i < len(arr)-1; i++ {
		m = max(m, max(st.QueryRangeFast(0, i), st.QueryRangeFast(i+1, len(arr)-1)))
	}
	return m
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

// SparseTable is fast in multiple range maximum/minimum queryies in immutable array.
//
// Ref: https://cp-algorithms.com/data_structures/sparse-table.html .
type SparseTable struct {
	t  [][]int
	f  func(d ...int) int
	n  int
	lg []int
}

// NewSparseTable creates a sparse table with the range rule l. Time complexity: O(nlgn).
//
// NOTICE: Parameter f must be an [idempotent function](https://en.wikipedia.org/wiki/Idempotence) if you want to do range queries in O(1). If not, wrong query results will be returned.
func NewSparseTable(data []int, f func(d ...int) int) *SparseTable {
	n := len(data)
	t := make([][]int, n+1)
	for i := 0; i < n; i++ {
		t[i] = append(t[i], data[i])
	}
	for i := 1; 1<<i <= n && 1<<i > 0; i++ {
		for j := 0; j+(1<<i) <= n && j+(1<<i) > 0; j++ {
			t[j] = append(t[j], f(t[j][i-1], t[j+(1<<(i-1))][i-1]))
		}
	}
	lg := make([]int, n+1)
	lg[1] = 0
	for i := 2; i <= n; i++ {
		lg[i] = lg[i/2] + 1
	}
	return &SparseTable{
		t:  t,
		f:  f,
		n:  n,
		lg: lg,
	}
}

// QueryRangeFast queries the range [left, right] with function f.
//
// NOTICE: Only available if f is an [idempotent function](https://en.wikipedia.org/wiki/Idempotence).
//
// Time complexity: O(1).
func (s *SparseTable) QueryRangeFast(left, right int) int {
	l := right - left + 1
	return s.f(s.t[left][s.lg[l]], s.t[right-(1<<s.lg[l])+1][s.lg[l]])
}

// QueryRange queries the range [left, right] with function f.
//
// Time complexity: O(lgn).
func (s *SparseTable) QueryRange(left, right int) int {
	l := right - left + 1
	res := s.t[left][s.lg[l]]
	for left += 1 << s.lg[l]; left <= right; left += 1 << s.lg[l] {
		l = right - left + 1
		res = s.f(res, s.t[left][s.lg[l]])
	}
	return res
}

package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
	"strings"
)

func main() {
	data, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		fmt.Println(err.Error())
	}
	lines := strings.Split(string(data), "\r\n")
	parts := strings.Split(lines[0], " ")
	n, _ := strconv.Atoi(parts[0])
	k, _ := strconv.Atoi(parts[1])
	parts = strings.Split(lines[1], " ")
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i], err = strconv.Atoi(parts[i])
		if err != nil {
			fmt.Println(parts[i], err.Error())
		}
	}
	fmt.Println(solve(n, k, arr))
}

func solve(n, k int, arr []int) int {
	st := NewSparseTable(arr, func(d ...int) int {
		res := d[0]
		for _, v := range d {
			if v < res {
				res = v
			}
		}
		return res
	})
	if k == 1 {
		return st.QueryRangeFast(0, len(arr)-1)
	}
	if k > 3 {
		res := arr[0]
		for i := 1; i < len(arr); i++ {
			if arr[i] > res {
				res = arr[i]
			}
		}
		return res
	}
	m := st.QueryRangeFast(0, 0)
	for i := 0; i < len(arr)-1; i++ {
		m = max(m, max(st.QueryRangeFast(0, i), st.QueryRangeFast(i+1, len(arr)-1)))
	}
	return m
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

// SparseTable is fast in multiple range maximum/minimum queryies in immutable array.
//
// Ref: https://cp-algorithms.com/data_structures/sparse-table.html .
type SparseTable struct {
	t  [][]int
	f  func(d ...int) int
	n  int
	lg []int
}

// NewSparseTable creates a sparse table with the range rule l. Time complexity: O(nlgn).
//
// NOTICE: Parameter f must be an [idempotent function](https://en.wikipedia.org/wiki/Idempotence) if you want to do range queries in O(1). If not, wrong query results will be returned.
func NewSparseTable(data []int, f func(d ...int) int) *SparseTable {
	n := len(data)
	t := make([][]int, n+1)
	for i := 0; i < n; i++ {
		t[i] = append(t[i], data[i])
	}
	for i := 1; 1<<i <= n && 1<<i > 0; i++ {
		for j := 0; j+(1<<i) <= n && j+(1<<i) > 0; j++ {
			t[j] = append(t[j], f(t[j][i-1], t[j+(1<<(i-1))][i-1]))
		}
	}
	lg := make([]int, n+1)
	lg[1] = 0
	for i := 2; i <= n; i++ {
		lg[i] = lg[i/2] + 1
	}
	return &SparseTable{
		t:  t,
		f:  f,
		n:  n,
		lg: lg,
	}
}

// QueryRangeFast queries the range [left, right] with function f.
//
// NOTICE: Only available if f is an [idempotent function](https://en.wikipedia.org/wiki/Idempotence).
//
// Time complexity: O(1).
func (s *SparseTable) QueryRangeFast(left, right int) int {
	l := right - left + 1
	return s.f(s.t[left][s.lg[l]], s.t[right-(1<<s.lg[l])+1][s.lg[l]])
}

// QueryRange queries the range [left, right] with function f.
//
// Time complexity: O(lgn).
func (s *SparseTable) QueryRange(left, right int) int {
	l := right - left + 1
	res := s.t[left][s.lg[l]]
	for left += 1 << s.lg[l]; left <= right; left += 1 << s.lg[l] {
		l = right - left + 1
		res = s.f(res, s.t[left][s.lg[l]])
	}
	return res
}

package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
	"strings"
)

func main() {
	data, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		fmt.Println(err.Error())
	}
	lines := strings.Split(string(data), "\n")
	parts := strings.Split(lines[0], " ")
	n, _ := strconv.Atoi(parts[0])
	k, _ := strconv.Atoi(parts[1])
	parts = strings.Split(lines[1], " ")
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i], _ = strconv.Atoi(parts[i])
	}
	fmt.Println(n, k, arr)
	fmt.Println(solve(n, k, arr))
}

func solve(n, k int, arr []int) int {
	st := NewSparseTable(arr, func(d ...int) int {
		res := d[0]
		for _, v := range d {
			if v < res {
				res = v
			}
		}
		return res
	})
	if k == 1 {
		return st.QueryRangeFast(0, len(arr)-1)
	}
	if k > 3 {
		res := arr[0]
		for i := 1; i < len(arr); i++ {
			if arr[i] > res {
				res = arr[i]
			}
		}
		return res
	}
	m := st.QueryRangeFast(0, 0)
	for i := 0; i < len(arr)-1; i++ {
		m = max(m, max(st.QueryRangeFast(0, i), st.QueryRangeFast(i+1, len(arr)-1)))
	}
	return m
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

// SparseTable is fast in multiple range maximum/minimum queryies in immutable array.
//
// Ref: https://cp-algorithms.com/data_structures/sparse-table.html .
type SparseTable struct {
	t  [][]int
	f  func(d ...int) int
	n  int
	lg []int
}

// NewSparseTable creates a sparse table with the range rule l. Time complexity: O(nlgn).
//
// NOTICE: Parameter f must be an [idempotent function](https://en.wikipedia.org/wiki/Idempotence) if you want to do range queries in O(1). If not, wrong query results will be returned.
func NewSparseTable(data []int, f func(d ...int) int) *SparseTable {
	n := len(data)
	t := make([][]int, n+1)
	for i := 0; i < n; i++ {
		t[i] = append(t[i], data[i])
	}
	for i := 1; 1<<i <= n && 1<<i > 0; i++ {
		for j := 0; j+(1<<i) <= n && j+(1<<i) > 0; j++ {
			t[j] = append(t[j], f(t[j][i-1], t[j+(1<<(i-1))][i-1]))
		}
	}
	lg := make([]int, n+1)
	lg[1] = 0
	for i := 2; i <= n; i++ {
		lg[i] = lg[i/2] + 1
	}
	return &SparseTable{
		t:  t,
		f:  f,
		n:  n,
		lg: lg,
	}
}

// QueryRangeFast queries the range [left, right] with function f.
//
// NOTICE: Only available if f is an [idempotent function](https://en.wikipedia.org/wiki/Idempotence).
//
// Time complexity: O(1).
func (s *SparseTable) QueryRangeFast(left, right int) int {
	l := right - left + 1
	return s.f(s.t[left][s.lg[l]], s.t[right-(1<<s.lg[l])+1][s.lg[l]])
}

// QueryRange queries the range [left, right] with function f.
//
// Time complexity: O(lgn).
func (s *SparseTable) QueryRange(left, right int) int {
	l := right - left + 1
	res := s.t[left][s.lg[l]]
	for left += 1 << s.lg[l]; left <= right; left += 1 << s.lg[l] {
		l = right - left + 1
		res = s.f(res, s.t[left][s.lg[l]])
	}
	return res
}

package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
	"strings"
)

func main() {
	data, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		fmt.Println(err.Error())
	}
	lines := strings.Split(string(data), "\n")
	parts := strings.Split(lines[0], " ")
	n, _ := strconv.Atoi(parts[0])
	k, _ := strconv.Atoi(parts[1])
	parts = strings.Split(lines[1], " ")
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i], err = strconv.Atoi(parts[i])
		if err != nil {
			fmt.Println(parts[i], err.Error())
		}
	}
	fmt.Println(n, k, arr)
	fmt.Println(solve(n, k, arr))
}

func solve(n, k int, arr []int) int {
	st := NewSparseTable(arr, func(d ...int) int {
		res := d[0]
		for _, v := range d {
			if v < res {
				res = v
			}
		}
		return res
	})
	if k == 1 {
		return st.QueryRangeFast(0, len(arr)-1)
	}
	if k > 3 {
		res := arr[0]
		for i := 1; i < len(arr); i++ {
			if arr[i] > res {
				res = arr[i]
			}
		}
		return res
	}
	m := st.QueryRangeFast(0, 0)
	for i := 0; i < len(arr)-1; i++ {
		m = max(m, max(st.QueryRangeFast(0, i), st.QueryRangeFast(i+1, len(arr)-1)))
	}
	return m
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

// SparseTable is fast in multiple range maximum/minimum queryies in immutable array.
//
// Ref: https://cp-algorithms.com/data_structures/sparse-table.html .
type SparseTable struct {
	t  [][]int
	f  func(d ...int) int
	n  int
	lg []int
}

// NewSparseTable creates a sparse table with the range rule l. Time complexity: O(nlgn).
//
// NOTICE: Parameter f must be an [idempotent function](https://en.wikipedia.org/wiki/Idempotence) if you want to do range queries in O(1). If not, wrong query results will be returned.
func NewSparseTable(data []int, f func(d ...int) int) *SparseTable {
	n := len(data)
	t := make([][]int, n+1)
	for i := 0; i < n; i++ {
		t[i] = append(t[i], data[i])
	}
	for i := 1; 1<<i <= n && 1<<i > 0; i++ {
		for j := 0; j+(1<<i) <= n && j+(1<<i) > 0; j++ {
			t[j] = append(t[j], f(t[j][i-1], t[j+(1<<(i-1))][i-1]))
		}
	}
	lg := make([]int, n+1)
	lg[1] = 0
	for i := 2; i <= n; i++ {
		lg[i] = lg[i/2] + 1
	}
	return &SparseTable{
		t:  t,
		f:  f,
		n:  n,
		lg: lg,
	}
}

// QueryRangeFast queries the range [left, right] with function f.
//
// NOTICE: Only available if f is an [idempotent function](https://en.wikipedia.org/wiki/Idempotence).
//
// Time complexity: O(1).
func (s *SparseTable) QueryRangeFast(left, right int) int {
	l := right - left + 1
	return s.f(s.t[left][s.lg[l]], s.t[right-(1<<s.lg[l])+1][s.lg[l]])
}

// QueryRange queries the range [left, right] with function f.
//
// Time complexity: O(lgn).
func (s *SparseTable) QueryRange(left, right int) int {
	l := right - left + 1
	res := s.t[left][s.lg[l]]
	for left += 1 << s.lg[l]; left <= right; left += 1 << s.lg[l] {
		l = right - left + 1
		res = s.f(res, s.t[left][s.lg[l]])
	}
	return res
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	line := scanner.Text()
	parts := strings.Split(line, " ")
	n, _ := strconv.Atoi(parts[0])
	k, _ := strconv.Atoi(parts[1])
	scanner.Scan()
	line = scanner.Text()
	parts = strings.Split(line, " ")
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i], _ = strconv.Atoi(parts[i])
	}
	fmt.Println(solve(n, k, arr))
}

func solve(n, k int, arr []int) int {
	st := NewSparseTable(arr, func(d ...int) int {
		res := d[0]
		for _, v := range d {
			if v < res {
				res = v
			}
		}
		return res
	})
	if k == 1 {
		return st.QueryRangeFast(0, len(arr)-1)
	}
	if k > 3 {
		res := arr[0]
		for i := 1; i < len(arr); i++ {
			if arr[i] > res {
				res = arr[i]
			}
		}
		return res
	}
	m := st.QueryRangeFast(0, 0)
	for i := 0; i < len(arr)-1; i++ {
		m = max(st.QueryRangeFast(0, i), st.QueryRangeFast(i+1, len(arr)-1))
	}
	return m
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

// SparseTable is fast in multiple range maximum/minimum queryies in immutable array.
//
// Ref: https://cp-algorithms.com/data_structures/sparse-table.html .
type SparseTable struct {
	t  [][]int
	f  func(d ...int) int
	n  int
	lg []int
}

// NewSparseTable creates a sparse table with the range rule l. Time complexity: O(nlgn).
//
// NOTICE: Parameter f must be an [idempotent function](https://en.wikipedia.org/wiki/Idempotence) if you want to do range queries in O(1). If not, wrong query results will be returned.
func NewSparseTable(data []int, f func(d ...int) int) *SparseTable {
	n := len(data)
	t := make([][]int, n+1)
	for i := 0; i < n; i++ {
		t[i] = append(t[i], data[i])
	}
	for i := 1; 1<<i <= n && 1<<i > 0; i++ {
		for j := 0; j+(1<<i) <= n && j+(1<<i) > 0; j++ {
			t[j] = append(t[j], f(t[j][i-1], t[j+(1<<(i-1))][i-1]))
		}
	}
	lg := make([]int, n+1)
	lg[1] = 0
	for i := 2; i <= n; i++ {
		lg[i] = lg[i/2] + 1
	}
	return &SparseTable{
		t:  t,
		f:  f,
		n:  n,
		lg: lg,
	}
}

// QueryRangeFast queries the range [left, right] with function f.
//
// NOTICE: Only available if f is an [idempotent function](https://en.wikipedia.org/wiki/Idempotence).
//
// Time complexity: O(1).
func (s *SparseTable) QueryRangeFast(left, right int) int {
	l := right - left + 1
	return s.f(s.t[left][s.lg[l]], s.t[right-(1<<s.lg[l])+1][s.lg[l]])
}

// QueryRange queries the range [left, right] with function f.
//
// Time complexity: O(lgn).
func (s *SparseTable) QueryRange(left, right int) int {
	l := right - left + 1
	res := s.t[left][s.lg[l]]
	for left += 1 << s.lg[l]; left <= right; left += 1 << s.lg[l] {
		l = right - left + 1
		res = s.f(res, s.t[left][s.lg[l]])
	}
	return res
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	scanner := bufio.NewReader(os.Stdin)
	line, _ := scanner.ReadString('\n')
	parts := strings.Split(strings.Trim(line, "\n"), " ")
	n, _ := strconv.Atoi(parts[0])
	k, _ := strconv.Atoi(parts[1])
	line, _ = scanner.ReadString('\n')
	parts = strings.Split(strings.Trim(line, "\n"), " ")
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i], _ = strconv.Atoi(parts[i])
	}
	fmt.Println(solve(n, k, arr))
}

func solve(n, k int, arr []int) int {
	st := NewSparseTable(arr, func(d ...int) int {
		res := d[0]
		for _, v := range d {
			if v < res {
				res = v
			}
		}
		return res
	})
	if k == 1 {
		return st.QueryRangeFast(0, len(arr)-1)
	}
	if k > 3 {
		res := arr[0]
		for i := 1; i < len(arr); i++ {
			if arr[i] > res {
				res = arr[i]
			}
		}
		return res
	}
	m := st.QueryRangeFast(0, 0)
	for i := 0; i < len(arr)-1; i++ {
		m = max(m, max(st.QueryRangeFast(0, i), st.QueryRangeFast(i+1, len(arr)-1)))
	}
	return m
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

// SparseTable is fast in multiple range maximum/minimum queryies in immutable array.
//
// Ref: https://cp-algorithms.com/data_structures/sparse-table.html .
type SparseTable struct {
	t  [][]int
	f  func(d ...int) int
	n  int
	lg []int
}

// NewSparseTable creates a sparse table with the range rule l. Time complexity: O(nlgn).
//
// NOTICE: Parameter f must be an [idempotent function](https://en.wikipedia.org/wiki/Idempotence) if you want to do range queries in O(1). If not, wrong query results will be returned.
func NewSparseTable(data []int, f func(d ...int) int) *SparseTable {
	n := len(data)
	t := make([][]int, n+1)
	for i := 0; i < n; i++ {
		t[i] = append(t[i], data[i])
	}
	for i := 1; 1<<i <= n && 1<<i > 0; i++ {
		for j := 0; j+(1<<i) <= n && j+(1<<i) > 0; j++ {
			t[j] = append(t[j], f(t[j][i-1], t[j+(1<<(i-1))][i-1]))
		}
	}
	lg := make([]int, n+1)
	lg[1] = 0
	for i := 2; i <= n; i++ {
		lg[i] = lg[i/2] + 1
	}
	return &SparseTable{
		t:  t,
		f:  f,
		n:  n,
		lg: lg,
	}
}

// QueryRangeFast queries the range [left, right] with function f.
//
// NOTICE: Only available if f is an [idempotent function](https://en.wikipedia.org/wiki/Idempotence).
//
// Time complexity: O(1).
func (s *SparseTable) QueryRangeFast(left, right int) int {
	l := right - left + 1
	return s.f(s.t[left][s.lg[l]], s.t[right-(1<<s.lg[l])+1][s.lg[l]])
}

// QueryRange queries the range [left, right] with function f.
//
// Time complexity: O(lgn).
func (s *SparseTable) QueryRange(left, right int) int {
	l := right - left + 1
	res := s.t[left][s.lg[l]]
	for left += 1 << s.lg[l]; left <= right; left += 1 << s.lg[l] {
		l = right - left + 1
		res = s.f(res, s.t[left][s.lg[l]])
	}
	return res
}


// Author: sighduck
// URL: https://codeforces.com/contest/1194/problem/C

package main

import (
	"bufio"
	"fmt"
	"os"
)

func charCountMap(s string) map[string]int {
	m := make(map[string]int)
	for _, char := range s {
		m[string(char)] += 1
	}
	return m
}

func Solve(s string, t string, p string) string {
	sPtr := 0
	tPtr := 0
	tCharCountMap := charCountMap(t)
	pCharCountMap := charCountMap(p)
	for tPtr < len(t) && sPtr < len(s) {
		if s[sPtr] == t[tPtr] {
			sPtr += 1
			tCharCountMap[string(t[tPtr])] -= 1
		}
		tPtr += 1
	}
	if sPtr != len(s) {
		return "NO"
	}
	for char, count := range tCharCountMap {
		if pCharCountMap[char] < count {
			return "NO"
		}
	}
	return "YES"
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var t int
	fmt.Fscanf(reader, "%d\n", &t)
	for i := 0; i < t; i++ {
		var s, t, p string
		fmt.Fscanf(reader, "%s\n%s\n%s\n", &s, &t, &p)
		fmt.Fprintf(writer, "%s\n", Solve(s, t, p))
	}
}

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
)

func main() {
	defer Flush()

	q := readi()
	for i := 0; i < q; i++ {
		s := readb()
		t := readb()
		p := readb()
		if solve(s, t, p) {
			println("YES")
		} else {
			println("NO")
		}
	}
}

// true if t is substring of s
func isSubstring(s, t []byte) bool {
	var i int
	for _, c := range t {
		for i < len(s) && s[i] != c {
			i++
		}
		if i == len(s) {
			return false
		}
		i++

	}
	return true
}

func solve(s, t, p []byte) bool {

	if !isSubstring(t, s) {
		return false
	}
	ms := make(map[byte]int)
	mt := make(map[byte]int)
	for _, c := range s {
		ms[c]++
	}
	for _, c := range t {
		mt[c]++
	}
	for _, c := range p {
		ms[c]++
	}

	for k, v := range mt {
		u := ms[k]
		if u < v {
			return false
		}
	}
	return true
}

// -----------------------------------------------------------------------------

// IO helper functions

// Returns next token from input.  It must be initialized by SetInput()
// before the first call.
var nextToken func() ([]byte, error)
var nextLine func() ([]byte, error)

// Holds a buffer for output.  It must be initialized by SetOutput().
// All IO fucntions (read*() and [e]print*()) should write to OutputWriter
// instead of this.
var OutputBuffer *bufio.Writer

// Holds an io.Writer.  It must be initialized by SetOutput()
var OutputWriter io.Writer

// Set IO functions for interactive input/output.
func SetInteractive(w io.Writer, r io.Reader) {
	SetUnbefferedInput(r)
	OutputBuffer = nil
	OutputWriter = w
}

// Setup OutputBuffer and OutputWriter.
func SetOutput(w io.Writer) {
	OutputBuffer = bufio.NewWriter(w)
	OutputWriter = OutputBuffer
}

// Flushes OutputBuffer
func Flush() {
	if OutputBuffer != nil {
		OutputBuffer.Flush()
	}
}

// Returns true if c is a white space
func IsSpace(c byte) bool {
	switch c {
	case '\t', '\n', '\v', '\f', '\r', ' ':
		return true
	}
	return false
}

func IsNewLine(c byte) bool {
	switch c {
	case '\n', '\r':
		return true
	}
	return false
}

// Setup nextToken with input buffer.
func SetInput(r io.Reader) {
	buf := new(bytes.Buffer)
	var b []byte

	var i int
	rest := func() ([]byte, error) {
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		return b[j:i], nil
	}
	initial := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextToken = rest
		return rest()
	}
	nextToken = initial

	restLn := func() ([]byte, error) {
		for i < len(b) && IsNewLine(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsNewLine(b[i]) {
			i++
		}
		return b[j:i], nil
	}

	initialLn := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextLine = restLn
		return restLn()
	}
	nextLine = initialLn
}

// Setup nextToken without input buffer.
func SetUnbefferedInput(r io.Reader) {
	buf := bufio.NewReader(r)
	var b []byte

	var i int
	nextToken = func() ([]byte, error) {
		var err error
		if i == len(b) {
			b, err = buf.ReadBytes('\n')
			if err != nil {
				return nil, err
			}
			i = 0
			j := len(b) - 1
			for 0 <= j && IsSpace(b[j]) {
				j--
			}
			b = b[0 : j+1]
		}
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		if i == j {
			return nil, io.ErrUnexpectedEOF
		}
		return b[j:i], nil
	}
}

// -----------------------------------------------------------------------------

// IO functions

// Reads next token and return it as []byte
func readb() []byte {
	b, err := nextToken()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Reads next token and return it as string
func reads() string {
	return string(readb())
}

// Read next line as []byte.  Trailing '\n' will not be included.
// See also comments on readb()
func readbln() []byte {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Read next line as string
func readsln() string {
	return string(readbln())
}

// Reads next token and return it as int64
func readll() int64 {
	i, err := strconv.ParseInt(reads(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// Reads next token and return it as int
func readi() int {
	return int(readll())
}

// Reads next token and return it as float64
func readf() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// Write args to OutputWriter with the format f
func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(OutputWriter, f, args...)
}

// Write args to OutputWriter without format
func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(OutputWriter, args...)
}

// Write args to stderr with the format f
func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

// Write args to stderr without format
func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

// -----------------------------------------------------------------------------

// Utilities

func sumSlice(a []int) int {
	var res int
	for _, v := range a {
		res += v
	}
	return res
}

func sumSlicell(a []int64) int64 {
	var res int64
	for _, v := range a {
		res += v
	}
	return res
}

func readInts(N int) (int, []int) {
	if N == 0 {
		N = readi()
	}
	a := make([]int, N)
	for i := range a {
		a[i] = readi()
	}
	return N, a
}

func readIntsll(N int) (int, []int64) {
	if N == 0 {
		N = readi()
	}
	a := make([]int64, N)
	for i := range a {
		a[i] = readll()
	}
	return N, a
}

// reverse slice in place
func reverse(a []int) {
	for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
		a[i], a[j] = a[j], a[i]
	}
}

// areverse: allocating reverse
func areverse(a []int) []int {
	b := make([]int, len(a))
	for i := range a {
		b[i] = a[len(a)-i-1]
	}
	return b
}

// -----------------------------------------------------------------------------

// Simple math functions

const (
	// big prime
	INF  = 1000000007
	INF2 = 1000000009
	INF3 = 998244353
)

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func minll(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func minf(a, b float64) float64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func maxll(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func maxf(a, b float64) float64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func absll(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func absf(a float64) float64 {
	if a < 0 {
		return -a
	}
	return a
}

// egcd(a, b) returns d, x, y:
//   d is gcd(a,b)
//   x, y are  integers that satisfy ax + by = d
func egcd(a, b int) (int, int, int) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcd(b, a%b)
	return d, y, x - a/b*y
}

func egcdll(a, b int64) (int64, int64, int64) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcdll(b, a%b)
	return d, y, x - a/b*y
}

func gcd(a, b int) int {
	d, _, _ := egcd(a, b)
	return d
}

func gcdll(a, b int64) int64 {
	d, _, _ := egcdll(a, b)
	return d
}

// set up IO functions
func init() {
	// for non-interactive
	SetInput(os.Stdin)
	SetOutput(os.Stdout)

	// Enable below when interactive.  Its ok to leave above intact.
	// SetInteractive(os.Stdout, os.Stdin)
}

package main

import (
	"fmt"
	"strings"
)

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	strs := make([][3]string, t)
	for i := 0; i < t; i++ {
		var s, t, p string
		fmt.Scanf("%s\n", &s)
		fmt.Scanf("%s\n", &t)
		fmt.Scanf("%s\n", &p)
		strs[i] = [3]string{s, t, p}
	}
	for i := range strs {
		fmt.Println(solve(strs[i][0], strs[i][1], strs[i][2]))
	}
}

func solve(s, t, p string) string {
	res := "YES"
	if len(s) > len(t) {
		return "NO"
	}
	freqs := map[byte]int{}
	freqt := map[byte]int{}
	freqp := map[byte]int{}
	for i := 0; i < len(s); i++ {
		freqs[s[i]]++
	}
	for i := 0; i < len(t); i++ {
		freqt[t[i]]++
	}
	for i := 0; i < len(p); i++ {
		freqp[p[i]]++
	}
	for k, v := range freqt {
		if v > freqp[k]+freqs[k] {
			return "NO"
		}
	}
	// check exists
	for k := range freqs {
		if freqt[k] == 0 {
			return "NO"
		}
	}
	if !contains2(t, s) {
		return "NO"
	}

	return res
}
func contains2(t, s string) bool {
	id := 0
	for i := 0; i < len(s); i++ {
		t = t[id:]
		id = strings.IndexByte(t, s[i])
		if id == -1 {
			return false
		}
		id++
	}

	return true
}
func contains(t, s string) bool {
	i := 0
	for j := 0; j < len(s); j++ {
		if i == len(t) {
			return false
		}
		for i < len(t) && s[j] != t[i] {
			i++
		}
		i++
	}
	return i <= len(t)
}

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	strs := make([][3]string, t)
	for i := 0; i < t; i++ {
		var s, t, p string
		fmt.Scanf("%s\n", &s)
		fmt.Scanf("%s\n", &t)
		fmt.Scanf("%s\n", &p)
		strs[i] = [3]string{s, t, p}
	}
	for i := range strs {
		fmt.Println(solve(strs[i][0], strs[i][1], strs[i][2]))
	}

}

func solve(s, t, p string) string {
	res := "YES"
	if len(s) > len(t) {
		return "NO"
	}
	freqs := map[byte]int{}
	freqt := map[byte]int{}
	freqp := map[byte]int{}
	for i := 0; i < len(s); i++ {
		freqs[s[i]]++
	}
	for i := 0; i < len(t); i++ {
		freqt[t[i]]++
	}
	for i := 0; i < len(p); i++ {
		freqp[p[i]]++
	}
	for k, v := range freqt {
		if v > freqp[k]+freqs[k] {
			return "NO"
		}
	}
	// check exists
	for k := range freqs {
		if freqt[k] == 0 {
			return "NO"
		}
	}
	if !contains(t, s) {
		return "NO"
	}

	return res
}

func contains(t, s string) bool {
	i := 0
	for j := 0; j < len(s); j++ {
		if i == len(t) {
			return false
		}
		for i < len(t) && s[j] != t[i] {
			i++
		}
		i++
	}
	return i <= len(t)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	sc := NewScanner()
	N := sc.NextInt()
	for iqqq := 0; iqqq < N; iqqq++ {
		core(sc)
	}

}

func core(sc *Scanner) {
	S, T := sc.NextLine(), sc.NextLine()
	p := sc.NextLine()
	mp := map[byte]int{}
	for _, v := range p {
		mp[byte(v)]++
	}
	if len(S) > len(T) {
		fmt.Println("NO")
		return
	}

	i := 0
	j := 0
	for j < len(T) {
		if i == len(S) || S[i] != T[j] {
			cnt := mp[T[j]]
			if cnt == 0 {
				fmt.Println("NO")
				return
			}
			mp[T[j]]--
			j++
		} else {
			i++
			j++
		}
	}
	if i == len(S) && j == len(T) {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextFloat() float64 {
	v, _ := strconv.ParseFloat(s.Next(), 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func (io *IO) Solve_() bool {
	s, t, p := io.Bytes(), io.Bytes(), io.Bytes()
	a, b := [26]int{}, [26]int{}
	i := 0
	for j := 0; j < len(t); j++ {
		if i >= len(s) || s[i] != t[j] {
			a[t[j]-'a']++
		} else {
			i++
		}
	}
	if i < len(s) {
		return false
	}
	for i = 0; i < len(p); i++ {
		b[p[i]-'a']++
	}
	for i := range a {
		if a[i] > b[i] {
			return false
		}
	}
	return true
}

func (io *IO) Solve() {
	n := io.Int()
	for i := 0; i < n; i++ {
		if io.Solve_() {
			io.Print("YES\n")
		} else {
			io.Print("NO\n")
		}
	}
}

func main() {
	NewIO().Solve()
}

type IO struct {
	scanner *bufio.Scanner
	writer  io.Writer
}

func NewIO() *IO {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	return &IO{scanner, os.Stdout}
}

func (io *IO) Skip() {
	io.scanner.Scan()
}

func (io *IO) Int32() (n int32) {
	io.scanner.Scan()
	buf := io.scanner.Bytes()
	for _, v := range buf {
		n = n*10 + int32(v-'0')
	}
	return
}

func (io *IO) Int() (n int) {
	io.scanner.Scan()
	buf := io.scanner.Bytes()
	for _, v := range buf {
		n = n*10 + int(v-'0')
	}
	return
}

func (io *IO) UInt() (n uint) {
	io.scanner.Scan()
	buf := io.scanner.Bytes()
	for _, v := range buf {
		n = n*10 + uint(v-'0')
	}
	return
}

func (io *IO) Bytes() (s []byte) {
	io.scanner.Scan()
	buf := io.scanner.Bytes()
	s = make([]byte, len(buf))
	copy(s, buf)
	return
}

func (io *IO) IntArray(n int) (arr []int) {
	arr = make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = io.Int()
	}
	return
}

func (io *IO) UIntArray(n int) (arr []uint) {
	arr = make([]uint, n)
	for i := 0; i < n; i++ {
		arr[i] = io.UInt()
	}
	return
}

func (io *IO) Print(a interface{}) {
	fmt.Fprint(io.writer, a)
}

func (io *IO) Printf(format string, a interface{}) {
	fmt.Fprintf(io.writer, format, a)
}

func (io *IO) setBuffer() {
	io.scanner.Buffer(make([]byte, 4096), 512*1024)
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func (io *IO) Solve_() bool {
	s, t, p := io.Bytes(), io.Bytes(), io.Bytes()
	a, b := [26]int{}, [26]int{}
	i := 0
	for j := 0; j < len(t); j++ {
		if i >= len(s) || s[i] != t[j] {
			c := t[j] - 'a'
			if c < 0 || c >= 26 {
				prt := fmt.Sprintf("b_%v,%v,%v,%v\n", j, len(t), len(a), t[j])
				io.Print(prt)
				return false
			}
			a[t[j]-'a']++
		} else {
			i++
		}
	}
	if i < len(s) {
		return false
	}
	for i = 0; i < len(p); i++ {
		b[p[i]-'a']++
	}
	for i := range a {
		if a[i] > b[i] {
			return false
		}
	}
	return true
}

func (io *IO) Solve() {
	n := io.Int()
	for i := 0; i < n; i++ {
		if io.Solve_() {
			io.Print("YES\n")
		} else {
			io.Print("NO\n")
		}
	}
}

func main() {
	NewIO().Solve()
}

type IO struct {
	scanner *bufio.Scanner
	writer  io.Writer
}

func NewIO() *IO {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	return &IO{scanner, os.Stdout}
}

func (io *IO) Skip() {
	io.scanner.Scan()
}

func (io *IO) Int32() (n int32) {
	io.scanner.Scan()
	buf := io.scanner.Bytes()
	for _, v := range buf {
		n = n*10 + int32(v-'0')
	}
	return
}

func (io *IO) Int() (n int) {
	io.scanner.Scan()
	buf := io.scanner.Bytes()
	for _, v := range buf {
		n = n*10 + int(v-'0')
	}
	return
}

func (io *IO) UInt() (n uint) {
	io.scanner.Scan()
	buf := io.scanner.Bytes()
	for _, v := range buf {
		n = n*10 + uint(v-'0')
	}
	return
}

func (io *IO) Bytes() []byte {
	io.scanner.Scan()
	return io.scanner.Bytes()
}

func (io *IO) IntArray(n int) (arr []int) {
	arr = make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = io.Int()
	}
	return
}

func (io *IO) UIntArray(n int) (arr []uint) {
	arr = make([]uint, n)
	for i := 0; i < n; i++ {
		arr[i] = io.UInt()
	}
	return
}

func (io *IO) Print(a interface{}) {
	fmt.Fprint(io.writer, a)
}

func (io *IO) Printf(format string, a interface{}) {
	fmt.Fprintf(io.writer, format, a)
}

func (io *IO) setBuffer() {
	io.scanner.Buffer(make([]byte, 4096), 512*1024)
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"runtime/debug"
)

func (io *IO) Solve_() bool {
	s, t, p := io.Bytes(), io.Bytes(), io.Bytes()
	var a, b [26]int
	i := 0
	for j := 0; j < len(t); j++ {
		if i >= len(s) || s[i] != t[j] {
			a[t[j]-'a']++
		} else {
			i++
		}
	}
	if i < len(s) {
		return false
	}
	for i = 0; i < len(p); i++ {
		b[p[i]-'a']++
	}
	for i = 0; i < 26; i++ {
		if a[i] > b[i] {
			return false
		}
	}
	return true
}

func (io *IO) Solve() {
	defer func() {
		if err := recover(); err != nil {
			io.Printf("---debug: %v\n", err)
			io.Print(string(debug.Stack()))
		}
	}()
	n := io.Int()
	for i := 0; i < n; i++ {
		if io.Solve_() {
			io.Print("YES\n")
		} else {
			io.Print("NO\n")
		}
	}
}

func main() {
	NewIO().Solve()
}

type IO struct {
	scanner *bufio.Scanner
	writer  io.Writer
}

func NewIO() *IO {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	return &IO{scanner, os.Stdout}
}

func (io *IO) Skip() {
	io.scanner.Scan()
}

func (io *IO) Int32() (n int32) {
	io.scanner.Scan()
	buf := io.scanner.Bytes()
	for _, v := range buf {
		n = n*10 + int32(v-'0')
	}
	return
}

func (io *IO) Int() (n int) {
	io.scanner.Scan()
	buf := io.scanner.Bytes()
	for _, v := range buf {
		n = n*10 + int(v-'0')
	}
	return
}

func (io *IO) UInt() (n uint) {
	io.scanner.Scan()
	buf := io.scanner.Bytes()
	for _, v := range buf {
		n = n*10 + uint(v-'0')
	}
	return
}

func (io *IO) Bytes() []byte {
	io.scanner.Scan()
	return io.scanner.Bytes()
}

func (io *IO) IntArray(n int) (arr []int) {
	arr = make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = io.Int()
	}
	return
}

func (io *IO) UIntArray(n int) (arr []uint) {
	arr = make([]uint, n)
	for i := 0; i < n; i++ {
		arr[i] = io.UInt()
	}
	return
}

func (io *IO) Print(a interface{}) {
	fmt.Fprint(io.writer, a)
}

func (io *IO) Printf(format string, a interface{}) {
	fmt.Fprintf(io.writer, format, a)
}

func (io *IO) setBuffer() {
	io.scanner.Buffer(make([]byte, 4096), 512*1024)
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"runtime/debug"
)

func (io *IO) Solve_() bool {
	s, t, p := io.Bytes(), io.Bytes(), io.Bytes()
	a, b := map[byte]int{}, map[byte]int{}
	i := 0
	for j := 0; j < len(t); j++ {
		if i >= len(s) || s[i] != t[j] {
			a[t[j]]++
		} else {
			i++
		}
	}
	if i < len(s) {
		return false
	}
	for i = 0; i < len(p); i++ {
		b[p[i]]++
	}
	for i := range a {
		if a[i] > b[i] {
			return false
		}
	}
	return true
}

func (io *IO) Solve() {
	defer func() {
		if err := recover(); err != nil {
			io.Printf("---debug: %v\n", err)
			io.Print(string(debug.Stack()))
			panic(err)
		}
	}()
	n := io.Int()
	for i := 0; i < n; i++ {
		if io.Solve_() {
			io.Print("YES\n")
		} else {
			io.Print("NO\n")
		}
	}
}

func main() {
	NewIO().Solve()
}

type IO struct {
	scanner *bufio.Scanner
	writer  io.Writer
}

func NewIO() *IO {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	return &IO{scanner, os.Stdout}
}

func (io *IO) Skip() {
	io.scanner.Scan()
}

func (io *IO) Int32() (n int32) {
	io.scanner.Scan()
	buf := io.scanner.Bytes()
	for _, v := range buf {
		n = n*10 + int32(v-'0')
	}
	return
}

func (io *IO) Int() (n int) {
	io.scanner.Scan()
	buf := io.scanner.Bytes()
	for _, v := range buf {
		n = n*10 + int(v-'0')
	}
	return
}

func (io *IO) UInt() (n uint) {
	io.scanner.Scan()
	buf := io.scanner.Bytes()
	for _, v := range buf {
		n = n*10 + uint(v-'0')
	}
	return
}

func (io *IO) Bytes() []byte {
	io.scanner.Scan()
	return io.scanner.Bytes()
}

func (io *IO) IntArray(n int) (arr []int) {
	arr = make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = io.Int()
	}
	return
}

func (io *IO) UIntArray(n int) (arr []uint) {
	arr = make([]uint, n)
	for i := 0; i < n; i++ {
		arr[i] = io.UInt()
	}
	return
}

func (io *IO) Print(a interface{}) {
	fmt.Fprint(io.writer, a)
}

func (io *IO) Printf(format string, a interface{}) {
	fmt.Fprintf(io.writer, format, a)
}

func (io *IO) setBuffer() {
	io.scanner.Buffer(make([]byte, 4096), 512*1024)
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func (io *IO) Solve_() bool {
	s, t, p := io.Bytes(), io.Bytes(), io.Bytes()
	a, b := [26]int{}, [26]int{}
	i := 0
	for j := 0; j < len(t); j++ {
		if i >= len(s) || s[i] != t[j] {
			c := t[j] - 'a'
			if c < 0 || c >= 26 {
				prt := fmt.Sprintf("b_%v,%v,%v,%v\n", j, len(t), len(s), len(p))
				io.Print(prt)
				io.Printf("%v\n", string(t))
				io.Printf("%v\n", string(s))
				io.Printf("%v\n", string(p))
				return false
			}
			a[t[j]-'a']++
		} else {
			i++
		}
	}
	if i < len(s) {
		return false
	}
	for i = 0; i < len(p); i++ {
		b[p[i]-'a']++
	}
	for i := range a {
		if a[i] > b[i] {
			return false
		}
	}
	return true
}

func (io *IO) Solve() {
	n := io.Int()
	for i := 0; i < n; i++ {
		if io.Solve_() {
			io.Print("YES\n")
		} else {
			io.Print("NO\n")
		}
	}
}

func main() {
	NewIO().Solve()
}

type IO struct {
	scanner *bufio.Scanner
	writer  io.Writer
}

func NewIO() *IO {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	return &IO{scanner, os.Stdout}
}

func (io *IO) Skip() {
	io.scanner.Scan()
}

func (io *IO) Int32() (n int32) {
	io.scanner.Scan()
	buf := io.scanner.Bytes()
	for _, v := range buf {
		n = n*10 + int32(v-'0')
	}
	return
}

func (io *IO) Int() (n int) {
	io.scanner.Scan()
	buf := io.scanner.Bytes()
	for _, v := range buf {
		n = n*10 + int(v-'0')
	}
	return
}

func (io *IO) UInt() (n uint) {
	io.scanner.Scan()
	buf := io.scanner.Bytes()
	for _, v := range buf {
		n = n*10 + uint(v-'0')
	}
	return
}

func (io *IO) Bytes() []byte {
	io.scanner.Scan()
	return io.scanner.Bytes()
}

func (io *IO) IntArray(n int) (arr []int) {
	arr = make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = io.Int()
	}
	return
}

func (io *IO) UIntArray(n int) (arr []uint) {
	arr = make([]uint, n)
	for i := 0; i < n; i++ {
		arr[i] = io.UInt()
	}
	return
}

func (io *IO) Print(a interface{}) {
	fmt.Fprint(io.writer, a)
}

func (io *IO) Printf(format string, a interface{}) {
	fmt.Fprintf(io.writer, format, a)
}

func (io *IO) setBuffer() {
	io.scanner.Buffer(make([]byte, 4096), 512*1024)
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func (io *IO) Solve_() bool {
	s, t, p := io.Bytes(), io.Bytes(), io.Bytes()
	a, b := [26]int{}, [26]int{}
	i := 0
	for j := 0; j < len(t); j++ {
		if i >= len(s) || s[i] != t[j] {
			c := t[j] - 'a'
			if c < 0 || c >= 26 {
				io.Printf("b1_%v\n", c)
				return false
			}
			a[t[j]-'a']++
		} else {
			i++
		}
	}
	if i < len(s) {
		return false
	}
	for i = 0; i < len(p); i++ {
		c := p[i] - 'a'
		if c < 0 || c >= 26 {
			io.Printf("b2_%v\n", c)
			return false
		}
		b[p[i]-'a']++
	}
	for i := range a {
		if a[i] > b[i] {
			return false
		}
	}
	return true
}

func (io *IO) Solve() {
	n := io.Int()
	for i := 0; i < n; i++ {
		if io.Solve_() {
			io.Print("YES\n")
		} else {
			io.Print("NO\n")
		}
	}
}

func main() {
	NewIO().Solve()
}

type IO struct {
	scanner *bufio.Scanner
	writer  io.Writer
}

func NewIO() *IO {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	return &IO{scanner, os.Stdout}
}

func (io *IO) Skip() {
	io.scanner.Scan()
}

func (io *IO) Int32() (n int32) {
	io.scanner.Scan()
	buf := io.scanner.Bytes()
	for _, v := range buf {
		n = n*10 + int32(v-'0')
	}
	return
}

func (io *IO) Int() (n int) {
	io.scanner.Scan()
	buf := io.scanner.Bytes()
	for _, v := range buf {
		n = n*10 + int(v-'0')
	}
	return
}

func (io *IO) UInt() (n uint) {
	io.scanner.Scan()
	buf := io.scanner.Bytes()
	for _, v := range buf {
		n = n*10 + uint(v-'0')
	}
	return
}

func (io *IO) Bytes() []byte {
	io.scanner.Scan()
	return io.scanner.Bytes()
}

func (io *IO) IntArray(n int) (arr []int) {
	arr = make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = io.Int()
	}
	return
}

func (io *IO) UIntArray(n int) (arr []uint) {
	arr = make([]uint, n)
	for i := 0; i < n; i++ {
		arr[i] = io.UInt()
	}
	return
}

func (io *IO) Print(a interface{}) {
	fmt.Fprint(io.writer, a)
}

func (io *IO) Printf(format string, a interface{}) {
	fmt.Fprintf(io.writer, format, a)
}

func (io *IO) setBuffer() {
	io.scanner.Buffer(make([]byte, 4096), 512*1024)
}

package main

import (
    "fmt"
    "strings"
    )

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	strs := make([][3]string, t)
	for i := 0; i < t; i++ {
		var s, t, p string
		fmt.Scanf("%s\n", &s)
		fmt.Scanf("%s\n", &t)
		fmt.Scanf("%s\n", &p)
		strs[i] = [3]string{s, t, p}
	}
	for i := range strs {
		fmt.Println(solve(strs[i][0], strs[i][1], strs[i][2]))
	}
}

func solve(s, t, p string) string {
	res := "YES"
	if s == t {
	    return res
	}
	if len(s) > len(t) {
	    return "NO"
	}
	freqs := map[byte]int{}
	freqt := map[byte]int{}
	freqp := map[byte]int{}
	for i := 0; i < len(s); i++ {
		freqs[s[i]]++
	}
	for i := 0; i < len(t); i++ {
		freqt[t[i]]++
	}
	for i := 0; i < len(p); i++ {
		freqp[p[i]]++
	}
	for k, v := range freqt {
		if v > freqp[k]+freqs[k] {
			return "NO"
		}
	}
		// check order
	var order []int
	for i := 0; i < len(s); i++ {
		id := strings.IndexByte(t, s[i])
		if id > -1 {
			order = append(order, id)
		}
	}
	for i := 1; i < len(order); i++ {
		if order[i-1] > order[i] {
			return "NO"
		}
	}

	return res
}

package main

import (
    "fmt"
    "strings"
    )

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	strs := make([][3]string, t)
	for i := 0; i < t; i++ {
		var s, t, p string
		fmt.Scanf("%s\n", &s)
		fmt.Scanf("%s\n", &t)
		fmt.Scanf("%s\n", &p)
		strs[i] = [3]string{s, t, p}
	}
	for i := range strs {
		fmt.Println(solve(strs[i][0], strs[i][1], strs[i][2]))
	}
}

func solve(s, t, p string) string {
	res := "YES"
	freqs := map[byte]int{}
	freqt := map[byte]int{}
	freqp := map[byte]int{}
	for i := 0; i < len(s); i++ {
		freqs[s[i]]++
	}
	for i := 0; i < len(t); i++ {
		freqt[t[i]]++
	}
	for i := 0; i < len(p); i++ {
		freqp[p[i]]++
	}
	for k, v := range freqt {
		if v > freqp[k]+freqs[k] {
			return "NO"
		}
	}
		// check order
	var order []int
	for i := 0; i < len(s); i++ {
		id := strings.IndexByte(t, s[i])
		if id > -1 {
			order = append(order, id)
		}
	}
	for i := 1; i < len(order); i++ {
		if order[i-1] > order[i] {
			return "NO"
		}
	}

	return res
}

package main

import (
	"fmt"
	"strings"
)

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	strs := make([][3]string, t)
	for i := 0; i < t; i++ {
		var s, t, p string
		fmt.Scanf("%s\n", &s)
		fmt.Scanf("%s\n", &t)
		fmt.Scanf("%s\n", &p)
		strs[i] = [3]string{s, t, p}
	}
	for i := range strs {
		fmt.Println(solve(strs[i][0], strs[i][1], strs[i][2]))
	}

}

func solve(s, t, p string) string {
	res := "YES"
	if s == t {
		return res
	}
	if len(s) > len(t) {
		return "NO"
	}
	freqs := map[byte]int{}
	freqt := map[byte]int{}
	freqp := map[byte]int{}
	for i := 0; i < len(s); i++ {
		freqs[s[i]]++
	}
	for i := 0; i < len(t); i++ {
		freqt[t[i]]++
	}
	for i := 0; i < len(p); i++ {
		freqp[p[i]]++
	}
	for k, v := range freqt {
		if v > freqp[k]+freqs[k] {
			return "NO"
		}
	}
	// check exists
	for k := range freqs {
		if freqt[k] == 0 {
			return "NO"
		}
	}
	id := 0
	for i := 0; i < len(s); i++ {
		id = strings.IndexByte(t[id:], s[i])
		if id == -1 {
			return "NO"
		}
		id++
	}
	return res
}

package main

import (
	"fmt"
	"strings"
)

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	strs := make([][3]string, t)
	for i := 0; i < t; i++ {
		var s, t, p string
		fmt.Scanf("%s\n", &s)
		fmt.Scanf("%s\n", &t)
		fmt.Scanf("%s\n", &p)
		strs[i] = [3]string{s, t, p}
	}
	for i := range strs {
		fmt.Println(solve(strs[i][0], strs[i][1], strs[i][2]))
	}
}

func solve(s, t, p string) string {
	res := "YES"
	freqs := map[byte]int{}
	freqt := map[byte]int{}
	freqp := map[byte]int{}
	for i := 0; i < len(s); i++ {
		freqs[s[i]]++
	}
	for i := 0; i < len(t); i++ {
		freqt[t[i]]++
	}
	for i := 0; i < len(p); i++ {
		freqp[p[i]]++
	}
	for k, v := range freqt {
		if v > freqp[k]+freqs[k] {
			return "NO"
		}
	}
	// check exists
	for k := range freqs {
		if freqt[k] == 0 {
			return "NO"
		}
	}

	id := 0
	for i := 0; i < len(s); i++ {
		id = strings.IndexByte(t[id:], s[i])
		if id == -1 {
			return "NO"
		}
	}

	return res
}

package main

import (
	"fmt"
	"strings"
)

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	strs := make([][3]string, t)
	for i := 0; i < t; i++ {
		var s, t, p string
		fmt.Scanf("%s\n", &s)
		fmt.Scanf("%s\n", &t)
		fmt.Scanf("%s\n", &p)
		strs[i] = [3]string{s, t, p}
	}
	for i := range strs {
		fmt.Println(solve(strs[i][0], strs[i][1], strs[i][2]))
	}

}

func solve(s, t, p string) string {
	res := "YES"
	if s == t {
		return res
	}
	if len(s) > len(t) {
		return "NO"
	}
	freqs := map[byte]int{}
	freqt := map[byte]int{}
	freqp := map[byte]int{}
	for i := 0; i < len(s); i++ {
		freqs[s[i]]++
	}
	for i := 0; i < len(t); i++ {
		freqt[t[i]]++
	}
	for i := 0; i < len(p); i++ {
		freqp[p[i]]++
	}
	for k, v := range freqt {
		if v > freqp[k]+freqs[k] {
			return "NO"
		}
	}
	// check exists
	for k := range freqs {
		if freqt[k] == 0 {
			return "NO"
		}
	}
	id := 0
	for i := 0; i < len(s); i++ {
		id = strings.IndexByte(t[id:], s[i])
		if id == -1 {
			return "NO"
		}
	}
	return res
}


package main

import (
	"fmt"
	"strings"
)

func count(a []string, e string) int {
	ret := 0
	for _, i := range a {
		if i == e {
			ret++
		}
	}
	return ret
}

func comb(n, r int) int {
	if n == r || r == 0 {
		return 1
	} else {
		return comb(n, r-1) * (n - r + 1) / r
	}
}

func main() {
	var n int
	var s string
	cake := [][]string{}
	chocolate := 0
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&s)
		r := strings.Split(s, "")
		c := count(r, "C")
		if c > 1 {
			chocolate += comb(c, 2)
		}
		cake = append(cake, r)
	}
	for i := 0; i < n; i++ {
		c := 0
		for j := 0; j < n; j++ {
			if cake[j][i] == "C" {
				c++
			}
		}
		if c > 1 {
			chocolate += comb(c, 2)
		}
	}
	fmt.Println(chocolate)
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	cake := make([]string, n)
	for i := range cake {
		fmt.Scan(&cake[i])
	}
	ans := 0
	for i := range cake {
		h, v := 0, 0
		for j := range cake[i] {
			if cake[i][j] == 'C' {
				ans += h
				h++
			}
			if cake[j][i] == 'C' {
				ans += v
				v++
			}
		}
	}
	fmt.Println(ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func I(i interface{}, delim string) error {
	_, err := fmt.Fscanf(r, "%v"+delim, i)
	return err
}

func O(o interface{}, delim string) {
	fmt.Fprintf(w, "%v"+delim, o)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	if f, err := os.Open("in.txt"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

func main() {
	defer w.Flush()
	var n int
	for I(&n, "\n") == nil {
		solve(n)
	}
}

func solve(n int) {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		I(&s[i], "\n")
	}
	ans := 0
	for i := 0; i < n; i++ {
		t := 0
		for j := 0; j < n; j++ {
			if s[i][j] == 'C' {
				t++
			}
		}
		ans += (t * (t - 1)) / 2
		t = 0
		for j := 0; j < n; j++ {
			if s[j][i] == 'C' {
				t++
			}
		}
		ans += (t * (t - 1)) / 2
	}
	O(ans, "\n")
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func Print(args ...interface{})                 { out.WriteString(fmt.Sprint(args...)) }
func Printf(format string, args ...interface{}) { out.WriteString(fmt.Sprintf(format, args...)) }
func Println(args ...interface{})               { out.WriteString(fmt.Sprintln(args...)) }

func Scan(args ...interface{})   { fmt.Fscan(in, args...) }
func Scanln(args ...interface{}) { fmt.Fscanln(in, args...) }

func numCalc(n int) int {
	res := 0
	for i := 1; i < n; i++ {
		res += i
	}
	return res
}

func main() {
	defer out.Flush()

	var n int
	Scan(&n)
	cells := make([][]byte, n)
	for i := 0; i < n; i++ {
		var str string
		Scan(&str)
		cells[i] = []byte(str)
	}

	nums := 0
	for i := 0; i < n; i++ {
		cc := 0
		rc := 0
		for j := 0; j < n; j++ {
			if cells[i][j] == 'C' {
				rc++
			}
			if cells[j][i] == 'C' {
				cc++
			}
		}
		nums += numCalc(cc)
		nums += numCalc(rc)
	}
	Println(nums)
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	var n int
	if _, err := fmt.Scanf("%d\n", &n); err != nil {
		return
	}

	cake := make([][]byte, n)
	for i := 0; i < n; i++ {
		cake[i] = make([]byte, n)
	}

	reader := bufio.NewReader(os.Stdin)
	for i := 0; i < n; i++ {
		if s, err := reader.ReadString('\n'); err != nil {
			if err == io.EOF {
				break
			} else {
				return
			}
		} else {
			for j := 0; j < n; j++ {
				if s[j] == 'C' {
					cake[i][j] = 1
				}
			}
		}
	}

	var pairs int = 0
	for i := 0; i < n; i++ {
		cnt := 0
		for j := 0; j < n; j++ {
			cnt += int(cake[i][j])
		}
		if cnt > 1 {
			pairs += (cnt * (cnt - 1) / 2)
		}
	}
	for i := 0; i < n; i++ {
		cnt := 0
		for j := 0; j < n; j++ {
			cnt += int(cake[j][i])
		}
		if cnt > 1 {
			pairs += (cnt * (cnt - 1) / 2)
		}
	}

	fmt.Println(pairs)
}

package main

import (
  "fmt"
)

func main() {
  var n int
  cnt := 0

  fmt.Scanf("%d\n", &n)

  data := [][]int{}
  var buf string

  for i := 0; i < n; i++ {
    fmt.Scanf("%s\n", &buf)
    a := []int{}
    for j := 0; j < len(buf); j++ {
      if buf[j] == 'C' {
        a = append(a, 1)
      } else {
        a = append(a, 0)
      }
    }
    data = append(data, a)
  }

  for v := 0; v < n; v++ {
    for i := 0; i < n; i++ {
      for j := i + 1; j < n; j++ {
        if data[v][i] == 1 && data[v][j] == 1 {
          cnt++
        }
      }
    }
  }

  for v := 0; v < n; v++ {
    for i := 0; i < n; i++ {
      for j := i + 1; j < n; j++ {
        if data[i][v] == 1 && data[j][v] == 1 {
          cnt++
        }
      }
    }
  }

  fmt.Println(cnt)
}

package main

import (
	"fmt"
)

func main() {
	var n int
	var g []string
	ans := 0

	fmt.Scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		var line string
		fmt.Scanf("%s\n", &line)
		g = append(g, line)

		cnt := 0
		for _, c := range line {
			if c == 'C' {
				cnt++
			}
		}
		ans += cnt * (cnt - 1) / 2
	}

	for c := 0; c < n; c++ {
		cnt := 0
		for r := 0; r < n; r++ {
			if g[r][c] == 'C' {
				cnt++
			}
		}
		ans += cnt * (cnt - 1) / 2
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)
	var s = make([]string, n)
	for i := range s {
		fmt.Scan(&s[i])
	}
	x := make([]int, n)
	y := make([]int, n)
	for i := range s {
		for j := range s[i] {
			if s[i][j] == 'C' {
				x[i]++
				y[j]++
			}
		}
	}
	count := 0
	for i := range x {
		count += x[i] * (x[i] - 1) / 2
		count += y[i] * (y[i] - 1) / 2
	}
	fmt.Print(count)
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	t := make([]string, n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%s\n", &t[i])
	}
	ans := 0
	for i := 0; i < n; i++ {
		cl, cr := 0, 0
		for j := 0; j < n; j++ {
			if t[i][j] == 'C' {
				cl++
			}
			if t[j][i] == 'C' {
				cr++
			}
		}
		ans += (cl * (cl - 1)) / 2
		ans += (cr * (cr - 1)) / 2
	}
	fmt.Println(ans)
}


package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF894C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n int
	Fscan(in, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
		if a[i]%a[0] != 0 {
			Fprint(out, -1)
			return
		}
	}
	Fprintln(out, 2*n)
	for _, v := range a {
		Fprint(out, v, a[0], " ")
	}
}

func main() { CF894C(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF894C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	gcd := func(a, b int) int {
		for a != 0 {
			a, b = b%a, a
		}
		return b
	}

	var n, g int
	Fscan(in, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
		g = gcd(g, a[i])
	}
	if g != a[0] {
		Fprint(out, -1)
		return
	}
	Fprintln(out, 2*n)
	for _, v := range a {
		Fprint(out, v, a[0], " ")
	}
}

func main() { CF894C(os.Stdin, os.Stdout) }

package main

import(
	"fmt"
	// "sort"
	"os"
	"bufio"
	"strconv"
	// "strings"
)

var n int
var b [5003]int
var a []int

func min(a,b int) int{
	if a<b{
		return a
	}
	return b
}

func max(a,b int) int{
	if a>b{
		return a
	}
	return b
}

func main() {
	fmt.Scanf("%d",&n)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i:=0;i<n;i++{
		scanner.Scan()
		var x int
		x, _ = strconv.Atoi(scanner.Text())
		a = append(a,x)
	}
	if n == 1{
		fmt.Println(1)
		fmt.Println(a[0])
		os.Exit(0)
	}
	j := 0
	for i:=1;i<n;i++{
		if a[i] % a[0] !=0 {
			fmt.Println(-1)
			os.Exit(0)
		}
		b[j] = a[i]
		j++
		b[j] = a[0]
		j++
	}
	fmt.Println(j)
	for i:=0;i<j;i++{
		fmt.Println(b[i])
	}
}

package main

import(
	"fmt"
	"sort"
	"os"
	"bufio"
	"strconv"
	// "strings"
)

var n int
var b [5003]int
var a []int

func min(a,b int) int{
	if a<b{
		return a
	}
	return b
}

func max(a,b int) int{
	if a>b{
		return a
	}
	return b
}

func main() {
	fmt.Scanf("%d",&n)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i:=0;i<n;i++{
		scanner.Scan()
		var x int
		x, _ = strconv.Atoi(scanner.Text())
		a = append(a,x)
	}
	sort.Ints(a)
	j := 0
	for i:=1;i<n;i++{
		if a[i] % a[0] !=0 {
			fmt.Println(-1)
			os.Exit(0)
		}
		b[j] = a[i]
		j++
		b[j] = a[0]
		j++
	}
	fmt.Println(j)
	for i:=0;i<j;i++{
		fmt.Println(b[i])
	}
}

package main

import(
	"fmt"
	// "sort"
	"os"
	"bufio"
	"strconv"
	// "strings"
)

var n int
var a,b [5003]int

func min(a,b int) int{
	if a<b{
		return a
	}
	return b
}

func max(a,b int) int{
	if a>b{
		return a
	}
	return b
}

func main() {
	fmt.Scanf("%d",&n)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i:=0;i<n;i++{
		scanner.Scan()
		a[i], _ = strconv.Atoi(scanner.Text())
	}
	j := 0
	for i:=1;i<n;i++{
		if a[i] % a[0] !=0 {
			fmt.Println(-1)
			os.Exit(0)
		}
		b[j] = a[i]
		j++
		b[j] = a[0]
		j++
	}
	fmt.Println(j)
	for i:=0;i<j;i++{
		fmt.Println(b[i])
	}
}


package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
	"sort"
)

type odtBlock struct {
	next *odtBlock
	l, r int
	val  int64
}

type odt struct {
	root *odtBlock
}

func (t odt) split(mid int) {
	for b := t.root; b != nil; b = b.next {
		if b.l <= mid && mid < b.r {
			b.next = &odtBlock{b.next, mid + 1, b.r, b.val}
			b.r = mid
		}
	}
}

func (t odt) prepare(l, r int) {
	t.split(l - 1)
	t.split(r)
}

func (t odt) add(l, r int, val int64) {
	for b := t.root; b != nil; b = b.next {
		if b.l > r {
			break
		}
		if l <= b.l && b.r <= r {
			b.val += val
		}
	}
}

func (t odt) set(l, r int, val int64) {
	for b := t.root; b != nil; b = b.next {
		if b.l == l {
			b.r = r
			b.val = val
			e := b.next
			for e != nil && e.l <= r {
				e = e.next
			}
			b.next = e
			break
		}
	}
}

func (t odt) kth(l, r, k int) int64 {
	blocks := []odtBlock{}
	for b := t.root; b != nil; b = b.next {
		if l <= b.l && b.r <= r {
			if b.l > r {
				break
			}
			blocks = append(blocks, *b)
		}
	}
	sort.Slice(blocks, func(i, j int) bool { return blocks[i].val < blocks[j].val })
	k--
	for _, b := range blocks {
		if cnt := b.r - b.l + 1; k >= cnt {
			k -= cnt
		} else {
			return b.val
		}
	}
	panic(k)
}

func (odt) quickPow(x int64, n int, mod int64) int64 {
	x %= mod
	res := int64(1) % mod
	for ; n > 0; n >>= 1 {
		if n&1 == 1 {
			res = res * x % mod
		}
		x = x * x % mod
	}
	return res
}

func (t odt) powSum(l, r int, n int, mod int64) (res int64) {
	for b := t.root; b != nil; b = b.next {
		if l <= b.l && b.r <= r {
			if b.l > r {
				break
			}
			res += int64(b.r-b.l+1) * t.quickPow(b.val, n, mod)
		}
	}
	return res % mod
}

// github.com/EndlessCheng/codeforces-go
func Sol896C(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n, m, vMax int
	var seed int64
	Fscan(in, &n, &m, &seed, &vMax)
	rand := func(_n int) int {
		const mod int64 = 1e9 + 7
		ret := seed
		seed = (seed*7 + 13) % mod
		return int(ret) % _n
	}

	t := odt{&odtBlock{val: int64(rand(vMax) + 1)}}
	o := t.root
	for i := 1; i < n; i++ {
		o.next = &odtBlock{nil, i, i, int64(rand(vMax) + 1)}
		o = o.next
	}
	for ; m > 0; m-- {
		op := rand(4) + 1
		l, r := rand(n), rand(n)
		if l > r {
			l, r = r, l
		}
		var x int
		if op == 3 {
			x = rand(r-l+1) + 1
		} else {
			x = rand(vMax) + 1
		}
		t.prepare(l, r)
		switch op {
		case 1:
			t.add(l, r, int64(x))
		case 2:
			t.set(l, r, int64(x))
		case 3:
			Fprintln(out, t.kth(l, r, x))
		default:
			y := int64(rand(vMax) + 1)
			Fprintln(out, t.powSum(l, r, x, y))
		}
	}
}

func main() {
	Sol896C(os.Stdin, os.Stdout)
}

package main

import (
	"bufio"
	"container/list"
	. "fmt"
	"io"
	"os"
	"sort"
)

type odtBlock struct {
	l, r int
	val  int64
}

type odt struct {
	*list.List
}

func (t odt) split(mid int) {
	for o := t.Front(); o != nil; o = o.Next() {
		b := o.Value.(odtBlock)
		if b.l <= mid && mid < b.r {
			t.InsertAfter(odtBlock{mid + 1, b.r, b.val}, o)
			b.r = mid
			o.Value = b
		}
	}
}

func (t odt) prepare(l, r int) {
	t.split(l - 1)
	t.split(r)
}

func (t odt) add(l, r int, val int64) {
	for o := t.Front(); o != nil; o = o.Next() {
		b := o.Value.(odtBlock)
		if b.l > r {
			break
		}
		if l <= b.l && b.r <= r {
			b.val += val
			o.Value = b
		}
	}
}

func (t odt) set(l, r int, val int64) {
	for o := t.Front(); o != nil; o = o.Next() {
		b := o.Value.(odtBlock)
		if b.l == l {
			o.Value = odtBlock{l, r, val}
			e := o.Next()
			for e != nil && e.Value.(odtBlock).l <= r {
				n := e.Next()
				t.Remove(e)
				e = n
			}
			//if e != nil && e != o.Next() {
			//	oNext := (**list.Element)(unsafe.Pointer(reflect.ValueOf(o).Elem().FieldByName("next").UnsafeAddr()))
			//	*oNext = e
			//	ePrev := (**list.Element)(unsafe.Pointer(reflect.ValueOf(e).Elem().FieldByName("prev").UnsafeAddr()))
			//	*ePrev = o
			//}
			break
		}
	}
}

func (t odt) kth(l, r, k int) int64 {
	blocks := []odtBlock{}
	for o := t.Front(); o != nil; o = o.Next() {
		b := o.Value.(odtBlock)
		if l <= b.l && b.r <= r {
			blocks = append(blocks, b)
		}
	}
	sort.Slice(blocks, func(i, j int) bool { return blocks[i].val < blocks[j].val })
	k--
	for _, b := range blocks {
		if cnt := b.r - b.l + 1; k >= cnt {
			k -= cnt
		} else {
			return b.val
		}
	}
	panic(k)
}

func (odt) quickPow(x int64, n int, mod int64) int64 {
	x %= mod
	res := int64(1) % mod
	for ; n > 0; n >>= 1 {
		if n&1 == 1 {
			res = res * x % mod
		}
		x = x * x % mod
	}
	return res
}

func (t odt) powSum(l, r int, n int, mod int64) (res int64) {
	for o := t.Front(); o != nil; o = o.Next() {
		b := o.Value.(odtBlock)
		if l <= b.l && b.r <= r {
			res += int64(b.r-b.l+1) * t.quickPow(b.val, n, mod)
		}
	}
	return res % mod
}

// github.com/EndlessCheng/codeforces-go
func Sol896C(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n, m, vMax int
	var seed int64
	Fscan(in, &n, &m, &seed, &vMax)
	rand := func(_n int) int {
		const mod int64 = 1e9 + 7
		ret := seed
		seed = (seed*7 + 13) % mod
		return int(ret) % _n
	}

	t := odt{list.New()}
	for i := 0; i < n; i++ {
		t.PushBack(odtBlock{i, i, int64(rand(vMax) + 1)})
	}
	for ; m > 0; m-- {
		op := rand(4) + 1
		l, r := rand(n), rand(n)
		if l > r {
			l, r = r, l
		}
		var x int
		if op == 3 {
			x = rand(r-l+1) + 1
		} else {
			x = rand(vMax) + 1
		}
		t.prepare(l, r)
		switch op {
		case 1:
			t.add(l, r, int64(x))
		case 2:
			t.set(l, r, int64(x))
		case 3:
			Fprintln(out, t.kth(l, r, x))
		default:
			y := int64(rand(vMax) + 1)
			Fprintln(out, t.powSum(l, r, x, y))
		}
	}
}

func main() {
	Sol896C(os.Stdin, os.Stdout)
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
	"sort"
)

type odtBlock struct {
	l, r int
	val  int64
}

type odt []odtBlock

func (t *odt) split(mid int) int {
	ot := *t
	for i, b := range ot {
		if b.l == mid+1 {
			return i
		}
		if b.l <= mid && mid < b.r {
			*t = append(ot[:i+1], append(odt{{mid + 1, b.r, b.val}}, ot[i+1:]...)...)
			ot[i].r = mid
			return i + 1
		}
	}
	return len(ot)
}

func (t *odt) prepare(l, r int) (begin, end int) {
	begin = t.split(l - 1)
	end = t.split(r)
	return
}

func (t *odt) merge(begin, end, r int, val int64) {
	ot := *t
	ot[begin].r = r
	ot[begin].val = val
	if begin+1 < end {
		*t = append(ot[:begin+1], ot[end:]...)
	}
}

func (t odt) add(begin, end int, val int64) {
	for i := begin; i < end; i++ {
		t[i].val += val
	}
}

func (t odt) kth(begin, end, k int) int64 {
	blocks := make(odt, end-begin)
	copy(blocks, t[begin:end])
	sort.Slice(blocks, func(i, j int) bool { return blocks[i].val < blocks[j].val })
	k--
	for _, b := range blocks {
		if cnt := b.r - b.l + 1; k >= cnt {
			k -= cnt
		} else {
			return b.val
		}
	}
	panic(k)
}

func (odt) quickPow(x int64, n int, mod int64) int64 {
	x %= mod
	res := int64(1) % mod
	for ; n > 0; n >>= 1 {
		if n&1 == 1 {
			res = res * x % mod
		}
		x = x * x % mod
	}
	return res
}

func (t odt) powSum(begin, end int, n int, mod int64) (res int64) {
	for _, b := range t[begin:end] {
		res += int64(b.r-b.l+1) * t.quickPow(b.val, n, mod)
	}
	return res % mod
}

// github.com/EndlessCheng/codeforces-go
func Sol896C(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n, m, vMax int
	var seed int64
	Fscan(in, &n, &m, &seed, &vMax)
	rand := func(_n int) int {
		const mod int64 = 1e9 + 7
		ret := seed
		seed = (seed*7 + 13) % mod
		return int(ret) % _n
	}

	t := make(odt, n)
	for i := range t {
		t[i] = odtBlock{i, i, int64(rand(vMax) + 1)}
	}
	for ; m > 0; m-- {
		op := rand(4) + 1
		l, r := rand(n), rand(n)
		if l > r {
			l, r = r, l
		}
		var x int
		if op == 3 {
			x = rand(r-l+1) + 1
		} else {
			x = rand(vMax) + 1
		}
		begin, end := t.prepare(l, r)
		switch op {
		case 1:
			t.add(begin, end, int64(x))
		case 2:
			t.merge(begin, end, r, int64(x))
		case 3:
			Fprintln(out, t.kth(begin, end, x))
		default:
			y := int64(rand(vMax) + 1)
			Fprintln(out, t.powSum(begin, end, x, y))
		}
	}
}

func main() {
	Sol896C(os.Stdin, os.Stdout)
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
	"sort"
)

type odtBlock struct {
	l, r int
	val  int64
}

type odt []odtBlock

func (t *odt) split(mid int) {
	for i, b := range *t {
		if b.l <= mid && mid < b.r {
			*t = append((*t)[:i+1], append([]odtBlock{{mid + 1, b.r, b.val}}, (*t)[i+1:]...)...)
			(*t)[i].r = mid
			break
		}
	}
}

func (t *odt) prepare(l, r int) {
	t.split(l - 1)
	t.split(r)
}

func (t odt) add(l, r int, val int64) {
	for i, b := range t {
		if b.l > r {
			break
		}
		if l <= b.l && b.r <= r {
			t[i].val += val
		}
	}
}

func (t *odt) set(l, r int, val int64) {
	ot := *t
	for i, b := range ot {
		if b.l == l {
			ot[i] = odtBlock{b.l, r, val}
			j := i + 1
			for ; j < len(ot) && ot[j].l <= r; j++ {
			}
			if j > i+1 {
				*t = append(ot[:i+1], ot[j:]...)
			}
			break
		}
	}
}

func (t odt) kth(l, r, k int) int64 {
	blocks := []odtBlock{}
	for _, b := range t {
		if l <= b.l && b.r <= r {
			blocks = append(blocks, b)
		}
	}
	sort.Slice(blocks, func(i, j int) bool { return blocks[i].val < blocks[j].val })
	k--
	for _, b := range blocks {
		if cnt := b.r - b.l + 1; k >= cnt {
			k -= cnt
		} else {
			return b.val
		}
	}
	panic(k)
}

func (odt) quickPow(x int64, n int, mod int64) int64 {
	x %= mod
	res := int64(1) % mod
	for ; n > 0; n >>= 1 {
		if n&1 == 1 {
			res = res * x % mod
		}
		x = x * x % mod
	}
	return res
}

func (t odt) powSum(l, r int, n int, mod int64) (res int64) {
	for _, b := range t {
		if l <= b.l && b.r <= r {
			res += int64(b.r-b.l+1) * t.quickPow(b.val, n, mod)
		}
	}
	return res % mod
}

// github.com/EndlessCheng/codeforces-go
func Sol896C(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n, m, vMax int
	var seed int64
	Fscan(in, &n, &m, &seed, &vMax)
	rand := func(_n int) int {
		const mod int64 = 1e9 + 7
		ret := seed
		seed = (seed*7 + 13) % mod
		return int(ret) % _n
	}

	t := make(odt, n)
	for i := range t {
		t[i] = odtBlock{i, i, int64(rand(vMax) + 1)}
	}
	for ; m > 0; m-- {
		op := rand(4) + 1
		l, r := rand(n), rand(n)
		if l > r {
			l, r = r, l
		}
		var x int
		if op == 3 {
			x = rand(r-l+1) + 1
		} else {
			x = rand(vMax) + 1
		}
		t.prepare(l, r)
		switch op {
		case 1:
			t.add(l, r, int64(x))
		case 2:
			t.set(l, r, int64(x))
		case 3:
			Fprintln(out, t.kth(l, r, x))
		default:
			y := int64(rand(vMax) + 1)
			Fprintln(out, t.powSum(l, r, x, y))
		}
	}
}

func main() {
	Sol896C(os.Stdin, os.Stdout)
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
	"sort"
)

type odtBlock struct {
	next *odtBlock
	l, r int
	val  int64
}

type odt struct {
	root *odtBlock
}

func (t odt) split(mid int) {
	for b := t.root; b != nil; b = b.next {
		if b.l <= mid && mid < b.r {
			b.next = &odtBlock{b.next, mid + 1, b.r, b.val}
			b.r = mid
		}
	}
}

func (t odt) prepare(l, r int) {
	t.split(l - 1)
	t.split(r)
}

func (t odt) add(l, r int, val int64) {
	for b := t.root; b != nil; b = b.next {
		if b.l > r {
			break
		}
		if l <= b.l && b.r <= r {
			b.val += val
		}
	}
}

func (t odt) set(l, r int, val int64) {
	for b := t.root; b != nil; b = b.next {
		if b.l == l {
			b.r = r
			b.val = val
			e := b.next
			for e != nil && e.l <= r {
				e = e.next
			}
			b.next = e
			break
		}
	}
}

func (t odt) kth(l, r, k int) int64 {
	blocks := []odtBlock{}
	for b := t.root; b != nil; b = b.next {
		if l <= b.l && b.r <= r {
			blocks = append(blocks, *b)
		}
	}
	sort.Slice(blocks, func(i, j int) bool { return blocks[i].val < blocks[j].val })
	k--
	for _, b := range blocks {
		if cnt := b.r - b.l + 1; k >= cnt {
			k -= cnt
		} else {
			return b.val
		}
	}
	panic(k)
}

func (odt) quickPow(x int64, n int, mod int64) int64 {
	x %= mod
	res := int64(1) % mod
	for ; n > 0; n >>= 1 {
		if n&1 == 1 {
			res = res * x % mod
		}
		x = x * x % mod
	}
	return res
}

func (t odt) powSum(l, r int, n int, mod int64) (res int64) {
	for b := t.root; b != nil; b = b.next {
		if l <= b.l && b.r <= r {
			res += int64(b.r-b.l+1) * t.quickPow(b.val, n, mod)
		}
	}
	return res % mod
}

// github.com/EndlessCheng/codeforces-go
func Sol896C(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n, m, vMax int
	var seed int64
	Fscan(in, &n, &m, &seed, &vMax)
	rand := func(_n int) int {
		const mod int64 = 1e9 + 7
		ret := seed
		seed = (seed*7 + 13) % mod
		return int(ret) % _n
	}

	t := odt{&odtBlock{val: int64(rand(vMax) + 1)}}
	o := t.root
	for i := 1; i < n; i++ {
		o.next = &odtBlock{nil, i, i, int64(rand(vMax) + 1)}
		o = o.next
	}
	for ; m > 0; m-- {
		op := rand(4) + 1
		l, r := rand(n), rand(n)
		if l > r {
			l, r = r, l
		}
		var x int
		if op == 3 {
			x = rand(r-l+1) + 1
		} else {
			x = rand(vMax) + 1
		}
		t.prepare(l, r)
		switch op {
		case 1:
			t.add(l, r, int64(x))
		case 2:
			t.set(l, r, int64(x))
		case 3:
			Fprintln(out, t.kth(l, r, x))
		default:
			y := int64(rand(vMax) + 1)
			Fprintln(out, t.powSum(l, r, x, y))
		}
	}
}

func main() {
	Sol896C(os.Stdin, os.Stdout)
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
	"sort"
)

type odtBlock struct {
	l, r int
	val  int64
}

type odt struct {
	blocks []odtBlock
	size   int
}

func newODT(arr []int64) *odt {
	n := len(arr)
	blocks := make([]odtBlock, n)
	for i := range blocks {
		blocks[i] = odtBlock{i, i, arr[i]}
	}
	return &odt{
		blocks: blocks,
		size:   n,
	}
}

func (t *odt) split(mid int) {
	for i := 0; i < t.size; i++ {
		b := t.blocks[i]
		if b.l <= mid && mid < b.r {
			copy(t.blocks[i+1:t.size+1], t.blocks[i:t.size])
			t.size++
			t.blocks[i+1] = odtBlock{mid + 1, b.r, b.val}
			t.blocks[i].r = mid
		}
	}
}

func (t *odt) prepare(l, r int) {
	t.split(l - 1)
	t.split(r)
}

func (t *odt) add(l, r int, val int64) {
	for i := 0; i < t.size; i++ {
		b := t.blocks[i]
		if b.l > r {
			break
		}
		if l <= b.l && b.r <= r {
			t.blocks[i].val += val
		}
	}
}

func (t *odt) set(l, r int, val int64) {
	for i := 0; i < t.size; i++ {
		b := t.blocks[i]
		if b.l == l {
			t.blocks[i].r = r
			t.blocks[i].val = val
			j := i + 1
			for ; j < t.size && t.blocks[j].l <= r; j++ {
			}
			if shift := j - (i + 1); shift > 0 {
				copy(t.blocks[i+1:t.size-shift], t.blocks[j:t.size])
				t.size -= shift
			}
			break
		}
	}
}

func (t *odt) kth(l, r, k int) int64 {
	blocks := []odtBlock{}
	for i := 0; i < t.size; i++ {
		b := t.blocks[i]
		if l <= b.l && b.r <= r {
			blocks = append(blocks, b)
		}
	}
	sort.Slice(blocks, func(i, j int) bool { return blocks[i].val < blocks[j].val })
	k--
	for _, b := range blocks {
		if cnt := b.r - b.l + 1; k >= cnt {
			k -= cnt
		} else {
			return b.val
		}
	}
	panic(k)
}

func (t *odt) quickPow(x int64, n int, mod int64) int64 {
	x %= mod
	res := int64(1) % mod
	for ; n > 0; n >>= 1 {
		if n&1 == 1 {
			res = res * x % mod
		}
		x = x * x % mod
	}
	return res
}

func (t *odt) powSum(l, r int, n int, mod int64) (res int64) {
	for i := 0; i < t.size; i++ {
		b := t.blocks[i]
		if l <= b.l && b.r <= r {
			res += int64(b.r-b.l+1) * t.quickPow(b.val, n, mod)
		}
	}
	return res % mod
}

// github.com/EndlessCheng/codeforces-go
func Sol896C(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n, m, vMax int
	var seed int64
	Fscan(in, &n, &m, &seed, &vMax)
	rand := func(_n int) int {
		const mod int64 = 1e9 + 7
		ret := seed
		seed = (seed*7 + 13) % mod
		return int(ret) % _n
	}

	arr := make([]int64, n)
	for i := range arr {
		arr[i] = int64(rand(vMax) + 1)
	}
	t := newODT(arr)
	for ; m > 0; m-- {
		op := rand(4) + 1
		l, r := rand(n), rand(n)
		if l > r {
			l, r = r, l
		}
		var x int
		if op == 3 {
			x = rand(r-l+1) + 1
		} else {
			x = rand(vMax) + 1
		}
		t.prepare(l, r)
		switch op {
		case 1:
			t.add(l, r, int64(x))
		case 2:
			t.set(l, r, int64(x))
		case 3:
			Fprintln(out, t.kth(l, r, x))
		default:
			y := int64(rand(vMax) + 1)
			Fprintln(out, t.powSum(l, r, x, y))
		}
	}
}

func main() {
	Sol896C(os.Stdin, os.Stdout)
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
	"sort"
)

type odtBlock struct {
	l, r int
	val  int64
}

type odt struct {
	blocks []odtBlock
	size   int
}

func newODT(arr []int64) *odt {
	n := len(arr)
	blocks := make([]odtBlock, n)
	for i := range blocks {
		blocks[i] = odtBlock{i, i, arr[i]}
	}
	return &odt{
		blocks: blocks,
		size:   n,
	}
}

func (t *odt) split(mid int) {
	for i := 0; i < t.size; i++ {
		b := t.blocks[i]
		if b.l <= mid && mid < b.r {
			copy(t.blocks[i+1:t.size+1], t.blocks[i:t.size])
			t.size++
			t.blocks[i+1] = odtBlock{mid + 1, b.r, b.val}
			t.blocks[i].r = mid
			break
		}
	}
}

func (t *odt) prepare(l, r int) {
	t.split(l - 1)
	t.split(r)
}

func (t *odt) add(l, r int, val int64) {
	for i := 0; i < t.size; i++ {
		b := t.blocks[i]
		if b.l > r {
			break
		}
		if l <= b.l && b.r <= r {
			t.blocks[i].val += val
		}
	}
}

func (t *odt) set(l, r int, val int64) {
	for i := 0; i < t.size; i++ {
		b := t.blocks[i]
		if b.l == l {
			t.blocks[i].r = r
			t.blocks[i].val = val
			j := i + 1
			for ; j < t.size && t.blocks[j].l <= r; j++ {
			}
			if shift := j - (i + 1); shift > 0 {
				copy(t.blocks[i+1:t.size-shift], t.blocks[j:t.size])
				t.size -= shift
			}
			break
		}
	}
}

func (t *odt) kth(l, r, k int) int64 {
	blocks := []odtBlock{}
	for i := 0; i < t.size; i++ {
		b := t.blocks[i]
		if l <= b.l && b.r <= r {
			blocks = append(blocks, b)
		}
	}
	sort.Slice(blocks, func(i, j int) bool { return blocks[i].val < blocks[j].val })
	k--
	for _, b := range blocks {
		if cnt := b.r - b.l + 1; k >= cnt {
			k -= cnt
		} else {
			return b.val
		}
	}
	panic(k)
}

func (t *odt) quickPow(x int64, n int, mod int64) int64 {
	x %= mod
	res := int64(1) % mod
	for ; n > 0; n >>= 1 {
		if n&1 == 1 {
			res = res * x % mod
		}
		x = x * x % mod
	}
	return res
}

func (t *odt) powSum(l, r int, n int, mod int64) (res int64) {
	for i := 0; i < t.size; i++ {
		b := t.blocks[i]
		if l <= b.l && b.r <= r {
			res += int64(b.r-b.l+1) * t.quickPow(b.val, n, mod)
		}
	}
	return res % mod
}

// github.com/EndlessCheng/codeforces-go
func Sol896C(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n, m, vMax int
	var seed int64
	Fscan(in, &n, &m, &seed, &vMax)
	rand := func(_n int) int {
		const mod int64 = 1e9 + 7
		ret := seed
		seed = (seed*7 + 13) % mod
		return int(ret) % _n
	}

	arr := make([]int64, n)
	for i := range arr {
		arr[i] = int64(rand(vMax) + 1)
	}
	t := newODT(arr)
	for ; m > 0; m-- {
		op := rand(4) + 1
		l, r := rand(n), rand(n)
		if l > r {
			l, r = r, l
		}
		var x int
		if op == 3 {
			x = rand(r-l+1) + 1
		} else {
			x = rand(vMax) + 1
		}
		t.prepare(l, r)
		switch op {
		case 1:
			t.add(l, r, int64(x))
		case 2:
			t.set(l, r, int64(x))
		case 3:
			Fprintln(out, t.kth(l, r, x))
		default:
			y := int64(rand(vMax) + 1)
			Fprintln(out, t.powSum(l, r, x, y))
		}
	}
}

func main() {
	Sol896C(os.Stdin, os.Stdout)
}

package main

import (
	"bufio"
	"container/list"
	. "fmt"
	"io"
	"os"
	"reflect"
	"sort"
	"unsafe"
)

type odtBlock struct {
	l, r int
	val  int64
}

type odt struct {
	*list.List
}

func (t odt) split(mid int) {
	for o := t.Front(); o != nil; o = o.Next() {
		b := o.Value.(odtBlock)
		if b.l <= mid && mid < b.r {
			t.InsertAfter(odtBlock{mid + 1, b.r, b.val}, o)
			b.r = mid
			o.Value = b
		}
	}
}

func (t odt) prepare(l, r int) {
	t.split(l - 1)
	t.split(r)
}

func (t odt) add(l, r int, val int64) {
	for o := t.Front(); o != nil; o = o.Next() {
		b := o.Value.(odtBlock)
		if b.l > r {
			break
		}
		if l <= b.l && b.r <= r {
			b.val += val
			o.Value = b
		}
	}
}

func (t odt) set(l, r int, val int64) {
	for o := t.Front(); o != nil; o = o.Next() {
		b := o.Value.(odtBlock)
		if b.l == l {
			o.Value = odtBlock{l, r, val}
			e := o.Next()
			for e != nil && e.Value.(odtBlock).l <= r {
				e = e.Next()
			}
			if e != nil && e != o.Next() {
				oNext := (**list.Element)(unsafe.Pointer(reflect.ValueOf(o).Elem().FieldByName("next").UnsafeAddr()))
				ePrev := (**list.Element)(unsafe.Pointer(reflect.ValueOf(e).Elem().FieldByName("prev").UnsafeAddr()))
				*oNext = e
				*ePrev = o
			}
			break
		}
	}
}

func (t odt) kth(l, r, k int) int64 {
	blocks := []odtBlock{}
	for o := t.Front(); o != nil; o = o.Next() {
		b := o.Value.(odtBlock)
		if l <= b.l && b.r <= r {
			blocks = append(blocks, b)
		}
	}
	sort.Slice(blocks, func(i, j int) bool { return blocks[i].val < blocks[j].val })
	k--
	for _, b := range blocks {
		if cnt := b.r - b.l + 1; k >= cnt {
			k -= cnt
		} else {
			return b.val
		}
	}
	panic(k)
}

func (odt) quickPow(x int64, n int, mod int64) int64 {
	x %= mod
	res := int64(1) % mod
	for ; n > 0; n >>= 1 {
		if n&1 == 1 {
			res = res * x % mod
		}
		x = x * x % mod
	}
	return res
}

func (t odt) powSum(l, r int, n int, mod int64) (res int64) {
	for o := t.Front(); o != nil; o = o.Next() {
		b := o.Value.(odtBlock)
		if l <= b.l && b.r <= r {
			res += int64(b.r-b.l+1) * t.quickPow(b.val, n, mod)
		}
	}
	return res % mod
}

// github.com/EndlessCheng/codeforces-go
func Sol896C(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n, m, vMax int
	var seed int64
	Fscan(in, &n, &m, &seed, &vMax)
	rand := func(_n int) int {
		const mod int64 = 1e9 + 7
		ret := seed
		seed = (seed*7 + 13) % mod
		return int(ret) % _n
	}

	t := odt{list.New()}
	for i := 0; i < n; i++ {
		t.PushBack(odtBlock{i, i, int64(rand(vMax) + 1)})
	}
	for ; m > 0; m-- {
		op := rand(4) + 1
		l, r := rand(n), rand(n)
		if l > r {
			l, r = r, l
		}
		var x int
		if op == 3 {
			x = rand(r-l+1) + 1
		} else {
			x = rand(vMax) + 1
		}
		t.prepare(l, r)
		switch op {
		case 1:
			t.add(l, r, int64(x))
		case 2:
			t.set(l, r, int64(x))
		case 3:
			Fprintln(out, t.kth(l, r, x))
		default:
			y := int64(rand(vMax) + 1)
			Fprintln(out, t.powSum(l, r, x, y))
		}
	}
}

func main() {
	Sol896C(os.Stdin, os.Stdout)
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
	"sort"
)

type odtBlock struct {
	l, r int
	val  int64
}

type odt struct {
	blocks []odtBlock
	size   int
}

func newODT(arr []int64) *odt {
	n := len(arr)
	blocks := make([]odtBlock, n)
	for i := range blocks {
		blocks[i] = odtBlock{i, i, arr[i]}
	}
	return &odt{
		blocks: blocks,
		size:   n,
	}
}

func (t *odt) split(mid int) {
	for i := 0; i < t.size; i++ {
		b := t.blocks[i]
		if b.l <= mid && mid < b.r {
			copy(t.blocks[i+1:t.size+1], t.blocks[i:t.size])
			t.size++
			t.blocks[i+1] = odtBlock{mid + 1, b.r, b.val}
			t.blocks[i].r = mid
		}
	}
}

func (t *odt) prepare(l, r int) {
	t.split(l - 1)
	t.split(r)
}

func (t *odt) add(l, r int, val int64) {
	for i := 0; i < t.size; i++ {
		b := t.blocks[i]
		if b.l > r {
			break
		}
		if l <= b.l && b.r <= r {
			t.blocks[i].val += val
		}
	}
}

func (t *odt) set(l, r int, val int64) {
	for i := 0; i < t.size; i++ {
		b := t.blocks[i]
		if b.l == l {
			t.blocks[i].r = r
			t.blocks[i].val = val
			j := i + 1
			for ; j < t.size && t.blocks[j].l <= r; j++ {
			}
			if shift := j - (i + 1); shift > 0 {
				copy(t.blocks[i+1:t.size-shift], t.blocks[j:t.size])
				t.size -= shift
			}
			break
		}
	}
}

func (t *odt) kth(l, r, k int) int64 {
	blocks := []odtBlock{}
	for i := 0; i < t.size; i++ {
		b := t.blocks[i]
		if l <= b.l && b.r <= r {
			blocks = append(blocks, b)
		}
	}
	sort.Slice(blocks, func(i, j int) bool { return blocks[i].val < blocks[j].val })
	k--
	for _, b := range blocks {
		if cnt := b.r - b.l + 1; k >= cnt {
			k -= cnt
		}
		return b.val
	}
	panic(k)
}

func (t *odt) quickPow(x int64, n int, mod int64) int64 {
	x %= mod
	res := int64(1) % mod
	for ; n > 0; n >>= 1 {
		if n&1 == 1 {
			res = res * x % mod
		}
		x = x * x % mod
	}
	return res
}

func (t *odt) powSum(l, r int, n int, mod int64) (res int64) {
	for i := 0; i < t.size; i++ {
		b := t.blocks[i]
		if l <= b.l && b.r <= r {
			res += int64(b.r-b.l+1) * t.quickPow(b.val, n, mod)
		}
	}
	return res % mod
}

// github.com/EndlessCheng/codeforces-go
func Sol896C(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n, m, vMax int
	var seed int64
	Fscan(in, &n, &m, &seed, &vMax)
	rand := func(_n int) int {
		const mod int64 = 1e9 + 7
		ret := seed
		seed = (seed*7 + 13) % mod
		return int(ret) % _n
	}
	arr := make([]int64, n)
	for i := range arr {
		arr[i] = int64(rand(vMax) + 1)
	}
	t := newODT(arr)
	for ; m > 0; m-- {
		op := rand(4) + 1
		l, r := rand(n), rand(n)
		if l > r {
			l, r = r, l
		}
		var x int
		if op == 3 {
			x = rand(r-l+1) + 1
		} else {
			x = rand(vMax) + 1
		}
		t.prepare(l, r)
		switch op {
		case 1:
			t.add(l, r, int64(x))
		case 2:
			t.set(l, r, int64(x))
		case 3:
			Fprintln(out, t.kth(l, r, x))
		default:
			y := int64(rand(vMax) + 1)
			Fprintln(out, t.powSum(l, r, x, y))
		}
	}
}

func main() {
	Sol896C(os.Stdin, os.Stdout)
}

package main

import (
	"bufio"
	"container/list"
	. "fmt"
	"io"
	"os"
	"reflect"
	"sort"
	"unsafe"
)

type odtBlock struct {
	l, r int
	val  int64
}

type odt struct {
	*list.List
}

func (t odt) split(mid int) {
	for o := t.Front(); o != nil; o = o.Next() {
		b := o.Value.(odtBlock)
		if b.l <= mid && mid < b.r {
			t.InsertAfter(odtBlock{mid + 1, b.r, b.val}, o)
			b.r = mid
			o.Value = b
		}
	}
}

func (t odt) prepare(l, r int) {
	t.split(l - 1)
	t.split(r)
}

func (t odt) add(l, r int, val int64) {
	for o := t.Front(); o != nil; o = o.Next() {
		b := o.Value.(odtBlock)
		if b.l > r {
			break
		}
		if l <= b.l && b.r <= r {
			b.val += val
			o.Value = b
		}
	}
}

func (t odt) set(l, r int, val int64) {
	for o := t.Front(); o != nil; o = o.Next() {
		b := o.Value.(odtBlock)
		if b.l == l {
			o.Value = odtBlock{l, r, val}
			e := o.Next()
			for ; e != nil && e.Value.(odtBlock).l <= r; e = e.Next() {
			}
			if e != nil && e != o.Next() {
				oNext := (**list.Element)(unsafe.Pointer(reflect.ValueOf(o).Elem().FieldByName("next").UnsafeAddr()))
				*oNext = e
				ePrev := (**list.Element)(unsafe.Pointer(reflect.ValueOf(e).Elem().FieldByName("prev").UnsafeAddr()))
				*ePrev = o
			}
			break
		}
	}
}

func (t odt) kth(l, r, k int) int64 {
	blocks := []odtBlock{}
	for o := t.Front(); o != nil; o = o.Next() {
		b := o.Value.(odtBlock)
		if l <= b.l && b.r <= r {
			blocks = append(blocks, b)
		}
	}
	sort.Slice(blocks, func(i, j int) bool { return blocks[i].val < blocks[j].val })
	k--
	for _, b := range blocks {
		if cnt := b.r - b.l + 1; k >= cnt {
			k -= cnt
		} else {
			return b.val
		}
	}
	panic(k)
}

func (odt) quickPow(x int64, n int, mod int64) int64 {
	x %= mod
	res := int64(1) % mod
	for ; n > 0; n >>= 1 {
		if n&1 == 1 {
			res = res * x % mod
		}
		x = x * x % mod
	}
	return res
}

func (t odt) powSum(l, r int, n int, mod int64) (res int64) {
	for o := t.Front(); o != nil; o = o.Next() {
		b := o.Value.(odtBlock)
		if l <= b.l && b.r <= r {
			res += int64(b.r-b.l+1) * t.quickPow(b.val, n, mod)
		}
	}
	return res % mod
}

// github.com/EndlessCheng/codeforces-go
func Sol896C(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n, m, vMax int
	var seed int64
	Fscan(in, &n, &m, &seed, &vMax)
	rand := func(_n int) int {
		const mod int64 = 1e9 + 7
		ret := seed
		seed = (seed*7 + 13) % mod
		return int(ret) % _n
	}

	t := odt{list.New()}
	for i := 0; i < n; i++ {
		t.PushBack(odtBlock{i, i, int64(rand(vMax) + 1)})
	}
	for ; m > 0; m-- {
		op := rand(4) + 1
		l, r := rand(n), rand(n)
		if l > r {
			l, r = r, l
		}
		var x int
		if op == 3 {
			x = rand(r-l+1) + 1
		} else {
			x = rand(vMax) + 1
		}
		t.prepare(l, r)
		switch op {
		case 1:
			t.add(l, r, int64(x))
		case 2:
			t.set(l, r, int64(x))
		case 3:
			Fprintln(out, t.kth(l, r, x))
		default:
			y := int64(rand(vMax) + 1)
			Fprintln(out, t.powSum(l, r, x, y))
		}
	}
}

func main() {
	Sol896C(os.Stdin, os.Stdout)
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
	"sort"
)

type odtBlock struct {
	next *odtBlock
	l, r int
	val  int64
}

type odt struct {
	root *odtBlock
}

func (t odt) split(mid int) {
	for b := t.root; b != nil; b = b.next {
		if b.l <= mid && mid < b.r {
			b.next = &odtBlock{b.next, mid + 1, b.r, b.val}
			b.r = mid
		}
	}
}

func (t odt) prepare(l, r int) {
	t.split(l - 1)
	t.split(r)
}

func (t odt) add(l, r int, val int64) {
	for b := t.root; b != nil; b = b.next {
		if b.l > r {
			break
		}
		if l <= b.l && b.r <= r {
			b.val += val
		}
	}
}

func (t odt) set(l, r int, val int64) {
	for b := t.root; b != nil; b = b.next {
		if b.l == l {
			b.r = r
			b.val = val
			e := b.next
			for e != nil && e.l <= r {
				e = e.next
			}
			b.next = e
			break
		}
	}
}

func (t odt) kth(l, r, k int) int64 {
	blocks := []odtBlock{}
	for b := t.root; b != nil; b = b.next {
		if l <= b.l && b.r <= r {
			blocks = append(blocks, *b)
		}
	}
	sort.Slice(blocks, func(i, j int) bool { return blocks[i].val < blocks[j].val })
	k--
	for _, b := range blocks {
		if cnt := b.r - b.l + 1; k >= cnt {
			k -= cnt
		} else {
			return b.val
		}
	}
	panic(k)
}

func (odt) quickPow(x int64, n int, mod int64) int64 {
	x %= mod
	res := int64(1) % mod
	for ; n > 0; n >>= 1 {
		if n&1 == 1 {
			res = res * x % mod
		}
		x = x * x % mod
	}
	return res
}

func (t odt) powSum(l, r int, n int, mod int64) (res int64) {
	for b := t.root; b != nil; b = b.next {
		if l <= b.l && b.r <= r {
			res += int64(b.r-b.l+1) * t.quickPow(b.val, n, mod)
		}
	}
	return res % mod
}

// github.com/EndlessCheng/codeforces-go
func Sol896C(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n, m, vMax int
	var seed int64
	Fscan(in, &n, &m, &seed, &vMax)
	rand := func(_n int) int {
		const mod int64 = 1e9 + 7
		ret := seed
		seed = (seed*7 + 13) % mod
		return int(ret) % _n
	}

	t := odt{&odtBlock{val: int64(rand(vMax) + 1)}}
	o := t.root
	for i := 1; i < n; i++ {
		o.next = &odtBlock{nil, i, i, int64(rand(vMax) + 1)}
	}
	for ; m > 0; m-- {
		op := rand(4) + 1
		l, r := rand(n), rand(n)
		if l > r {
			l, r = r, l
		}
		var x int
		if op == 3 {
			x = rand(r-l+1) + 1
		} else {
			x = rand(vMax) + 1
		}
		t.prepare(l, r)
		switch op {
		case 1:
			t.add(l, r, int64(x))
		case 2:
			t.set(l, r, int64(x))
		case 3:
			Fprintln(out, t.kth(l, r, x))
		default:
			y := int64(rand(vMax) + 1)
			Fprintln(out, t.powSum(l, r, x, y))
		}
	}
}

func main() {
	Sol896C(os.Stdin, os.Stdout)
}