You are given three strings $$$s$$$, $$$t$$$ and $$$p$$$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.During each operation you choose any character from $$$p$$$, erase it from $$$p$$$ and insert it into string $$$s$$$ (you may insert this character anywhere you want: in the beginning of $$$s$$$, in the end or between any two consecutive characters). For example, if $$$p$$$ is aba, and $$$s$$$ is de, then the following outcomes are possible (the character we erase from $$$p$$$ and insert into $$$s$$$ is highlighted):  aba $$$\rightarrow$$$ ba, de $$$\rightarrow$$$ ade;  aba $$$\rightarrow$$$ ba, de $$$\rightarrow$$$ dae;  aba $$$\rightarrow$$$ ba, de $$$\rightarrow$$$ dea;  aba $$$\rightarrow$$$ aa, de $$$\rightarrow$$$ bde;  aba $$$\rightarrow$$$ aa, de $$$\rightarrow$$$ dbe;  aba $$$\rightarrow$$$ aa, de $$$\rightarrow$$$ deb;  aba $$$\rightarrow$$$ ab, de $$$\rightarrow$$$ ade;  aba $$$\rightarrow$$$ ab, de $$$\rightarrow$$$ dae;  aba $$$\rightarrow$$$ ab, de $$$\rightarrow$$$ dea; Your goal is to perform several (maybe zero) operations so that $$$s$$$ becomes equal to $$$t$$$. Please determine whether it is possible.Note that you have to answer $$$q$$$ independent queries.

The first line contains one integer $$$q$$$ ($$$1 \le q \le 100$$$) — the number of queries. Each query is represented by three consecutive lines. The first line of each query contains the string $$$s$$$ ($$$1 \le |s| \le 100$$$) consisting of lowercase Latin letters. The second line of each query contains the string $$$t$$$ ($$$1 \le |t| \le 100$$$) consisting of lowercase Latin letters. The third line of each query contains the string $$$p$$$ ($$$1 \le |p| \le 100$$$) consisting of lowercase Latin letters.

For each query print YES if it is possible to make $$$s$$$ equal to $$$t$$$, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).

4
ab
acxb
cax
a
aaaa
aaabbcc
a
aaaa
aabbcc
ab
baaa
aaaaa

NoteIn the first test case there is the following sequence of operation:   $$$s = $$$ ab, $$$t = $$$ acxb, $$$p = $$$ cax;  $$$s = $$$ acb, $$$t = $$$ acxb, $$$p = $$$ ax;  $$$s = $$$ acxb, $$$t = $$$ acxb, $$$p = $$$ a. In the second test case there is the following sequence of operation:   $$$s = $$$ a, $$$t = $$$ aaaa, $$$p = $$$ aaabbcc;  $$$s = $$$ aa, $$$t = $$$ aaaa, $$$p = $$$ aabbcc;  $$$s = $$$ aaa, $$$t = $$$ aaaa, $$$p = $$$ abbcc;  $$$s = $$$ aaaa, $$$t = $$$ aaaa, $$$p = $$$ bbcc.

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"
	"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
		}
	}
}

// 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"
	"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
}


Door's family is going celebrate Famil Doors's birthday party. They love Famil Door so they are planning to make his birthday cake weird!The cake is a n × n square consisting of equal squares with side length 1. Each square is either empty or consists of a single chocolate. They bought the cake and randomly started to put the chocolates on the cake. The value of Famil Door's happiness will be equal to the number of pairs of cells with chocolates that are in the same row or in the same column of the cake. Famil Doors's family is wondering what is the amount of happiness of Famil going to be?Please, note that any pair can be counted no more than once, as two different cells can't share both the same row and the same column.

In the first line of the input, you are given a single integer n (1 ≤ n ≤ 100) — the length of the side of the cake. Then follow n lines, each containing n characters. Empty cells are denoted with '.', while cells that contain chocolates are denoted by 'C'.

Print the value of Famil Door's happiness, i.e. the number of pairs of chocolate pieces that share the same row or the same column.

NoteIf we number rows from top to bottom and columns from left to right, then, pieces that share the same row in the first sample are:   (1, 2) and (1, 3)  (3, 1) and (3, 3)  Pieces that share the same column are:   (2, 1) and (3, 1)  (1, 3) and (3, 3)

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"
	"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)
	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)
}


In a dream Marco met an elderly man with a pair of black glasses. The man told him the key to immortality and then disappeared with the wind of time.When he woke up, he only remembered that the key was a sequence of positive integers of some length n, but forgot the exact sequence. Let the elements of the sequence be a1, a2, ..., an. He remembered that he calculated gcd(ai, ai + 1, ..., aj) for every 1 ≤ i ≤ j ≤ n and put it into a set S. gcd here means the greatest common divisor.Note that even if a number is put into the set S twice or more, it only appears once in the set.Now Marco gives you the set S and asks you to help him figure out the initial sequence. If there are many solutions, print any of them. It is also possible that there are no sequences that produce the set S, in this case print -1.

The first line contains a single integer m (1 ≤ m ≤ 1000) — the size of the set S. The second line contains m integers s1, s2, ..., sm (1 ≤ si ≤ 106) — the elements of the set S. It's guaranteed that the elements of the set are given in strictly increasing order, that means s1 < s2 < ... < sm.

If there is no solution, print a single line containing -1. Otherwise, in the first line print a single integer n denoting the length of the sequence, n should not exceed 4000. In the second line print n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the sequence. We can show that if a solution exists, then there is a solution with n not exceeding 4000 and ai not exceeding 106. If there are multiple solutions, print any of them.

NoteIn the first example 2 = gcd(4, 6), the other elements from the set appear in the sequence, and we can show that there are no values different from 2, 4, 6 and 12 among gcd(ai, ai + 1, ..., aj) for every 1 ≤ i ≤ j ≤ n.

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 (
	"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(
	"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])
	}
}


— Willem...— What's the matter?— It seems that there's something wrong with Seniorious...— I'll have a look...Seniorious is made by linking special talismans in particular order.After over 500 years, the carillon is now in bad condition, so Willem decides to examine it thoroughly.Seniorious has n pieces of talisman. Willem puts them in a line, the i-th of which is an integer ai.In order to maintain it, Willem needs to perform m operations.There are four types of operations: 1 l r x: For each i such that l ≤ i ≤ r, assign ai + x to ai. 2 l r x: For each i such that l ≤ i ≤ r, assign x to ai. 3 l r x: Print the x-th smallest number in the index range [l, r], i.e. the element at the x-th position if all the elements ai such that l ≤ i ≤ r are taken and sorted into an array of non-decreasing integers. It's guaranteed that 1 ≤ x ≤ r - l + 1. 4 l r x y: Print the sum of the x-th power of ai such that l ≤ i ≤ r, modulo y, i.e. .

The only line contains four integers n, m, seed, vmax (1 ≤ n, m ≤ 105, 0 ≤ seed < 109 + 7, 1 ≤ vmax ≤ 109). The initial values and operations are generated using following pseudo code: def rnd():    ret = seed    seed = (seed * 7 + 13) mod 1000000007    return retfor i = 1 to n:    a[i] = (rnd() mod vmax) + 1for i = 1 to m:    op = (rnd() mod 4) + 1    l = (rnd() mod n) + 1    r = (rnd() mod n) + 1    if (l > r):          swap(l, r)    if (op == 3):        x = (rnd() mod (r - l + 1)) + 1    else:        x = (rnd() mod vmax) + 1    if (op == 4):        y = (rnd() mod vmax) + 1 Here op is the type of the operation mentioned in the legend.

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

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)
}