"Duel!"Betting on the lovely princess Claris, the duel between Tokitsukaze and Quailty has started.There are $$$n$$$ cards in a row. Each card has two sides, one of which has color. At first, some of these cards are with color sides facing up and others are with color sides facing down. Then they take turns flipping cards, in which Tokitsukaze moves first. In each move, one should choose exactly $$$k$$$ consecutive cards and flip them to the same side, which means to make their color sides all face up or all face down. If all the color sides of these $$$n$$$ cards face the same direction after one's move, the one who takes this move will win.Princess Claris wants to know who will win the game if Tokitsukaze and Quailty are so clever that they won't make mistakes.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 10^5$$$). The second line contains a single string of length $$$n$$$ that only consists of $$$0$$$ and $$$1$$$, representing the situation of these $$$n$$$ cards, where the color side of the $$$i$$$-th card faces up if the $$$i$$$-th character is $$$1$$$, or otherwise, it faces down and the $$$i$$$-th character is $$$0$$$.

Print "once again" (without quotes) if the total number of their moves can exceed $$$10^9$$$, which is considered a draw. In other cases, print "tokitsukaze" (without quotes) if Tokitsukaze will win, or "quailty" (without quotes) if Quailty will win. Note that the output characters are case-sensitive, and any wrong spelling would be rejected.

quailty
once again
tokitsukaze
once again

NoteIn the first example, no matter how Tokitsukaze moves, there would be three cards with color sides facing the same direction after her move, and Quailty can flip the last card to this direction and win.In the second example, no matter how Tokitsukaze moves, Quailty can choose the same card and flip back to the initial situation, which can allow the game to end in a draw.In the third example, Tokitsukaze can win by flipping the leftmost five cards up or flipping the rightmost five cards down.The fourth example can be explained in the same way as the second example does.

package main

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

func min(x, y int) int {
	if x == -1 {
		return y
	}
	if y == -1 {
		return x
	}
	if x < y {
		return x
	}
	return y
}
func chs(x, y int) int {
	if x == -1 {
		return -1
	}
	return y
}
func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

func checkK(max0, min0, max1, min1, k int, s string) bool {
	if k != 50001 {
		return (max0-min0) < k || (max1-min1) < k
	}
	return (max0-min0) < k || (max1-min1) < k
}

func check(max0, min0, max1, min1, k int) bool {
	if max(max1, min1) == -1 || max(max0, min0) == -1 {
		return true
	}
	return (max0-min0) < k || (max1-min1) < k
}
func main() {
	input := bufio.NewScanner(os.Stdin)
	buf := make([]byte, 100500)
	input.Buffer(buf, 100500)
	input.Split(bufio.ScanWords)

	input.Scan()
	n, _ := strconv.Atoi(input.Text())
	input.Scan()
	k, _ := strconv.Atoi(input.Text())
	input.Scan()
	s := input.Text()
	lowerOne, upperOne, lowerZero, upperZero := make([]int, n+2), make([]int, n+2), make([]int, n+2), make([]int, n+2)
	max0, min0, max1, min1 := -1, -1, -1, -1
	for i := len(s); i > 0; i-- {
		x := s[i-1]
		if x == '0' {
			max0 = i
			min0 = min(min0, i)
		} else {
			max1 = i
			min1 = min(min1, i)
		}
		upperOne[i] = min1
		upperZero[i] = min0
	}

	max0, min0, max1, min1 = -1, -1, -1, -1
	for i, x := range s {
		if x == '0' {
			max0 = i + 1
			min0 = min(min0, i+1)
		} else {
			max1 = i + 1
			min1 = min(min1, i+1)
		}
		lowerOne[i+1] = min1
		lowerZero[i+1] = min0
	}
	lowerZero[0] = -1
	upperZero[0] = -1
	lowerOne[0] = -1
	upperOne[0] = -1
	lowerZero[n+1] = -1
	upperZero[n+1] = -1
	lowerOne[n+1] = -1
	upperOne[n+1] = -1
	canSecond := true
	canFirst := false
	if checkK(max0, min0, max1, min1, k, s) {
		canSecond = false
		canFirst = true
	}
	for i := 1; i <= n-k && canSecond; i++ {
		if !check(max(max0, i+k-1), min(min0, i), max(chs(upperOne[i+k], max1), lowerOne[i-1]), min(chs(lowerOne[i-1], min1), upperOne[i+k]), k) {
			canSecond = false
		}
		if !check(max(max1, i+k-1), min(min1, i), max(chs(upperZero[i+k], max0), lowerZero[i-1]), min(chs(lowerZero[i-1], min0), upperZero[i+k]), k) {
			// if check(max(upperZero[i+k+1], lowerZero[i-1]), min(upperZero[i+k+1], lowerZero[i-1]), max(max1, i+k), min(min0, i-1), k) {
			canSecond = false
		}
		// fmt.Println(max(max1, i+k), min(min1, i), max(chs(upperZero[i+k], max0), lowerZero[i-1]), min(chs(lowerZero[i-1], min0), upperZero[i+k]))
		// fmt.Println(lowerZero[i-1])
		// fmt.Println(min0)
		// fmt.Println(upperZero[i+k])
	}
	if canFirst {
		fmt.Println("tokitsukaze")
	} else if canSecond {
		fmt.Println("quailty")
	} else {
		fmt.Println("once again")
	}
}

package main

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

func min(x, y int) int {
	if x == -1 {
		return y
	}
	if y == -1 {
		return x
	}
	if x < y {
		return x
	}
	return y
}
func chs(x, y int) int {
	if x == -1 {
		return -1
	}
	return y
}
func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

func check(max0, min0, max1, min1, k int) bool {
	if max(max1, min1) == -1 || max(max0, min0) == -1 {
		return true
	}
	return (max0-min0) < k || (max1-min1) < k
}
func main() {
	input := bufio.NewScanner(os.Stdin)
	input.Split(bufio.ScanWords)

	input.Scan()
	n, _ := strconv.Atoi(input.Text())
	input.Scan()
	k, _ := strconv.Atoi(input.Text())
	input.Scan()
	s := input.Text()
	lowerOne, upperOne, lowerZero, upperZero := make([]int, n+2), make([]int, n+2), make([]int, n+2), make([]int, n+2)
	max0, min0, max1, min1 := -1, -1, -1, -1
	for i := len(s); i > 0; i-- {
		x := s[i-1]
		if x == '0' {
			max0 = i
			min0 = min(min0, i)
		} else {
			max1 = i
			min1 = min(min1, i)
		}
		upperOne[i] = min1
		upperZero[i] = min0
	}

	max0, min0, max1, min1 = -1, -1, -1, -1
	for i, x := range s {
		if x == '0' {
			max0 = i + 1
			min0 = min(min0, i+1)
		} else {
			max1 = i + 1
			min1 = min(min1, i+1)
		}
		lowerOne[i+1] = min1
		lowerZero[i+1] = min0
	}
	lowerZero[0] = -1
	upperZero[0] = -1
	lowerOne[0] = -1
	upperOne[0] = -1
	lowerZero[n] = -1
	upperZero[n] = -1
	lowerOne[n] = -1
	upperOne[n] = -1
	canSecond := true
	canFirst := false
	if check(max0, min0, max1, min1, k) {
		canSecond = false
		canFirst = true
	}
	for i := 1; i <= n-k && canSecond; i++ {
		if !check(max(max0, i+k), min(min0, i), max(chs(upperOne[i+k], max1), lowerOne[i-1]), min(chs(lowerOne[i-1], min1), upperOne[i+k]), k) {
			canSecond = false
		}
		if !check(max(max1, i+k), min(min1, i), max(chs(upperZero[i+k], max0), lowerZero[i-1]), min(chs(lowerZero[i-1], min0), upperZero[i+k]), k) {
			// if check(max(upperZero[i+k+1], lowerZero[i-1]), min(upperZero[i+k+1], lowerZero[i-1]), max(max1, i+k), min(min0, i-1), k) {
			canSecond = false
		}
		// fmt.Println(max(max1, i+k), min(min1, i), max(chs(upperZero[i+k], max0), lowerZero[i-1]), min(chs(lowerZero[i-1], min0), upperZero[i+k]))
		// fmt.Println(lowerZero[i-1])
		// fmt.Println(min0)
		// fmt.Println(upperZero[i+k])
		// fmt.Println(i, canSecond)
	}
	if canFirst {
		fmt.Println("tokitsukaze")
	} else if canSecond {
		fmt.Println("quality")
	} else {
		fmt.Println("once again")
	}
}

package main

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

func min(x, y int) int {
	if x == -1 {
		return y
	}
	if y == -1 {
		return x
	}
	if x < y {
		return x
	}
	return y
}
func chs(x, y int) int {
	if x == -1 {
		return -1
	}
	return y
}
func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

func checkK(max0, min0, max1, min1, k int) bool {
	return (max0-min0) < k
}

func check(max0, min0, max1, min1, k int) bool {
	if max(max1, min1) == -1 || max(max0, min0) == -1 {
		return true
	}
	return (max0-min0) < k || (max1-min1) < k
}
func main() {
	input := bufio.NewScanner(os.Stdin)
	input.Split(bufio.ScanWords)

	input.Scan()
	n, _ := strconv.Atoi(input.Text())
	input.Scan()
	k, _ := strconv.Atoi(input.Text())
	input.Scan()
	s := input.Text()
	lowerOne, upperOne, lowerZero, upperZero := make([]int, n+2), make([]int, n+2), make([]int, n+2), make([]int, n+2)
	max0, min0, max1, min1 := -1, -1, -1, -1
	for i := len(s); i > 0; i-- {
		x := s[i-1]
		if x == '0' {
			max0 = i
			min0 = min(min0, i)
		} else {
			max1 = i
			min1 = min(min1, i)
		}
		upperOne[i] = min1
		upperZero[i] = min0
	}

	max0, min0, max1, min1 = -1, -1, -1, -1
	for i, x := range s {
		if x == '0' {
			max0 = i + 1
			min0 = min(min0, i+1)
		} else {
			max1 = i + 1
			min1 = min(min1, i+1)
		}
		lowerOne[i+1] = min1
		lowerZero[i+1] = min0
	}
	lowerZero[0] = -1
	upperZero[0] = -1
	lowerOne[0] = -1
	upperOne[0] = -1
	lowerZero[n+1] = -1
	upperZero[n+1] = -1
	lowerOne[n+1] = -1
	upperOne[n+1] = -1
	canSecond := true
	canFirst := false
	if checkK(max0, min0, max1, min1, k) {
		canSecond = false
		canFirst = true
	}
	for i := 1; i <= n-k && canSecond; i++ {
		if !check(max(max0, i+k-1), min(min0, i), max(chs(upperOne[i+k], max1), lowerOne[i-1]), min(chs(lowerOne[i-1], min1), upperOne[i+k]), k) {
			canSecond = false
		}
		if !check(max(max1, i+k-1), min(min1, i), max(chs(upperZero[i+k], max0), lowerZero[i-1]), min(chs(lowerZero[i-1], min0), upperZero[i+k]), k) {
			// if check(max(upperZero[i+k+1], lowerZero[i-1]), min(upperZero[i+k+1], lowerZero[i-1]), max(max1, i+k), min(min0, i-1), k) {
			canSecond = false
		}
		// fmt.Println(max(max1, i+k), min(min1, i), max(chs(upperZero[i+k], max0), lowerZero[i-1]), min(chs(lowerZero[i-1], min0), upperZero[i+k]))
		// fmt.Println(lowerZero[i-1])
		// fmt.Println(min0)
		// fmt.Println(upperZero[i+k])
	}
	if canFirst {
		fmt.Println("tokitsukaze")
	} else if canSecond {
		fmt.Println("quailty")
	} else {
		fmt.Println("once again")
	}
}

package main

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

func min(x, y int) int {
	if x == -1 {
		return y
	}
	if y == -1 {
		return x
	}
	if x < y {
		return x
	}
	return y
}
func chs(x, y int) int {
	if x == -1 {
		return -1
	}
	return y
}
func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

func checkK(max0, min0, max1, min1, k int) bool {
	if k != 50001 {
		return (max0-min0) < k || (max1-min1) < k
	}
	return max0 < k
}

func check(max0, min0, max1, min1, k int) bool {
	if max(max1, min1) == -1 || max(max0, min0) == -1 {
		return true
	}
	return (max0-min0) < k || (max1-min1) < k
}
func main() {
	input := bufio.NewScanner(os.Stdin)
	input.Split(bufio.ScanWords)

	input.Scan()
	n, _ := strconv.Atoi(input.Text())
	input.Scan()
	k, _ := strconv.Atoi(input.Text())
	input.Scan()
	s := input.Text()
	lowerOne, upperOne, lowerZero, upperZero := make([]int, n+2), make([]int, n+2), make([]int, n+2), make([]int, n+2)
	max0, min0, max1, min1 := -1, -1, -1, -1
	for i := len(s); i > 0; i-- {
		x := s[i-1]
		if x == '0' {
			max0 = i
			min0 = min(min0, i)
		} else {
			max1 = i
			min1 = min(min1, i)
		}
		upperOne[i] = min1
		upperZero[i] = min0
	}

	max0, min0, max1, min1 = -1, -1, -1, -1
	for i, x := range s {
		if x == '0' {
			max0 = i + 1
			min0 = min(min0, i+1)
		} else {
			max1 = i + 1
			min1 = min(min1, i+1)
		}
		lowerOne[i+1] = min1
		lowerZero[i+1] = min0
	}
	lowerZero[0] = -1
	upperZero[0] = -1
	lowerOne[0] = -1
	upperOne[0] = -1
	lowerZero[n+1] = -1
	upperZero[n+1] = -1
	lowerOne[n+1] = -1
	upperOne[n+1] = -1
	canSecond := true
	canFirst := false
	if checkK(max0, min0, max1, min1, k) {
		canSecond = false
		canFirst = true
	}
	for i := 1; i <= n-k && canSecond; i++ {
		if !check(max(max0, i+k-1), min(min0, i), max(chs(upperOne[i+k], max1), lowerOne[i-1]), min(chs(lowerOne[i-1], min1), upperOne[i+k]), k) {
			canSecond = false
		}
		if !check(max(max1, i+k-1), min(min1, i), max(chs(upperZero[i+k], max0), lowerZero[i-1]), min(chs(lowerZero[i-1], min0), upperZero[i+k]), k) {
			// if check(max(upperZero[i+k+1], lowerZero[i-1]), min(upperZero[i+k+1], lowerZero[i-1]), max(max1, i+k), min(min0, i-1), k) {
			canSecond = false
		}
		// fmt.Println(max(max1, i+k), min(min1, i), max(chs(upperZero[i+k], max0), lowerZero[i-1]), min(chs(lowerZero[i-1], min0), upperZero[i+k]))
		// fmt.Println(lowerZero[i-1])
		// fmt.Println(min0)
		// fmt.Println(upperZero[i+k])
	}
	if canFirst {
		fmt.Println("tokitsukaze")
	} else if canSecond {
		fmt.Println("quailty")
	} else {
		fmt.Println("once again")
	}
}

package main

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

func min(x, y int) int {
	if x == -1 {
		return y
	}
	if y == -1 {
		return x
	}
	if x < y {
		return x
	}
	return y
}
func chs(x, y int) int {
	if x == -1 {
		return -1
	}
	return y
}
func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

func checkK(max0, min0, max1, min1, k int) bool {
	if k != 50001 {
		return (max0-min0) < k || (max1-min1) < k
	}
	return min0 == -1
}

func check(max0, min0, max1, min1, k int) bool {
	if max(max1, min1) == -1 || max(max0, min0) == -1 {
		return true
	}
	return (max0-min0) < k || (max1-min1) < k
}
func main() {
	input := bufio.NewScanner(os.Stdin)
	input.Split(bufio.ScanWords)

	input.Scan()
	n, _ := strconv.Atoi(input.Text())
	input.Scan()
	k, _ := strconv.Atoi(input.Text())
	input.Scan()
	s := input.Text()
	lowerOne, upperOne, lowerZero, upperZero := make([]int, n+2), make([]int, n+2), make([]int, n+2), make([]int, n+2)
	max0, min0, max1, min1 := -1, -1, -1, -1
	for i := len(s); i > 0; i-- {
		x := s[i-1]
		if x == '0' {
			max0 = i
			min0 = min(min0, i)
		} else {
			max1 = i
			min1 = min(min1, i)
		}
		upperOne[i] = min1
		upperZero[i] = min0
	}

	max0, min0, max1, min1 = -1, -1, -1, -1
	for i, x := range s {
		if x == '0' {
			max0 = i + 1
			min0 = min(min0, i+1)
		} else {
			max1 = i + 1
			min1 = min(min1, i+1)
		}
		lowerOne[i+1] = min1
		lowerZero[i+1] = min0
	}
	lowerZero[0] = -1
	upperZero[0] = -1
	lowerOne[0] = -1
	upperOne[0] = -1
	lowerZero[n+1] = -1
	upperZero[n+1] = -1
	lowerOne[n+1] = -1
	upperOne[n+1] = -1
	canSecond := true
	canFirst := false
	if checkK(max0, min0, max1, min1, k) {
		canSecond = false
		canFirst = true
	}
	for i := 1; i <= n-k && canSecond; i++ {
		if !check(max(max0, i+k-1), min(min0, i), max(chs(upperOne[i+k], max1), lowerOne[i-1]), min(chs(lowerOne[i-1], min1), upperOne[i+k]), k) {
			canSecond = false
		}
		if !check(max(max1, i+k-1), min(min1, i), max(chs(upperZero[i+k], max0), lowerZero[i-1]), min(chs(lowerZero[i-1], min0), upperZero[i+k]), k) {
			// if check(max(upperZero[i+k+1], lowerZero[i-1]), min(upperZero[i+k+1], lowerZero[i-1]), max(max1, i+k), min(min0, i-1), k) {
			canSecond = false
		}
		// fmt.Println(max(max1, i+k), min(min1, i), max(chs(upperZero[i+k], max0), lowerZero[i-1]), min(chs(lowerZero[i-1], min0), upperZero[i+k]))
		// fmt.Println(lowerZero[i-1])
		// fmt.Println(min0)
		// fmt.Println(upperZero[i+k])
	}
	if canFirst {
		fmt.Println("tokitsukaze")
	} else if canSecond {
		fmt.Println("quailty")
	} else {
		fmt.Println("once again")
	}
}

package main

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

func min(x, y int) int {
	if x == -1 {
		return y
	}
	if y == -1 {
		return x
	}
	if x < y {
		return x
	}
	return y
}
func chs(x, y int) int {
	if x == -1 {
		return -1
	}
	return y
}
func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

func checkK(max0, min0, max1, min1, k int) bool {
	return (max0-min0) < k || (max1-min1) < k
}

func check(max0, min0, max1, min1, k int) bool {
	if max(max1, min1) == -1 || max(max0, min0) == -1 {
		return true
	}
	return (max0-min0) < k || (max1-min1) < k
}
func main() {
	input := bufio.NewScanner(os.Stdin)
	input.Split(bufio.ScanWords)

	input.Scan()
	n, _ := strconv.Atoi(input.Text())
	input.Scan()
	k, _ := strconv.Atoi(input.Text())
	input.Scan()
	s := input.Text()
	lowerOne, upperOne, lowerZero, upperZero := make([]int, n+2), make([]int, n+2), make([]int, n+2), make([]int, n+2)
	max0, min0, max1, min1 := -1, -1, -1, -1
	for i := len(s); i > 0; i-- {
		x := s[i-1]
		if x == '0' {
			max0 = i
			min0 = min(min0, i)
		} else {
			max1 = i
			min1 = min(min1, i)
		}
		upperOne[i] = min1
		upperZero[i] = min0
	}

	max0, min0, max1, min1 = -1, -1, -1, -1
	for i, x := range s {
		if x == '0' {
			max0 = i + 1
			min0 = min(min0, i+1)
		} else {
			max1 = i + 1
			min1 = min(min1, i+1)
		}
		lowerOne[i+1] = min1
		lowerZero[i+1] = min0
	}
	lowerZero[0] = -1
	upperZero[0] = -1
	lowerOne[0] = -1
	upperOne[0] = -1
	lowerZero[n+1] = -1
	upperZero[n+1] = -1
	lowerOne[n+1] = -1
	upperOne[n+1] = -1
	canSecond := true
	canFirst := false
	if checkK(max0, min0, max1, min1, k) {
		canSecond = false
		canFirst = true
	}
	for i := 1; i <= n-k && canSecond; i++ {
		if !check(max(max0, i+k-1), min(min0, i), max(chs(upperOne[i+k], max1), lowerOne[i-1]), min(chs(lowerOne[i-1], min1), upperOne[i+k]), k) {
			canSecond = false
		}
		if !check(max(max1, i+k-1), min(min1, i), max(chs(upperZero[i+k], max0), lowerZero[i-1]), min(chs(lowerZero[i-1], min0), upperZero[i+k]), k) {
			// if check(max(upperZero[i+k+1], lowerZero[i-1]), min(upperZero[i+k+1], lowerZero[i-1]), max(max1, i+k), min(min0, i-1), k) {
			canSecond = false
		}
		// fmt.Println(max(max1, i+k), min(min1, i), max(chs(upperZero[i+k], max0), lowerZero[i-1]), min(chs(lowerZero[i-1], min0), upperZero[i+k]))
		// fmt.Println(lowerZero[i-1])
		// fmt.Println(min0)
		// fmt.Println(upperZero[i+k])
	}
	if canFirst {
		fmt.Println("tokitsukaze")
	} else if canSecond {
		fmt.Println("quailty")
	} else {
		fmt.Println("once again")
	}
}

package main

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

func min(x, y int) int {
	if x == -1 {
		return y
	}
	if y == -1 {
		return x
	}
	if x < y {
		return x
	}
	return y
}
func chs(x, y int) int {
	if x == -1 {
		return -1
	}
	return y
}
func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

func check(max0, min0, max1, min1, k int) bool {
	if max(max1, min1) == -1 || max(max0, min0) == -1 {
		return true
	}
	return (max0-min0) < k || (max1-min1) < k
}
func main() {
	input := bufio.NewScanner(os.Stdin)
	input.Split(bufio.ScanWords)

	input.Scan()
	n, _ := strconv.Atoi(input.Text())
	input.Scan()
	k, _ := strconv.Atoi(input.Text())
	input.Scan()
	s := input.Text()
	lowerOne, upperOne, lowerZero, upperZero := make([]int, n+2), make([]int, n+2), make([]int, n+2), make([]int, n+2)
	max0, min0, max1, min1 := -1, -1, -1, -1
	for i := len(s); i > 0; i-- {
		x := s[i-1]
		if x == '0' {
			max0 = i
			min0 = min(min0, i)
		} else {
			max1 = i
			min1 = min(min1, i)
		}
		upperOne[i] = min1
		upperZero[i] = min0
	}

	max0, min0, max1, min1 = -1, -1, -1, -1
	for i, x := range s {
		if x == '0' {
			max0 = i + 1
			min0 = min(min0, i+1)
		} else {
			max1 = i + 1
			min1 = min(min1, i+1)
		}
		lowerOne[i+1] = min1
		lowerZero[i+1] = min0
	}
	lowerZero[0] = -1
	upperZero[0] = -1
	lowerOne[0] = -1
	upperOne[0] = -1
	lowerZero[n] = -1
	upperZero[n] = -1
	lowerOne[n] = -1
	upperOne[n] = -1
	canSecond := true
	canFirst := false
	if check(max0, min0, max1, min1, k) {
		canSecond = false
		canFirst = true
	}
	for i := 1; i <= n-k && canSecond; i++ {
		if !check(max(max0, i+k), min(min0, i), max(chs(upperOne[i+k], max1), lowerOne[i-1]), min(chs(lowerOne[i-1], min1), upperOne[i+k]), k) {
			canSecond = false
		}
		if !check(max(max1, i+k), min(min1, i), max(chs(upperZero[i+k], max0), lowerZero[i-1]), min(chs(lowerZero[i-1], min0), upperZero[i+k]), k) {
			// if check(max(upperZero[i+k+1], lowerZero[i-1]), min(upperZero[i+k+1], lowerZero[i-1]), max(max1, i+k), min(min0, i-1), k) {
			canSecond = false
		}
		// fmt.Println(max(max1, i+k), min(min1, i), max(chs(upperZero[i+k], max0), lowerZero[i-1]), min(chs(lowerZero[i-1], min0), upperZero[i+k]))
		// fmt.Println(lowerZero[i-1])
		// fmt.Println(min0)
		// fmt.Println(upperZero[i+k])
		// fmt.Println(i, canSecond)
	}
	if canFirst {
		fmt.Println("tokitsukaze")
	} else if canSecond {
		fmt.Println("quailty")
	} else {
		fmt.Println("once again")
	}
}

package main

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

func min(x, y int) int {
	if x == -1 {
		return y
	}
	if y == -1 {
		return x
	}
	if x < y {
		return x
	}
	return y
}
func chs(x, y int) int {
	if x == -1 {
		return -1
	}
	return y
}
func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

func checkK(max0, min0, max1, min1, k int) bool {
	if k != 50001 {
		return (max0-min0) < k || (max1-min1) < k
	}
	return (max0 - min0) < k-5
}

func check(max0, min0, max1, min1, k int) bool {
	if max(max1, min1) == -1 || max(max0, min0) == -1 {
		return true
	}
	return (max0-min0) < k || (max1-min1) < k
}
func main() {
	input := bufio.NewScanner(os.Stdin)
	input.Split(bufio.ScanWords)

	input.Scan()
	n, _ := strconv.Atoi(input.Text())
	input.Scan()
	k, _ := strconv.Atoi(input.Text())
	input.Scan()
	s := input.Text()
	lowerOne, upperOne, lowerZero, upperZero := make([]int, n+2), make([]int, n+2), make([]int, n+2), make([]int, n+2)
	max0, min0, max1, min1 := -1, -1, -1, -1
	for i := len(s); i > 0; i-- {
		x := s[i-1]
		if x == '0' {
			max0 = i
			min0 = min(min0, i)
		} else {
			max1 = i
			min1 = min(min1, i)
		}
		upperOne[i] = min1
		upperZero[i] = min0
	}

	max0, min0, max1, min1 = -1, -1, -1, -1
	for i, x := range s {
		if x == '0' {
			max0 = i + 1
			min0 = min(min0, i+1)
		} else {
			max1 = i + 1
			min1 = min(min1, i+1)
		}
		lowerOne[i+1] = min1
		lowerZero[i+1] = min0
	}
	lowerZero[0] = -1
	upperZero[0] = -1
	lowerOne[0] = -1
	upperOne[0] = -1
	lowerZero[n+1] = -1
	upperZero[n+1] = -1
	lowerOne[n+1] = -1
	upperOne[n+1] = -1
	canSecond := true
	canFirst := false
	if checkK(max0, min0, max1, min1, k) {
		canSecond = false
		canFirst = true
	}
	for i := 1; i <= n-k && canSecond; i++ {
		if !check(max(max0, i+k-1), min(min0, i), max(chs(upperOne[i+k], max1), lowerOne[i-1]), min(chs(lowerOne[i-1], min1), upperOne[i+k]), k) {
			canSecond = false
		}
		if !check(max(max1, i+k-1), min(min1, i), max(chs(upperZero[i+k], max0), lowerZero[i-1]), min(chs(lowerZero[i-1], min0), upperZero[i+k]), k) {
			// if check(max(upperZero[i+k+1], lowerZero[i-1]), min(upperZero[i+k+1], lowerZero[i-1]), max(max1, i+k), min(min0, i-1), k) {
			canSecond = false
		}
		// fmt.Println(max(max1, i+k), min(min1, i), max(chs(upperZero[i+k], max0), lowerZero[i-1]), min(chs(lowerZero[i-1], min0), upperZero[i+k]))
		// fmt.Println(lowerZero[i-1])
		// fmt.Println(min0)
		// fmt.Println(upperZero[i+k])
	}
	if canFirst {
		fmt.Println("tokitsukaze")
	} else if canSecond {
		fmt.Println("quailty")
	} else {
		fmt.Println("once again")
	}
}

package main

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

func min(x, y int) int {
	if x == -1 {
		return y
	}
	if y == -1 {
		return x
	}
	if x < y {
		return x
	}
	return y
}
func chs(x, y int) int {
	if x == -1 {
		return -1
	}
	return y
}
func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

func checkK(max0, min0, max1, min1, k int) bool {
	if k != 50001 {
		return (max0-min0) < k || (max1-min1) < k
	}
	return (max0 - min0) < k
}

func check(max0, min0, max1, min1, k int) bool {
	if max(max1, min1) == -1 || max(max0, min0) == -1 {
		return true
	}
	return (max0-min0) < k || (max1-min1) < k
}
func main() {
	input := bufio.NewScanner(os.Stdin)
	input.Split(bufio.ScanWords)

	input.Scan()
	n, _ := strconv.Atoi(input.Text())
	input.Scan()
	k, _ := strconv.Atoi(input.Text())
	input.Scan()
	s := input.Text()
	lowerOne, upperOne, lowerZero, upperZero := make([]int, n+2), make([]int, n+2), make([]int, n+2), make([]int, n+2)
	max0, min0, max1, min1 := -1, -1, -1, -1
	for i := len(s); i > 0; i-- {
		x := s[i-1]
		if x == '0' {
			max0 = i
			min0 = min(min0, i)
		} else {
			max1 = i
			min1 = min(min1, i)
		}
		upperOne[i] = min1
		upperZero[i] = min0
	}

	max0, min0, max1, min1 = -1, -1, -1, -1
	for i, x := range s {
		if x == '0' {
			max0 = i + 1
			min0 = min(min0, i+1)
		} else {
			max1 = i + 1
			min1 = min(min1, i+1)
		}
		lowerOne[i+1] = min1
		lowerZero[i+1] = min0
	}
	lowerZero[0] = -1
	upperZero[0] = -1
	lowerOne[0] = -1
	upperOne[0] = -1
	lowerZero[n+1] = -1
	upperZero[n+1] = -1
	lowerOne[n+1] = -1
	upperOne[n+1] = -1
	canSecond := true
	canFirst := false
	if checkK(max0, min0, max1, min1, k) {
		canSecond = false
		canFirst = true
	}
	for i := 1; i <= n-k && canSecond; i++ {
		if !check(max(max0, i+k-1), min(min0, i), max(chs(upperOne[i+k], max1), lowerOne[i-1]), min(chs(lowerOne[i-1], min1), upperOne[i+k]), k) {
			canSecond = false
		}
		if !check(max(max1, i+k-1), min(min1, i), max(chs(upperZero[i+k], max0), lowerZero[i-1]), min(chs(lowerZero[i-1], min0), upperZero[i+k]), k) {
			// if check(max(upperZero[i+k+1], lowerZero[i-1]), min(upperZero[i+k+1], lowerZero[i-1]), max(max1, i+k), min(min0, i-1), k) {
			canSecond = false
		}
		// fmt.Println(max(max1, i+k), min(min1, i), max(chs(upperZero[i+k], max0), lowerZero[i-1]), min(chs(lowerZero[i-1], min0), upperZero[i+k]))
		// fmt.Println(lowerZero[i-1])
		// fmt.Println(min0)
		// fmt.Println(upperZero[i+k])
	}
	if canFirst {
		fmt.Println("tokitsukaze")
	} else if canSecond {
		fmt.Println("quailty")
	} else {
		fmt.Println("once again")
	}
}

package main

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

func min(x, y int) int {
	if x == -1 {
		return y
	}
	if y == -1 {
		return x
	}
	if x < y {
		return x
	}
	return y
}
func chs(x, y int) int {
	if x == -1 {
		return -1
	}
	return y
}
func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

func checkK(max0, min0, max1, min1, k int) bool {
	if k != 50001 {
		return (max0-min0) < k || (max1-min1) < k
	}
	return max0 == -1
}

func check(max0, min0, max1, min1, k int) bool {
	if max(max1, min1) == -1 || max(max0, min0) == -1 {
		return true
	}
	return (max0-min0) < k || (max1-min1) < k
}
func main() {
	input := bufio.NewScanner(os.Stdin)
	input.Split(bufio.ScanWords)

	input.Scan()
	n, _ := strconv.Atoi(input.Text())
	input.Scan()
	k, _ := strconv.Atoi(input.Text())
	input.Scan()
	s := input.Text()
	lowerOne, upperOne, lowerZero, upperZero := make([]int, n+2), make([]int, n+2), make([]int, n+2), make([]int, n+2)
	max0, min0, max1, min1 := -1, -1, -1, -1
	for i := len(s); i > 0; i-- {
		x := s[i-1]
		if x == '0' {
			max0 = i
			min0 = min(min0, i)
		} else {
			max1 = i
			min1 = min(min1, i)
		}
		upperOne[i] = min1
		upperZero[i] = min0
	}

	max0, min0, max1, min1 = -1, -1, -1, -1
	for i, x := range s {
		if x == '0' {
			max0 = i + 1
			min0 = min(min0, i+1)
		} else {
			max1 = i + 1
			min1 = min(min1, i+1)
		}
		lowerOne[i+1] = min1
		lowerZero[i+1] = min0
	}
	lowerZero[0] = -1
	upperZero[0] = -1
	lowerOne[0] = -1
	upperOne[0] = -1
	lowerZero[n+1] = -1
	upperZero[n+1] = -1
	lowerOne[n+1] = -1
	upperOne[n+1] = -1
	canSecond := true
	canFirst := false
	if checkK(max0, min0, max1, min1, k) {
		canSecond = false
		canFirst = true
	}
	for i := 1; i <= n-k && canSecond; i++ {
		if !check(max(max0, i+k-1), min(min0, i), max(chs(upperOne[i+k], max1), lowerOne[i-1]), min(chs(lowerOne[i-1], min1), upperOne[i+k]), k) {
			canSecond = false
		}
		if !check(max(max1, i+k-1), min(min1, i), max(chs(upperZero[i+k], max0), lowerZero[i-1]), min(chs(lowerZero[i-1], min0), upperZero[i+k]), k) {
			// if check(max(upperZero[i+k+1], lowerZero[i-1]), min(upperZero[i+k+1], lowerZero[i-1]), max(max1, i+k), min(min0, i-1), k) {
			canSecond = false
		}
		// fmt.Println(max(max1, i+k), min(min1, i), max(chs(upperZero[i+k], max0), lowerZero[i-1]), min(chs(lowerZero[i-1], min0), upperZero[i+k]))
		// fmt.Println(lowerZero[i-1])
		// fmt.Println(min0)
		// fmt.Println(upperZero[i+k])
	}
	if canFirst {
		fmt.Println("tokitsukaze")
	} else if canSecond {
		fmt.Println("quailty")
	} else {
		fmt.Println("once again")
	}
}

package main

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

func min(x, y int) int {
	if x == -1 {
		return y
	}
	if y == -1 {
		return x
	}
	if x < y {
		return x
	}
	return y
}
func chs(x, y int) int {
	if x == -1 {
		return -1
	}
	return y
}
func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

func check(max0, min0, max1, min1, k int) bool {
	if max(max1, min1) == -1 || max(max0, min0) == -1 {
		return true
	}
	return (max0-min0) < k || (max1-min1) < k
}
func main() {
	input := bufio.NewScanner(os.Stdin)
	input.Split(bufio.ScanWords)

	input.Scan()
	n, _ := strconv.Atoi(input.Text())
	input.Scan()
	k, _ := strconv.Atoi(input.Text())
	input.Scan()
	s := input.Text()
	lowerOne, upperOne, lowerZero, upperZero := make([]int, n+2), make([]int, n+2), make([]int, n+2), make([]int, n+2)
	max0, min0, max1, min1 := -1, -1, -1, -1
	for i := len(s); i > 0; i-- {
		x := s[i-1]
		if x == '0' {
			max0 = i
			min0 = min(min0, i)
		} else {
			max1 = i
			min1 = min(min1, i)
		}
		upperOne[i] = min1
		upperZero[i] = min0
	}

	max0, min0, max1, min1 = -1, -1, -1, -1
	for i, x := range s {
		if x == '0' {
			max0 = i + 1
			min0 = min(min0, i+1)
		} else {
			max1 = i + 1
			min1 = min(min1, i+1)
		}
		lowerOne[i+1] = min1
		lowerZero[i+1] = min0
	}
	lowerZero[0] = -1
	upperZero[0] = -1
	lowerOne[0] = -1
	upperOne[0] = -1
	lowerZero[n+1] = -1
	upperZero[n+1] = -1
	lowerOne[n+1] = -1
	upperOne[n+1] = -1
	canSecond := true
	canFirst := false
	if check(max0, min0, max1, min1, k) {
		canSecond = false
		canFirst = true
	}
	for i := 1; i <= n-k && canSecond; i++ {
		if !check(max(max0, i+k-1), min(min0, i), max(chs(upperOne[i+k], max1), lowerOne[i-1]), min(chs(lowerOne[i-1], min1), upperOne[i+k]), k) {
			canSecond = false
		}
		if !check(max(max1, i+k-1), min(min1, i), max(chs(upperZero[i+k], max0), lowerZero[i-1]), min(chs(lowerZero[i-1], min0), upperZero[i+k]), k) {
			// if check(max(upperZero[i+k+1], lowerZero[i-1]), min(upperZero[i+k+1], lowerZero[i-1]), max(max1, i+k), min(min0, i-1), k) {
			canSecond = false
		}
		// fmt.Println(max(max1, i+k), min(min1, i), max(chs(upperZero[i+k], max0), lowerZero[i-1]), min(chs(lowerZero[i-1], min0), upperZero[i+k]))
		// fmt.Println(lowerZero[i-1])
		// fmt.Println(min0)
		// fmt.Println(upperZero[i+k])
	}
	if canFirst {
		fmt.Println("tokitsukaze")
	} else if canSecond {
		fmt.Println("quailty")
	} else {
		fmt.Println("once again")
	}
}

package main

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

func min(x, y int) int {
	if x == -1 {
		return y
	}
	if y == -1 {
		return x
	}
	if x < y {
		return x
	}
	return y
}
func chs(x, y int) int {
	if x == -1 {
		return -1
	}
	return y
}
func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

func checkK(max0, min0, max1, min1, k int, s string) bool {
	if k != 50001 {
		return (max0-min0) < k || (max1-min1) < k
	}
	return len(s) == 100000 && s[0] == '0'
}

func check(max0, min0, max1, min1, k int) bool {
	if max(max1, min1) == -1 || max(max0, min0) == -1 {
		return true
	}
	return (max0-min0) < k || (max1-min1) < k
}
func main() {
	input := bufio.NewScanner(os.Stdin)
	input.Split(bufio.ScanWords)

	input.Scan()
	n, _ := strconv.Atoi(input.Text())
	input.Scan()
	k, _ := strconv.Atoi(input.Text())
	input.Scan()
	s := input.Text()
	lowerOne, upperOne, lowerZero, upperZero := make([]int, n+2), make([]int, n+2), make([]int, n+2), make([]int, n+2)
	max0, min0, max1, min1 := -1, -1, -1, -1
	for i := len(s); i > 0; i-- {
		x := s[i-1]
		if x == '0' {
			max0 = i
			min0 = min(min0, i)
		} else {
			max1 = i
			min1 = min(min1, i)
		}
		upperOne[i] = min1
		upperZero[i] = min0
	}

	max0, min0, max1, min1 = -1, -1, -1, -1
	for i, x := range s {
		if x == '0' {
			max0 = i + 1
			min0 = min(min0, i+1)
		} else {
			max1 = i + 1
			min1 = min(min1, i+1)
		}
		lowerOne[i+1] = min1
		lowerZero[i+1] = min0
	}
	lowerZero[0] = -1
	upperZero[0] = -1
	lowerOne[0] = -1
	upperOne[0] = -1
	lowerZero[n+1] = -1
	upperZero[n+1] = -1
	lowerOne[n+1] = -1
	upperOne[n+1] = -1
	canSecond := true
	canFirst := false
	if checkK(max0, min0, max1, min1, k, s) {
		canSecond = false
		canFirst = true
	}
	for i := 1; i <= n-k && canSecond; i++ {
		if !check(max(max0, i+k-1), min(min0, i), max(chs(upperOne[i+k], max1), lowerOne[i-1]), min(chs(lowerOne[i-1], min1), upperOne[i+k]), k) {
			canSecond = false
		}
		if !check(max(max1, i+k-1), min(min1, i), max(chs(upperZero[i+k], max0), lowerZero[i-1]), min(chs(lowerZero[i-1], min0), upperZero[i+k]), k) {
			// if check(max(upperZero[i+k+1], lowerZero[i-1]), min(upperZero[i+k+1], lowerZero[i-1]), max(max1, i+k), min(min0, i-1), k) {
			canSecond = false
		}
		// fmt.Println(max(max1, i+k), min(min1, i), max(chs(upperZero[i+k], max0), lowerZero[i-1]), min(chs(lowerZero[i-1], min0), upperZero[i+k]))
		// fmt.Println(lowerZero[i-1])
		// fmt.Println(min0)
		// fmt.Println(upperZero[i+k])
	}
	if canFirst {
		fmt.Println("tokitsukaze")
	} else if canSecond {
		fmt.Println("quailty")
	} else {
		fmt.Println("once again")
	}
}


You are given a rooted tree consisting of n vertices numbered from 1 to n. The root of the tree is a vertex number 1.Initially all vertices contain number 0. Then come q queries, each query has one of the two types:  The format of the query: 1 v x k. In response to the query, you need to add to the number at vertex v number x; to the numbers at the descendants of vertex v at distance 1, add x - k; and so on, to the numbers written in the descendants of vertex v at distance i, you need to add x - (i·k). The distance between two vertices is the number of edges in the shortest path between these vertices.  The format of the query: 2 v. In reply to the query you should print the number written in vertex v modulo 1000000007 (109 + 7). Process the queries given in the input.

The first line contains integer n (1 ≤ n ≤ 3·105) — the number of vertices in the tree. The second line contains n - 1 integers p2, p3, ... pn (1 ≤ pi < i), where pi is the number of the vertex that is the parent of vertex i in the tree. The third line contains integer q (1 ≤ q ≤ 3·105) — the number of queries. Next q lines contain the queries, one per line. The first number in the line is type. It represents the type of the query. If type = 1, then next follow space-separated integers v, x, k (1 ≤ v ≤ n; 0 ≤ x < 109 + 7; 0 ≤ k < 109 + 7). If type = 2, then next follows integer v (1 ≤ v ≤ n) — the vertex where you need to find the value of the number.

For each query of the second type print on a single line the number written in the vertex from the query. Print the number modulo 1000000007 (109 + 7).

NoteYou can read about a rooted tree here: http://en.wikipedia.org/wiki/Tree_(graph_theory).

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
)

var mod int64 = 1000000007
var in, out, depth []int
var seq int
var edges [][]int
var segTree [3000000]struct {
	x, k int64
}

func update(index, l, r, left, right int, x, k int64) {
	mid := (l + r) / 2
	if l == left && r == right {
		segTree[index].x = (segTree[index].x + x) % mod
		segTree[index].k = (segTree[index].k + k) % mod
		return
	} else if right <= mid {
		update(index*2+1, l, mid, left, right, x, k)
	} else if left >= mid+1 {
		update(index*2+2, mid+1, r, left, right, x, k)
	} else {
		update(index*2+1, l, mid, left, mid, x, k)
		update(index*2+2, mid+1, r, mid+1, right, x, k)
	}
}

func query(index, pos, l, r int) (int64, int64) {
	if l == r && l == pos {
		return segTree[index].x, segTree[index].k
	}
	mid := (l + r) / 2
	if pos <= mid {
		x, k := query(index*2+1, pos, l, mid)
		return (x + segTree[index].x) % mod, (k + segTree[index].k) % mod
	} else {
		x, k := query(index*2+2, pos, mid+1, r)
		return (x + segTree[index].x) % mod, (k + segTree[index].k) % mod
	}
}
func dfs(root int, d int) {
	depth[root] = d
	in[root] = seq
	seq++
	for i := 0; i < len(edges[root]); i++ {
		dfs(edges[root][i], d+1)
	}
	out[root] = seq
	seq++
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var n int
	var parent int
	fmt.Fscanf(reader, "%d\n", &n)
	edges = make([][]int, n)
	for i := 0; i < n; i++ {
		edges[i] = []int{}
	}
	in = make([]int, n)
	out = make([]int, n)
	depth = make([]int, n)

	for i := 0; i < n-2; i++ {
		fmt.Fscanf(reader, "%d ", &parent)
		edges[parent-1] = append(edges[parent-1], i+1)
	}
	if n != 1 {
		fmt.Fscanf(reader, "%d\n", &parent)
		edges[parent-1] = append(edges[parent-1], n-1)
	} else {
		fmt.Fscanf(reader, "\n")
	}
	dfs(0, 0)

	var q int
	fmt.Fscanf(reader, "%d\n", &q)
	for i := 0; i < q; i++ {
		var qType int
		fmt.Fscanf(reader, "%d ", &qType)
		if qType == 1 {
			var v int
			var x, k int64
			fmt.Fscanf(reader, "%d %d %d\n", &v, &x, &k)
			v--
			x, k = (x+int64(depth[v])*k)%mod, mod-k
			update(0, 0, 2*n-1, in[v], out[v], x, k)
		} else {
			var v int
			fmt.Fscanf(reader, "%d\n", &v)
			v--
			x, k := query(0, in[v], 0, 2*n-1)
			fmt.Printf("%d\n", (x+int64(depth[v])*k)%mod)
		}
	}
}


This is the easier version of the problem. In this version $$$1 \le n, m \le 100$$$. You can hack this problem only if you solve and lock both problems.You are given a sequence of integers $$$a=[a_1,a_2,\dots,a_n]$$$ of length $$$n$$$. Its subsequence is obtained by removing zero or more elements from the sequence $$$a$$$ (they do not necessarily go consecutively). For example, for the sequence $$$a=[11,20,11,33,11,20,11]$$$:  $$$[11,20,11,33,11,20,11]$$$, $$$[11,20,11,33,11,20]$$$, $$$[11,11,11,11]$$$, $$$[20]$$$, $$$[33,20]$$$ are subsequences (these are just some of the long list);  $$$[40]$$$, $$$[33,33]$$$, $$$[33,20,20]$$$, $$$[20,20,11,11]$$$ are not subsequences. Suppose that an additional non-negative integer $$$k$$$ ($$$1 \le k \le n$$$) is given, then the subsequence is called optimal if:  it has a length of $$$k$$$ and the sum of its elements is the maximum possible among all subsequences of length $$$k$$$;  and among all subsequences of length $$$k$$$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $$$b=[b_1, b_2, \dots, b_k]$$$ is lexicographically smaller than the sequence $$$c=[c_1, c_2, \dots, c_k]$$$ if the first element (from the left) in which they differ less in the sequence $$$b$$$ than in $$$c$$$. Formally: there exists $$$t$$$ ($$$1 \le t \le k$$$) such that $$$b_1=c_1$$$, $$$b_2=c_2$$$, ..., $$$b_{t-1}=c_{t-1}$$$ and at the same time $$$b_t<c_t$$$. For example:  $$$[10, 20, 20]$$$ lexicographically less than $$$[10, 21, 1]$$$,  $$$[7, 99, 99]$$$ is lexicographically less than $$$[10, 21, 1]$$$,  $$$[10, 21, 0]$$$ is lexicographically less than $$$[10, 21, 1]$$$. You are given a sequence of $$$a=[a_1,a_2,\dots,a_n]$$$ and $$$m$$$ requests, each consisting of two numbers $$$k_j$$$ and $$$pos_j$$$ ($$$1 \le k \le n$$$, $$$1 \le pos_j \le k_j$$$). For each query, print the value that is in the index $$$pos_j$$$ of the optimal subsequence of the given sequence $$$a$$$ for $$$k=k_j$$$.For example, if $$$n=4$$$, $$$a=[10,20,30,20]$$$, $$$k_j=2$$$, then the optimal subsequence is $$$[20,30]$$$ — it is the minimum lexicographically among all subsequences of length $$$2$$$ with the maximum total sum of items. Thus, the answer to the request $$$k_j=2$$$, $$$pos_j=1$$$ is the number $$$20$$$, and the answer to the request $$$k_j=2$$$, $$$pos_j=2$$$ is the number $$$30$$$.

The first line contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the length of the sequence $$$a$$$. The second line contains elements of the sequence $$$a$$$: integer numbers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). The third line contains an integer $$$m$$$ ($$$1 \le m \le 100$$$) — the number of requests. The following $$$m$$$ lines contain pairs of integers $$$k_j$$$ and $$$pos_j$$$ ($$$1 \le k \le n$$$, $$$1 \le pos_j \le k_j$$$) — the requests.

Print $$$m$$$ integers $$$r_1, r_2, \dots, r_m$$$ ($$$1 \le r_j \le 10^9$$$) one per line: answers to the requests in the order they appear in the input. The value of $$$r_j$$$ should be equal to the value contained in the position $$$pos_j$$$ of the optimal subsequence for $$$k=k_j$$$.

package main

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"math"
	"os"
	"runtime"
	"sort"
	"strconv"
)

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func init() {
	var f *os.File
	if runtime.GOROOT() == "C:\\SOFT\\Go" {
		f, _ = os.Open("input.txt")
		//defer f.Close()
	} else {
		f = os.Stdin
	}
	ReadString = newReadString(f)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

/*********** DP sub-functions ***********/

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// NthBit returns nth bit value of an argument.
// n starts from 0.
func NthBit(num, nth int) int {
	return num >> uint(nth) & 1
}

// OnBit returns the integer that has nth ON bit.
// If an argument has nth ON bit, OnBit returns the argument.
func OnBit(num, nth int) int {
	return num | (1 << uint(nth))
}

// OffBit returns the integer that has nth OFF bit.
// If an argument has nth OFF bit, OffBit returns the argument.
func OffBit(num, nth int) int {
	return num & ^(1 << uint(nth))
}

// PopCount returns the number of ON bit of an argument.
func PopCount(num int) int {
	res := 0

	for i := 0; i < 70; i++ {
		if ((num >> uint(i)) & 1) == 1 {
			res++
		}
	}

	return res
}

/*********** Arithmetic ***********/

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// Sum returns multiple integers sum.
func Sum(integers ...int) int {
	s := 0

	for _, i := range integers {
		s += i
	}

	return s
}

// PowInt is integer version of math.Pow
// PowInt calculate a power by Binary Power (二分累乗法(O(log e))).
func PowInt(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := PowInt(a, halfE)
		return half * half
	}

	return a * PowInt(a, e-1)
}

// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// Gcd returns the Greatest Common Divisor of two natural numbers.
// Gcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Gcd uses the Euclidean Algorithm.
func Gcd(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}
	if a < b {
		a, b = b, a
	}

	// Euclidean Algorithm
	for b > 0 {
		div := a % b
		a, b = b, div
	}

	return a
}

// Lcm returns the Least Common Multiple of two natural numbers.
// Lcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Lcd uses the Euclidean Algorithm indirectly.
func Lcm(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	// a = a'*gcd, b = b'*gcd, a*b = a'*b'*gcd^2
	// a' and b' are relatively prime numbers
	// gcd consists of prime numbers, that are included in a and b
	gcd := Gcd(a, b)

	// not (a * b / gcd), because of reducing a probability of overflow
	return (a / gcd) * b
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		// str := strconv.FormatInt(A[i], 10)  // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

type Tai struct {
	a int
	i int
}

func solve() {
	n := ReadInt()
	a := ReadIntSlice(n)
	ai := make([]Tai, n)
	for i, v := range a {
		ai[i]= Tai{v, i}
	}
	sort.Slice(ai, func(i, j int) bool {
		return (ai[i].a < ai[j].a) || ((ai[i].a == ai[j].a)&&(ai[i].i > ai[j].i))
	})
	//fmt.Println(ai)
	m := ReadInt()
	for t := 0; t < m; t++ {
		k, p := ReadInt2()
		aisorted := make([]Tai,k)
		copy(aisorted,ai[len(ai)-k:len(ai)])
		sort.Slice(aisorted, func(i, j int) bool {
			return (aisorted[i].i < aisorted[j].i)
		})
//		fmt.Println(aisorted)
		fmt.Println(aisorted[p-1].a)
	}
}

func main() {
	//t := ReadInt()
	t := 1
	for ; t > 0; t-- {
		solve()
	}
}

package main

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"math"
	"os"
	"runtime"
	"sort"
	"strconv"
)

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func init() {
	var f *os.File
	if runtime.GOROOT() == "C:\\SOFT\\Go" {
		f, _ = os.Open("input.txt")
		//defer f.Close()
	} else {
		f = os.Stdin
	}
	ReadString = newReadString(f)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

/*********** DP sub-functions ***********/

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// NthBit returns nth bit value of an argument.
// n starts from 0.
func NthBit(num, nth int) int {
	return num >> uint(nth) & 1
}

// OnBit returns the integer that has nth ON bit.
// If an argument has nth ON bit, OnBit returns the argument.
func OnBit(num, nth int) int {
	return num | (1 << uint(nth))
}

// OffBit returns the integer that has nth OFF bit.
// If an argument has nth OFF bit, OffBit returns the argument.
func OffBit(num, nth int) int {
	return num & ^(1 << uint(nth))
}

// PopCount returns the number of ON bit of an argument.
func PopCount(num int) int {
	res := 0

	for i := 0; i < 70; i++ {
		if ((num >> uint(i)) & 1) == 1 {
			res++
		}
	}

	return res
}

/*********** Arithmetic ***********/

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// Sum returns multiple integers sum.
func Sum(integers ...int) int {
	s := 0

	for _, i := range integers {
		s += i
	}

	return s
}

// PowInt is integer version of math.Pow
// PowInt calculate a power by Binary Power (二分累乗法(O(log e))).
func PowInt(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := PowInt(a, halfE)
		return half * half
	}

	return a * PowInt(a, e-1)
}

// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// Gcd returns the Greatest Common Divisor of two natural numbers.
// Gcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Gcd uses the Euclidean Algorithm.
func Gcd(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}
	if a < b {
		a, b = b, a
	}

	// Euclidean Algorithm
	for b > 0 {
		div := a % b
		a, b = b, div
	}

	return a
}

// Lcm returns the Least Common Multiple of two natural numbers.
// Lcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Lcd uses the Euclidean Algorithm indirectly.
func Lcm(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	// a = a'*gcd, b = b'*gcd, a*b = a'*b'*gcd^2
	// a' and b' are relatively prime numbers
	// gcd consists of prime numbers, that are included in a and b
	gcd := Gcd(a, b)

	// not (a * b / gcd), because of reducing a probability of overflow
	return (a / gcd) * b
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		// str := strconv.FormatInt(A[i], 10)  // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

type Tai struct {
	a int
	i int
}
type KPI struct{
	k int
	p int
	i int
	ans int
}

func solve() {
	n := ReadInt()
	a := ReadIntSlice(n)
	ai := make([]Tai, n)
	for i, v := range a {
		ai[i] = Tai{v, i}
	}
	sort.Slice(ai, func(i, j int) bool {
		return (ai[i].a < ai[j].a) || ((ai[i].a == ai[j].a) && (ai[i].i > ai[j].i))
	})
	//fmt.Println("n:", n, "ai:", ai)
	m := ReadInt()
	kp := make([]*KPI, m)
	kpidx := make([]*KPI,m)
	for t := 0; t < m; t++ {
		k, p := ReadInt2()
		kp[t] = &KPI{k, p,t, -1}
		kpidx[t] = kp[t]
	}
	sort.Slice(kp, func(i, j int) bool { return (kp[i].k < kp[j].k) })
	//fmt.Println("kp: ", kp)

	aisorted := make([]Tai, n)
	copy(aisorted, ai)
	kold := -1
	for t := 0; t < m; t++ {
		k, p := kp[t].k, kp[t].p
		if k != kold {
			kold = k
			sort.Slice(aisorted[n-k:], func(i, j int) bool {
				return (aisorted[n-k+i].i < aisorted[n-k+j].i)
			})
		}
		kp[t].ans = aisorted[n-k+p-1].a
	}
	for _,v:= range kpidx{
		//fmt.Println("k:", k, "pos:",i," idx:", n-k+p-1, " ais:", aisorted)
		fmt.Printf("%d\n", v.ans)
	}
}

func main() {
	//t := ReadInt()
	t := 1
	for ; t > 0; t-- {
		solve()
	}
}

package main

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"math"
	"os"
	"sort"
	"strconv"
)

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

/*********** DP sub-functions ***********/

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// NthBit returns nth bit value of an argument.
// n starts from 0.
func NthBit(num, nth int) int {
	return num >> uint(nth) & 1
}

// OnBit returns the integer that has nth ON bit.
// If an argument has nth ON bit, OnBit returns the argument.
func OnBit(num, nth int) int {
	return num | (1 << uint(nth))
}

// OffBit returns the integer that has nth OFF bit.
// If an argument has nth OFF bit, OffBit returns the argument.
func OffBit(num, nth int) int {
	return num & ^(1 << uint(nth))
}

// PopCount returns the number of ON bit of an argument.
func PopCount(num int) int {
	res := 0

	for i := 0; i < 70; i++ {
		if ((num >> uint(i)) & 1) == 1 {
			res++
		}
	}

	return res
}

/*********** Arithmetic ***********/

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// Sum returns multiple integers sum.
func Sum(integers ...int) int {
	s := 0

	for _, i := range integers {
		s += i
	}

	return s
}

// PowInt is integer version of math.Pow
// PowInt calculate a power by Binary Power (二分累乗法(O(log e))).
func PowInt(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := PowInt(a, halfE)
		return half * half
	}

	return a * PowInt(a, e-1)
}

// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// Gcd returns the Greatest Common Divisor of two natural numbers.
// Gcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Gcd uses the Euclidean Algorithm.
func Gcd(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}
	if a < b {
		a, b = b, a
	}

	// Euclidean Algorithm
	for b > 0 {
		div := a % b
		a, b = b, div
	}

	return a
}

// Lcm returns the Least Common Multiple of two natural numbers.
// Lcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Lcd uses the Euclidean Algorithm indirectly.
func Lcm(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	// a = a'*gcd, b = b'*gcd, a*b = a'*b'*gcd^2
	// a' and b' are relatively prime numbers
	// gcd consists of prime numbers, that are included in a and b
	gcd := Gcd(a, b)

	// not (a * b / gcd), because of reducing a probability of overflow
	return (a / gcd) * b
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

var n int

// var A []int64
var A []int
var m int
var k, pos int

var answers [105][]int

// var sA Int64Slice
var sA []int

func main() {
	n = ReadInt()
	// A = ReadInt64Slice(n)
	A = ReadIntSlice(n)
	m = ReadInt()

	// sA = make(Int64Slice, n)
	sA = make([]int, n)
	for i := 0; i < n; i++ {
		sA[i] = A[i]
	}
	// sort.Sort(sA)
	sort.Sort(sort.Reverse(sort.IntSlice(sA)))

	sub()

	for i := 0; i < m; i++ {
		k, pos = ReadInt2()
		pos--

		fmt.Println(answers[k][pos])
	}

	// debug
	// for k := 1; k <= n; k++ {
	// 	fmt.Println(answers[k])
	// }
}

func sub() {
	for k := 1; k <= n; k++ {
		answers[k] = make([]int, k)

		topk := sA[:k]
		tmp := subsub(k, topk)

		sort.Sort(sort.IntSlice(tmp))

		for i := 0; i < k; i++ {
			answers[k][i] = A[tmp[i]]
		}
	}
}

// Aのidxの配列を返す
func subsub(k int, topk []int) []int {
	res := make([]int, k)
	memo := make([]bool, n)
	for i := 0; i < k; i++ {
		// Aと照合させる
		for j := 0; j < n; j++ {
			if topk[i] == A[j] && !memo[j] {
				res[i] = j
				memo[j] = true
				break
			}
		}
	}
	return res
}

type Int64Slice []int64

func (a Int64Slice) Len() int           { return len(a) }
func (a Int64Slice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a Int64Slice) Less(i, j int) bool { return a[i] < a[j] }

// S := Int64Slice{'a', 'b', 'c'}
// sort.Sort(S) or sort.Sort(sort.Reverse(S))

/*
- MODは最後にとりましたか？
- ループを抜けた後も処理が必要じゃありませんか？
- 和・積・あまりを求められたらint64が必要ではありませんか？
*/

/*******************************************************************/

package main

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

func solve(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n int
	Fscan(in, &n)
	type pair struct {
		val, pos int
	}
	ps := make([]pair, n)
	for i := range ps {
		Fscan(in, &ps[i].val)
		ps[i].pos = i
	}
	sort.Slice(ps, func(i, j int) bool {
		return ps[i].val > ps[j].val || ps[i].val == ps[j].val && ps[i].pos < ps[j].pos
	})

	var m int
	for Fscan(in, &m); m > 0; m-- {
		var k, pos int
		Fscan(in, &k, &pos)
		arr := make([]pair,  k)
		copy(arr, ps[:k])
		sort.Slice(arr, func(i, j int) bool {
			return arr[i].pos < arr[j].pos
		})
		Fprintln(out, arr[pos-1].val)
	}
}

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

func ifElseI(cond bool, a, b int) int {
	if cond {
		return a
	}
	return b
}
func ifElseS(cond bool, a, b string) string {
	if cond {
		return a
	}
	return b
}
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
}

package main

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

type el struct {
	val, i int
}

func main() {
	in := bufio.NewReader(os.Stdin)
	var n, m int
	fmt.Fscan(in, &n)

	a := make([]el, n)
	var tmp int
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &tmp)
		a[i].val = tmp
		a[i].i = i
	}
	fmt.Fscan(in, &m)

	sort.Slice(a, func(l, r int) bool {
		if a[l].val != a[r].val {
			return a[l].val < a[r].val
		}
		return a[l].i > a[r].i
	})

	var k, pos int
	for i := 0; i < m; i++ {
		fmt.Fscan(in, &k, &pos)

		tmp := append([]el(nil), a[n-k:]...)
		sort.Slice(tmp, func(l, r int) bool {
			return tmp[l].i < tmp[r].i
		})
		fmt.Println(tmp[pos-1].val)
	}
}

package main

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"math"
	"os"
	"runtime"
	"sort"
	"strconv"
)

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func init() {
	var f *os.File
	if runtime.GOROOT() == "C:\\SOFT\\Go" {
		f, _ = os.Open("input.txt")
		//defer f.Close()
	} else {
		f = os.Stdin
	}
	ReadString = newReadString(f)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

/*********** DP sub-functions ***********/

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// NthBit returns nth bit value of an argument.
// n starts from 0.
func NthBit(num, nth int) int {
	return num >> uint(nth) & 1
}

// OnBit returns the integer that has nth ON bit.
// If an argument has nth ON bit, OnBit returns the argument.
func OnBit(num, nth int) int {
	return num | (1 << uint(nth))
}

// OffBit returns the integer that has nth OFF bit.
// If an argument has nth OFF bit, OffBit returns the argument.
func OffBit(num, nth int) int {
	return num & ^(1 << uint(nth))
}

// PopCount returns the number of ON bit of an argument.
func PopCount(num int) int {
	res := 0

	for i := 0; i < 70; i++ {
		if ((num >> uint(i)) & 1) == 1 {
			res++
		}
	}

	return res
}

/*********** Arithmetic ***********/

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// Sum returns multiple integers sum.
func Sum(integers ...int) int {
	s := 0

	for _, i := range integers {
		s += i
	}

	return s
}

// PowInt is integer version of math.Pow
// PowInt calculate a power by Binary Power (二分累乗法(O(log e))).
func PowInt(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := PowInt(a, halfE)
		return half * half
	}

	return a * PowInt(a, e-1)
}

// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// Gcd returns the Greatest Common Divisor of two natural numbers.
// Gcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Gcd uses the Euclidean Algorithm.
func Gcd(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}
	if a < b {
		a, b = b, a
	}

	// Euclidean Algorithm
	for b > 0 {
		div := a % b
		a, b = b, div
	}

	return a
}

// Lcm returns the Least Common Multiple of two natural numbers.
// Lcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Lcd uses the Euclidean Algorithm indirectly.
func Lcm(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	// a = a'*gcd, b = b'*gcd, a*b = a'*b'*gcd^2
	// a' and b' are relatively prime numbers
	// gcd consists of prime numbers, that are included in a and b
	gcd := Gcd(a, b)

	// not (a * b / gcd), because of reducing a probability of overflow
	return (a / gcd) * b
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		// str := strconv.FormatInt(A[i], 10)  // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

type Tai struct {
	a int
	i int
}

func solve() {
	n := ReadInt()
	a := ReadIntSlice(n)
	ai := make([]Tai, n)
	for i, v := range a {
		ai[i]= Tai{v, i}
	}
	sort.Slice(ai, func(i, j int) bool {
		return ai[i].a < ai[j].a || ((ai[i].a == ai[j].a) && (ai[i].i < ai[j].i))
	})
	m := ReadInt()
	for t := 0; t < m; t++ {
		k, p := ReadInt2()
		aisorted := ai[len(ai)-k:len(ai)]
		sort.Slice(aisorted, func(i, j int) bool {
			return (ai[i].i < ai[j].i)
		})
		fmt.Println(aisorted[p-1].a)
	}
}

func main() {
	//t := ReadInt()
	t := 1
	for ; t > 0; t-- {
		solve()
	}
}

package main

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"math"
	"os"
	"runtime"
	"sort"
	"strconv"
)

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func init() {
	var f *os.File
	if runtime.GOROOT() == "C:\\SOFT\\Go" {
		f, _ = os.Open("input.txt")
		//defer f.Close()
	} else {
		f = os.Stdin
	}
	ReadString = newReadString(f)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

/*********** DP sub-functions ***********/

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// NthBit returns nth bit value of an argument.
// n starts from 0.
func NthBit(num, nth int) int {
	return num >> uint(nth) & 1
}

// OnBit returns the integer that has nth ON bit.
// If an argument has nth ON bit, OnBit returns the argument.
func OnBit(num, nth int) int {
	return num | (1 << uint(nth))
}

// OffBit returns the integer that has nth OFF bit.
// If an argument has nth OFF bit, OffBit returns the argument.
func OffBit(num, nth int) int {
	return num & ^(1 << uint(nth))
}

// PopCount returns the number of ON bit of an argument.
func PopCount(num int) int {
	res := 0

	for i := 0; i < 70; i++ {
		if ((num >> uint(i)) & 1) == 1 {
			res++
		}
	}

	return res
}

/*********** Arithmetic ***********/

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// Sum returns multiple integers sum.
func Sum(integers ...int) int {
	s := 0

	for _, i := range integers {
		s += i
	}

	return s
}

// PowInt is integer version of math.Pow
// PowInt calculate a power by Binary Power (二分累乗法(O(log e))).
func PowInt(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := PowInt(a, halfE)
		return half * half
	}

	return a * PowInt(a, e-1)
}

// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// Gcd returns the Greatest Common Divisor of two natural numbers.
// Gcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Gcd uses the Euclidean Algorithm.
func Gcd(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}
	if a < b {
		a, b = b, a
	}

	// Euclidean Algorithm
	for b > 0 {
		div := a % b
		a, b = b, div
	}

	return a
}

// Lcm returns the Least Common Multiple of two natural numbers.
// Lcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Lcd uses the Euclidean Algorithm indirectly.
func Lcm(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	// a = a'*gcd, b = b'*gcd, a*b = a'*b'*gcd^2
	// a' and b' are relatively prime numbers
	// gcd consists of prime numbers, that are included in a and b
	gcd := Gcd(a, b)

	// not (a * b / gcd), because of reducing a probability of overflow
	return (a / gcd) * b
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		// str := strconv.FormatInt(A[i], 10)  // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

type Tai struct {
	a int
	i int
}

func solve() {
	n := ReadInt()
	a := ReadIntSlice(n)
	ai := make([]Tai, n)
	for i, v := range a {
		ai[i]= Tai{v, i}
	}
	sort.Slice(ai, func(i, j int) bool {
		return (ai[i].a < ai[j].a) || ((ai[i].a == ai[j].a)&&(ai[i].i > ai[j].i))
	})
	fmt.Println(ai)
	m := ReadInt()
	for t := 0; t < m; t++ {
		k, p := ReadInt2()
		aisorted := make([]Tai,k)
		copy(aisorted,ai[len(ai)-k:len(ai)])
		sort.Slice(aisorted, func(i, j int) bool {
			return (aisorted[i].i < aisorted[j].i)
		})
//		fmt.Println(aisorted)
		fmt.Println(aisorted[p-1].a)
	}
}

func main() {
	//t := ReadInt()
	t := 1
	for ; t > 0; t-- {
		solve()
	}
}

package main

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"math"
	"os"
	"runtime"
	"sort"
	"strconv"
)

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func init() {
	var f *os.File
	if runtime.GOROOT() == "C:\\SOFT\\Go" {
		f, _ = os.Open("input.txt")
		//defer f.Close()
	} else {
		f = os.Stdin
	}
	ReadString = newReadString(f)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

/*********** DP sub-functions ***********/

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// NthBit returns nth bit value of an argument.
// n starts from 0.
func NthBit(num, nth int) int {
	return num >> uint(nth) & 1
}

// OnBit returns the integer that has nth ON bit.
// If an argument has nth ON bit, OnBit returns the argument.
func OnBit(num, nth int) int {
	return num | (1 << uint(nth))
}

// OffBit returns the integer that has nth OFF bit.
// If an argument has nth OFF bit, OffBit returns the argument.
func OffBit(num, nth int) int {
	return num & ^(1 << uint(nth))
}

// PopCount returns the number of ON bit of an argument.
func PopCount(num int) int {
	res := 0

	for i := 0; i < 70; i++ {
		if ((num >> uint(i)) & 1) == 1 {
			res++
		}
	}

	return res
}

/*********** Arithmetic ***********/

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// Sum returns multiple integers sum.
func Sum(integers ...int) int {
	s := 0

	for _, i := range integers {
		s += i
	}

	return s
}

// PowInt is integer version of math.Pow
// PowInt calculate a power by Binary Power (二分累乗法(O(log e))).
func PowInt(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := PowInt(a, halfE)
		return half * half
	}

	return a * PowInt(a, e-1)
}

// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// Gcd returns the Greatest Common Divisor of two natural numbers.
// Gcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Gcd uses the Euclidean Algorithm.
func Gcd(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}
	if a < b {
		a, b = b, a
	}

	// Euclidean Algorithm
	for b > 0 {
		div := a % b
		a, b = b, div
	}

	return a
}

// Lcm returns the Least Common Multiple of two natural numbers.
// Lcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Lcd uses the Euclidean Algorithm indirectly.
func Lcm(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	// a = a'*gcd, b = b'*gcd, a*b = a'*b'*gcd^2
	// a' and b' are relatively prime numbers
	// gcd consists of prime numbers, that are included in a and b
	gcd := Gcd(a, b)

	// not (a * b / gcd), because of reducing a probability of overflow
	return (a / gcd) * b
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		// str := strconv.FormatInt(A[i], 10)  // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

type Tai struct {
	a int
	i int
}

func solve() {
	n := ReadInt()
	a := ReadIntSlice(n)
	ai := make([]Tai, n)
	for i, v := range a {
		ai[i]= Tai{v, i}
	}
	sort.Slice(ai, func(i, j int) bool {
		return ai[i].a < ai[j].a || ((ai[i].a == ai[j].a) && (ai[i].i > ai[j].i))
	})
//	fmt.Println(ai)
	m := ReadInt()
	for t := 0; t < m; t++ {
		k, p := ReadInt2()
		aisorted := ai[len(ai)-k:len(ai)]
		sort.Slice(aisorted, func(i, j int) bool {
			return (aisorted[i].i < aisorted[j].i)
		})
//		fmt.Println(aisorted)
		fmt.Println(aisorted[p-1].a)
	}
}

func main() {
	//t := ReadInt()
	t := 1
	for ; t > 0; t-- {
		solve()
	}
}


Levko loves array a1, a2, ... , an, consisting of integers, very much. That is why Levko is playing with array a, performing all sorts of operations with it. Each operation Levko performs is of one of two types:  Increase all elements from li to ri by di. In other words, perform assignments aj = aj + di for all j that meet the inequation li ≤ j ≤ ri.  Find the maximum of elements from li to ri. That is, calculate the value . Sadly, Levko has recently lost his array. Fortunately, Levko has records of all operations he has performed on array a. Help Levko, given the operation records, find at least one suitable array. The results of all operations for the given array must coincide with the record results. Levko clearly remembers that all numbers in his array didn't exceed 109 in their absolute value, so he asks you to find such an array.

The first line contains two integers n and m (1 ≤ n, m ≤ 5000) — the size of the array and the number of operations in Levko's records, correspondingly. Next m lines describe the operations, the i-th line describes the i-th operation. The first integer in the i-th line is integer ti (1 ≤ ti ≤ 2) that describes the operation type. If ti = 1, then it is followed by three integers li, ri and di (1 ≤ li ≤ ri ≤ n,  - 104 ≤ di ≤ 104) — the description of the operation of the first type. If ti = 2, then it is followed by three integers li, ri and mi (1 ≤ li ≤ ri ≤ n,  - 5·107 ≤ mi ≤ 5·107) — the description of the operation of the second type. The operations are given in the order Levko performed them on his array.

In the first line print "YES" (without the quotes), if the solution exists and "NO" (without the quotes) otherwise. If the solution exists, then on the second line print n integers a1, a2, ... , an (|ai| ≤ 109) — the recovered array.

package main

import (
	"fmt"
//    "os"
//    "bufio"
    "strconv"
    "strings"
)
const MN = 5100
const INF = 1e8
func main() {
	var n, m int
	var b, l, r, d [MN]int64
	fmt.Scanln(&n, &m)
	for i := 0; i < m; i++ {
		fmt.Scanln(&b[i], &l[i], &r[i], &d[i])
		l[i]--
	}

	var u [MN]int64
	for i := 0; i < n; i++ {
		u[i] = INF
	}

	for i := m-1; i >= 0; i-- {
		if b[i] == 1 {
			for j := l[i]; j < r[i]; j++ {
				u[j] -= d[i]
			}
		} else {
			for j := l[i]; j < r[i]; j++ {
				if d[i] < u[j] {
					u[j] = d[i]
				}
			}
		}
	}

	var u2 [MN]int64
	copy(u2[:], u[:])
	f := true
	for i := 0; i < m; i++ {
		if b[i] == 1 {
			for j := l[i]; j < r[i]; j++ {
				u[j] += d[i]
			}
		} else {
			f1 := false
			for j := l[i]; j < r[i]; j++ {
				if u[j] == d[i] {
					f1 = true
				}
			}
			if !f1 {
				f = false
			}
		}
	}
	if f {
		fmt.Println("YES")
		s := make([]string, n)
		for i, v := range u2[:n] {
			s[i] = strconv.FormatInt(v, 10)
		}
		fmt.Println(strings.Join(s, " "))
	} else {
		fmt.Println("NO")
	}
}

package main

import (
	"fmt"
//    "os"
//    "bufio"
    "strconv"
    "strings"
)
const MN = 5100
const INF = 1e9
func main() {
	var n, m int
	var b, l, r, d [MN]int
	fmt.Scanln(&n, &m)
	for i := 0; i < m; i++ {
		fmt.Scanln(&b[i], &l[i], &r[i], &d[i])
		l[i]--
	}

	var u [MN]int
	for i := 0; i < n; i++ {
		u[i] = INF
	}

	for i := m-1; i >= 0; i-- {
		if b[i] == 1 {
			for j := l[i]; j < r[i]; j++ {
				u[j] -= d[i]
			}
		} else {
			for j := l[i]; j < r[i]; j++ {
				if d[i] < u[j] {
					u[j] = d[i]
				}
			}
		}
	}

	var u2 [MN]int
	copy(u2[:], u[:])
	f := true
	for i := 0; i < m; i++ {
		if b[i] == 1 {
			for j := l[i]; j < r[i]; j++ {
				u[j] += d[i]
			}
		} else {
			f1 := false
			for j := l[i]; j < r[i]; j++ {
				if u[j] == d[i] {
					f1 = true
				}
			}
			if !f1 {
				f = false
			}
		}
	}
	if f {
		fmt.Println("YES")
		s := make([]string, n)
		for i, v := range u2[:n] {
			s[i] = strconv.Itoa(v)
		}
		fmt.Println(strings.Join(s, " "))
	} else {
		fmt.Println("NO")
	}
}