← Home
```go
package main

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

func alloc2D(n, m int) [][]int64 {
	flat := make([]int64, n*m)
	res := make([][]int64, n)
	for i := 0; i < n; i++ {
		res[i] = flat[i*m : (i+1)*m]
	}
	return res
}

type Interval struct {
	l, r int
}

type Action struct {
	L    int
	val  int
	ways int64
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buf := make([]byte, 1024*1024)
	scanner.Buffer(buf, 1024*1024)

	nextInt := func() int {
		scanner.Scan()
		res := 0
		for _, b := range scanner.Bytes() {
			res = res*10 + int(b-'0')
		}
		return res
	}

	nextInt64 := func() int64 {
		scanner.Scan()
		res := int64(0)
		for _, b := range scanner.Bytes() {
			res = res*10 + int64(b-'0')
		}
		return res
	}

	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	scanner.Scan()
	t := 0
	for _, b := range scanner.Bytes() {
		t = t*10 + int(b-'0')
	}

	for tc := 0; tc < t; tc++ {
		n := nextInt()
		c := nextInt()
		q := nextInt()

		p := make([]int, n+1)
		for i := 1; i <= n; i++ {
			p[i] = nextInt()
		}

		dp_s := alloc2D(n+2, c+1)
		for rem := 0; rem <= c; rem++ {
			dp_s[0][rem] = 1
			dp_s[1][rem] = 1
		}
		for sz := 2; sz <= n+1; sz++ {
			for rem := 0; rem <= c; rem++ {
				dp_s[sz][rem] = dp_s[sz-1][rem]
				for L := 2; L <= rem+1; L++ {
					if sz-L >= 0 {
						dp_s[sz][rem] += dp_s[sz-L][rem-(L-1)]
					}
				}
			}
		}

		dp := make([][]int64, n+2)
		for i := 1; i <= n+1; i++ {
			dp[i] = dp_s[n-i+1]
		}

		delta := alloc2D(n+2, c+1)
		for k := 1; k <= n; k++ {
			for rem := 0; rem <= c; rem++ {
				for L := 2; L <= rem+1; L++ {
					if k+L-1 <= n {
						if p[k+L-1] < p[k] {
							delta[k][rem] += dp[k+L][rem-(L-1)]
						}
					}
				}
			}
		}

		pref := alloc2D(n+2, c+1)
		for rem := 0; rem <= c; rem++ {
			for k := 1; k <= n; k++ {
				pref[k][rem] = pref[k-1][rem] + delta[k][rem]
			}
		}

		intervals := make([]Interval, 0, 4)
		actions := make([]Action, 0, 5)

		for qi := 0; qi < q; qi++ {
			i := nextInt()
			j := nextInt64()

			if j > dp[1][c] {
				fmt.Fprintln(writer, -1)
				continue
			}

			intervals = intervals[:0]
			idx0 := 1
			rem := c
			curr_j := j

			for idx0 <= n {
				C := curr_j + pref[idx0-1][rem]
				low := idx0 - 1
				high := n
				R := idx0 - 1

				for low <= high {
					mid := (low + high) / 2
					if C > pref[mid][rem] && C <= pref[mid][rem]+dp[mid+1][rem] {
						R = mid
						low = mid + 1
					} else {
						high = mid - 1
					}
				}

				if R == n {
					break
				}

				k := R + 1
				curr_j = C - pref[k-1][rem]

				actions = actions[:0]
				actions = append(actions, Action{L: 0, val: p[k], ways: dp[k+1][rem]})
				for L := 2; L <= rem+1; L++ {
					if k+L-1 <= n {
						actions = append(actions, Action{L: L, val: p[k+L-1], ways: dp[k+L][rem-(L-1)]})
					}
				}

				for x := 0; x < len(actions); x++ {
					for y := x + 1; y < len(actions); y++ {
						if actions[x].val > actions[y].val {
							actions[x], actions[y] = actions[y], actions[x]
						}
					}
				}

				for _, act := range actions {
					if curr_j <= act.ways {
						if act.L == 0 {
							idx0 = k + 1
						} else {
							intervals = append(intervals, Interval{l: k, r: k + act.L - 1})
							idx0 = k + act.L
							rem -= act.L - 1
						}
						break
					} else {
						curr_j -= act.ways
					}
				}
			}

			ans := p[i]
			for _, inv := range intervals {
				if i >= inv.l && i <= inv.r {
					ans = p[inv.r-(i-inv.l)]
					break
				}
			}
			fmt.Fprintln(writer, ans)
		}
	}
}
```