import std.stdio, std.string;
import std.algorithm;

class SegmentTree(T)
{
    class Node
    {
        int leftIndex;
        int rightIndex;
        T[] maximum;
        T[] minimum;
        Node left;
        Node right;

        this(int leftIndex, int rightIndex, int m)
        {
            this.maximum = new T[m];
            this.minimum = new T[m];
            this.leftIndex = leftIndex;
            this.rightIndex = rightIndex;
            this.left = this.right = null;
        }
    }

    protected Node root;
    immutable static T bound = T.max;

    this(T[][] arr)
    {
        this.initializeTree(this.root, arr, 0, cast(int)arr.length - 1);
    }

    protected void initializeTree(ref Node node, T[][] arr, int leftIndex, int rightIndex)
    {
        auto m = cast(int)arr[0].length;
        node = new Node(leftIndex, rightIndex, m);
        if (leftIndex == rightIndex)
        {
            foreach (i; 0 .. m)
            {
                node.maximum[i] = node.minimum[i] = arr[leftIndex][i];
            }
            return;
        }
        auto mid = (leftIndex + rightIndex) >> 1;
        initializeTree(node.left, arr, leftIndex, mid);
        initializeTree(node.right, arr, mid + 1, rightIndex);
        foreach (i; 0 .. m)
        {
            node.maximum[i] = max(node.left.maximum[i], node.right.maximum[i]);
            node.minimum[i] = min(node.left.minimum[i], node.right.minimum[i]);
        }
    }

    T[5] queryMax(int left, int right)
    {
        return _queryMax(root, left, right);
    }

    protected T[5] _queryMax(Node node, int left, int right)
    {
        auto m = cast(int)node.maximum.length;
        T[5] res;
        if (left <= node.leftIndex && right >= node.rightIndex)
        {
            foreach (i; 0 .. m)
            {
                res[i] = node.maximum[i];
            }
            return res;
        }
        if (left > node.rightIndex || right < node.leftIndex)
        {
            foreach (i; 0 .. m)
            {
                res[i] = -T.max;
            }
            return res;
        }
        auto leftMax = _queryMax(node.left, left, right);
        auto rightMax = _queryMax(node.right, left, right);
        foreach (i; 0 .. m)
        {
            res[i] = max(leftMax[i], rightMax[i]);
        }
        return res;
    }
}

void solve(int n, int m, int k, int[][] a)
{
    auto st = new SegmentTree!int(a);
    auto sl = -1, sr = -1;
    auto longest = 0;
    auto prev = new int[m];
    foreach (i; 0 .. n)
    {
        auto left = i + longest, right = n - 1;
        if (left >= n)
        {
            break;
        }
        fill(prev, 0);
        if (left > i)
        {
            auto ret = st.queryMax(i, left - 1);
            foreach (j; 0 .. m)
            {
                prev[j] = ret[j];
            }
        }
        while (left <= right)
        {
            auto mid = (left + right) >> 1;
            auto ret = st.queryMax(left, mid);
            auto need = 0;
            foreach (j; 0 .. m)
            {
                need += max(prev[j], ret[j]);
                if (need > k)
                {
                    break;
                }
            }
            if (need > k)
            {
                right = mid - 1;
            }
            else
            {
                left = mid + 1;
                foreach (j; 0 .. m)
                {
                    prev[j] = max(prev[j], ret[j]);
                }
                if (mid - i + 1 > longest)
                {
                    longest = mid - i + 1;
                    sl = i;
                    sr = mid;
                }
            }
        }
    }
    auto s = new int[m];
    fill(s, 0);
    if (longest > 0)
    {
        foreach (i; sl .. sr + 1)
        {
            foreach (j; 0 .. m)
            {
                s[j] = max(s[j], a[i][j]);
            }
        }
    }
    foreach (i; 0 .. m)
    {
        write(s[i]);
        if (i < m)
        {
            write(" ");
        }
    }
    writeln();
}

int main(string[] args)
{
    int n, m, k;
    while (readf("%d %d %d\n", &n, &m, &k) == 3)
    {
        auto a = new int[][](n, m);
        foreach (i; 0 .. n)
        {
            foreach (j; 0 .. m)
            {
                readf(" %d", &a[i][j]);
            }
            readln;
        }
        solve(n, m, k, a);
    }
    return 0;
}

import std.stdio, std.string;
import std.algorithm;

class SegmentTree(T)
{
    class Node
    {
        int leftIndex;
        int rightIndex;
        T[5] maximum;
        T[5] minimum;
        Node left;
        Node right;

        this(int leftIndex, int rightIndex, int m)
        {
            this.leftIndex = leftIndex;
            this.rightIndex = rightIndex;
            this.left = this.right = null;
        }
    }

    protected Node root;
    immutable static T bound = T.max;

    this(T[][] arr)
    {
        this.initializeTree(this.root, arr, 0, cast(int)arr.length - 1);
    }

    protected void initializeTree(ref Node node, T[][] arr, int leftIndex, int rightIndex)
    {
        auto m = cast(int)arr[0].length;
        node = new Node(leftIndex, rightIndex, m);
        if (leftIndex == rightIndex)
        {
            foreach (i; 0 .. m)
            {
                node.maximum[i] = node.minimum[i] = arr[leftIndex][i];
            }
            return;
        }
        auto mid = (leftIndex + rightIndex) >> 1;
        initializeTree(node.left, arr, leftIndex, mid);
        initializeTree(node.right, arr, mid + 1, rightIndex);
        foreach (i; 0 .. m)
        {
            node.maximum[i] = max(node.left.maximum[i], node.right.maximum[i]);
            node.minimum[i] = min(node.left.minimum[i], node.right.minimum[i]);
        }
    }

    T[5] queryMax(int left, int right)
    {
        return _queryMax(root, left, right);
    }

    protected T[5] _queryMax(Node node, int left, int right)
    {
        auto m = cast(int)node.maximum.length;
        T[5] res;
        if (left <= node.leftIndex && right >= node.rightIndex)
        {
            foreach (i; 0 .. m)
            {
                res[i] = node.maximum[i];
            }
            return res;
        }
        if (left > node.rightIndex || right < node.leftIndex)
        {
            foreach (i; 0 .. m)
            {
                res[i] = -T.max;
            }
            return res;
        }
        auto leftMax = _queryMax(node.left, left, right);
        auto rightMax = _queryMax(node.right, left, right);
        foreach (i; 0 .. m)
        {
            res[i] = max(leftMax[i], rightMax[i]);
        }
        return res;
    }
}

void solve(int n, int m, int k, int[][] a)
{
    auto st = new SegmentTree!int(a);
    auto sl = -1, sr = -1;
    auto longest = 0;
    auto prev = new int[m];
    foreach (i; 0 .. n)
    {
        auto left = i + longest, right = n - 1;
        if (left >= n)
        {
            break;
        }
        fill(prev, 0);
        if (left > i)
        {
            auto ret = st.queryMax(i, left - 1);
            foreach (j; 0 .. m)
            {
                prev[j] = ret[j];
            }
        }
        while (left <= right)
        {
            auto mid = (left + right) >> 1;
            auto ret = st.queryMax(left, mid);
            auto need = 0;
            foreach (j; 0 .. m)
            {
                need += max(prev[j], ret[j]);
                if (need > k)
                {
                    break;
                }
            }
            if (need > k)
            {
                right = mid - 1;
            }
            else
            {
                left = mid + 1;
                foreach (j; 0 .. m)
                {
                    prev[j] = max(prev[j], ret[j]);
                }
                if (mid - i + 1 > longest)
                {
                    longest = mid - i + 1;
                    sl = i;
                    sr = mid;
                }
            }
        }
    }
    auto s = new int[m];
    fill(s, 0);
    if (longest > 0)
    {
        foreach (i; sl .. sr + 1)
        {
            foreach (j; 0 .. m)
            {
                s[j] = max(s[j], a[i][j]);
            }
        }
    }
    foreach (i; 0 .. m)
    {
        write(s[i]);
        if (i < m)
        {
            write(" ");
        }
    }
    writeln();
}

int main(string[] args)
{
    int n, m, k;
    while (readf("%d %d %d\n", &n, &m, &k) == 3)
    {
        auto a = new int[][](n, m);
        foreach (i; 0 .. n)
        {
            foreach (j; 0 .. m)
            {
                readf(" %d", &a[i][j]);
            }
            readln;
        }
        solve(n, m, k, a);
    }
    return 0;
}

import std.stdio, std.string;
import std.algorithm;

class SegmentTree(T)
{
    class Node
    {
        int leftIndex;
        int rightIndex;
        T[] maximum;
        T[] minimum;
        Node left;
        Node right;

        this(int leftIndex, int rightIndex, int m)
        {
            this.maximum = new T[m];
            this.minimum = new T[m];
            this.leftIndex = leftIndex;
            this.rightIndex = rightIndex;
            this.left = this.right = null;
        }
    }

    protected Node root;
    immutable static T bound = T.max;

    this(T[][] arr)
    {
        this.initializeTree(this.root, arr, 0, cast(int)arr.length - 1);
    }

    protected void initializeTree(ref Node node, T[][] arr, int leftIndex, int rightIndex)
    {
        auto m = cast(int)arr[0].length;
        node = new Node(leftIndex, rightIndex, m);
        if (leftIndex == rightIndex)
        {
            foreach (i; 0 .. m)
            {
                node.maximum[i] = node.minimum[i] = arr[leftIndex][i];
            }
            return;
        }
        auto mid = (leftIndex + rightIndex) >> 1;
        initializeTree(node.left, arr, leftIndex, mid);
        initializeTree(node.right, arr, mid + 1, rightIndex);
        foreach (i; 0 .. m)
        {
            node.maximum[i] = max(node.left.maximum[i], node.right.maximum[i]);
            node.minimum[i] = min(node.left.minimum[i], node.right.minimum[i]);
        }
    }

    T count(int left, int right)
    {
        auto m = cast(int)root.maximum.length;
        auto res = 0;
        foreach (i; 0 .. m)
        {
            auto ret = _queryMax(root, left, right, i);
            res += ret;
        }
        return res;
    }

    protected T _queryMax(Node node, int left, int right, int idx)
    {
        if (left <= node.leftIndex && right >= node.rightIndex)
        {
            return node.maximum[idx];
        }
        if (left > node.rightIndex || right < node.leftIndex)
        {
            return -T.max;
        }
        auto leftMax = _queryMax(node.left, left, right, idx);
        auto rightMax = _queryMax(node.right, left, right, idx);
        return max(leftMax, rightMax);
    }
}

void solve(int n, int m, int k, int[][] a)
{
    auto st = new SegmentTree!int(a);
    auto sl = -1, sr = -1;
    auto longest = 0;
    foreach (i; 0 .. n)
    {
        auto left = i + longest, right = n - 1;
        if (left >= n)
        {
            break;
        }
        auto leftBound = i;
        auto need = 0;
        while (left <= right)
        {
            auto mid = (left + right) >> 1;
            auto inc = st.count(leftBound, mid);
            if (need + inc <= k)
            {
                left = mid + 1;
                need += inc;
                leftBound = left;
                if (mid - i + 1 > longest)
                {
                    longest = mid - i + 1;
                    sl = i;
                    sr = mid;
                }
            }
            else
            {
                right = mid - 1;
            }
        }
    }
    auto s = new int[m];
    fill(s, 0);
    if (longest > 0)
    {
        foreach (i; sl .. sr + 1)
        {
            foreach (j; 0 .. m)
            {
                s[j] = max(s[j], a[i][j]);
            }
        }
    }
    foreach (i; 0 .. m)
    {
        write(s[i]);
        if (i < m)
        {
            write(" ");
        }
    }
    writeln();
}

int main(string[] args)
{
    int n, m, k;
    while (readf("%d %d %d\n", &n, &m, &k) == 3)
    {
        auto a = new int[][](n, m);
        foreach (i; 0 .. n)
        {
            foreach (j; 0 .. m)
            {
                readf(" %d", &a[i][j]);
            }
            readln;
        }
        solve(n, m, k, a);
    }
    return 0;
}

import std.stdio, std.string;
import std.algorithm;

class SegmentTree(T)
{
    class Node
    {
        int leftIndex;
        int rightIndex;
        T[] maximum;
        T[] minimum;
        Node left;
        Node right;

        this(int leftIndex, int rightIndex, int m)
        {
            this.maximum = new T[m];
            this.minimum = new T[m];
            this.leftIndex = leftIndex;
            this.rightIndex = rightIndex;
            this.left = this.right = null;
        }
    }

    protected Node root;
    immutable static T bound = T.max;

    this(T[][] arr)
    {
        this.initializeTree(this.root, arr, 0, cast(int)arr.length - 1);
    }

    protected void initializeTree(ref Node node, T[][] arr, int leftIndex, int rightIndex)
    {
        auto m = cast(int)arr[0].length;
        node = new Node(leftIndex, rightIndex, m);
        if (leftIndex == rightIndex)
        {
            foreach (i; 0 .. m)
            {
                node.maximum[i] = node.minimum[i] = arr[leftIndex][i];
            }
            return;
        }
        auto mid = (leftIndex + rightIndex) >> 1;
        initializeTree(node.left, arr, leftIndex, mid);
        initializeTree(node.right, arr, mid + 1, rightIndex);
        foreach (i; 0 .. m)
        {
            node.maximum[i] = max(node.left.maximum[i], node.right.maximum[i]);
            node.minimum[i] = min(node.left.minimum[i], node.right.minimum[i]);
        }
    }

    T count(int left, int right)
    {
        auto m = cast(int)root.maximum.length;
        auto res = 0;
        foreach (i; 0 .. m)
        {
            auto ret = _queryMax(root, left, right, i);
            res += ret;
        }
        return res;
    }

    protected T _queryMax(Node node, int left, int right, int idx)
    {
        if (left <= node.leftIndex && right >= node.rightIndex)
        {
            return node.maximum[idx];
        }
        if (left > node.rightIndex || right < node.leftIndex)
        {
            return -T.max;
        }
        auto leftMax = _queryMax(node.left, left, right, idx);
        auto rightMax = _queryMax(node.right, left, right, idx);
        return max(leftMax, rightMax);
    }
}

void solve(int n, int m, int k, int[][] a)
{
    auto st = new SegmentTree!int(a);
    auto sl = -1, sr = -1;
    auto longest = 0;
    foreach (i; 0 .. n)
    {
        auto left = i + longest, right = n - 1;
        if (left >= n)
        {
            break;
        }
        auto need = 0;
        while (left <= right)
        {
            auto mid = (left + right) >> 1;
            auto inc = st.count(left, mid);
            if (need + inc <= k)
            {
                left = mid + 1;
                need += inc;
                if (mid - i + 1 > longest)
                {
                    longest = mid - i + 1;
                    sl = i;
                    sr = mid;
                }
            }
            else
            {
                right = mid - 1;
            }
        }
    }
    auto s = new int[m];
    fill(s, 0);
    if (longest > 0)
    {
        foreach (i; sl .. sr + 1)
        {
            foreach (j; 0 .. m)
            {
                s[j] = max(s[j], a[i][j]);
            }
        }
    }
    foreach (i; 0 .. m)
    {
        write(s[i]);
        if (i < m)
        {
            write(" ");
        }
    }
    writeln();
}

int main(string[] args)
{
    int n, m, k;
    while (readf("%d %d %d\n", &n, &m, &k) == 3)
    {
        auto a = new int[][](n, m);
        foreach (i; 0 .. n)
        {
            foreach (j; 0 .. m)
            {
                readf(" %d", &a[i][j]);
            }
            readln;
        }
        solve(n, m, k, a);
    }
    return 0;
}

import std.stdio, std.string;
import std.algorithm;

class SegmentTree(T)
{
    class Node
    {
        int leftIndex;
        int rightIndex;
        T[] maximum;
        T[] minimum;
        Node left;
        Node right;

        this(int leftIndex, int rightIndex, int m)
        {
            this.maximum = new T[m];
            this.minimum = new T[m];
            this.leftIndex = leftIndex;
            this.rightIndex = rightIndex;
            this.left = this.right = null;
        }
    }

    protected Node root;
    immutable static T bound = T.max;

    this(T[][] arr)
    {
        this.initializeTree(this.root, arr, 0, cast(int)arr.length - 1);
    }

    protected void initializeTree(ref Node node, T[][] arr, int leftIndex, int rightIndex)
    {
        auto m = cast(int)arr[0].length;
        node = new Node(leftIndex, rightIndex, m);
        if (leftIndex == rightIndex)
        {
            foreach (i; 0 .. m)
            {
                node.maximum[i] = node.minimum[i] = arr[leftIndex][i];
            }
            return;
        }
        auto mid = (leftIndex + rightIndex) >> 1;
        initializeTree(node.left, arr, leftIndex, mid);
        initializeTree(node.right, arr, mid + 1, rightIndex);
        foreach (i; 0 .. m)
        {
            node.maximum[i] = max(node.left.maximum[i], node.right.maximum[i]);
            node.minimum[i] = min(node.left.minimum[i], node.right.minimum[i]);
        }
    }

    T[] queryMax(int left, int right)
    {
        auto m = cast(int)root.maximum.length;
        int[] res;
        foreach (i; 0 .. m)
        {
            auto ret = _queryMax(root, left, right, i);
            res ~= ret;
        }
        return res;
    }

    protected T _queryMax(Node node, int left, int right, int idx)
    {
        if (left <= node.leftIndex && right >= node.rightIndex)
        {
            return node.maximum[idx];
        }
        if (left > node.rightIndex || right < node.leftIndex)
        {
            return -T.max;
        }
        auto leftMax = _queryMax(node.left, left, right, idx);
        auto rightMax = _queryMax(node.right, left, right, idx);
        return max(leftMax, rightMax);
    }
}

void solve(int n, int m, int k, int[][] a)
{
    auto st = new SegmentTree!int(a);
    auto sl = -1, sr = -1;
    auto longest = 0;
    foreach (i; 0 .. n)
    {
        auto left = i + longest, right = n - 1;
        if (left >= n)
        {
            break;
        }
        auto prev = new int[m];
        fill(prev, 0);
        while (left <= right)
        {
            auto mid = (left + right) >> 1;
            auto ret = st.queryMax(left, mid);
            auto need = 0;
            foreach (j; 0 .. m)
            {
                need += max(prev[j], ret[j]);
                if (need > k)
                {
                    break;
                }
            }
            if (need <= k)
            {
                left = mid + 1;
                foreach (j; 0 .. m)
                {
                    prev[j] = max(prev[j], ret[j]);
                }
                if (mid - i + 1 > longest)
                {
                    longest = mid - i + 1;
                    sl = i;
                    sr = mid;
                }
            }
            else
            {
                right = mid - 1;
            }
        }
    }
    auto s = new int[m];
    fill(s, 0);
    if (longest > 0)
    {
        foreach (i; sl .. sr + 1)
        {
            foreach (j; 0 .. m)
            {
                s[j] = max(s[j], a[i][j]);
            }
        }
    }
    foreach (i; 0 .. m)
    {
        write(s[i]);
        if (i < m)
        {
            write(" ");
        }
    }
    writeln();
}

int main(string[] args)
{
    int n, m, k;
    while (readf("%d %d %d\n", &n, &m, &k) == 3)
    {
        auto a = new int[][](n, m);
        foreach (i; 0 .. n)
        {
            foreach (j; 0 .. m)
            {
                readf(" %d", &a[i][j]);
            }
            readln;
        }
        solve(n, m, k, a);
    }
    return 0;
}


import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.datetime.stopwatch;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

void main()
{
    int n, k;
    readf("%s %s", &n, &k);
    readln;

    auto a = readln.chomp.split.map!(to!int).array;

    int q;
    readf("%s", &q);
    readln;

    int[int] cnt;
    cnt[0] = 0;
    foreach (e; a) {
        int cursum = 0;
        foreach (i; 1 .. k+1) {
            cursum += e;
            cnt[cursum] = min(i, cnt.get(cursum, k));
        }
    }

    debug { cnt.writeln; }

    while (q--) {
        int x;
        readf("%s", &x);
        readln;

        int ans = k+1;
        foreach (val; cnt.keys) {
            if (val > x) { continue; }

            int rst = x - val;
            int tot = cnt.get(val, k+1) + cnt.get(rst, k+1);

            ans = min(ans, tot);
        }

        if (ans == k+1) { ans = -1; }

        ans.writeln;
    }
}

import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import core.bitop;

void main ()
{
	int n, k;
	while (readf (" %s %s ", &n, &k) > 0)
	{
		auto a = readln.split.map !(to !(int)).array;

		alias Pair = Tuple !(int, q{x}, int, q{y});
		Pair [] v;
		foreach (i; 0..n)
		{
			foreach (u; 0..k + 1)
			{
				v ~= Pair (a[i] * u, u);
			}
		}
		sort (v);

		int q;
		readf (" %s", &q);
		foreach (r; 0..q)
		{
			int s;
			readf (" %s", &s);
			auto u = v;
			int res = k + 1;
			while (!u.empty)
			{
				while (!u.empty && u.front.x + u.back.x > s)
				{
					u.popBack ();
				}
				while (!u.empty && u.front.x + u.back.x == s)
				{
					res = min (res, u.front.y + u.back.y);
					u.popBack ();
				}
				if (!u.empty)
				{
					u.popFront ();
				}
			}
			if (res > k)
			{
				res = -1;
			}
			writeln (res);
		}
	}
}

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.datetime.stopwatch;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

void main()
{
    int n, k;
    readf("%s %s", &n, &k);
    readln;

    auto a = readln.chomp.split.map!(to!int).array;

    int q;
    readf("%s", &q);
    readln;

    int[int] cnt;
    foreach (e; a) {
        int cursum = 0;
        foreach (i; 1 .. k+1) {
            cursum += e;
            cnt[cursum] = min(i, cnt.get(cursum, k));
        }
    }

    debug { cnt.writeln; }

    while (q--) {
        int x;
        readf("%s", &x);
        readln;

        int ans = k+1;
        foreach (val; cnt.keys) {
            if (val > x) { continue; }

            int rst = x - val;
            int tot = cnt.get(val, k+1) + cnt.get(rst, k+1);

            ans = min(ans, tot);
        }

        if (ans == k+1) { ans = -1; }

        ans.writeln;
    }
}


import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;
T[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;
T chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
	foreach(_; 0 .. rint){
		int n = rint;
		long p = rlong;
		int k = rint;
		long[] as = rlong(n);
		as.sort();
		log("---");
		log("n:", n, "p:", p, "k:", k, "as:", as);

		int ans = 0;
		int tmp1 = 0;
		foreach(r; 0 .. k){
			int tmp2 = tmp1;
			for(int m = 0; r + k * m <= n; m ++){
				log("r:", r, "m:", m, "x:", r + k * m, "tmp1:", tmp1, "tmp2:", tmp2, "f:", tmp2 <= p);
				if(tmp2 <= p) ans.chmax(r + k * m);
				else break;
				if(r + k * m + k - 1 < n) tmp2 += as[r + k * m + k - 1];
				else break;
			}
			tmp1 += as[r];
		}
		ans.writeln;
	}
}

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

void main()
{
    int t;
    readf("%s", &t);
    readln;

    while (t--) {
        int n, p, k;
        readf("%s %s %s", &n, &p, &k);
        readln;

        auto arr = readln.chomp.split.map!(to!int).array;

        arr.sort();

        auto cost = new int[] (n+1);
        cost[0] = 0;
        foreach (i, e; arr.enumerate(1)) {
            if (i < k) { cost[i.to!int] = cost[i.to!int - 1] + e; }
            else { cost[i.to!int] = cost[i.to!int - k] + e; }
        }

        int ans = 0;
        foreach (i, e; cost) {
            if (e <= p) { ans = i.to!int; }
        }

        ans.writeln;
    }
}

//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;

class InputReader {
  private:
  ubyte[] p;
  ubyte[] buffer;
  size_t cur;
  public:
  this () {
    buffer = uninitializedArray!(ubyte[])(16<<20);
    p = stdin.rawRead (buffer);
  }
  final ubyte skipByte (ubyte lo) {
    while (true) {
      auto a = p[cur .. $];
      auto r = a.find! (c => c >= lo);
      if (!r.empty) {
        cur += a.length - r.length;
        return p[cur++];
      }
      p = stdin.rawRead (buffer);
      cur = 0;
      if (p.empty) return 0;
    }
  }
  final ubyte nextByte () {
    if (cur < p.length) {
       return p[cur++];
    }
    p = stdin.rawRead (buffer);
    if (p.empty) return 0;
    cur = 1;
    return p[0];
  }

  template next(T) if (isSigned!T) {
    final T next ()  {
      T res;
      ubyte b = skipByte (45);
      if (b == 45) {
        while (true) {
          b = nextByte ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 - (b - 48);
        }
      } else {
        res = b - 48;
        while (true) {
          b = nextByte ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 + (b - 48);
        }
      }
    }
  }
  template next(T) if (isUnsigned!T) {
    final T next () {
      T res = skipByte (48) - 48;
      while (true) {
        ubyte b = nextByte ();
        if (b < 48 || b >= 58) {
          break;
        }
        res = res * 10 + (b - 48);
      }
      return res;
    }
  }
  final T[] nextA(T) (int n) {
    auto a = uninitializedArray!(T[]) (n);
    foreach (i; 0 .. n) {
      a[i] = next!T;
    }
    return a;
  }
}

void main() {
  auto r = new InputReader;
  immutable t = r.next!uint;
  auto res = uninitializedArray!(int[]) (t);
  foreach (tid; 0 .. t) {
    immutable n = r.next!uint;
    auto p = r.next!uint;
    immutable k = r.next!uint;
    auto a = r.nextA!uint (n);
    sort (a);
    /*
    auto s = new long[n+1];
    s[0] = 0;
    foreach (i; 1 .. n + 1) s[i] = s[i-1] + a[i-1];
    */
    auto b = new long[n+1];
    b[0] = 0;
    foreach (i; 1 .. n + 1) {
      immutable val = a[i-1];
      b[i] = b[i-1] + val;
      int j = (i - (k - 1));
      if (j >= 1) {
        b[i] = min (b[i], b[j-1] + val);
      }
    }
    int ans;
    foreach_reverse (i; 1 .. n + 1) {
      if (b[i] <= p) {
        ans = i;
        break;
      }
    }
    res[tid] = ans;
  }
  writefln ("%(%s\n%)", res);
}

import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;
T[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;
T chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
	foreach(_; 0 .. rint){
		int n = rint;
		long p = rlong;
		int k = rint;
		long[] as = rlong(n);
		as.sort();
		log("---");
		log("n:", n, "p:", p, "k:", k, "as:", as);

		bool f(int x){ // "can buy x or more goods" (not necessarily can by exact x)
			int m = x / k, r = x % k;
			long q;
			foreach(a; as[0 .. r]){
				if(q + a < as[k - 1]) q += a;
				else{
					q = 0;
					r = 0;
					m += 1;
					break;
				}
			}
			foreach(i; 1 .. m + 1) q += as[r + k * i - 1];
			log("x:", x, "m:", m, "r:", r, "q:", q, "f:", q <= p);
			return q <= p;
		}

		int ans = uplimit(0, n, &f);
		ans.writeln;

	}
}

	// 二分探索テンプレート
	T uplimit(T)(T a, T c, bool delegate(T) f){
		if(f(c)) return c; if(! f(a)) return a - 1;
		while(a + 1 < c){ T b = (a + c) / 2; if(f(b)) a = b; else c = b; }
		return a;
	}
	T downlimit(T)(T a, T c, bool delegate(T) f){
		if(f(a)) return a; if(! f(c)) return c + 1;
		while(a + 1 < c){ T b = (a + c) / 2; if(f(b)) c = b; else a = b; }
		return c;
	}

import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;
T[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;
T chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
	foreach(_; 0 .. rint){
		int n = rint;
		long p = rlong;
		int k = rint;
		long[] as = rlong(n);
		as.sort();

		bool f(int x){
			int m = x / k, r = x % k;
			long q;
			log("x:", x, "m:", m, "r:", r);
			foreach(a; as[0 .. r]) q += a;
			foreach(i; 1 .. m + 1) q += as[r + k * m - 1];
			return q <= p;
		}

		int ans = uplimit(0, n, &f);
		ans.writeln;

	}
}

	// 二分探索テンプレート
	T uplimit(T)(T a, T c, bool delegate(T) f){
		if(f(c)) return c; if(! f(a)) return a - 1;
		while(a + 1 < c){ T b = (a + c) / 2; if(f(b)) a = b; else c = b; }
		return a;
	}
	T downlimit(T)(T a, T c, bool delegate(T) f){
		if(f(a)) return a; if(! f(c)) return c + 1;
		while(a + 1 < c){ T b = (a + c) / 2; if(f(b)) c = b; else a = b; }
		return c;
	}

import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;
T[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;
T chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
	foreach(_; 0 .. rint){
		int n = rint;
		long p = rlong;
		int k = rint;
		long[] as = rlong(n);
		as.sort();
		log("---");
		log("n:", n, "p:", p, "k:", k, "as:", as);

		bool f(int x){
			int m = x / k, r = x % k;
			long q;
			foreach(a; as[0 .. r]) q += a;
			foreach(i; 1 .. m + 1) q += as[r + k * i - 1];
			log("x:", x, "m:", m, "r:", r, "q:", q, "f:", q <= p);
			return q <= p;
		}

		int ans = uplimit(0, n, &f);
		ans.writeln;

	}
}

	// 二分探索テンプレート
	T uplimit(T)(T a, T c, bool delegate(T) f){
		if(f(c)) return c; if(! f(a)) return a - 1;
		while(a + 1 < c){ T b = (a + c) / 2; if(f(b)) a = b; else c = b; }
		return a;
	}
	T downlimit(T)(T a, T c, bool delegate(T) f){
		if(f(a)) return a; if(! f(c)) return c + 1;
		while(a + 1 < c){ T b = (a + c) / 2; if(f(b)) c = b; else a = b; }
		return c;
	}


import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;

void main() {
    long[long] cubic;
    for (long i = 1; i * i * i <= 10L ^^ 18; ++i) cubic[i * i * i] = i;

    auto N = readln.chomp.to!int;
    while (N--) {
        auto s = readln.split.map!(to!long);
        auto a = s[0];
        auto b = s[1];
        if (a * b in cubic && a % cubic[a * b] == 0 && b % cubic[a * b] == 0) {
            writeln("Yes");
        } else {
            writeln("No");
        }
    }
}

// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
import std.datetime, std.bigint, std.numeric;

int n;

void main() {
    scan(n);

    int a, b;

    while (n--) {
        scan(a, b);
        writeln(judge(a, b) ? "Yes" : "No");
    }
}

bool judge(int a, int b) {
    long btm = 0, top = 10^^6 + 1;
    long ab = 1L*a*b;

    while (top - btm > 1) {
        long mid = btm + (top - btm) / 2;

        if (mid*mid*mid <= ab) {
            btm = mid;
        }
        else {
            top = mid;
        }
    }

    if (btm*btm*btm != ab) {
        return false;
    }

    if (a % btm == 0 && b % btm == 0) {
        return true;
    }
    else {
        return false;
    }
}

void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

// floor(a^(1/k))
ulong floorKthRoot(ulong a, ulong k) {
  import core.bitop : bsr;
  import std.algorithm : min;
  if (a == 0) {
    return 0;
  } else if (k <= 1) {
    return a;
  } else if (k == 2) {
    ulong b = a, x = 0, y = 0;
    for (int e = bsr(a) & ~1; e >= 0; e -= 2) {
      x <<= 1;
      y <<= 1;
      if (b >= (y | 1) << e) {
        b -= (y | 1) << e;
        x |= 1;
        y += 2;
      }
    }
    return x;
  } else if (k <= 40) {
    // min x s.t. x^k >= 2^64
    enum ulong[] HIS =
        [0, 0, 4294967296UL, 2642246, 65536, 7132, 1626, 566, 256, 139, 85, 57,
         41, 31, 24, 20, 16, 14, 12, 11, 10, 9, 8, 7, 7, 6, 6, 6, 5, 5, 5, 5,
         4, 4, 4, 4, 4, 4, 4, 4, 4];
    ulong lo = 1UL << (bsr(a) / k);
    ulong hi = min(1UL << (bsr(a) / k + 1), HIS[cast(size_t)(k)]);
    for (; lo + 1 < hi; ) {
      const ulong mid = (lo + hi) / 2;
      ulong b = mid * mid;
      foreach (i; 2 .. k) b *= mid;
      ((b <= a) ? lo : hi) = mid;
    }
    return lo;
  } else if (k <= 63) {
    return ((1UL << k) <= a) ? 2 : 1;
  } else {
    return 1;
  }
}

void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const A = readLong();
        const B = readLong();

        const long x = floorKthRoot(A * B, 3);
        const ans = (A * B == x^^3 && A % x == 0 && B % x == 0);
        writeln(ans ? "Yes" : "No");
      }
    }
  } catch (EOFException e) {
  }
}


module main;
import std.bigint,std.container,std.array,std.algorithm,std.conv,
	std.functional,std.math,std.numeric,std.string,std.range,std.complex,
	std.typecons,std.regex,std.random,std.uni,std.traits,std.variant,
	std.stdio,std.bitmanip;
import core.stdc.stdio : freopen;
alias BitArray bitset;
alias RedBlackTree Rbt;
alias redBlackTree rbt;
alias BigInt big;
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias BoyerMooreFinder strfind;
alias make_pair mp;
alias binary_search bins;
alias orient_square orsq;
immutable int mod=1000000007;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b)
	{
		return a/gcd(a,b)*b;
	}
	X binpow(X,Y)(X base,Y exp)
		if(is(typeof(exp&1)))
	{
		if(exp<0)return X(0);
		X res=X(1);
		while(exp>0)
		{
			if(exp&1)res*=base;
			base*=base;
			exp>>=1;
		}
		return res;
	}
	auto binpow(X,Y,Z)(X base,Y exp,in Z mm)
		if(is(typeof(exp&1)))
	{
		if(mm==0) return binpow(base,exp);
		if(exp<0)return X(0);
		auto res=X(1)%mm;
		while(exp>0)
		{
			if(exp&1)res=(res*base)%mm;
			base*=base;
			base%=mm;
			exp>>=1;
		}
		return res;
	}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	size_t lowb(T,X)(in T a,auto ref X g)
		if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (!(a[l]<g))?l:r;
	}
	size_t upb(T,X)(in T a,auto ref X g)
		if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(g<a[m])r=m;
			else l=m;
		}
		return (g<a[l])?l:r;
	}
	size_t binf(T,X)(in T a,auto ref X g)
		if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (g==a[l])?l:a.length;
	}
	bool binary_search(T,X)(in T a,auto ref X g)
		if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		return binf(a,g)!=a.length;
	}
	V foldr(R,V,T)(R range,T arg,V nach)
		if(isFunction!T && isInputRange!R && is(typeof(arg(nach,R.front))==V))
	{
		for(;!range.empty;range.popFront()) nach=arg(nach,range.front);
		return nach;
	}
	size_t countr(R,T)(R range,T fun)
		if(isFunction!T && isInputRange!R && is(typeof(fun(R.front))==bool))
	{
		size_t nach;
		for(;!range.empty;range.popFront()) nach+=fun(range.front);
		return nach;
	}
	T orient_square(T)(pair!(T,T)[] figure...)
	{
		auto n=figure.front,t=figure.front;
		figure.popFront();
		T ans=0;
		for(;!figure.empty;figure.popFront())
		{
			ans+=(figure.front.fi-t.fi)*(figure.front.se+t.se);
			t=figure.front;
		}
		return ans+(n.fi-t.fi)*(n.se+t.se);
	}
	real square(T)(pair!(T,T)[] figure...)
		if(is(typeof(figure.front*figure.front)==T))
	{
		return abs(orient_square(figure))/2.000000000000000;
	}
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in size_t n,in size_t m)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n)
{bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(T ptrs) if (ptrs.length > 0)
{return readf(replicate(" %s", ptrs.length), ptrs)==ptrs.length;}
bool read(T...)(T ptrs) if (ptrs.length > 0)
{return readf(replicate(" %s", ptrs.length)~'\n', ptrs)==ptrs.length;}
struct pair(X,Y)
{
	X first;
	Y second;
	alias first fi;
	alias second se;
	this(X a_,Y b_){
		fi=a_;
		se=b_;
	}
	this(this){fi=fi;se=se;}
	this(A,B)(auto ref pair!(A,B) p) if(is(A:X) && is(B:Y))
	{
		fi=p.fi;
		se=p.se;
	}
	int opCmp(A,B)(in pair!(A,B) s_) const
		if(is(typeof(s_.fi<fi)==bool) && is(typeof(s_.se<se)==bool))
	{
		int cmp=0-(fi<s_.fi)+(fi>s_.fi);
		if(cmp!=0)return cmp;
		else return (0-(se<s_.se)+(se>s_.se));
	}
};
pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_)
{
	pair!(X,Y) pp;
	pp.fi=x_;
	pp.se=y_;
	return pp;
}
nothrow void inf(in char* name) {freopen(name,"r",core.stdc.stdio.stdin);}
nothrow void ouf(in char* name) {freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.split.map!(to!(T)).array;}
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold schwartzSort multiSort partialSort heapify join filter countUntil
//---------------------------------------------------------------program beginning-----------------------------------------------------------------

void main()
{
	string t;
	loop:while((t=readln.strip)!="")
	{
		int[char] q;
		foreach(i;"Bulbasaur")q[i]=0;

		foreach(i;t)q[i]++;
		writeln(min(q['B'],q['b'],q['a']/2,q['u']/2,q['l'],q['s'],q['r']));
	}
}

import std.algorithm;
import std.range;
import std.stdio;
import std.string;

immutable string str = "Bulbasaur";
void main(string[] args)
{
    string s;
    while((s = readln.strip) != "")
    {
        int [256] num;
        foreach(char c; s)
        {
            num[c]++;
        }
        int res = int.max;
        foreach(char c;str)
        {
            int d =cast(int)(str.count(c));
            res = min(res,num[c]/d);
        }
        writeln(res);
    }
}

import std.algorithm;
import std.range;
import std.stdio;
import std.string;

immutable string str = "Bulbasaur";

void main ()
{
	string s;
	while ((s = readln.strip) != "")
	{
		int [256] num;
		foreach (char c; s)
		{
			num[c] += 1;
		}
		int res = int.max;
		foreach (char c; str)
		{
			int d = cast (int) (str.count (c));
			res = min (res, num[c] / d);
		}
		writeln (res);
	}
}


import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;

immutable int NA = -1;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, k;
		readf !(" %s %s") (n, k);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		sort (a);
		int l = NA, r = NA;
		int curL = NA;
		int prev = NA;
		foreach (ref c; a.group)
		{
			auto value = c[0];
			auto num = c[1];
			if (prev + 1 != value)
			{
				curL = value;
			}
			if (num < k)
			{
				curL = value + 1;
			}
			else if (r - l <= value - curL)
			{
				l = curL;
				r = value;
			}
			prev = value;
		}
		writefln !("%(%s %)") (l == NA ? [NA] : [l, r]);
	}
}

void main() { runSolver(); }

void problem() {
  auto QN = scan!int;

  auto solve() {
    auto subSolve() {
      auto N = scan!int;
      auto K = scan!int;
      auto A = scan!long(N);

      long[] nums = A.sort.group.filter!(g => g[1] >= K).map!"a[0]".array;

      if (nums.empty) return "-1";

      nums.sort;
      long cur = nums[0];
      long con, maxCon;
      long l = cur, r = cur;
      foreach(n; nums[1..$]) {
        if (n == cur + 1) {
          con++;
          if (maxCon.chmax(con)) {
            r = n;
            l = n - con;
          }
        } else con = 0;

        cur = n;
      }

      return [l, r].toAnswerString;
    }

    foreach(i; 0..QN) {
      subSolve().writeln;
    }
  }

  outputForAtCoder(&solve);
}

// ----------------------------------------------

import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop;
T[][] combinations(T)(T[] s, in long m) {   if (!m) return [[]];   if (s.empty) return [];   return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
Point invert(Point p) { return Point(p.y, p.x); }
long[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; }
bool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; }
bool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; }
string charSort(alias S = "a < b")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; }
ulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}}
T[] compress(T)(T[] arr, T origin = T.init) { T[T] indecies; arr.dup.sort.uniq.enumerate(origin).each!((i, t) => indecies[t] = i); return arr.map!(t => indecies[t]).array; }
string toAnswerString(R)(R r) { return r.map!"a.to!string".joiner(" ").array.to!string; }
struct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x<MD)?x:x-MD;}static auto make(ulong x) {ModInt m; m.v = x; return m;}auto opBinary(string op:"+")(ModInt r) const {return make(normS(v+r.v));}auto opBinary(string op:"-")(ModInt r) const {return make(normS(v+MD-r.v));}auto opBinary(string op:"*")(ModInt r) const {return make((ulong(v)*r.v%MD).to!ulong);}auto opBinary(string op:"^^", T)(T r) const {long x=v;long y=1;while(r){if(r%2==1)y=(y*x)%MD;x=x^^2%MD;r/=2;} return make(y);}auto opBinary(string op:"/")(ModInt r) const {return this*memoize!inv(r);}static ModInt inv(ModInt x) {return x^^(MD-2);}string toString() const {return v.to!string;}auto opOpAssign(string op)(ModInt r) {return mixin ("this=this"~op~"r");}}
alias MInt1 = ModInt!(10^^9 + 7);
alias MInt9 = ModInt!(998_244_353);
void outputForAtCoder(T)(T delegate() fn) {
  static if (is(T == float) || is(T == double) || is(T == real)) "%.16f".writefln(fn());
  else static if (is(T == void)) fn();
  else static if (is(T == string)) fn().writeln;
  else static if (isInputRange!T) {
    static if (!is(string == ElementType!T) && isInputRange!(ElementType!T)) foreach(r; fn()) r.toAnswerString.writeln;
    else foreach(r; fn()) r.writeln;
  }
  else fn().writeln;
}
void runSolver() {
  enum BORDER = "==================================";
  debug { BORDER.writeln; while(!stdin.eof) { "<<< Process time: %s >>>".writefln(benchmark!problem(1)); BORDER.writeln; } }
  else problem();
}
enum YESNO = [true: "Yes", false: "No"];

// -----------------------------------------------


import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;

void get(Args...)(ref Args args)
{
    import std.traits, std.meta, std.typecons;

    static if (Args.length == 1) {
        alias Arg = Args[0];

        static if (isArray!Arg) {
          static if (isSomeChar!(ElementType!Arg)) {
            args[0] = readln.chomp.to!Arg;
          } else {
            args[0] = readln.split.to!Arg;
          }
        } else static if (isTuple!Arg) {
            auto input = readln.split;
            static foreach (i; 0..Fields!Arg.length) {
                args[0][i] = input[i].to!(Fields!Arg[i]);
            }
        } else {
            args[0] = readln.chomp.to!Arg;
        }
    } else {
        auto input = readln.split;
        assert(input.length == Args.length);

        static foreach (i; 0..Args.length) {
            args[i] = input[i].to!(Args[i]);
        }
    }
}

void get_lines(Args...)(size_t N, ref Args args)
{
    import std.traits, std.range;

    static foreach (i; 0..Args.length) {
        static assert(isArray!(Args[i]));
        args[i].length = N;
    }

    foreach (i; 0..N) {
        static if (Args.length == 1) {
            get(args[0][i]);
        } else {
            auto input = readln.split;
            static foreach (j; 0..Args.length) {
                args[j][i] = input[j].to!(ElementType!(Args[j]));
            }
        }
    }
}

void main()
{
    int A, B, K; get(A, B, K);
    if (K == 0) {
        char[] res;
        foreach (_; 0..B) res ~= '1';
        foreach (_; 0..A) res ~= '0';
        writeln("Yes");
        writeln(res);
        writeln(res);
        return;
    }

    if (A + B - 2 < K || A < 1 || B < 2) return writeln("No");
    auto a = A - 1, b = B - 2;
    char[] x;
    x ~= '0';
    foreach (_; 1..K) {
        if (a) {
            x ~= '0';
            --a;
        } else {
            x ~= '1';
            --b;
        }
    }
    x ~= '1';
    while (a) {
        x ~= '0';
        --a;
    }
    while (b) {
        x ~= '1';
        --b;
    }
    x ~= '1';
    reverse(x);

    a = A - 1; b = B - 2;
    char[] y;
    y ~= '1';
    foreach (_; 1..K) {
        if (a) {
            y ~= '0';
            --a;
        } else {
            y ~= '1';
            --b;
        }
    }
    y ~= '0';
    while (a) {
        y ~= '0';
        --a;
    }
    while (b) {
        y ~= '1';
        --b;
    }
    y ~= '1';
    reverse(y);
    writeln("Yes");
    writeln(x);
    writeln(y);
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto a = RD;
	auto b = RD;
	auto k = RD;

	string ans1, ans2;
	if (a == 0)
	{
		if (k == 0)
		{
			foreach (i; 0..b)
			{
				ans1 ~= '1';
				ans2 ~= '1';
			}
		}
	}
	else if (b == 1)
	{
		if (k == 0)
		{
			ans1 ~= '1';
			ans2 ~= '1';
			foreach (i; 0..a)
			{
				ans1 ~= '0';
				ans2 ~= '0';
			}
		}
	}
	else if (k == 0)
	{
		foreach (i; 0..b)
		{
			ans1 ~= '1';
			ans2 ~= '1';
		}
		foreach (i; 0..a)
		{
			ans1 ~= '0';
			ans2 ~= '0';
		}
	}
	else if (k <= a + b - 2)
	{
		auto d = k-1;
		ans1 ~= "11";
		ans2 ~= "10";
		auto aa = min(a-1, d);
		foreach (i; 0..aa)
		{
			ans1 ~= '0';
			ans2 ~= '0';
		}
		d -= aa;
		foreach (i; 0..d)
		{
			ans1 ~= '1';
			ans2 ~= '1';
		}
		ans1 ~= '0';
		ans2 ~= '1';
		auto rem_a = a - 1 - aa;
		auto rem_b = b - 2 - d;
		foreach (i; 0..rem_a)
		{
			ans1 ~= '0';
			ans2 ~= '0';
		}
		foreach (i; 0..rem_b)
		{
			ans1 ~= '1';
			ans2 ~= '1';
		}
	}

	if (ans1.empty)
		writeln("No");
	else
	{
		writeln("Yes");
		writeln(ans1);
		writeln(ans2);
	}
	stdout.flush;
	debug readln;
}

import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;

void get(Args...)(ref Args args)
{
    import std.traits, std.meta, std.typecons;

    static if (Args.length == 1) {
        alias Arg = Args[0];

        static if (isArray!Arg) {
          static if (isSomeChar!(ElementType!Arg)) {
            args[0] = readln.chomp.to!Arg;
          } else {
            args[0] = readln.split.to!Arg;
          }
        } else static if (isTuple!Arg) {
            auto input = readln.split;
            static foreach (i; 0..Fields!Arg.length) {
                args[0][i] = input[i].to!(Fields!Arg[i]);
            }
        } else {
            args[0] = readln.chomp.to!Arg;
        }
    } else {
        auto input = readln.split;
        assert(input.length == Args.length);

        static foreach (i; 0..Args.length) {
            args[i] = input[i].to!(Args[i]);
        }
    }
}

void get_lines(Args...)(size_t N, ref Args args)
{
    import std.traits, std.range;

    static foreach (i; 0..Args.length) {
        static assert(isArray!(Args[i]));
        args[i].length = N;
    }

    foreach (i; 0..N) {
        static if (Args.length == 1) {
            get(args[0][i]);
        } else {
            auto input = readln.split;
            static foreach (j; 0..Args.length) {
                args[j][i] = input[j].to!(ElementType!(Args[j]));
            }
        }
    }
}

void main()
{
    int A, B, K; get(A, B, K);
    if (K == 0) {
        char[] res;
        foreach (_; 0..B) res ~= '1';
        foreach (_; 0..A) res ~= '0';
        writeln("Yes");
        writeln(res);
        writeln(res);
        return;
    }

    if (A < K) return writeln("No");
    if (B < 2) return writeln("No");
    char[] x;
    foreach (_; 0..K) x ~= '0';
    foreach (_; 0..B - 1) x ~= '1';
    foreach (_; 0..A - K) x ~= '0';
    x ~= '1';
    reverse(x);

    char[] y;
    y ~= '1';
    foreach (_; 0..K) y ~= '0';
    foreach (_; 0..B - 2) y ~= '1';
    foreach (_; 0..A - K) y ~= '0';
    y ~= '1';
    reverse(y);
    writeln("Yes");
    writeln(x);
    writeln(y);
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto a = RD;
	auto b = RD;
	auto k = RD;

	string ans1, ans2;
	if (a == 0)
	{
		if (k == 0)
		{
			foreach (i; 0..b)
			{
				ans1 ~= '1';
				ans2 ~= '1';
			}
		}
	}
	else if (b == 1)
	{
		if (k == 0)
		{
			ans1 ~= '1';
			ans2 ~= '1';
			foreach (i; 0..a)
			{
				ans1 ~= '0';
				ans2 ~= '0';
			}
		}
	}
	else if (k <= a + b - 2)
	{
		auto d = a + b - 2 - k;
		ans1 ~= '1';
		ans2 ~= '1';
		auto aa = min(a-1, d);
		foreach (i; 0..aa)
		{
			ans1 ~= '0';
			ans2 ~= '0';
		}
		d -= aa;
		foreach (i; 0..d)
		{
			ans1 ~= '1';
			ans2 ~= '1';
		}
		auto rem_a = a - 1 - aa;
		auto rem_b = b - 2 - d;
		ans1 ~= '1';
		ans2 ~= '0';
		foreach (i; 0..rem_a)
		{
			ans1 ~= '0';
			ans2 ~= '0';
		}
		foreach (i; 0..rem_b)
		{
			ans1 ~= '1';
			ans2 ~= '1';
		}
		ans1 ~= '0';
		ans2 ~= '1';
	}

	if (ans1.empty)
		writeln("No");
	else
	{
		writeln("Yes");
		writeln(ans1);
		writeln(ans2);
	}
	stdout.flush;
	debug readln;
}


import core.bitop;
import core.checkedint;
import core.simd;
import core.stdc.stdlib;
import core.stdc.string;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.complex;
import std.container;
import std.conv;
import std.datetime;
import std.exception;
import std.format;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.range;
import std.regex;
import std.stdio;
import std.string;
import std.typecons;
import std.variant;

__gshared:

bool read(T...)(T ptrs) if (ptrs.length > 0) {
    return readf(' ' ~ replicate("%s ", ptrs.length), ptrs) == ptrs.length;
}

T[ ] allocArray(T)(size_t n) {
    return (cast(T*)malloc(n * T.sizeof))[0 .. n];
}

auto pairwise(R)(R range) if (isForwardRange!R) {
    return lockstep(range, dropOne(range));
}

int n, m, k;
int[100] _colors;
int[ ] colors;
int[100][100] _p;
int[100][ ] p;
ulong[101][101][100] dp;//[pos][groupsLeft][prevColor]

enum IMPOSSIBLE = ulong.max >>> 2;

ulong dyn(int pos, int groupsLeft, int prevColor) {
    if (pos == n)
        return groupsLeft ? IMPOSSIBLE : 0;
    if (~dp[pos][groupsLeft][prevColor])
        return dp[pos][groupsLeft][prevColor];
    ulong result = IMPOSSIBLE;
    if (colors[pos]) {
        if (prevColor == colors[pos])
            result = dyn(pos + 1, groupsLeft, colors[pos]);
        else if (groupsLeft)
            result = dyn(pos + 1, groupsLeft - 1, colors[pos]);
    } else {
        if (prevColor)
            result = dyn(pos + 1, groupsLeft, prevColor) + p[pos][prevColor - 1];
        if (groupsLeft)
            foreach (i; 1 .. m + 1)
                if (i != prevColor)
                    result = min(result, dyn(pos + 1, groupsLeft - 1, i) + p[pos][i - 1]);
    }
    return (dp[pos][groupsLeft][prevColor] = result);
}

void main() {
    while (read(&n, &m, &k)) {
        colors = _colors[0 .. n];
        p = _p[0 .. n];
        foreach (ref x; colors)
            read(&x);
        foreach (ref row; p)
            foreach (ref x; row[0 .. m])
                read(&x);
        memset(dp.ptr, 0xFF, dp.sizeof);
        const result = dyn(0, k, 0);
        if (result < IMPOSSIBLE)
            writeln(result);
        else
            write("-1\n");
    }
}

import core.bitop, std.bitmanip;
import std.algorithm, std.functional;
import std.array, std.container;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

immutable long INF = 10L ^^ 18 + 23;

void main()
{
    int n, m, k;
    readf("%s %s %s", &n, &m, &k);
    readln;

    auto c = [0] ~ readln.chomp.split.map!(to!int).array;

    auto p = new int[][] (n+1);
    foreach (i; 1 .. n+1) p[i] = [0] ~ readln.chomp.split.map!(to!int).array;

    debug { c.writeln; p.writeln; }

    auto dp = new long[][][] (n+1, k+1, m+1);
    foreach (i; 0 .. n+1) {
        foreach (j; 0 .. k+1) {
            dp[i][j].fill(INF);
        }
    }

    dp[0][0].fill(0);
    if (c[1] != 0) dp[1][1][c[1]] = 0;
    else dp[1][1][] = p[1].map!(to!long).array;

    foreach (i; 2 .. n+1) {
        foreach (j; 1 .. k+1) {
            foreach (pc; 1 .. m+1) {
                if (c[i] != 0) {
                    auto prevGroup = pc == c[i] ? j : j-1;
                    dp[i][j][c[i]] = min(dp[i][j][c[i]], dp[i-1][prevGroup][pc]);

                    continue;
                }

                foreach (cc; 1 .. m+1) {
                    auto prevGroups = pc == cc ? j : j-1;
                    dp[i][j][cc] = min(dp[i][j][cc], dp[i-1][prevGroups][pc] + p[i][cc]);
                }
            }
        }
    }

    debug { dp.each!(r => r.each!writeln); }

    auto ans = dp[n][k].dropOne.minElement;
    if (ans == INF) ans = -1;
    ans.writeln;
}

import core.bitop, std.bitmanip;
import std.algorithm, std.functional;
import std.array, std.container;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

immutable long INF = 10L ^^ 18 + 23;

void main()
{
    int n, m, k;
    readf("%s %s %s", &n, &m, &k);
    readln;

    auto c = [0] ~ readln.chomp.split.map!(to!int).array;

    auto p = new int[][] (n+1);
    foreach (i; 1 .. n+1) p[i] = [int.max] ~ readln.chomp.split.map!(to!int).array;

    debug { c.writeln; p.writeln; }

    auto mn = new int[][][] (n+1, k+1, 2);
    foreach (i; 1 .. n+1) foreach (j; 1 .. k+1) mn[i][j].fill(0);

    auto dp = new long[][][] (n+1, k+1, m+1);
    foreach (i; 0 .. n+1) {
        foreach (j; 0 .. k+1) {
            dp[i][j].fill(INF);
        }
    }

    dp[0][0].fill(0);

    foreach (i; 1 .. n+1) {
        foreach (j; 1 .. min(i+1, k+1)) {
            void calcMin(int cc, int cost) {
                if (i == 1 && j == 1) {
                    dp[i][j][cc] = cost;
                    return;
                }

                dp[i][j][cc] = min(dp[i][j][cc], dp[i-1][j][cc] + cost);

                auto mnOther = mn[i-1][j-1][0] != cc ? mn[i-1][j-1][0] : mn[i-1][j-1][1];
                dp[i][j][cc] = min(dp[i][j][cc], dp[i-1][j-1][mnOther] + cost);
            }

            if (c[i] != 0) {
                calcMin(c[i], 0);
            } else {
                foreach (cc; 1 .. m+1) {
                    calcMin(cc, p[i][cc]);
                }
            }

            foreach (cc; 1 .. m+1) {
                if (dp[i][j][cc] < dp[i][j][mn[i][j][0]]) mn[i][j][1] = mn[i][j][0], mn[i][j][0] = cc;
                else if (dp[i][j][cc] < dp[i][j][mn[i][j][1]]) mn[i][j][1] = cc;
            }
        }
    }

    debug {
        mn.dropOne.each!writeln;

        foreach (i; 1 .. n+1) {
            foreach (j; 1 .. k+1) dp[i][j].writeln;
            writeln;
        }
    }

    auto ans = dp[n][k].dropOne.minElement;
    if (ans == INF) ans = -1;
    ans.writeln;
}

import core.bitop;
import core.checkedint;
import core.simd;
import core.stdc.stdlib;
import core.stdc.string;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.complex;
import std.container;
import std.conv;
import std.datetime;
import std.exception;
import std.format;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.range;
import std.regex;
import std.stdio;
import std.string;
import std.typecons;
import std.variant;

__gshared:

bool read(T...)(T ptrs) if (ptrs.length > 0) {
    return readf(' ' ~ replicate("%s ", ptrs.length), ptrs) == ptrs.length;
}

T[ ] allocArray(T)(size_t n) {
    return (cast(T*)malloc(n * T.sizeof))[0 .. n];
}

auto pairwise(R)(R range) if (isForwardRange!R) {
    return lockstep(range, dropOne(range));
}

int n, m, k;
int[100] _colors;
int[ ] colors;
int[100][100] _p;
int[100][ ] p;
ulong[101][101][100] dp;//[pos][groupsLeft][prevColor]

enum IMPOSSIBLE = (ulong.max - 1) >>> 2;

ulong dyn(int pos, int groupsLeft, int prevColor) {
    if (pos == n)
        return groupsLeft ? IMPOSSIBLE : 0;
    if (~dp[pos][groupsLeft][prevColor])
        return dp[pos][groupsLeft][prevColor];
    ulong result = IMPOSSIBLE;
    if (colors[pos]) {
        if (prevColor == colors[pos])
            result = dyn(pos + 1, groupsLeft, colors[pos]);
        else if (groupsLeft)
            result = dyn(pos + 1, groupsLeft - 1, colors[pos]);
    } else {
        if (prevColor)
            result = dyn(pos + 1, groupsLeft, prevColor) + p[pos][prevColor - 1];
        if (groupsLeft)
            foreach (i; 1 .. m + 1)
                if (i != prevColor)
                    result =
                        min(result, dyn(pos + 1, groupsLeft - 1, i) + p[pos][i - 1]);
    }
    return (dp[pos][groupsLeft][prevColor] = result);
}

void main() {
    while (read(&n, &m, &k)) {
        colors = _colors[0 .. n];
        p = _p[0 .. n];
        foreach (ref x; colors)
            read(&x);
        foreach (ref row; p)
            foreach (ref x; row[0 .. m])
                read(&x);
        memset(dp.ptr, 0xFF, dp.sizeof);
        const result = dyn(0, k, 0);
        if (result != IMPOSSIBLE)
            writeln(result);
        else
            write("-1\n");
    }
}

import core.bitop, std.bitmanip;
import std.algorithm, std.functional;
import std.array, std.container;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

immutable long INF = 10L ^^ 18 + 23;

void main()
{
    int n, m, k;
    readf("%s %s %s", &n, &m, &k);
    readln;

    auto c = [0] ~ readln.chomp.split.map!(to!int).array;

    auto p = new int[][] (n+1);
    foreach (i; 1 .. n+1) p[i] = [int.max] ~ readln.chomp.split.map!(to!int).array;

    debug { c.writeln; p.writeln; }

    auto mn = new int[][][] (n+1, k+1, 2);
    foreach (i; 1 .. n+1) foreach (j; 1 .. k+1) mn[i][j].fill(0);

    auto dp = new long[][][] (n+1, k+1, m+1);
    foreach (i; 0 .. n+1) {
        foreach (j; 0 .. k+1) {
            dp[i][j].fill(INF);
        }
    }

    dp[0][0].fill(0);

    foreach (i; 2 .. n+1) {
        foreach (j; 1 .. min(i+1, k+1)) {
            void calcMin(int cc, int cost) {
                if (i == 1 && j == 1) {
                    dp[i][j][cc] = cost;
                    return;
                }

                dp[i][j][cc] = min(dp[i][j][cc], dp[i-1][j][cc] + cost);

                auto mnOther = mn[i-1][j-1][0] != cc ? mn[i-1][j-1][0] : mn[i-1][j-1][1];
                dp[i][j][cc] = min(dp[i][j][cc], dp[i-1][j-1][mnOther] + cost);
            }

            if (c[i] != 0) {
                calcMin(c[i], 0);
            } else {
                foreach (cc; 1 .. m+1) {
                    calcMin(cc, p[i][cc]);
                }
            }

            foreach (cc; 1 .. m+1) {
                if (dp[i][j][cc] < dp[i][j][mn[i][j][0]]) mn[i][j][1] = mn[i][j][0], mn[i][j][0] = cc;
                else if (dp[i][j][cc] < dp[i][j][mn[i][j][1]]) mn[i][j][1] = cc;
            }
        }
    }

    debug {
        mn.dropOne.each!writeln;

        foreach (i; 1 .. n+1) {
            foreach (j; 1 .. k+1) dp[i][j].writeln;
            writeln;
        }
    }

    auto ans = dp[n][k].dropOne.minElement;
    if (ans == INF) ans = -1;
    ans.writeln;
}

import core.bitop, std.bitmanip;
import std.algorithm, std.functional;
import std.array, std.container;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

immutable long INF = 10L ^^ 18 + 23;

void main()
{
    int n, m, k;
    readf("%s %s %s", &n, &m, &k);
    readln;

    auto c = [0] ~ readln.chomp.split.map!(to!int).array;

    auto p = new int[][] (n+1);
    foreach (i; 1 .. n+1) p[i] = [int.max] ~ readln.chomp.split.map!(to!int).array;

    debug { c.writeln; p.writeln; }

    auto mn = new int[][][] (n+1, k+1, 2);
    foreach (i; 1 .. n+1) foreach (j; 1 .. k+1) mn[i][j].fill(0);

    auto dp = new long[][][] (n+1, k+1, m+1);
    foreach (i; 0 .. n+1) {
        foreach (j; 0 .. k+1) {
            dp[i][j].fill(INF);
        }
    }

    dp[0][0].fill(0);
    if (c[1] != 0) dp[1][1][c[1]] = 0;
    else dp[1][1][] = p[1].map!(to!long).array;

    foreach (i; 2 .. n+1) {
        foreach (j; 1 .. k+1) {
            void calcMin(int cc, int cost) {
                debug { if (i == 2 && j == 2) writeln(cost); }
                dp[i][j][cc] = min(dp[i][j][cc], dp[i-1][j][cc] + cost);

                auto mnOther = mn[i-1][j-1][0] != cc ? mn[i-1][j-1][0] : mn[i-1][j-1][1];
                dp[i][j][cc] = min(dp[i][j][cc], dp[i-1][j-1][mnOther] + cost);
            }

            if (c[i] != 0) {
                calcMin(c[i], 0);
                continue;
            }

            foreach (cc; 1 .. m+1) {
                calcMin(cc, p[i][cc]);
            }

            foreach (cc; 1 .. m+1) {
                if (dp[i][j][cc] < dp[i][j][mn[i][j][0]]) mn[i][j][1] = mn[i][j][0], mn[i][j][0] = cc;
                else if (dp[i][j][cc] < dp[i][j][mn[i][j][1]]) mn[i][j][1] = cc;
            }
        }
    }

    debug { mn.dropOne.each!writeln; }

    debug { foreach (i; 1 .. n+1) { dp[i].each!(r => r.dropOne.writeln); writeln; }  }

    auto ans = dp[n][k].dropOne.minElement;
    if (ans == INF) ans = -1;
    ans.writeln;
}

import core.bitop, std.bitmanip;
import std.algorithm, std.functional;
import std.array, std.container;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

immutable long INF = 10L ^^ 18 + 23;

void main()
{
    int n, m, k;
    readf("%s %s %s", &n, &m, &k);
    readln;

    auto c = [0] ~ readln.chomp.split.map!(to!int).array;

    auto p = new int[][] (n+1);
    foreach (i; 1 .. n+1) p[i] = [int.max] ~ readln.chomp.split.map!(to!int).array;

    debug { c.writeln; p.writeln; }

    auto mn = new int[][][] (n+1, k+1, 2);
    foreach (i; 1 .. n+1) foreach (j; 1 .. k+1) mn[i][j].fill(0);

    auto dp = new long[][][] (n+1, k+1, m+1);
    foreach (i; 0 .. n+1) {
        foreach (j; 0 .. k+1) {
            dp[i][j].fill(INF);
        }
    }

    dp[0][0].fill(0);
    if (c[1] != 0) dp[1][1][c[1]] = 0;
    else dp[1][1][] = p[1].map!(to!long).array;

    mn[1][1] = p[1].enumerate.array.sort!((t1, t2) => t1[1] < t2[1])
        .map!(t => t[0].to!int).array.take(2).array;

    foreach (i; 2 .. n+1) {
        foreach (j; 1 .. k+1) {
            void calcMin(int cc, int cost) {
                dp[i][j][cc] = min(dp[i][j][cc], dp[i-1][j][cc] + cost);

                auto mnOther = mn[i-1][j-1][0] != cc ? mn[i-1][j-1][0] : mn[i-1][j-1][1];
                dp[i][j][cc] = min(dp[i][j][cc], dp[i-1][j-1][mnOther] + cost);
                debug { if (i == 1 && j == 1) writeln(cost, ' ', dp[i][j][cc]); }
            }

            if (c[i] != 0) {
                calcMin(c[i], 0);
            } else {
                foreach (cc; 1 .. m+1) {
                    calcMin(cc, p[i][cc]);
                }
            }

            foreach (cc; 1 .. m+1) {
                if (dp[i][j][cc] < dp[i][j][mn[i][j][0]]) mn[i][j][1] = mn[i][j][0], mn[i][j][0] = cc;
                else if (dp[i][j][cc] < dp[i][j][mn[i][j][1]]) mn[i][j][1] = cc;
            }
        }
    }

    debug {
        mn.dropOne.each!writeln;

        foreach (i; 1 .. n+1) {
            foreach (j; 1 .. k+1) dp[i][j].writeln;
            writeln;
        }
    }

    auto ans = dp[n][k].dropOne.minElement;
    if (ans == INF) ans = -1;
    ans.writeln;
}

import core.bitop, std.bitmanip;
import std.algorithm, std.functional;
import std.array, std.container;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

immutable long INF = 10L ^^ 18 + 23;

void main()
{
    int n, m, k;
    readf("%s %s %s", &n, &m, &k);
    readln;

    auto c = [0] ~ readln.chomp.split.map!(to!int).array;

    auto p = new int[][] (n+1);
    foreach (i; 1 .. n+1) p[i] = [0] ~ readln.chomp.split.map!(to!int).array;

    debug { c.writeln; p.writeln; }

    auto dp = new long[][][] (n+1, k+1, m+1);
    foreach (i; 0 .. n+1) {
        foreach (j; 0 .. k+1) {
            dp[i][j].fill(INF);
        }
    }

    foreach (j; 0 .. k+1) dp[0][j].fill(0);

    foreach (i; 1 .. n+1) {
        foreach (j; 1 .. k+1) {
            foreach (pc; 1 .. m+1) {
                if (c[i] != 0) {
                    auto prevGroup = pc == c[i] ? j : j-1;
                    dp[i][j][c[i]] = min(dp[i][j][c[i]], dp[i-1][prevGroup][pc]);

                    continue;
                }

                foreach (cc; 1 .. m+1) {
                    auto prevGroups = pc == cc ? j : j-1;
                    dp[i][j][cc] = min(dp[i][j][cc], dp[i-1][prevGroups][pc] + p[i][cc]);
                }
            }
        }
    }

    auto ans = dp[n][k].minElement;
    if (ans == INF) ans = -1;
    ans.writeln;
}


import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	int tests;
	readf !(" %s") (tests);
	readln;
	foreach (test; 0..tests)
	{
		immutable int n = 9;
		foreach (i; 0..n)
		{
			auto s = readln.strip.dup;
			s.find ('9')[0] = '1';
			writeln (s);
		}
	}
}

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.ascii, std.typecons;

void main()
{
    int t;
    readf("%s", &t);
    readln;

    while (t--) {
        auto arr = new dchar[][] (9);
        foreach (r; 0 .. 9) {
            arr[r] = readln.chomp.map!(to!dchar).array;
        }

        int c = 0;
        foreach (r; 0 .. 9) {
            arr[r][c] = arr[r][(c+1)%9];
            c += 3;
            if (c >= 9) {
                c %= 9;
                c += 1;
            }
        }

        foreach (r; 0 .. 9) {
            writeln(arr[r]);
        }
    }
}

import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;

char change(char c) {
    c += 1;
    if (c > '9') c = '1';
    return c;
}

void main()
{
    auto T = readln.chomp.to!int;
    foreach (_; 0..T) {
        char[9][9] S;
        foreach (i; 0..9) foreach (j, c; readln.chomp) S[i][j] = c;
        S[0][0] = change(S[0][0]);
        S[1][3] = change(S[1][3]);
        S[2][6] = change(S[2][6]);
        S[3][1] = change(S[3][1]);
        S[4][4] = change(S[4][4]);
        S[5][7] = change(S[5][7]);
        S[6][2] = change(S[6][2]);
        S[7][5] = change(S[7][5]);
        S[8][8] = change(S[8][8]);
        foreach (s; S) writeln(s);
    }
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }

void main()
{
	auto t = RD!int;
	auto ans = new char[][][](t);
	foreach (ti; 0..t)
	{
		auto s = new char[][](9);
		foreach (i; 0..9)
		{
			s[i] = RD!(char[]);
		}

		foreach (i; 0..3)
		{
			foreach (j; 0..3)
			{
				if (i % 3 == 2)
					s[i+j*3][i*3+j] = s[i+j*3-1][i*3+j];
				else
					s[i+j*3][i*3+j] = s[i+j*3+1][i*3+j];
			}
		}
		ans[ti] = s;
	}

	foreach (e; ans)
	{
		foreach (ee; e)
			writeln(ee);
	}
	stdout.flush;
	debug readln;
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }

void main()
{
	auto t = RD!int;
	auto ans = new char[][][](t);
	foreach (ti; 0..t)
	{
		auto s = new char[][](9);
		foreach (i; 0..9)
		{
			s[i] = RD!(char[]);
		}

		foreach (i; 0..9)
		{
			if (i % 3 == 2)
				s[i][i] = s[i-1][i];
			else
				s[i][i] = s[i+1][i];
		}
		ans[ti] = s;
	}

	foreach (e; ans)
	{
		foreach (ee; e)
			writeln(ee);
	}
	stdout.flush;
	debug readln;
}


import std.stdio;
import std.algorithm;
import std.range;
import std.string;
import std.conv;

void main()
{
    readln;

    auto items = readln
                .split
                .map!(a => to!uint(a))
                .array
                .sort!"a > b";

    uint summ = items
                .save
                .reduce!((a,b) => a+b);

    uint money;
    uint halfMoney = summ / 2;
    uint counter;

    foreach(item; items)
    {
        money += item;
        ++counter;
        if (money > halfMoney) break;
    }

    counter.writeln;
}

import std.stdio;
import std.algorithm.sorting;

/* Condizioni:
   - valore maggiore del restante;
   - minor numero possibile di monete;
*/
ulong sum(uint[] coins) {
  ulong sum = 0;
  foreach(coin; coins) {
    sum += coin;
  }
  return sum;
}

uint minimum_coins(uint[] coins) {
  auto sortedCoins = sort!("a > b")(coins);
  auto sum = sum(coins);
  uint mine = 0, n_coins=0;
  foreach(coin; sortedCoins) {
    mine += coin;
    n_coins++;
    if (mine > sum / 2) {
      break;
    }
  }
  return n_coins;
}

int main() {
  uint n, coin;
  uint[] coins;
  readf("%s", &n);
  for(int i = 0; i < n; i++) {
    readf(" %s", &coin);
    coins ~= coin;
  }
  auto n_coins = minimum_coins(coins);
  write(n_coins);
  return 0;
}

import std.stdio;
import std.uni;
import std.string;
import core.stdc.stdio;
import std.array;
import std.algorithm;

int main(string[] argv)
{
	int n;
	scanf("%d", &n);
	int[] coins = new int[n];
	foreach (int i; 0..n)
	{
		int a;
		scanf("%d", &a);
		coins[i] = a;
	}

	sort!((a, b) => a > b)(coins);
	int sum = 0;
	foreach (int i; coins)
	{
		sum += i;
	}
	int ms = 0;
	int noc = 0;
	foreach (int i; coins)
	{
		ms += i;
		noc++;
		sum -= i;
		if (ms > sum)
			break;
	}
	printf("%d", noc);
	return 0;
}

import std.stdio;
import std.algorithm;
import std.range;
import std.string;
import std.conv;
import std.math;

void main()
{
    readln;
    auto items = readln
                .split
                .map!(a => to!uint(a))
                .array
                .sort!"a > b";
    uint summ = items
                .save
                .reduce!((a,b) => a+b);

    uint money;
    uint halfMoney = cast(uint)ceil(cast(double)summ/2);
    uint counter;

    foreach(item; items)
    {
        money += item;
        ++counter;
        if (money > halfMoney) break;
    }

    counter.writeln;
}


import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto s = readln.strip;

		int balance = 0;
		int res = 0;
		auto num = new int [n + 1];
		foreach (ref c; s)
		{
			if (c == '(')
			{
				res += (num[balance] == 0);
				num[balance] += 1;
				balance += 1;
			}
			else
			{
				num[balance] = 0;
				balance -= 1;
			}
		}
		writeln (res);
	}
}

import std;
void main () {
	foreach (r; 0..readln.strip.to !(int)) {
		auto n = readln.strip.to !(int);
		writeln (n - readln.strip.count (")("));
	}
}


import std.math;
import std.typecons;
import std.algorithm;

alias Tuple!(int, "x", int, "y") Point;

bool solve(const Point a, const Point b, out Point c, out Point d) {
	auto dx = abs(a.x - b.x);
	auto dy = abs(a.y - b.y);
	if (0 != dx && 0 != dy && dx != dy) {
		return false;
	}

	auto dd = max(dx, dy);
	auto addX = (0 == dx) ? dd : 0;
	auto addY = (0 == dy) ? dd : 0;
	c.x = a.x + addX;
	c.y = b.y + addY;
	d.x = b.x + addX;
	d.y = a.y + addY;

	return true;
}

unittest {
	{
		Point c, d;
		assert(solve(Point(0, 0), Point(0, 1), c, d));
	}
}

int main(string[] argv) {
	import std.stdio;
	Point a, b, c, d;
	while (readf(" %s %s %s %s", &(a.x), &(a.y), &(b.x), &(b.y))) {
		if (solve(a, b, c, d)) {
			writeln(c.x, ' ', c.y, ' ', d.x, ' ', d.y);
		} else {
			writeln(-1);
		}
	}
    return 0;
}