description: Vlad has n friends, for each of whom he wants to buy one gift for the New Year.
  
  There are m shops in the city, in each of which he can buy a gift for any of his friends. If the j-th friend (1 ≤ j ≤ n) receives a gift bought in the shop with the number i (1 ≤ i ≤ m), then the friend receives p_{ij} units of joy. The rectangular table p_{ij} is given in the input.
  
  Vlad has time to visit at most n-1 shops (where n is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them.
  
  Let the j-th friend receive a_j units of joy from Vlad's gift. Let's find the value α=min\\{a_1, a_2, ..., a_n\}. Vlad's goal is to buy gifts so that the value of α is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends.
  
  For example, let m = 2, n = 2. Let the joy from the gifts that we can buy in the first shop: p_{11} = 1, p_{12}=2, in the second shop: p_{21} = 3, p_{22}=4.
  
  Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy 3, and for the second — bringing joy 4. In this case, the value α will be equal to min\{3, 4\} = 3
  
  Help Vlad choose gifts for his friends so that the value of α is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most n-1 shops (where n is the number of friends). In the shop, he can buy any number of gifts.
  
  Input
  
  The first line of the input contains an integer t (1 ≤ t ≤ 10^4) — the number of test cases in the input.
  
  An empty line is written before each test case. Then there is a line containing integers m and n (2 ≤ n, 2 ≤ n ⋅ m ≤ 10^5) separated by a space — the number of shops and the number of friends, where n ⋅ m is the product of n and m.
  
  Then m lines follow, each containing n numbers. The number in the i-th row of the j-th column p_{ij} (1 ≤ p_{ij} ≤ 10^9) is the joy of the product intended for friend number j in shop number i.
  
  It is guaranteed that the sum of the values n ⋅ m over all test cases in the test does not exceed 10^5.
  
  Output
  
  Print t lines, each line must contain the answer to the corresponding test case — the maximum possible value of α, where α is the minimum of the joys from a gift for all of Vlad's friends.
  
  Example
  
  Input
  
  
  5
  
  2 2
  1 2
  3 4
  
  4 3
  1 3 1
  3 1 1
  1 2 2
  1 1 3
  
  2 3
  5 3 4
  2 5 1
  
  4 2
  7 9
  8 1
  9 6
  10 8
  
  2 4
  6 5 2 1
  7 9 7 2
  
  
  Output
  
  
  3
  2
  4
  8
  2
solution: import io, os
  import sys 
  from sys import stdin
  
  from bisect import bisect_left, bisect_right
  from collections import defaultdict, deque, namedtuple
  from heapq import heappush, heappop
  from math import gcd, ceil, floor, factorial, sqrt
  from itertools import combinations, permutations
  
  input = sys.stdin.buffer.readline
  # input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
  # input = sys.stdin.readline
  
  def work():
      ans = 0 
  
      return 
  
  # sys.setrecursionlimit(200010)
  def main_11():
      test = int(input())
  
      for idt in range(test):
          _ = input()
          # n = int(input())
          m, n = map(int, input().split())
          # a = list(map(int, input().split()))
          max1, max2 = [0] * n, [0] * n 
          g = [[0] * n for _ in range(m)]
          for i in range(m):
              a = list(map(int, input().split()))
              for j in range(n):
                  g[i][j] = a[j]
                  if g[i][j] >= max1[j]:
                      max2[j] = max1[j]
                      max1[j] = g[i][j]
                  elif g[i][j] >= max2[j]:
                      max2[j] = g[i][j]
          
          ans = 0 
          if n - 1 >= m:
              ans = min(max1)
              print(ans)
              continue 
          
          s = [[[0] * 3 for _ in range(m)] for _ in range(n)]
          for i in range(n):
              for j in range(m):
                  # s[i].append([g[i][j], j])
                  s[i][j][0] = g[j][i]
                  s[i][j][1] = j 
                  s[i][j][2] = i 
          for i in range(n):
              s[i].sort(reverse=True)
          # print(*s, sep="\n")
  
          cnt = [0] * m 
          maxv = [0] * n
          for i in range(n):
              x, idx, pid = s[i][0]
              cnt[idx] += 1 
              maxv[pid] = max(maxv[pid], x)
          count = 0 
          for i in cnt:
              if i > 0:
                  count += 1 
          # print("info", maxv, cnt)
          if count <= n - 1:
              print(min(maxv))
              continue 
          
          ans = 0 
          ops = defaultdict(list)
          for j in range(1, m):
              for i in range(n):
                  x, idx, pid = s[i][j]
                  # print("store", idx, "person", pid, x)
                  cnt[idx] += 1 
                  ops[idx].append(pid)
                  if cnt[idx] > 1:
                      # t = maxv[pid]
                      # maxv[pid] = min(maxv[pid], x)
                      # ans = max(ans, min(maxv))
                      # maxv[pid] = t 
                      cnt[idx] -= 1 
                      t = float('inf')
                      for k in ops[idx]:
                          # print(f"pid = {j}, g = {g[idx][pid]}")
                          t = min(t, g[idx][k])
                      # print("info", j,  idx, t, pid, ops[idx])
                      t = min(min(maxv), t)
                      ans = max(t, ans)
          print(ans)
      return 
  
  
  def main():
      test = int(input())
  
      for idt in range(test):
          _ = input()
          m, n = map(int, input().split())
          store_person = [[0] * n for _ in range(m)]
          for i in range(m):
              a = list(map(int, input().split()))
              for j in range(n):
                  store_person[i][j] = a[j]
          person_store = [[] for _ in range(n)]
          for i in range(n):
              for j in range(m):
                  # person_store[i][j] = store_person[j][i]
                  person_store[i].append([store_person[j][i], j, i])
              person_store[i].sort(reverse=True)
          # print(*person_store, sep="\n")
          selected = set()
          maxs = [0] * n 
          for i in range(n):
              score, store, person = person_store[i][0]
              # print("person_store_item", person_store[i][0])
              # return 
              maxs[person] = max(maxs[person], score)
              selected.add(store)
          # print("maxs", maxs)
          if len(selected) <= n - 1:
              print(min(maxs))
              continue 
          
          ans = 0 
          for j in range(n):
              for k in range(j + 1, n):
                  cur = float('inf')
                  for i in range(n):
                      if i == j or i == k:
                          continue
                      cur = min(cur, maxs[i])
                  # print("info", j, k, cur)
                  opt = 0 
                  for i in range(m):
                      opt = max(opt, min(store_person[i][j], store_person[i][k]))
                  cur = min(cur, opt)
                  ans = max(ans, cur)
          print(ans)
      
      return 
  
  def main_whd():
      test = int(input())
  
      for _ in range(test):
          _ = input()
          m, n = map(int, input().split())
          shop_person = [[0] * n for _ in range(m)]
          for i in range(m):
              t = list(map(int, input().split()))
              for j in range(n):
                  shop_person[i][j] = t[j]
          
          def check(x):
              cnt = [0] * m 
  
              for i in range(n):
                  t = 0 
                  for j in range(m):
                      if shop_person[j][i] >= x:
                          cnt[j] += 1 
                          t += 1 
                  if t == 0:
                      return False 
              
              if max(cnt) >= 2:
                  return True 
              return False
  
          l, r = 0, 10 ** 9 
          while l < r:
              mid = l + r + 1 >> 1 
              if check(mid):
                  l = mid 
              else:
                  r = mid - 1 
          print(r)
      
      return 
  
  if __name__ == '__main__':
      # main()
      main_whd()