← Home
package main

import (
	"io"
	"os"
)

func main() {
	buf, _ := io.ReadAll(os.Stdin)
	var pos int

	nextInt := func() int {
		for pos < len(buf) && (buf[pos] < '0' || buf[pos] > '9') {
			pos++
		}
		if pos >= len(buf) {
			return 0
		}
		res := 0
		for pos < len(buf) && buf[pos] >= '0' && buf[pos] <= '9' {
			res = res*10 + int(buf[pos]-'0')
			pos++
		}
		return res
	}

	t := nextInt()
	if t == 0 {
		return
	}

	out := make([]byte, 0, 64*t)

	for i := 0; i < t; i++ {
		n := nextInt()
		k := nextInt()

		freq := make([]int, n+1)
		for j := 0; j < n; j++ {
			freq[nextInt()]++
		}

		suff := make([]int, n+2)
		for j := n; j >= 1; j-- {
			suff[j] = suff[j+1] + freq[j]
		}

		for d := n; d >= 1; d-- {
			goodCount := 0
			if 4*d <= n {
				goodCount += suff[4*d]
			}
			if d <= n {
				goodCount += freq[d]
			}
			if 2*d <= n {
				goodCount += freq[2*d]
			}
			if 3*d <= n {
				goodCount += freq[3*d]
			}

			if goodCount >= n-k {
				out = appendInt(out, d)
				out = append(out, '\n')
				break
			}
		}
	}
	os.Stdout.Write(out)
}

func appendInt(b []byte, v int) []byte {
	if v == 0 {
		return append(b, '0')
	}
	var buf [20]byte
	i := 19
	for v > 0 {
		buf[i] = byte(v%10 + '0')
		v /= 10
		i--
	}
	return append(b, buf[i+1:]...)
}