package main
import (
"bufio"
"io"
"os"
"strconv"
)
func main() {
data, _ := io.ReadAll(os.Stdin)
idx := 0
nextInt := func() int {
for idx < len(data) && (data[idx] < '0' || data[idx] > '9') {
idx++
}
val := 0
for idx < len(data) && data[idx] >= '0' && data[idx] <= '9' {
val = val*10 + int(data[idx]-'0')
idx++
}
return val
}
t := nextInt()
out := bufio.NewWriterSize(os.Stdout, 1<<20)
defer out.Flush()
for ; t > 0; t-- {
m := nextInt()
n := nextInt()
colMax := make([]int, n)
maxSecond := 0
for i := 0; i < m; i++ {
first, second := 0, 0
for j := 0; j < n; j++ {
x := nextInt()
if x > colMax[j] {
colMax[j] = x
}
if x >= first {
second = first
first = x
} else if x > second {
second = x
}
}
if second > maxSecond {
maxSecond = second
}
}
minCol := colMax[0]
for j := 1; j < n; j++ {
if colMax[j] < minCol {
minCol = colMax[j]
}
}
ans := minCol
if maxSecond < ans {
ans = maxSecond
}
out.WriteString(strconv.Itoa(ans))
out.WriteByte('\n')
}
}