-- Try yukicoder -- author: Leonardone @ NEETSDKASU import Data.List (any, sort) main = putStrLn . unlines . solve . map read . tail . words =<< getContents solve x = map show . reverse $ foo [] x where foo a [] = a foo a (n1:n2:m:xs) = foo b zs where (ys, zs) = splitAt m xs ss = sort ys n = n1 + n2 (u, v) = foldl (\(uu, vv) w -> if uu > n then (uu, vv) else (uu + w, vv + 1)) (0, 0) ss ks = take v ss [mn, mx] = sort [n1, n2] fff xx ps = foldl (\(rk, ki) vk -> let xk = rk + vk in if xk <= xx then (xk, ki + 1) else (rk ,ki)) (0, 0) ps (dk, di) = fff mn ks (h1, h2) = splitAt di ks ek = sum (0 : h2) r = case mn - dk of q | q == mn -> fi where (_, fi) = fff mx ks 0 -> if ek > mx then v - 1 else v _ -> if ub then ui + fi else v - 1 where (ub, ui, ww) = bar ss 0 0 [] uu = sort ww (_, fi) = fff mx uu bar tts v1 i oo = case compare v1 mn of EQ -> (True, i, oo ++ tts) GT -> (False, i, oo ++ tts) LT -> case tts of [] -> (False, i, oo) t:ts -> if zi > ai then (zb, zi, pp) else (ab, ai, ap) where (zb, zi, pp) = bar ts (v1 + t) (i + 1) oo (ab, ai, ap) = if zb then (zb, zi, pp) else (bar ts v1 i (t : oo)) b = r : a