#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)
for (i = j = 0, x = (k - N + 1) / N; i < 3; ++i)
if ((y = m[i] + x) < n[i])
if ((s[i] = num[i][y]) < s[j])
j = i;
return s[j];
}