#include <cstring>
using namespace std;
int query_kth(const int *a, int n_a, const int *b, int n_b, const int *c, int n_c, int k)
{
static const int *num[] = {a, b, c}, n[] = {n_a, n_b, n_c}, N = 3;
static int i, j, m[N], s[N], x, y;
for (memset(m, x = 0, sizeof(m)); k -= x; m[j] += x)
{
x = (k - N + 1) / N;
for (i = j = 0; i < 3; ++i)
{
if ((y = m[i] + x) < n[i])
s[i] = num[i][y];
else
s[i] = 1000000001;
if (s[j] > s[i])
j = i;
}
++x;
}
return s[j];
}