1002 Just another board game

#include<iostream>
#include<cstring>
#include<queue>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<vector>

#define fi first
#define se second

#define lowbit(x) (x&-x)

using namespace std;

namespace ae86{
    const int bufl=1<<15;
    char buf[bufl],*s=buf,*t=buf;
    inline int fetch(){
        if(s==t){t=(s=buf)+fread(buf,1,bufl,stdin);if(s==t)return EOF;}
        return*s++;
    }
    inline int read(){
        int a=0,b=1,c=fetch();
        while(!isdigit(c))b^=c=='-',c=fetch();
        while(isdigit(c))a=a*10+c-48,c=fetch();
        return b?a:-a;
    }
}
using ae86::read;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<int,double> pid;

const int inf=0x3f3f3f3f;
const ll INF=2e18;
const double eps=1e-8;
const double pi=acos(-1);
const int mod=1e9+7;

const int N=100010;

ll T;
ll n,m;
ll k;

ll mx[N],mn[N];

int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%lld%lld%lld",&n,&m,&k);
        for(int i=1;i<=n;i++)    mx[i]=-INF;
        for(int i=1;i<=m;i++)    mn[i]=INF;
        ll x;
        ll ans;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                scanf("%lld",&x);
                if(i==1&&j==1)    ans=x;
                mn[j]=min(mn[j],x);
                mx[i]=max(mx[i],x);
            }
        }
        ll tmp=mx[1];
        ll maxn=0,minn=INF;
        for(int i=1;i<=m;i++)    maxn=max(maxn,mn[i]);
        for(int i=1;i<=n;i++)    minn=min(minn,mx[i]);
        if(k==1)    printf("%lld\n",mx[1]);
        else if(k%2==0){
            ans=max(ans,maxn);
            printf("%lld\n",ans);
        }
        else{
            if(tmp<minn){
                ans=max(ans,tmp);
                printf("%lld\n",ans);
            }
            else{
                ans=max(ans,minn);
                printf("%lld\n",ans);    
            }
        }
    }
}