#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include"kth.h"
using namespace std;
int query_kth(int na,int nb,int nc,int k){
int a=0,b=0,c=0;
int mn,t;
for (;k;k-=t+1){
t=(k-2)/3;
int s1=get_a(a+t);
int s2=get_b(b+t);
int s3=get_c(c+t);
mn=min(s1,min(s2,s3));
if (s1==mn) a+=t+1;
else if (s2==mn) b+=t+1;
else c+=t+1;
}
return mn;
}