{-# LANGUAGE MultiWayIf #-}
module Main where

main :: IO ()
main = do
  (t1, t2) <- pair
  cinema <- pair
  home <- pair
  shop <- pair
  print $ solve t1 t2 cinema home shop

solve :: Double -> Double -> Point -> Point -> Point -> Double
solve t1 t2 c h s = if
    | ch + t2 >= cs + sh -> maxPath
    | cs + sh + t1 >= ch + t2 + 2 * sh -> ch + t2
    | otherwise -> search 0 maxPath
  where
    sh = dist s h
    cs = dist c s
    ch = dist c h

    maxPath = min (ch + t2) (cs + sh + t1)

    search l r = let x = (r + l) / 2 in if
        | r - l < 1e-6 -> x
        | isPossible x -> search x r
        | otherwise -> search l x
    isPossible x = check (c, x) (h, max 0 $ ch + t2 - x) (s, max 0 $ cs + t1 - x)

type Point = (Double, Double)
type Circle = (Point, Double)

check :: Circle -> Circle -> Circle -> Bool
check c1 c2 c3 = any f [(c1, c2, c3), (c1, c3, c2), (c2, c3, c1)]
  where
    f (c1, c2, c3) = case intersect c1 c2 of
      NOT_INTERSECT -> False
      CONTAIN c0 -> intersect c3 c0 /= NOT_INTERSECT
      POINTS ps -> any (inside c3) ps

inside :: Circle -> Point -> Bool
inside (c, r) p =  dist c p < r + eps

(|+) :: Point -> Point -> Point
(x1, y1) |+ (x2, y2) = (x1 + x2, y1 + y2)

(|-) :: Point -> Point -> Point
(x1, y1) |- (x2, y2) = (x1 - x2, y1 - y2)

scale :: Point -> Double -> Point
scale (x, y) k = (x * k, y * k)

eps :: Double
eps = 1e-10

data Intersection = NOT_INTERSECT | POINTS [Point] | CONTAIN Circle deriving (Show, Eq)

intersect :: Circle -> Circle -> Intersection
intersect a b = if
    | r2 > r1 -> intersect b a
    | abs (r1 - r2) > l + eps -> CONTAIN b
    | r1 + r2 + eps < l -> NOT_INTERSECT
    | r1 + r2 < l + eps -> POINTS [c1 |+ scale (c2 |- c1) (r1 / l)]
    | otherwise -> POINTS [ u |+ v, u |- v]
  where
    (c1, r1) = a
    (c2, r2) = b
    l = dist c1 c2
    la = (r1 * r1 + l * l - r2 * r2) / (2 * l)
    h = sqrt $ r1 * r1 - la * la
    u = c1 |+ scale (c2 |- c1) (la / l)
    (x1, y1) = c1
    (x2, y2) = c2
    v = scale (y1 - y2, x2 - x1) (h / l)

dist :: Point -> Point -> Double
dist p q = norm (p |- q)

norm :: Point -> Double
norm (x, y) = sqrt $ x * x + y * y

pair :: IO (Double, Double)
pair = words <$> getLine >>= \[x, y] -> pure (read x, read y)

import Debug.Trace

data Point = Point { x :: Double, y :: Double } deriving Show

eps = 1e-10

add p1 p2 = Point { x = x p1 + x p2, y = y p1 + y p2 }
mult p k = Point { x = k * x p, y = k * y p }

dist p1 p2 = sqrt $ sqr (x p1 - x p2) + sqr (y p1 - y p2) where
	sqr x = x * x

solve t1' t2' ps pm pt =
	if d1 < t2 + eps
		then min t1 t2
		else maximum [maxtime 0, maxtime 1, ternary_search maxtime 0 1]
	where
		dmt = dist pm pt
		d1 = dist ps pm + dmt
		d2 = dist ps pt
		t1 = t1' + d1
		t2 = t2' + d2
		ternary_search f l r =
			if r - l <= eps
				then f $ (l + r) / 2
				else let
						m1 = (2 * l + r) / 3
						m2 = (l + 2 * r) / 3
				     in
				     	if f m1 > f m2
				     		then ternary_search f l m2
				     		else ternary_search f m1 r
		binary_search f l r =
			if r - l <= eps
				then l
				else
					if f $ (l + r) / 2
						then binary_search f ((l + r) / 2) r
						else binary_search f l ((l + r) / 2)
		maxtime rat =
			let
				p = add (mult pm $ 1 - rat) (mult pt rat)
				d1 = dist ps p
				d2 = dist pt p
				chk x =
					dist ps p' + dist p' pm + dmt < t1 + eps && dist ps p' + dist p' pt < t2 + eps
					where
						p' = add (mult ps (1 - x)) (mult p x)

			in
				if d1 + d2 < t2 + eps && d1 + dmt * (1 + rat) < t1 + eps
					then min (t2 - d2) (t1 - dmt * (1 + rat))
					else dist ps p * binary_search chk 0 1

main = do
	[t1, t2, x0, y0, x2, y2, x1, y1] <- (map read . words) `fmap` getContents
	putStrLn $ show $ solve t1 t2 Point {x = x0, y = y0} Point {x = x1, y = y1} Point {x = x2, y = y2}

{-# LANGUAGE MultiWayIf #-}
module Main where

main :: IO ()
main = do
  (t1, t2) <- pair
  cinema <- pair
  home <- pair
  shop <- pair
  print $ solve t1 t2 cinema home shop

solve :: Double -> Double -> Point -> Point -> Point -> Double
solve t1 t2 c h s = if
    | ch + t2 >= cs + sh -> maxPath
    | otherwise -> search 0 maxPath
  where
    sh = dist s h
    cs = dist c s
    ch = dist c h

    maxPath = min (ch + t2) (cs + sh + t1)

    search l r = let x = (r + l) / 2 in if
        | r - l < 1e-6 -> x
        | isPossible x -> search x r
        | otherwise -> search l x
    isPossible x = check (c, x) (h, max 0 $ ch + t2 - x) (s, max 0 $ cs + t1 - x)

type Point = (Double, Double)
type Circle = (Point, Double)

check :: Circle -> Circle -> Circle -> Bool
check c1 c2 c3 = any f [(c1, c2, c3), (c1, c3, c2), (c2, c3, c1)]
  where
    f (c1, c2, c3) = case intersect c1 c2 of
      NOT_INTERSECT -> False
      CONTAIN c0 -> intersect c3 c0 /= NOT_INTERSECT
      POINTS ps -> any (inside c3) ps

inside :: Circle -> Point -> Bool
inside (c, r) p =  dist c p < r + eps

(|+) :: Point -> Point -> Point
(x1, y1) |+ (x2, y2) = (x1 + x2, y1 + y2)

(|-) :: Point -> Point -> Point
(x1, y1) |- (x2, y2) = (x1 - x2, y1 - y2)

scale :: Point -> Double -> Point
scale (x, y) k = (x * k, y * k)

eps :: Double
eps = 1e-10

data Intersection = NOT_INTERSECT | POINTS [Point] | CONTAIN Circle deriving (Show, Eq)

intersect :: Circle -> Circle -> Intersection
intersect a b = if
    | r2 > r1 -> intersect b a
    | abs (r1 - r2) > l + eps -> CONTAIN b
    | r1 + r2 + eps < l -> NOT_INTERSECT
    | r1 + r2 < l + eps -> POINTS [c1 |+ scale (c2 |- c1) (r1 / l)]
    | otherwise -> POINTS [ u |+ v, u |- v]
  where
    (c1, r1) = a
    (c2, r2) = b
    l = dist c1 c2
    la = (r1 * r1 + l * l - r2 * r2) / (2 * l)
    h = sqrt $ r1 * r1 - la * la
    u = c1 |+ scale (c2 |- c1) (la / l)
    (x1, y1) = c1
    (x2, y2) = c2
    v = scale (y1 - y2, x2 - x1) (h / l)

dist :: Point -> Point -> Double
dist p q = norm (p |- q)

norm :: Point -> Double
norm (x, y) = sqrt $ x * x + y * y

pair :: IO (Double, Double)
pair = words <$> getLine >>= \[x, y] -> pure (read x, read y)

{-# LANGUAGE MultiWayIf #-}
module Main where
import Data.List (minimumBy)
import Data.Ord (comparing)

main :: IO ()
main = do
  (t1, t2) <- pair
  cinema <- pair
  home <- pair
  shop <- pair
  print $ solve t1 t2 cinema home shop

solve :: Double -> Double -> (Double, Double) -> (Double, Double) -> (Double, Double) -> Double
solve t1 t2 c h s = if
    | ch + t2 >= cs + sh -> min (ch + t2) (cs + sh + t1)
    | cs + sh + t1 >= ch + t2 + 2 * sh -> ch + t2
    | d <= r -> r
    | otherwise -> search 0 r
  where
    sh = dist s h
    cs = dist c s
    ch = dist c h

    r = (cs + t1 + ch + t2 - sh) / 2
    sr = (cs + t1 - r) / sh
    hr = (ch + t2 - r) / sh

    cs_ = cs + t1
    ch_ = ch + t2

    (hx, hy) = h
    (sx, sy) = s
    (cx, cy) = c

    x = sx * hr + hx * sr
    y = sy * hr + hy * sr

    d = sqrt $ (cx - x) ^ 2 + (cy - y) ^ 2

    a = angle cs ch sh

    search l r = let x = (r + l) / 2 in if
        | r - l < 1e-6 -> x
        | isPossible x -> search x r
        | otherwise -> search l x

    isPossible x = angle cs x (cs + t1 - x) + angle ch x (ch + t2 - x) > a

angle :: Double -> Double -> Double -> Double
angle a b c = acos . max (-1) . min 1 $ (a * a + b * b - c * c) / (2 * a * b)

dist :: (Double, Double) -> (Double, Double) -> Double
dist (x1, y1) (x2, y2) = sqrt $ (x1 - x2) ^ 2 + (y1 - y2) ^ 2

pair :: IO (Double, Double)
pair = words <$> getLine >>= \[x, y] -> pure (read x, read y)

{-# LANGUAGE MultiWayIf #-}
module Main where
import Data.List (permutations)

main :: IO ()
main = do
  (t1, t2) <- pair
  cinema <- pair
  home <- pair
  shop <- pair
  print $ solve t1 t2 cinema home shop

solve :: Double -> Double -> Point -> Point -> Point -> Double
solve t1 t2 c h s = if
    | ch + t2 >= cs + sh -> maxPath
    | cs + sh + t1 >= ch + t2 + 2 * sh -> ch + t2
    | otherwise -> search 0 maxPath
  where
    maxPath = min (ch + t2) (cs + sh + t1)

    sh = dist s h
    cs = dist c s
    ch = dist c h

    f (l,r) = let x = (r + l) / 2 in if isPossible x then (x, r) else (l, x)

    search l r = let x = (r + l) / 2 in if
        | r - l < 1e-6 -> x
        | isPossible x -> search x r
        | otherwise -> search l x
    isPossible x = check (c, x) (h, max 0 $ ch + t2 - x) (s, max 0 $ cs + t1 - x)

type Point = (Double, Double)
type Circle = (Point, Double)

check :: Circle -> Circle -> Circle -> Bool
check c1 c2 c3 = any f $ [(c1, c2, c3), (c1, c3, c2), (c2, c3, c1)]
  where
    f (c1, c2, c3) = case intersect c1 c2 of
      NOT_INTERSECT -> False
      CONTAIN c0 -> intersect c3 c0 /= NOT_INTERSECT
      POINTS ps -> any (inside c3) ps

inside :: Circle -> Point -> Bool
inside (c, r) p =  dist c p < r + 1e-10

(|+) :: Point -> Point -> Point
(x1, y1) |+ (x2, y2) = (x1 + x2, y1 + y2)

(|-) :: Point -> Point -> Point
(x1, y1) |- (x2, y2) = (x1 - x2, y1 - y2)

scale :: Point -> Double -> Point
scale (x, y) k = (x * k, y * k)

data Intersection = NOT_INTERSECT | POINTS [Point] | CONTAIN Circle deriving (Show, Eq)

intersect :: Circle -> Circle -> Intersection
intersect a b = if
    | r2 > r1 -> intersect b a
    | r1 + r2 < l -> NOT_INTERSECT
    | abs (r1 - r2) > l -> CONTAIN b
    | otherwise -> POINTS [ u |+ v, u |- v]
  where
    (c1, r1) = a
    (c2, r2) = b
    l = dist c1 c2
    la = (r1 * r1 + l * l - r2 * r2) / (2 * l)
    h = sqrt $ r1 * r1 - la * la
    u = c1 |+ scale (c2 |- c1) (la / l)
    (x1, y1) = c1
    (x2, y2) = c2
    v = scale (y1 - y2, x2 - x1) (h / l)

dist :: Point -> Point -> Double
dist p q = norm (p |- q)

norm :: Point -> Double
norm (x, y) = sqrt $ x * x + y * y

pair :: IO (Double, Double)
pair = words <$> getLine >>= \[x, y] -> pure (read x, read y)

{-# LANGUAGE MultiWayIf #-}
module Main where
import Data.List (permutations)

main :: IO ()
main = do
  (t1, t2) <- pair
  cinema <- pair
  home <- pair
  shop <- pair
  print $ solve t1 t2 cinema home shop

solve :: Double -> Double -> Point -> Point -> Point -> Double
solve t1 t2 c h s = if tsh + dist c s <= tB then min (tA + tsh) tB else search 0 tsh
  where
    tsh = dist s h
    tA =  dist c s + t1
    tB = dist c h + t2

    f (l,r) = let x = (r + l) / 2 in if isPossible x then (x, r) else (l, x)

    search l r = let x = (r + l) / 2 in if
        | r - l < 1e-5 -> x
        | isPossible x -> search x r
        | otherwise -> search l x
    isPossible x = check (c, x) (h, max 0 $ tB - x) (s, max 0 $ tA - x)

type Point = (Double, Double)
type Circle = (Point, Double)

eps :: Double
eps = 1e-5

check :: Circle -> Circle -> Circle -> Bool
check c1 c2 c3 = any f $ permutations [c1,c2,c3]
  where
    f [c1, c2, c3] = case intersect c1 c2 of
      NOT_INTERSECT -> False
      CONTAIN c0 -> intersect c3 c0 /= NOT_INTERSECT
      POINTS ps -> any (inside c3) ps

inside :: Circle -> Point -> Bool
inside (c, r) p =  dist c p <= r

(|+) :: Point -> Point -> Point
(x1, y1) |+ (x2, y2) = (x1 + x2, y1 + y2)

(|-) :: Point -> Point -> Point
(x1, y1) |- (x2, y2) = (x1 - x2, y1 - y2)

scale :: Point -> Double -> Point
scale (x, y) k = (x * k, y * k)

data Intersection = NOT_INTERSECT | POINTS [Point] | CONTAIN Circle deriving (Show, Eq)

intersect :: Circle -> Circle -> Intersection
intersect a@(c1@(x1, y1), r1) b@(c2@(x2, y2), r2)
  | r1 + r2 + eps < l = NOT_INTERSECT
  | abs (r1 - r2) > l + eps = CONTAIN $ if r1 > r2 then b else a
  | otherwise = POINTS [ u |+ v, u |- v]
  where
    l = dist c1 c2
    la = (r1 * r1 + l * l - r2 * r2) / (2 * l)
    h = sqrt $ r1 * r1 - la * la
    u = c1 |+ scale (c2 |- c1) (la / l)
    v = scale (y1- y2, x2 - x1) (h / l)

dist :: Point -> Point -> Double
dist p q = norm (p |- q)

norm :: Point -> Double
norm (x, y) = sqrt $ x * x + y * y

pair :: IO (Double, Double)
pair = words <$> getLine >>= \[x, y] -> pure (read x, read y)

{-# LANGUAGE MultiWayIf #-}
module Main where

main :: IO ()
main = do
  (t1, t2) <- pair
  cinema <- pair
  home <- pair
  shop <- pair
  print $ solve t1 t2 cinema home shop

solve :: Double -> Double -> Point -> Point -> Point -> Double
solve t1 t2 c h s = if
    | ch + t2 >= cs + sh -> maxPath
    | cs + sh + t1 >= ch + t2 + 2 * sh -> ch + t2
    | otherwise -> search 0 maxPath
  where
    sh = dist s h
    cs = dist c s
    ch = dist c h

    maxPath = min (ch + t2) (cs + sh + t1)

    f (l,r) = let x = (r + l) / 2 in if isPossible x then (x, r) else (l, x)

    search l r = let x = (r + l) / 2 in if
        | r - l < 1e-6 -> x
        | isPossible x -> search x r
        | otherwise -> search l x
    isPossible x = check (c, x) (h, max 0 $ ch + t2 - x) (s, max 0 $ cs + t1 - x)

type Point = (Double, Double)
type Circle = (Point, Double)

check :: Circle -> Circle -> Circle -> Bool
check c1 c2 c3 = any f $ [(c1, c2, c3), (c1, c3, c2), (c2, c3, c1)]
  where
    f (c1, c2, c3) = case intersect c1 c2 of
      NOT_INTERSECT -> False
      CONTAIN c0 -> intersect c3 c0 /= NOT_INTERSECT
      POINTS ps -> any (inside c3) ps

inside :: Circle -> Point -> Bool
inside (c, r) p =  dist c p < r + 1e-10

(|+) :: Point -> Point -> Point
(x1, y1) |+ (x2, y2) = (x1 + x2, y1 + y2)

(|-) :: Point -> Point -> Point
(x1, y1) |- (x2, y2) = (x1 - x2, y1 - y2)

scale :: Point -> Double -> Point
scale (x, y) k = (x * k, y * k)

data Intersection = NOT_INTERSECT | POINTS [Point] | CONTAIN Circle deriving (Show, Eq)

intersect :: Circle -> Circle -> Intersection
intersect a b = if
    | r1 + r2 + 1e-5 < l -> NOT_INTERSECT
    | r1 + r2 + 1e-5 > l -> POINTS [c1 |+ scale (c2 |- c1) (r1 / l)]
    | abs (r1 - r2) > l -> CONTAIN b
    | otherwise -> POINTS [ u |+ v, u |- v]
  where
    (c1, r1) = a
    (c2, r2) = b
    l = dist c1 c2
    la = (r1 * r1 + l * l - r2 * r2) / (2 * l)
    h = sqrt $ r1 * r1 - la * la
    u = c1 |+ scale (c2 |- c1) (la / l)
    (x1, y1) = c1
    (x2, y2) = c2
    v = scale (y1 - y2, x2 - x1) (h / l)

dist :: Point -> Point -> Double
dist p q = norm (p |- q)

norm :: Point -> Double
norm (x, y) = sqrt $ x * x + y * y

pair :: IO (Double, Double)
pair = words <$> getLine >>= \[x, y] -> pure (read x, read y)

{-# LANGUAGE MultiWayIf #-}
module Main where

main :: IO ()
main = do
  (t1, t2) <- pair
  cinema <- pair
  home <- pair
  shop <- pair
  print $ solve t1 t2 cinema home shop

solve :: Double -> Double -> Point -> Point -> Point -> Double
solve t1 t2 c h s = search 0 maxPath
  where
    sh = dist s h
    cs = dist c s
    ch = dist c h

    maxPath = min (ch + t2) (cs + sh + t1)

    search l r = let x = (r + l) / 2 in if
        | r - l < 1e-6 -> x
        | isPossible x -> search x r
        | otherwise -> search l x
    isPossible x = check (c, x) (h, max 0 $ ch + t2 - x) (s, max 0 $ cs + t1 - x)

type Point = (Double, Double)
type Circle = (Point, Double)

check :: Circle -> Circle -> Circle -> Bool
check c1 c2 c3 = any f [(c1, c2, c3), (c1, c3, c2), (c2, c3, c1)]
  where
    f (c1, c2, c3) = case intersect c1 c2 of
      NOT_INTERSECT -> False
      CONTAIN c0 -> intersect c3 c0 /= NOT_INTERSECT
      POINTS ps -> any (inside c3) ps

inside :: Circle -> Point -> Bool
inside (c, r) p =  dist c p < r + eps

(|+) :: Point -> Point -> Point
(x1, y1) |+ (x2, y2) = (x1 + x2, y1 + y2)

(|-) :: Point -> Point -> Point
(x1, y1) |- (x2, y2) = (x1 - x2, y1 - y2)

scale :: Point -> Double -> Point
scale (x, y) k = (x * k, y * k)

eps :: Double
eps = 1e-10

data Intersection = NOT_INTERSECT | POINTS [Point] | CONTAIN Circle deriving (Show, Eq)

intersect :: Circle -> Circle -> Intersection
intersect a b = if
    | r2 > r1 -> intersect b a
    | abs (r1 - r2) > l + eps -> CONTAIN b
    | r1 + r2 + eps < l -> NOT_INTERSECT
    | r1 + r2 < l + eps -> POINTS [c1 |+ scale (c2 |- c1) (r1 / l)]
    | otherwise -> POINTS [ u |+ v, u |- v]
  where
    (c1, r1) = a
    (c2, r2) = b
    l = dist c1 c2
    la = (r1 * r1 + l * l - r2 * r2) / (2 * l)
    h = sqrt $ r1 * r1 - la * la
    u = c1 |+ scale (c2 |- c1) (la / l)
    (x1, y1) = c1
    (x2, y2) = c2
    v = scale (y1 - y2, x2 - x1) (h / l)

dist :: Point -> Point -> Double
dist p q = norm (p |- q)

norm :: Point -> Double
norm (x, y) = sqrt $ x * x + y * y

pair :: IO (Double, Double)
pair = words <$> getLine >>= \[x, y] -> pure (read x, read y)

{-# LANGUAGE MultiWayIf #-}
module Main where

main :: IO ()
main = do
  (t1, t2) <- pair
  cinema <- pair
  home <- pair
  shop <- pair
  print $ solve t1 t2 cinema home shop

solve :: Double -> Double -> Point -> Point -> Point -> Double
solve t1 t2 c h s = if tsh + dist c s <= tB then min (tA + tsh) tB else search 0 tsh
  where
    tsh = dist s h
    tA =  dist c s + t1
    tB = dist c h + t2
    search l r = let x = (r + l) / 2 in if
        | r - l < eps -> x
        | isPossible x -> search x r
        | otherwise -> search l x
    isPossible x = check (c, x) (h, max 0 $ tB - x) (s, max 0 $ tA - x)

type Point = (Double, Double)
type Circle = (Point, Double)

eps :: Double
eps = 1e-5

check :: Circle -> Circle -> Circle -> Bool
check c1 c2 c3 = any f [ (c1, c2, c3), (c1, c3, c2), (c2, c3, c1) ]
  where
    f (c1, c2, c3) = case intersect c1 c2 of
      NOT_INTERSECT -> False
      CONTAIN c0 -> intersect c3 c0 /= NOT_INTERSECT
      POINTS ps -> any (inside c3) ps

inside :: Circle -> Point -> Bool
inside (c, r) p =  dist c p <= r + eps

(|+) :: Point -> Point -> Point
(x1, y1) |+ (x2, y2) = (x1 + x2, y1 + y2)

(|-) :: Point -> Point -> Point
(x1, y1) |- (x2, y2) = (x1 - x2, y1 - y2)

scale :: Point -> Double -> Point
scale (x, y) k = (x * k, y * k)

data Intersection = NOT_INTERSECT | POINTS [Point] | CONTAIN Circle deriving (Show, Eq)

intersect :: Circle -> Circle -> Intersection
intersect a@(c1@(x1, y1), r1) b@(c2@(x2, y2), r2)
  | r1 + r2 + eps < l = NOT_INTERSECT
  | abs (r1 - r2) > l + eps = CONTAIN $ if r1 > r2 then b else a
  | otherwise = POINTS [ u |+ v, u |- v]
  where
    l = dist c1 c2
    la = (r1 * r1 + l * l - r2 * r2) / (2 * l)
    h = sqrt $ r1 * r1 - la * la
    u = c1 |+ scale (c2 |- c1) (la / l)
    v = scale (y1- y2, x2 - x1) (h / l)

dist :: Point -> Point -> Double
dist p q = norm (p |- q)

norm :: Point -> Double
norm (x, y) = sqrt $ x * x + y * y

pair :: IO (Double, Double)
pair = words <$> getLine >>= \[x, y] -> pure (read x, read y)

{-# LANGUAGE MultiWayIf #-}
module Main where
import Data.List (minimumBy)
import Data.Ord (comparing)

main :: IO ()
main = do
  (t1, t2) <- pair
  cinema <- pair
  home <- pair
  shop <- pair
  print $ solve t1 t2 cinema home shop

solve :: Double -> Double -> (Double, Double) -> (Double, Double) -> (Double, Double) -> Double
solve t1 t2 c h s = if
    | ch + t2 >= cs + sh -> min (ch + t2) (cs + sh + t1)
    | cs + sh + t1 >= ch + t2 + 2 * sh -> ch + t2
    | otherwise -> search 0 r
  where
    sh = dist s h
    cs = dist c s
    ch = dist c h

    r = (cs + t1 + ch + t2 - sh) / 2
    sr = (cs + t1 - r) / sh
    hr = (ch + t2 - r) / sh

    a = angle cs ch sh

    search l r = let x = (r + l) / 2 in if
        | r - l < 1e-6 -> x
        | isPossible x -> search x r
        | otherwise -> search l x

    isPossible x = angle cs x (cs + t1 - x) + angle ch x (ch + t2 - x) > a

angle :: Double -> Double -> Double -> Double
angle a b c = acos . max (-1) . min 1 $ (a * a + b * b - c * c) / (2 * a * b)

dist :: (Double, Double) -> (Double, Double) -> Double
dist (x1, y1) (x2, y2) = sqrt $ (x1 - x2) ^ 2 + (y1 - y2) ^ 2

pair :: IO (Double, Double)
pair = words <$> getLine >>= \[x, y] -> pure (read x, read y)

{-# LANGUAGE MultiWayIf #-}
module Main where

main :: IO ()
main = do
  (t1, t2) <- pair
  cinema <- pair
  home <- pair
  shop <- pair
  print $ solve t1 t2 cinema home shop

solve :: Double -> Double -> Point -> Point -> Point -> Double
solve t1 t2 c h s = if
    | ch + t2 >= cs + sh -> maxPath
    | cs + sh + t1 >= ch + t2 + 2 * sh -> ch + t2
    | otherwise -> search 0 maxPath
  where
    sh = dist s h
    cs = dist c s
    ch = dist c h

    maxPath = min (ch + t2) (cs + sh + t1)

    f (l,r) = let x = (r + l) / 2 in if isPossible x then (x, r) else (l, x)

    search l r = let x = (r + l) / 2 in if
        | r - l < 1e-6 -> x
        | isPossible x -> search x r
        | otherwise -> search l x
    isPossible x = check (c, x) (h, max 0 $ ch + t2 - x) (s, max 0 $ cs + t1 - x)

type Point = (Double, Double)
type Circle = (Point, Double)

check :: Circle -> Circle -> Circle -> Bool
check c1 c2 c3 = any f $ [(c1, c2, c3), (c1, c3, c2), (c2, c3, c1)]
  where
    f (c1, c2, c3) = case intersect c1 c2 of
      NOT_INTERSECT -> False
      CONTAIN c0 -> intersect c3 c0 /= NOT_INTERSECT
      POINTS ps -> any (inside c3) ps

inside :: Circle -> Point -> Bool
inside (c, r) p =  dist c p <= r

(|+) :: Point -> Point -> Point
(x1, y1) |+ (x2, y2) = (x1 + x2, y1 + y2)

(|-) :: Point -> Point -> Point
(x1, y1) |- (x2, y2) = (x1 - x2, y1 - y2)

scale :: Point -> Double -> Point
scale (x, y) k = (x * k, y * k)

data Intersection = NOT_INTERSECT | POINTS [Point] | CONTAIN Circle deriving (Show, Eq)

intersect :: Circle -> Circle -> Intersection
intersect a b = if
    | r1 + r2 < l -> NOT_INTERSECT
    | r1 + r2 == l -> POINTS [c1 |+ scale (c2 |- c1) (r1 / l)]
    | abs (r1 - r2) > l -> CONTAIN b
    | otherwise -> POINTS [ u |+ v, u |- v]
  where
    (c1, r1) = a
    (c2, r2) = b
    l = dist c1 c2
    la = (r1 * r1 + l * l - r2 * r2) / (2 * l)
    h = sqrt $ r1 * r1 - la * la
    u = c1 |+ scale (c2 |- c1) (la / l)
    (x1, y1) = c1
    (x2, y2) = c2
    v = scale (y1 - y2, x2 - x1) (h / l)

dist :: Point -> Point -> Double
dist p q = norm (p |- q)

norm :: Point -> Double
norm (x, y) = sqrt $ x * x + y * y

pair :: IO (Double, Double)
pair = words <$> getLine >>= \[x, y] -> pure (read x, read y)

{-# LANGUAGE MultiWayIf #-}
module Main where
import Data.List (permutations)

main :: IO ()
main = do
  (t1, t2) <- pair
  cinema <- pair
  home <- pair
  shop <- pair
  print $ solve t1 t2 cinema home shop

solve :: Double -> Double -> Point -> Point -> Point -> Double
solve t1 t2 c h s = if tsh + dist c s <= tB then maxP else search 0 maxP
  where
    tsh = dist s h
    tA =  dist c s + t1
    tB = dist c h + t2
    maxP = min (tA + tsh) tB

    f (l,r) = let x = (r + l) / 2 in if isPossible x then (x, r) else (l, x)

    search l r = let x = (r + l) / 2 in if
        | r - l < 1e-5 -> x
        | isPossible x -> search x r
        | otherwise -> search l x
    isPossible x = check (c, x) (h, max 0 $ tB - x) (s, max 0 $ tA - x)

type Point = (Double, Double)
type Circle = (Point, Double)

eps :: Double
eps = 1e-5

check :: Circle -> Circle -> Circle -> Bool
check c1 c2 c3 = any f $ permutations [c1, c2, c3]
  where
    f [c1, c2, c3] = case intersect c1 c2 of
      NOT_INTERSECT -> False
      CONTAIN c0 -> intersect c3 c0 /= NOT_INTERSECT
      POINTS ps -> any (inside c3) ps

inside :: Circle -> Point -> Bool
inside (c, r) p =  dist c p <= r

(|+) :: Point -> Point -> Point
(x1, y1) |+ (x2, y2) = (x1 + x2, y1 + y2)

(|-) :: Point -> Point -> Point
(x1, y1) |- (x2, y2) = (x1 - x2, y1 - y2)

scale :: Point -> Double -> Point
scale (x, y) k = (x * k, y * k)

data Intersection = NOT_INTERSECT | POINTS [Point] | CONTAIN Circle deriving (Show, Eq)

intersect :: Circle -> Circle -> Intersection
intersect a@(c1@(x1, y1), r1) b@(c2@(x2, y2), r2)
  | r1 + r2 < l = NOT_INTERSECT
  | abs (r1 - r2) > l = CONTAIN $ if r1 > r2 then b else a
  | otherwise = POINTS [ u |+ v, u |- v]
  where
    l = dist c1 c2
    la = (r1 * r1 + l * l - r2 * r2) / (2 * l)
    h = sqrt $ r1 * r1 - la * la
    u = c1 |+ scale (c2 |- c1) (la / l)
    v = scale (y1- y2, x2 - x1) (h / l)

dist :: Point -> Point -> Double
dist p q = norm (p |- q)

norm :: Point -> Double
norm (x, y) = sqrt $ x * x + y * y

pair :: IO (Double, Double)
pair = words <$> getLine >>= \[x, y] -> pure (read x, read y)

{-# LANGUAGE MultiWayIf #-}
module Main where
import Data.List (permutations)

main :: IO ()
main = do
  (t1, t2) <- pair
  cinema <- pair
  home <- pair
  shop <- pair
  print $ solve t1 t2 cinema home shop

solve :: Double -> Double -> Point -> Point -> Point -> Double
solve t1 t2 c h s = if
    | ch + t2 >= cs + sh -> maxPath
    | cs + sh + t1 >= ch + t2 + 2 * sh -> ch + t2
    | otherwise -> search 0 maxPath
  where
    sh = dist s h
    cs = dist c s
    ch = dist c h

    maxPath = min (ch + t2) (cs + sh + t1)

    f (l,r) = let x = (r + l) / 2 in if isPossible x then (x, r) else (l, x)

    search l r = let x = (r + l) / 2 in if
        | r - l < 1e-6 -> x
        | isPossible x -> search x r
        | otherwise -> search l x
    isPossible x = check (c, x) (h, max 0 $ ch + t2 - x) (s, max 0 $ cs + t1 - x)

type Point = (Double, Double)
type Circle = (Point, Double)

check :: Circle -> Circle -> Circle -> Bool
check c1 c2 c3 = any f $ [(c1, c2, c3), (c1, c3, c2), (c2, c3, c1)]
  where
    f (c1, c2, c3) = case intersect c1 c2 of
      NOT_INTERSECT -> False
      CONTAIN c0 -> intersect c3 c0 /= NOT_INTERSECT
      POINTS ps -> any (inside c3) ps

inside :: Circle -> Point -> Bool
inside (c, r) p =  dist c p < r + 1e-10

(|+) :: Point -> Point -> Point
(x1, y1) |+ (x2, y2) = (x1 + x2, y1 + y2)

(|-) :: Point -> Point -> Point
(x1, y1) |- (x2, y2) = (x1 - x2, y1 - y2)

scale :: Point -> Double -> Point
scale (x, y) k = (x * k, y * k)

data Intersection = NOT_INTERSECT | POINTS [Point] | CONTAIN Circle deriving (Show, Eq)

intersect :: Circle -> Circle -> Intersection
intersect a b = if
    | r2 > r1 -> intersect b a
    | r1 + r2 < l -> NOT_INTERSECT
    | abs (r1 - r2) > l -> CONTAIN b
    | otherwise -> POINTS [ u |+ v, u |- v]
  where
    (c1, r1) = a
    (c2, r2) = b
    l = dist c1 c2
    la = (r1 * r1 + l * l - r2 * r2) / (2 * l)
    h = sqrt $ r1 * r1 - la * la
    u = c1 |+ scale (c2 |- c1) (la / l)
    (x1, y1) = c1
    (x2, y2) = c2
    v = scale (y1 - y2, x2 - x1) (h / l)

dist :: Point -> Point -> Double
dist p q = norm (p |- q)

norm :: Point -> Double
norm (x, y) = sqrt $ x * x + y * y

pair :: IO (Double, Double)
pair = words <$> getLine >>= \[x, y] -> pure (read x, read y)

{-# LANGUAGE MultiWayIf #-}
module Main where
import Data.List (permutations)

main :: IO ()
main = do
  (t1, t2) <- pair
  cinema <- pair
  home <- pair
  shop <- pair
  print $ solve t1 t2 cinema home shop

solve :: Double -> Double -> Point -> Point -> Point -> Double
-- solve t1 t2 c h s = trace (show $ isPossible 1.0002) 0.0
solve t1 t2 c h s = if
    | ch + t2 >= cs + sh -> min (ch + t2) (cs + sh + t1)
    | cs + sh + t1 >= ch + t2 + 2 * sh -> ch + t2
    | otherwise -> search 0 sh
  where
    sh = dist s h
    cs = dist c s
    ch = dist c h

    f (l,r) = let x = (r + l) / 2 in if isPossible x then (x, r) else (l, x)

    -- search l r = trace (show (l, r)) $ let x = (r + l) / 2 in if
    search l r = let x = (r + l) / 2 in if
        | r - l < 1e-6 -> x
        | isPossible x -> search x r
        | otherwise -> search l x
    isPossible x = check (c, x) (h, max 0 $ ch + t2 - x) (s, max 0 $ cs + t1 - x)

type Point = (Double, Double)
type Circle = (Point, Double)

check :: Circle -> Circle -> Circle -> Bool
check c1 c2 c3 = any f $ [(c1, c2, c3), (c1, c3, c2), (c2, c3, c1)]
  where
    f (c1, c2, c3) = case intersect c1 c2 of
      NOT_INTERSECT -> False
      CONTAIN c0 -> intersect c3 c0 /= NOT_INTERSECT
      POINTS ps -> any (inside c3) ps

inside :: Circle -> Point -> Bool
inside (c, r) p =  dist c p < r + 1e-10

(|+) :: Point -> Point -> Point
(x1, y1) |+ (x2, y2) = (x1 + x2, y1 + y2)

(|-) :: Point -> Point -> Point
(x1, y1) |- (x2, y2) = (x1 - x2, y1 - y2)

scale :: Point -> Double -> Point
scale (x, y) k = (x * k, y * k)

data Intersection = NOT_INTERSECT | POINTS [Point] | CONTAIN Circle deriving (Show, Eq)

intersect :: Circle -> Circle -> Intersection
intersect a b = if
    | r2 > r1 -> intersect b a
    | r1 + r2 < l -> NOT_INTERSECT
    | abs (r1 - r2) > l -> CONTAIN b
    | otherwise -> POINTS [ u |+ v, u |- v]
  where
    (c1, r1) = a
    (c2, r2) = b
    l = dist c1 c2
    la = (r1 * r1 + l * l - r2 * r2) / (2 * l)
    h = sqrt $ r1 * r1 - la * la
    u = c1 |+ scale (c2 |- c1) (la / l)
    (x1, y1) = c1
    (x2, y2) = c2
    v = scale (y1 - y2, x2 - x1) (h / l)

dist :: Point -> Point -> Double
dist p q = norm (p |- q)

norm :: Point -> Double
norm (x, y) = sqrt $ x * x + y * y

pair :: IO (Double, Double)
pair = words <$> getLine >>= \[x, y] -> pure (read x, read y)

{-# LANGUAGE MultiWayIf #-}
module Main where
import Data.List (permutations)

main :: IO ()
main = do
  (t1, t2) <- pair
  cinema <- pair
  home <- pair
  shop <- pair
  print $ solve t1 t2 cinema home shop

solve :: Double -> Double -> Point -> Point -> Point -> Double
solve t1 t2 c h s = if tsh + dist c s <= tB then maxP else search 0 maxP
  where
    tsh = dist s h
    tA =  dist c s + t1
    tB = dist c h + t2
    maxP = min (tA + tsh) tB

    f (l,r) = let x = (r + l) / 2 in if isPossible x then (x, r) else (l, x)

    search l r = let x = (r + l) / 2 in if
        | r - l < 1e-6 -> x
        | isPossible x -> search x r
        | otherwise -> search l x
    isPossible x = check (c, x) (h, max 0 $ tB - x) (s, max 0 $ tA - x)

type Point = (Double, Double)
type Circle = (Point, Double)

check :: Circle -> Circle -> Circle -> Bool
check c1 c2 c3 = any f $ [(c1, c2, c3), (c1, c3, c2), (c2, c3, c1)]
  where
    f (c1, c2, c3) = case intersect c1 c2 of
      NOT_INTERSECT -> False
      CONTAIN c0 -> intersect c3 c0 /= NOT_INTERSECT
      POINTS ps -> any (inside c3) ps

inside :: Circle -> Point -> Bool
inside (c, r) p =  dist c p < r + 1e-10

(|+) :: Point -> Point -> Point
(x1, y1) |+ (x2, y2) = (x1 + x2, y1 + y2)

(|-) :: Point -> Point -> Point
(x1, y1) |- (x2, y2) = (x1 - x2, y1 - y2)

scale :: Point -> Double -> Point
scale (x, y) k = (x * k, y * k)

data Intersection = NOT_INTERSECT | POINTS [Point] | CONTAIN Circle deriving (Show, Eq)

intersect :: Circle -> Circle -> Intersection
intersect a@(c1@(x1, y1), r1) b@(c2@(x2, y2), r2)
  | r1 + r2 + 1e-10 < l = NOT_INTERSECT
  | abs (r1 - r2) > l + 1e-10 = CONTAIN $ if r1 > r2 then b else a
  | otherwise = POINTS [ u |+ v, u |- v]
  where
    l = dist c1 c2
    la = (r1 * r1 + l * l - r2 * r2) / (2 * l)
    h = sqrt $ r1 * r1 - la * la
    u = c1 |+ scale (c2 |- c1) (la / l)
    v = scale (y1- y2, x2 - x1) (h / l)

dist :: Point -> Point -> Double
dist p q = norm (p |- q)

norm :: Point -> Double
norm (x, y) = sqrt $ x * x + y * y

pair :: IO (Double, Double)
pair = words <$> getLine >>= \[x, y] -> pure (read x, read y)

{-# LANGUAGE MultiWayIf #-}
module Main where

main :: IO ()
main = do
  (t1, t2) <- pair
  cinema <- pair
  home <- pair
  shop <- pair
  print $ solve t1 t2 cinema home shop

solve :: Double -> Double -> Point -> Point -> Point -> Double
solve t1 t2 c h s = if sh + dist c s <= tB then min tA tB else search 0 sh
  where
    sh = dist s h
    tA =  dist c s + sh + t1
    tB = dist c h + t2
    search l r = let x = (r + l) / 2 in if
        | r - l < 1e-5 -> x
        | isPossible x -> search x r
        | otherwise -> search l x
    isPossible x = check (c, x) (h, max 0 $ tB - x) (s, max 0 $ tA - sh - x)

type Point = (Double, Double)
type Circle = (Point, Double)

check :: Circle -> Circle -> Circle -> Bool
check c1 c2 c3 = any (\(c1, c2, c3) -> any (inside c3) (intersect c1 c2)) [ (c1, c2, c3), (c1, c3, c2), (c2, c3, c1)]

inside :: Circle -> Point -> Bool
inside (x, r) y = dist x y <= r

intersect :: Circle -> Circle -> [Point]
intersect (p@(x1, y1), r1) (q@(x2, y2), r2) = if d > r1 + r2 || d < abs (r1 - r2)
    then []
    else [(x3 + h * (y2 - y1) / d, y3 + h * (x1 - x2) / d), (x3 + h * (y1 - y2) / d, y3 + h * (x2 - x1) / d)]
  where
    d = dist p q
    a = (r1 ^ 2 - r2 ^ 2 + d ^ 2) / (2 * d)
    h = (sqrt $ r1 ^ 2 - a ^ 2)
    x3 = x1 + a * (x2 - x1) / d
    y3 = y1 + a * (y2 - y1) / d

dist :: Point -> Point -> Double
dist (x1, y1) (x2, y2) = sqrt $ (x1 - x2) ^ 2 + (y1 - y2) ^ 2

pair :: IO (Double, Double)
pair = words <$> getLine >>= \[x, y] -> pure (read x, read y)

{-# LANGUAGE MultiWayIf #-}
module Main where

main :: IO ()
main = do
  (t1, t2) <- pair
  cinema <- pair
  home <- pair
  shop <- pair
  print $ solve t1 t2 cinema home shop

solve :: Double -> Double -> Point -> Point -> Point -> Double
solve t1 t2 c h s = if tsh + dist c s <= tB then min (tA + tsh) tB else search 0 tsh
  where
    tsh = dist s h
    tA =  dist c s + t1
    tB = dist c h + t2
    search l r = let x = (r + l) / 2 in if
        | r - l < eps -> r
        | isPossible x -> search x r
        | otherwise -> search l x
    isPossible x = check (c, x) (h, max 0 $ tB - x) (s, max 0 $ tA - x)

type Point = (Double, Double)
type Circle = (Point, Double)

eps :: Double
eps = 1e-5

check :: Circle -> Circle -> Circle -> Bool
check c1 c2 c3 = any f [ (c1, c2, c3), (c1, c3, c2), (c2, c3, c1) ]
  where
    f (c1, c2, c3) = case intersect c1 c2 of
      NOT_INTERSECT -> False
      CONTAIN c0 -> intersect c3 c0 /= NOT_INTERSECT
      POINTS ps -> any (inside c3) ps

inside :: Circle -> Point -> Bool
inside (c, r) p =  dist c p <= r + eps

(|+) :: Point -> Point -> Point
(x1, y1) |+ (x2, y2) = (x1 + x2, y1 + y2)

(|-) :: Point -> Point -> Point
(x1, y1) |- (x2, y2) = (x1 - x2, y1 - y2)

scale :: Point -> Double -> Point
scale (x, y) k = (x * k, y * k)

data Intersection = NOT_INTERSECT | POINTS [Point] | CONTAIN Circle deriving (Show, Eq)

intersect :: Circle -> Circle -> Intersection
intersect a@(c1@(x1, y1), r1) b@(c2@(x2, y2), r2)
  | r1 + r2 + eps < l = NOT_INTERSECT
  | abs (r1 - r2) > l + eps = CONTAIN $ if r1 > r2 then b else a
  | otherwise = POINTS [ u |+ v, u |- v]
  where
    l = dist c1 c2
    la = (r1 * r1 + l * l - r2 * r2) / (2 * l)
    h = sqrt $ r1 * r1 - la * la
    u = c1 |+ scale (c2 |- c1) (la / l)
    v = scale (y1- y2, x2 - x1) (h / l)

dist :: Point -> Point -> Double
dist p q = norm (p |- q)

norm :: Point -> Double
norm (x, y) = sqrt $ x * x + y * y

pair :: IO (Double, Double)
pair = words <$> getLine >>= \[x, y] -> pure (read x, read y)

{-# LANGUAGE MultiWayIf #-}
module Main where

main :: IO ()
main = do
  (t1, t2) <- pair
  cinema <- pair
  home <- pair
  shop <- pair
  print $ solve t1 t2 cinema home shop

solve :: Double -> Double -> Point -> Point -> Point -> Double
solve t1 t2 c h s = if
    | ch + t2 >= cs + sh -> maxPath
    | cs + sh + t1 >= ch + t2 + 2 * sh -> ch + t2
    | otherwise -> search 0 maxPath
  where
    sh = dist s h
    cs = dist c s
    ch = dist c h

    maxPath = min (ch + t2) (cs + sh + t1)

    f (l,r) = let x = (r + l) / 2 in if isPossible x then (x, r) else (l, x)

    search l r = let x = (r + l) / 2 in if
        | r - l < 1e-6 -> x
        | isPossible x -> search x r
        | otherwise -> search l x
    isPossible x = check (c, x) (h, max 0 $ ch + t2 - x) (s, max 0 $ cs + t1 - x)

type Point = (Double, Double)
type Circle = (Point, Double)

check :: Circle -> Circle -> Circle -> Bool
check c1 c2 c3 = any f [(c1, c2, c3), (c1, c3, c2), (c2, c3, c1)]
  where
    f (c1, c2, c3) = case intersect c1 c2 of
      NOT_INTERSECT -> False
      CONTAIN c0 -> intersect c3 c0 /= NOT_INTERSECT
      POINTS ps -> any (inside c3) ps

inside :: Circle -> Point -> Bool
inside (c, r) p =  dist c p < r + eps

(|+) :: Point -> Point -> Point
(x1, y1) |+ (x2, y2) = (x1 + x2, y1 + y2)

(|-) :: Point -> Point -> Point
(x1, y1) |- (x2, y2) = (x1 - x2, y1 - y2)

scale :: Point -> Double -> Point
scale (x, y) k = (x * k, y * k)

eps :: Double
eps = 1e-10

data Intersection = NOT_INTERSECT | POINTS [Point] | CONTAIN Circle deriving (Show, Eq)

intersect :: Circle -> Circle -> Intersection
intersect a b = if
    | r2 > r1 -> intersect b a
    | abs (r1 - r2) > l + eps -> CONTAIN b
    | r1 + r2 + eps < l -> NOT_INTERSECT
    | r1 + r2 + eps > l -> POINTS [c1 |+ scale (c2 |- c1) (r1 / l)]
    | otherwise -> POINTS [ u |+ v, u |- v]
  where
    (c1, r1) = a
    (c2, r2) = b
    l = dist c1 c2
    la = (r1 * r1 + l * l - r2 * r2) / (2 * l)
    h = sqrt $ r1 * r1 - la * la
    u = c1 |+ scale (c2 |- c1) (la / l)
    (x1, y1) = c1
    (x2, y2) = c2
    v = scale (y1 - y2, x2 - x1) (h / l)

dist :: Point -> Point -> Double
dist p q = norm (p |- q)

norm :: Point -> Double
norm (x, y) = sqrt $ x * x + y * y

pair :: IO (Double, Double)
pair = words <$> getLine >>= \[x, y] -> pure (read x, read y)


import Control.Monad

main :: IO ()
main = do
  n <- readLn
  replicateM_ n routine

routine :: IO ()
routine = do
  [n0,n1,n2] <- (map read.words) <$> getLine
  putStrLn $ solve n0 n1 n2

solve :: Int -> Int -> Int -> String
solve n0 n1 n2
  | n1 == 0 =
    if n2 == 0
      then replicate (n0+1) '0'
      else replicate (n2+1) '1'
  | n2 == 0 =
    replicate (n0+1) '0' ++
    take n1 (cycle "10")
  | n0 == 0 =
    replicate (n2+1) '1' ++
    take n1 (cycle "01")
  | otherwise =
    take (fromEnum (even n1)) "1" ++
    replicate (n0+1) '0' ++
    take (2*((n1-1) `div` 2)) (cycle "10") ++
    replicate (n2+1) '1'

solve :: Int -> Int -> Int -> String
solve 0 0 n = replicate (n+1) '1'
solve n 0 0 = replicate (n+1) '0'
solve n0 n1 n2 = (replicate (n0+1) '0') ++ (replicate (n2+1) '1') ++ (take (n1-1) $ cycle "01")

testCases :: Int -> IO ()
testCases 0 = return ()
testCases t = do
    (n0:n1:n2:_) <- readNums
    putStrLn $ solve n0 n1 n2
    testCases (t-1)

main :: IO ()
main = do
    (t:_) <- readNums
    testCases t

readNums :: (Read n) => IO [n]
readNums = map read . words <$> getLine

import           Control.Arrow
import           Control.Monad.State
import           Data.Array
import           Data.Bits
import           Data.Bool
import qualified Data.ByteString.Lazy.Char8 as C
import           Data.Char
import           Data.Function
import           Data.List
import           Data.Map                   (Map, (!))
import qualified Data.Map                   as M
import           Data.Maybe
import           Data.Ord
import           Data.Ratio
import           Data.Set                   (Set)
import qualified Data.Set                   as S
import           Text.Printf

-- import Text.Parsec
-- import Text.Parsec.Expr
-- import Text.Parsec.Language (emptyDef)
-- import Text.Parsec.String
-- import qualified Text.Parsec.Token as T

infixl 0 >$>
(>$>) = flip ($)

main = interact $
  lines >>> drop 1 >>> map (words >>> map read >>> solve) >>> unlines

solve :: [Int] -> String
solve [n0,0,0] = replicate (n0+1) '0'
solve [0,0,n2] = replicate (n2+1) '1'
solve [n0,n1,n2] = replicate (n2+1) '1' ++ replicate (n0+1) '0' ++ take (n1-1) (cycle "10")

solve :: Int -> Int -> Int -> String
solve n0 n1 n2 = (replicate (n0+1) '0') ++ (replicate (n2+1) '1') ++ (take (n1-1) $ cycle "01")

testCases :: Int -> IO ()
testCases 0 = return ()
testCases t = do
    (n0:n1:n2:_) <- readNums
    putStrLn $ solve n0 n1 n2
    testCases (t-1)

main :: IO ()
main = do
    (t:_) <- readNums
    testCases t

readNums :: (Read n) => IO [n]
readNums = map read . words <$> getLine


-- 2019-11-24 05:31:08.52513552 UTC
import Data.Maybe
import Control.Monad
import Text.Read
nat_para :: Integral i => i -> a -> (i -> a -> a) -> a
nat_para i x f = np (abs i)
             where np 0 = x
                   np i = let i' = i - 1
                           in f i' (np i')
hd :: [a] -> a
hd ([]) = error "empty head"
hd (a : as) = a
tl = drop 1
nat_cata :: Integral i => i -> a -> (a -> a) -> a
nat_cata i x f = nc (abs i)
             where nc 0 = x
                   nc i = f (nc (i - 1))
iF :: Bool -> a -> a -> a
iF (True) t f = t
iF (False) t f = f
list_para :: [b] -> a -> (b -> [b] -> a -> a) -> a
list_para ([]) x f = x
list_para (y : ys) x f = f y ys (list_para ys x f)
magiciF :: Bool -> String
magiciF b = if b then "EASY" else "HARD"
uncurry' f (a, b) = f a b
parser :: String -> (Int, Int)
parser = fromJust . parseTwoInts
parseTwoInts :: String -> Maybe (Int, Int)
parseTwoInts i = do {when (length (lines i) /= 1) Nothing;
                     ns <- mapM readMaybe $ words i :: Maybe ([Int]);
                     when (length ns /= 2) Nothing;
                     let {[a, b] = ns};
                     return (a, b)}
solve = \a b -> iF (1 >= b) "YES" (iF (iF (3 >= b) 1 3 >= a) "NO" "YES")
main = getContents >>= (\c -> (mapM_ (putStrLn . (uncurry' solve . parser)) . (tail . lines)) $ c)

import Control.Monad

solve :: Int -> Int -> Bool
solve x y
  |x==1 = y==1
  |x<=3 = y<=3
  |otherwise = True

routine :: IO ()
routine = do
  [x,y] <- fmap (map read.words) getLine
  putStrLn $ if solve x y then "YES" else "NO"

main :: IO ()
main = do
  n <- readLn
  replicateM_ n routine

-- 2019-11-19 19:41:35.813043782 UTC
import Data.Maybe
import Control.Monad
import Text.Read
nat_para :: Integral i => i -> a -> (i -> a -> a) -> a
nat_para i x f = np (abs i)
             where np 0 = x
                   np i = let i' = i - 1
                           in f i' (np i')
hd :: [a] -> a
hd ([]) = error "empty head"
hd (a : as) = a
tl = drop 1
nat_cata :: Integral i => i -> a -> (a -> a) -> a
nat_cata i x f = nc (abs i)
             where nc 0 = x
                   nc i = f (nc (i - 1))
iF :: Bool -> a -> a -> a
iF (True) t f = t
iF (False) t f = f
list_para :: [b] -> a -> (b -> [b] -> a -> a) -> a
list_para ([]) x f = x
list_para (y : ys) x f = f y ys (list_para ys x f)
magiciF :: Bool -> String
magiciF b = if b then "EASY" else "HARD"
uncurry' f (a, b) = f a b
parser :: String -> (Int, Int)
parser = fromJust . parseTwoInts
parseTwoInts :: String -> Maybe (Int, Int)
parseTwoInts i = do {when (length (lines i) /= 1) Nothing;
                     ns <- mapM readMaybe $ words i :: Maybe ([Int]);
                     when (length ns /= 2) Nothing;
                     let {[a, b] = ns};
                     return (a, b)}
solve = \a b -> iF (1 >= b) "YES" (iF (iF (3 >= b) 1 3 >= a) "NO" "YES")
main = getContents >>= (\c -> (mapM_ (putStrLn . (uncurry' solve . parser)) . (tail . lines)) $ c)

-- 2019-11-20 21:46:29.481091921 UTC
import Data.Maybe
import Control.Monad
import Text.Read
nat_para :: Integral i => i -> a -> (i -> a -> a) -> a
nat_para i x f = np (abs i)
             where np 0 = x
                   np i = let i' = i - 1
                           in f i' (np i')
hd :: [a] -> a
hd ([]) = error "empty head"
hd (a : as) = a
tl = drop 1
nat_cata :: Integral i => i -> a -> (a -> a) -> a
nat_cata i x f = nc (abs i)
             where nc 0 = x
                   nc i = f (nc (i - 1))
iF :: Bool -> a -> a -> a
iF (True) t f = t
iF (False) t f = f
list_para :: [b] -> a -> (b -> [b] -> a -> a) -> a
list_para ([]) x f = x
list_para (y : ys) x f = f y ys (list_para ys x f)
magiciF :: Bool -> String
magiciF b = if b then "EASY" else "HARD"
uncurry' f (a, b) = f a b
parser :: String -> (Int, Int)
parser = fromJust . parseTwoInts
parseTwoInts :: String -> Maybe (Int, Int)
parseTwoInts i = do {when (length (lines i) /= 1) Nothing;
                     ns <- mapM readMaybe $ words i :: Maybe ([Int]);
                     when (length ns /= 2) Nothing;
                     let {[a, b] = ns};
                     return (a, b)}
solve = \a b -> iF (1 >= b) "YES" (iF (iF (3 >= b) 1 3 >= a) "NO" "YES")
main = getContents >>= (\c -> (mapM_ (putStrLn . (uncurry' solve . parser)) . (tail . lines)) $ c)

-- 2019-11-24 05:33:31.10142959 UTC
import Data.Maybe
import Control.Monad
import Text.Read
nat_para :: Integral i => i -> a -> (i -> a -> a) -> a
nat_para i x f = np (abs i)
             where np 0 = x
                   np i = let i' = i - 1
                           in f i' (np i')
hd :: [a] -> a
hd ([]) = error "empty head"
hd (a : as) = a
tl = drop 1
nat_cata :: Integral i => i -> a -> (a -> a) -> a
nat_cata i x f = nc (abs i)
             where nc 0 = x
                   nc i = f (nc (i - 1))
iF :: Bool -> a -> a -> a
iF (True) t f = t
iF (False) t f = f
list_para :: [b] -> a -> (b -> [b] -> a -> a) -> a
list_para ([]) x f = x
list_para (y : ys) x f = f y ys (list_para ys x f)
magiciF :: Bool -> String
magiciF b = if b then "EASY" else "HARD"
uncurry' f (a, b) = f a b
parser :: String -> (Int, Int)
parser = fromJust . parseTwoInts
parseTwoInts :: String -> Maybe (Int, Int)
parseTwoInts i = do {when (length (lines i) /= 1) Nothing;
                     ns <- mapM readMaybe $ words i :: Maybe ([Int]);
                     when (length ns /= 2) Nothing;
                     let {[a, b] = ns};
                     return (a, b)}
solve = \a b -> iF (1 >= b) "YES" (iF (iF (3 >= b) 1 3 >= a) "NO" "YES")
main = getContents >>= (\c -> (mapM_ (putStrLn . (uncurry' solve . parser)) . (tail . lines)) $ c)

-- 2019-11-24 05:32:16.025342204 UTC
import Data.Maybe
import Control.Monad
import Text.Read
nat_para :: Integral i => i -> a -> (i -> a -> a) -> a
nat_para i x f = np (abs i)
             where np 0 = x
                   np i = let i' = i - 1
                           in f i' (np i')
hd :: [a] -> a
hd ([]) = error "empty head"
hd (a : as) = a
tl = drop 1
nat_cata :: Integral i => i -> a -> (a -> a) -> a
nat_cata i x f = nc (abs i)
             where nc 0 = x
                   nc i = f (nc (i - 1))
iF :: Bool -> a -> a -> a
iF (True) t f = t
iF (False) t f = f
list_para :: [b] -> a -> (b -> [b] -> a -> a) -> a
list_para ([]) x f = x
list_para (y : ys) x f = f y ys (list_para ys x f)
magiciF :: Bool -> String
magiciF b = if b then "EASY" else "HARD"
uncurry' f (a, b) = f a b
parser :: String -> (Int, Int)
parser = fromJust . parseTwoInts
parseTwoInts :: String -> Maybe (Int, Int)
parseTwoInts i = do {when (length (lines i) /= 1) Nothing;
                     ns <- mapM readMaybe $ words i :: Maybe ([Int]);
                     when (length ns /= 2) Nothing;
                     let {[a, b] = ns};
                     return (a, b)}
solve = \a b -> iF (1 >= b) "YES" (iF (iF (3 >= b) 1 3 >= a) "NO" "YES")
main = getContents >>= (\c -> (mapM_ (putStrLn . (uncurry' solve . parser)) . (tail . lines)) $ c)

-- 2019-11-24 05:35:19.450643282 UTC
import Data.Maybe
import Control.Monad
import Text.Read
nat_para :: Integral i => i -> a -> (i -> a -> a) -> a
nat_para i x f = np (abs i)
             where np 0 = x
                   np i = let i' = i - 1
                           in f i' (np i')
hd :: [a] -> a
hd ([]) = error "empty head"
hd (a : as) = a
tl = drop 1
nat_cata :: Integral i => i -> a -> (a -> a) -> a
nat_cata i x f = nc (abs i)
             where nc 0 = x
                   nc i = f (nc (i - 1))
iF :: Bool -> a -> a -> a
iF (True) t f = t
iF (False) t f = f
list_para :: [b] -> a -> (b -> [b] -> a -> a) -> a
list_para ([]) x f = x
list_para (y : ys) x f = f y ys (list_para ys x f)
magiciF :: Bool -> String
magiciF b = if b then "EASY" else "HARD"
uncurry' f (a, b) = f a b
parser :: String -> (Int, Int)
parser = fromJust . parseTwoInts
parseTwoInts :: String -> Maybe (Int, Int)
parseTwoInts i = do {when (length (lines i) /= 1) Nothing;
                     ns <- mapM readMaybe $ words i :: Maybe ([Int]);
                     when (length ns /= 2) Nothing;
                     let {[a, b] = ns};
                     return (a, b)}
solve = \a b -> iF (1 >= b) "YES" (iF (iF (3 >= b) 1 3 >= a) "NO" "YES")
main = getContents >>= (\c -> (mapM_ (putStrLn . (uncurry' solve . parser)) . (tail . lines)) $ c)

-- 2019-11-19 20:10:07.141272712 UTC
import Data.Maybe
import Control.Monad
import Text.Read
nat_para :: Integral i => i -> a -> (i -> a -> a) -> a
nat_para i x f = np (abs i)
             where np 0 = x
                   np i = let i' = i - 1
                           in f i' (np i')
hd :: [a] -> a
hd ([]) = error "empty head"
hd (a : as) = a
tl = drop 1
nat_cata :: Integral i => i -> a -> (a -> a) -> a
nat_cata i x f = nc (abs i)
             where nc 0 = x
                   nc i = f (nc (i - 1))
iF :: Bool -> a -> a -> a
iF (True) t f = t
iF (False) t f = f
list_para :: [b] -> a -> (b -> [b] -> a -> a) -> a
list_para ([]) x f = x
list_para (y : ys) x f = f y ys (list_para ys x f)
magiciF :: Bool -> String
magiciF b = if b then "EASY" else "HARD"
uncurry' f (a, b) = f a b
parser :: String -> (Int, Int)
parser = fromJust . parseTwoInts
parseTwoInts :: String -> Maybe (Int, Int)
parseTwoInts i = do {when (length (lines i) /= 1) Nothing;
                     ns <- mapM readMaybe $ words i :: Maybe ([Int]);
                     when (length ns /= 2) Nothing;
                     let {[a, b] = ns};
                     return (a, b)}
solve = \a b -> iF (1 >= b) "YES" (iF (iF (3 >= b) 1 3 >= a) "NO" "YES")
main = getContents >>= (\c -> (mapM_ (putStrLn . (uncurry' solve . parser)) . (tail . lines)) $ c)

-- http://codeforces.com/problemset/problem/1257/B

import qualified Data.Map as Map

data Case = Case Int Int
type History = Map.Map
data Response = YES | NO deriving (Show)

toCase :: String -> Case
toCase line = do
  let numbers = map read $ words line :: [Int]
  Case (head numbers) (last numbers)

solveWithHistory :: Case -> History Int Int -> Response
solveWithHistory (Case source target) history
  | source >= target = YES -- We can always reach a lower number by doing source - 1
  | source == 2 && target == 3 = YES -- This is an aceptional case because (3 * 2) / 2 = 3
  | source > 3 = YES -- If source > 3 source can increase (slowly) until reaching the target
  | otherwise = NO
  where updatedHistory = Map.insert source source history

solve :: Case -> Response
solve (Case source target) = solveWithHistory (Case source target) Map.empty

main :: IO ()
main = interact $ unlines . map (show . solve . toCase) . tail . lines

-- http://codeforces.com/problemset/problem/1257/B

import qualified Data.Map as Map

data Case = Case Int Int
type History = Map.Map
data Response = YES | NO deriving (Show)

toCase :: String -> Case
toCase line = do
  let numbers = map read $ words line :: [Int]
  Case (head numbers) (last numbers)

firstOperation :: Int -> Int
firstOperation number = (3 * number) `div` 2

secondOperation :: Int -> Int
secondOperation number = number - 1

solveWithHistory :: Case -> History Int Int -> Response
solveWithHistory (Case source target) history
  | source >= target = YES
  | source == 2 && target == 3 = YES
  | source > 3 = YES
  -- | Map.member source history = NO -- A loop has been detected
  -- | even source = solveWithHistory (Case (firstOperation source) target) updatedHistory
  -- | source > 1 = solveWithHistory (Case (secondOperation source) target) updatedHistory
  | otherwise = NO
  where updatedHistory = Map.insert source source history

solve :: Case -> Response
solve (Case source target) = solveWithHistory (Case source target) Map.empty

main :: IO ()
main = interact $ unlines . map (show . solve . toCase) . tail . lines

module Main where

import Control.Monad

main :: IO ()
main = do
    t <- (read::String->Int) <$> getLine

    replicateM_ t $ do
        [x, y] <- fmap (read::String->Int) . words <$> getLine
        let v | x == y = "YES"
              | x == 1 = "NO"
              | x > 3 = "YES"
              | x <= 3 && y <= 3 = "YES"
              | otherwise = "NO"

        putStrLn v
        return ()

    return ()

main = interact $ unlines . map yesno . solveTests . map read . words

solveTests (t:ls) = solveTests' t ls

solveTests' 0 _ = []
solveTests' t (x:y:ls) = (solveTest x y) : solveTests' (t-1) ls
solveTests' _ _ = []

solveTest x y
    | x > 3 = True
    | x == 2 = y <= 3
    | otherwise = y <= x

yesno v = if v then "YES" else "NO"

-- 2019-11-20 21:50:07.269294269 UTC
import Data.Maybe
import Control.Monad
import Text.Read
nat_para :: Integral i => i -> a -> (i -> a -> a) -> a
nat_para i x f = np (abs i)
             where np 0 = x
                   np i = let i' = i - 1
                           in f i' (np i')
hd :: [a] -> a
hd ([]) = error "empty head"
hd (a : as) = a
tl = drop 1
nat_cata :: Integral i => i -> a -> (a -> a) -> a
nat_cata i x f = nc (abs i)
             where nc 0 = x
                   nc i = f (nc (i - 1))
iF :: Bool -> a -> a -> a
iF (True) t f = t
iF (False) t f = f
list_para :: [b] -> a -> (b -> [b] -> a -> a) -> a
list_para ([]) x f = x
list_para (y : ys) x f = f y ys (list_para ys x f)
magiciF :: Bool -> String
magiciF b = if b then "EASY" else "HARD"
uncurry' f (a, b) = f a b
parser :: String -> (Int, Int)
parser = fromJust . parseTwoInts
parseTwoInts :: String -> Maybe (Int, Int)
parseTwoInts i = do {when (length (lines i) /= 1) Nothing;
                     ns <- mapM readMaybe $ words i :: Maybe ([Int]);
                     when (length ns /= 2) Nothing;
                     let {[a, b] = ns};
                     return (a, b)}
solve = \a b -> iF (3 >= a) (iF (iF (b >= 3) 3 a >= b) "YES" "NO") "YES"
main = getContents >>= (\c -> (mapM_ (putStrLn . (uncurry' solve . parser)) . (tail . lines)) $ c)

-- http://codeforces.com/problemset/problem/1257/B

import qualified Data.Map as Map

data Case = Case Int Int
type History = Map.Map
data Response = YES | NO deriving (Show)

toCase :: String -> Case
toCase line = do
  let numbers = map read $ words line :: [Int]
  Case (head numbers) (last numbers)

firstOperation :: Int -> Int
firstOperation number = (3 * number) `div` 2

secondOperation :: Int -> Int
secondOperation number = number - 1

solveWithHistory :: Case -> History Int Int -> Response
solveWithHistory (Case source target) history
  | source >= target = YES
  | source > 3 = YES
  -- | Map.member source history = NO -- A loop has been detected
  -- | even source = solveWithHistory (Case (firstOperation source) target) updatedHistory
  -- | source > 1 = solveWithHistory (Case (secondOperation source) target) updatedHistory
  | otherwise = NO
  where updatedHistory = Map.insert source source history

solve :: Case -> Response
solve (Case source target) = solveWithHistory (Case source target) Map.empty

main :: IO ()
main = interact $ unlines . map (show . solve . toCase) . tail . lines

-- http://codeforces.com/problemset/problem/1257/B

import qualified Data.Map as Map

data Case = Case Int Int
type History = Map.Map
data Response = YES | NO deriving (Show)

toCase :: String -> Case
toCase line = do
  let numbers = map read $ words line :: [Int]
  Case (head numbers) (last numbers)

firstOperation :: Int -> Int
firstOperation number = (3 * number) `div` 2

secondOperation :: Int -> Int
secondOperation number = number - 1

solveWithHistory :: Case -> History Int Int -> Response
solveWithHistory (Case source target) history
  | source >= target = YES
  | Map.member source history = NO -- A loop has been detected
  | even source = solveWithHistory (Case (firstOperation source) target) updatedHistory
  | source > 1 = solveWithHistory (Case (secondOperation source) target) updatedHistory
  | otherwise = NO
  where updatedHistory = Map.insert source source history

solve :: Case -> Response
solve (Case source target) = solveWithHistory (Case source target) Map.empty

main :: IO ()
main = interact $ unlines . map (show . solve . toCase) . tail . lines

module Main where

import Control.Monad

main :: IO ()
main = do
    t <- (read::String->Int) <$> getLine

    replicateM_ t $ do
        [x, y] <- fmap (read::String->Int) . words <$> getLine
        let v | x == y = "YES"
              | x == 1 = "NO"
              | x > 3 = "YES"
              | x <= 3 && y <= 3 = "YES"
              | otherwise = "NO"

        print v
        return ()

    return ()


#!/usr/bin/runhaskell

-- We do not check indexes vs. string length because of speed.

-- This function do left shift
shift :: String -> Int -> String
shift s k = bs ++ as
    where (as, bs) = splitAt k s

-- This function do right shift of substring
shiftSub :: String -> [Int] -> String
shiftSub s [l, r, k]
    | k == 0    = s
    | n == 0    = s
    | otherwise = as ++ (shift bs n) ++ cs
    where l' = l - 1
          n = r - l' - k `mod` (r - l')
          (as, rs) = splitAt l' s
          (bs, cs) = splitAt (r - l') rs

main = do
    s <- getLine
    getLine -- Skip second line
    ls <- fmap lines getContents
    let qs = map (map read . words) ls :: [[Int]] in putStrLn (foldl shiftSub s qs)

-- rotate :: String -> [Int] -> String
rotate s [l, r, k] =
		take (l - 1) s ++
	  	(take m $ drop (m - k') (ss ++ ss)) ++
	    	drop r s
	where
	 	m = r - l + 1
		k' = k `mod` m
  		ss = take m $ drop (l - 1) s

main = do
	s <- getLine
	_ <- getLine
	ops <- (map (map read . words) . lines) `fmap` getContents

	putStrLn $ foldl rotate s ops

import Control.Applicative ((<$>))

readInt :: String -> Int
readInt = read

main = do
  str <- getLine
  ops <- map (map readInt . words) . tail . lines <$> getContents
  putStrLn $ foldl rotate str ops

rotate :: String -> [Int] -> String
rotate str [l1, r1, k]
  | k == 0 || l == r = str
  | otherwise        = take l str ++ (take (r - l + 1) $ drop (r - l + 1 - n) $ ss ++ ss) ++ drop r1 str
  where l = l1 - 1
        r = r1 - 1
        n = k `mod` (r - l + 1)
        ss = take (r - l + 1) $ drop l str

import Control.Applicative
import Control.Arrow

main = do
    (str, queries) <- (head &&& (map read . drop 2)) . words <$> getContents
    putStrLn $ solve str queries

solve str [] = str
solve str (l : r : k : qs) = let str' = rotate str (l - 1) r k in solve str' qs

rotate str l r k
    | k >= r - l    = rotate str l r (k `mod` (r - l))
    | otherwise     = let ((pref, str'), suff) = first (splitAt l) $ splitAt r str in pref ++ ((shift str' k) ++ suff)

shift str k = uncurry (flip (++)) . splitAt ((length str) - k) $ str

-- rotate :: String -> [Int] -> String
rotate s [l, r, k] =
		take (l - 1) s ++
	  	(take m $ drop (m - k) (ss ++ ss)) ++
	    	drop r s
	where
	 	m = r - l + 1
		k' = k `mod` m
  		ss = take m $ drop (l - 1) s

main = do
	s <- getLine
	_ <- getLine
	ops <- (map (map read . words) . lines) `fmap` getContents

	putStrLn $ foldl rotate s ops

import Control.Applicative ((<$>))

readInt :: String -> Int
readInt = read

main = do
  str <- getLine
  ops <- map (map readInt . words) . tail . lines <$> getContents
  putStrLn $ foldl rotate str ops

rotate :: String -> [Int] -> String
rotate str [l1, r1, k]
  | k == 0 || l == r = str
  | otherwise        = take l str ++ (take (r - l + 1) $ drop (r - l + 1 - k) $ ss ++ ss) ++ drop r1 str
  where l = l1 - 1
        r = r1 - 1
        n = k `mod` (r - l + 1)
        ss = take (r - l + 1) $ drop l str

import Control.Applicative ((<$>))

readInt :: String -> Int
readInt = read

main = do
  str <- getLine
  ops <- map (map readInt . words) . tail . lines <$> getContents
  print $ foldl rotate str ops

rotate :: String -> [Int] -> String
rotate str [l1, r1, k]
  | k == 0 || l == r = str
  | otherwise        = take l str ++ (take (r - l + 1) $ drop (r - l + 1 - k) $ ss ++ ss) ++ drop r1 str
  where l = l1 - 1
        r = r1 - 1
        n = k `mod` (r - l + 1)
        ss = take (r - l + 1) $ drop l str


import Control.Applicative ((<$>))
import Data.List (sort)

main = do
	[n, m] <- map read . words <$> getLine
 	as <- map read . words <$> getLine

	let
 		res = find 1 (sort as) m
	   		where
				find i [] m
      					| i <= m = i : find (i + 1) [] (m - i)
					| otherwise = []
				find i (a:as) m
					| i > m = []
					| i == a = find (i + 1) as m
					| otherwise = i : find (i + 1) (a:as) (m - i)

	print $ length res
	putStr . unwords . map show $ res

import Data.List (sort)
main = do
    [n,m] <- fmap ((map read) . words) getLine
    a <- fmap ((map read) . words) getLine
    let (len,l) = solve 0 [] m (sort a) [1..]
    print len
    mapM_ (\c -> putStr ((show c) ++ " ")) l

solve len l m [] (a:as) = if (m-a>=0) then solve (len+1) (a:l) (m-a) [] as else (len,l)
solve len l m ip (a:as)
 |a==head ip = solve len l m (tail ip) as
 |otherwise = if (m-a>=0) then solve (len+1) (a:l) (m-a) ip as else (len,l)

import Data.Foldable (toList)
import Data.Sequence (fromList , sort)
main = do
    [n,m] <- fmap ((map read) . words) getLine
    a <- fmap ((map read) . words) getLine
    let x = toList . sort $ fromList a
    let (len,l) = solve 0 [] m (x) [1..]
    print len
    mapM_ (\c -> putStr ((show c) ++ " ")) l

solve len l m [] (a:as) = if (m-a>=0) then solve (len+1) (a:l) (m-a) [] as else (len,l)
solve len l m ip (a:as)
 |a==head ip = solve len l m (tail ip) as
 |otherwise = if (m-a>=0) then solve (len+1) (a:l) (m-a) ip as else (len,l)

{-# LANGUAGE BangPatterns#-}
import Data.List (sort)
main = do
    [n,m] <- fmap ((map read) . words) getLine :: IO [Int]
    a <- fmap ((map read) . words) getLine :: IO [Int]
    let (len,l) = solve 0 [] m (sort a) [1..]
    print len
    mapM_ (\c -> putStr ((show c) ++ " ")) l

solve !len l !m [] (a:as) = if (m-a>=0) then solve (len+1) (a:l) (m-a) [] as else (len,l)
solve !len l !m ip (a:as)
 |a==head ip = solve len l m (tail ip) as
 |otherwise = if (m-a>=0) then solve (len+1) (a:l) (m-a) ip as else (len,l)

{-# LANGUAGE BangPatterns#-}
import Data.Foldable (toList)
import Data.Sequence (fromList , unstableSort)
main = do
    [n,m] <- fmap ((map read) . words) getLine
    a <- fmap ((map read) . words) getLine
    let x = toList . unstableSort $ fromList a
    let (len,l) = solve 0 [] m (x) [1..]
    print len
    mapM_ (\c -> putStr ((show c) ++ " ")) l

solve !len !l !m [] !(a:as) = if (m-a>=0) then solve (len+1) (a:l) (m-a) [] as else (len,l)
solve !len !l !m !ip !(a:as)
 |a==head ip = solve len l m (tail ip) as
 |otherwise = if (m-a>=0) then solve (len+1) (a:l) (m-a) ip as else (len,l)

import Data.Foldable (toList)
import Data.Sequence (fromList , unstableSort)
main = do
    [n,m] <- fmap ((map read) . words) getLine :: IO [Int]
    a <- fmap ((map read) . words) getLine :: IO [Int]
    let x = toList . unstableSort $ fromList a
    let (len,l) = solve 0 [] m (x) [1..]
    print len
    mapM_ (\c -> putStr ((show c) ++ " ")) l

solve len l m [] (a:as) = if (m-a>=0) then solve (len+1) (a:l) (m-a) [] as else (len,l)
solve len l m ip (a:as)
 |a==head ip = solve len l m (tail ip) as
 |otherwise = if (m-a>=0) then solve (len+1) (a:l) (m-a) ip as else (len,l)

import Control.Applicative ((<$>))
import Data.List (sort)

main = do
	[n, m] <- map read . words <$> getLine
 	as <- map read . words <$> getLine

	let
 		res = find 1 (sort as) m
	   		where
				find i [] m | i <= m = i : find (i + 1) [] (m - i)
				find i [] m | otherwise = []
				find i (a:as) m
					| i > m = []
					| i == a = find (i + 1) as m
					| otherwise = i : find (i + 1) (a:as) (m - i)

	print $ length res
	putStr . unwords . map show $ res

import qualified Data.Set as S
greedy :: Int -> S.Set Int -> [Int]
greedy cash haves = zipWith (-) (tail bought) bought
    where bought = takeWhile (<=cash) $ scanl (+) 0 $ filter (notIn haves) [1..]
          notIn s x = not $ S.member x s
solve (x:xs) = show (length soln) ++ "\n" ++ unwords (map show soln)
    where soln = greedy x $ S.fromList xs
main = putStrLn . solve . map read . tail . words =<< getContents

import Data.List
import Data.Functor

main :: IO()
main = do
    [n, m] <- map read . words <$> getLine
    a <- sort . map read . words <$> getLine
    let r = solve m 1 a []
    print $ length r
    putStrLn $ unwords $ map show r

bound :: Int
bound = 1000000000

solve :: Int -> Int -> [Int] -> [Int] -> [Int]
solve m i a r | m < i = r
              | i > bound = r
              | otherwise = if null a
                            then solve (m - i) (i + 1) a (i:r)
                            else let (av:ax) = a
                                 in if i < av
                                    then solve (m - i) (i + 1) a (i:r)
                                    else if i == av
                                         then solve m (i + 1) ax r
                                         else solve m i ax r

import Control.Applicative ((<$>))
import Data.List (sort)

main = do
	[n, m] <- map read . words <$> getLine
 	as <- map read . words <$> getLine

	let
 		res = find 1 (sort as) m
	   		where
				find i [] m | i <= m = i : find (i + 1) [] (i - m)
				find i [] m | otherwise = []
				find i (a:as) m
					| i > m = []
					| i == a = find (i + 1) as m
					| otherwise = i : find (i + 1) (a:as) (m - i)

	print $ length res
	putStr . unwords . map show $ res


module Main where

-- http://codeforces.com/contest/1199/problem/A

import qualified Data.ByteString.Char8 as B
import Control.Arrow ((>>>))
--import Debug.Trace
--import qualified Data.Array as A

solD :: Int -> Int -> [Int] -> Int
solD l r xs = day l r 0 (replicate l m ++ xs)
  where
    m = 1 + maximum xs

day :: Int -> Int -> Int -> [Int] -> Int
day l r acc xs = if min' x  lx && min' x (take r rx)
                 then acc + 1
                 else day l r (acc+1) (tail xs)
  where
    min' _ [] = True
    min' a b = a < minimum b
    (lx, x:rx) = splitAt l xs

readInt0 :: B.ByteString -> Int
readInt0 = maybe 0 fst . B.readInt

main = B.interact $ B.lines >>> fmap B.words
      >>> fmap (fmap readInt0)
      >>> (\[[_,l,r], xs] -> solD l r xs)
      >>> B.pack . show

main = getContents >>= print . solve . map read . tail . words

solve (x:y:xs) = (+1) $ length $ takeWhile (== False) $ map notRainy $ zipper [] xs
    where notRainy (a:as, b) = a == minimum (take (x + 1) (a:as) ++ take y b)

zipper rec [] = []
zipper rec (x:xs) = (x:rec, xs) : zipper (x:rec) xs

import Data.Array
import Data.List
main = do
  [n,x,y] <- map read . words <$> getLine
  as <- listArray (1,n) . map read . words <$> getLine :: IO (Array Int Int)
  let Just i = find (\i -> all ((>= as!i) . (as !)) $ filter (inRange (bounds as)) [i-x..i+y]) [1..n]
  print i

module Main where

-- http://codeforces.com/contest/1199/problem/A

import qualified Data.ByteString.Char8 as B
import Control.Arrow ((>>>))
--import Debug.Trace
--import qualified Data.Array as A

solD :: Int -> Int -> [Int] -> Int
solD l r xs = day l r 0 (replicate l m ++ xs) - l
  where
    m = maximum xs

day :: Int -> Int -> Int -> [Int] -> Int
day l r acc xs = if min' x  lx && min' x (take r rx)
                 then acc + l + 1
                 else day l r (acc+1) (tail xs)
  where
    min' _ [] = True
    min' a b = a < minimum b
    (lx, x:rx) = splitAt l xs

readInt0 :: B.ByteString -> Int
readInt0 = maybe 0 fst . B.readInt

main = B.interact $ B.lines >>> fmap B.words
      >>> fmap (fmap readInt0)
      >>> (\[[_,l,r], xs] -> solD l r xs)
      >>> B.pack . show

module Main where

-- http://codeforces.com/contest/1199/problem/A

import qualified Data.ByteString.Char8 as B
import Control.Arrow ((>>>))
--import Debug.Trace
--import qualified Data.Array as A

solD :: Int -> Int -> [Int] -> Int
solD l r xs = if length xs <= l
              then length xs
              else day l r 0 xs

day :: Int -> Int -> Int -> [Int] -> Int
day l r acc xs = if min' x  lx && min' x (take r rx)
                 then acc + l + 1
                 else day l r (acc+1) (tail xs)
  where
    min' _ [] = True
    min' a b = a < minimum b
    (lx, x:rx) = splitAt l xs

readInt0 :: B.ByteString -> Int
readInt0 = maybe 0 fst . B.readInt

main = B.interact $ B.lines >>> fmap B.words
      >>> fmap (fmap readInt0)
      >>> (\[[_,l,r], xs] -> solD l r xs)
      >>> B.pack . show

-- import Debug.Trace
import Data.List
main = interact $ show . solve . map read . words
data T = T {tI :: !Int, tJ :: !Int, tM :: !Int, tMI :: !Int, tT :: B} deriving Show
data B = B {bLeft :: T, bRight :: T} | N { nA :: !Int } deriving Show
solve :: [Int] -> Int
solve (n:x:y:as) = go 1 where
  t = makeTree $ zipWith5 T [1..] [1..] as [1..] (map N as)
  makeTree :: [T] -> T
  makeTree [t] = t
  makeTree ts = makeTree $ pairs ts
  pairs :: [T] -> [T]
  pairs [] = []
  pairs [t] = [t]
  pairs (a:b:ts) = T (tI a) (tJ b) m mi (B a b) : pairs ts where
    (m,mi) = min (tM a,tMI a) (tM b,tMI b)
  go l | l > n = 42
       | max 1 (i - x) == l = i
       | otherwise = go (i-x) where
    i = locateMin l (min n (l+x+y))
  locateMin l r = -- traceShowId $ traceShow (l,r) $
                  snd $ go l r t where
    go :: Int -> Int -> T -> (Int,Int)
--    go l r _ | traceShow (l,r) False = undefined
    go l r (T i j m mi t) | l == i && r == j = (m,mi)
    go l r (T i j m _ (B a b)) | r <= tJ a = go l r a
                               | l >= tI b = go l r b
                               | otherwise = min left right
      where left = go l (tJ a) a
            right = go (tI b) r b
--    go l r t | traceShow (l,r,t) False = undefined


{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}
{-# LANGUAGE BangPatterns, ViewPatterns, TupleSections, OverloadedStrings #-}
import Control.Applicative
import Control.Monad
import Control.Monad.ST
import Data.Array.Base
import Data.Array.ST (runSTUArray, STArray)
import Data.Bits
import qualified Data.ByteString.Char8 as B
import Data.Char
import Data.Function
import Data.Int
import Data.List
import qualified Data.Map as M
import qualified Data.Set as S
import Data.STRef
import GHC.Arr (Array, Ix, unsafeIndex)
bool :: a -> a -> Bool -> a
bool t f b=if b then t else f
readInt :: B.ByteString -> Int
readInt bs = case B.readInt bs of Just (n,_) -> n
rep, rev :: Monad m => Int -> (Int -> m ()) -> m ()
rep !n f=go 0 where go !i=when(i<n)$f i>>go(i+1)
rev !n f=go(n-1)where go !i=when(i>=0)$f i>>go(i-1)
{-# INLINE rep #-}
{-# INLINE rev #-}
modifyArray :: (MArray a e m, Ix i) => a i e -> i -> (e -> e) -> m ()
modifyArray a i f=readArray a i>>=writeArray a i.f
{-# INLINE modifyArray #-}
unsafeModify :: (MArray a e m, Ix i) => a i e -> Int -> (e -> e) -> m ()
unsafeModify a i f=unsafeRead a i>>=unsafeWrite a i.f
{-# INLINE unsafeModify #-}

main :: IO ()
main = do
  [n,m] <- map read.words <$> getLine
  xs <- sort.map (fromIntegral.readInt).B.words <$> B.getLine
  ys <- sort.map (fromIntegral.readInt).B.words <$> B.getLine
  print $ solve n m xs ys

solve :: Int -> Int -> [Int64] -> [Int64] -> Int64
solve n m xs ys = minimum $ map calc bds
   where
     bds = xs ++ ys :: [Int64]
     !arrX = listArray (0,n-1) xs :: UArray Int Int64
     !arrY = listArray (0,m-1) ys :: UArray Int Int64
     !sumX = listArray (0,n-1) $ scanl1 (+) xs :: UArray Int Int64
     !sumY = listArray (0,m-1) $ scanr1 (+) ys :: UArray Int Int64
     calc bd = calcX bd + calcY bd
      where
        calcX bd
          | bd <= unsafeAt arrX 0 = 0
          | otherwise = fromIntegral (i+1) * bd - unsafeAt sumX i
          where
            i = upperBound (\i->unsafeAt arrX i < bd) 0 (n-1)
        calcY bd
          | unsafeAt arrY (m-1) <= bd = 0
          | otherwise = unsafeAt sumY j - bd * (fromIntegral $ m-1-j+1)
           where
             j = lowerBound (\j->unsafeAt arrY j > bd) 0 (m-1)

lowerBound :: (Integral i) => (i -> Bool) -> i -> i -> i
lowerBound p low high = go low high
   where
     go !low !high
       | high <= low = high
       | p mid       = go low       mid
       | otherwise   = go (mid + 1) high
      where
        mid = (low + high) `quot` 2
{-# INLINE lowerBound #-}

upperBound :: (Integral i) => (i -> Bool) -> i -> i -> i
upperBound p low high = go low high
   where
     go !low !high
       | high <= low = low
       | p mid       = go mid high
       | otherwise   = go low (mid - 1)
      where
        mid = (low + high + 1) `quot` 2
{-# INLINE upperBound #-}

import Data.List
z[] _=[]
z _ []=[]
z(a:as)(b:bs)|b>a=(b-a):z as bs|1>0=[]
s[a,b]=foldl'(+)0$z(sort a)(reverse$sort b)
main=interact$show.s.map(map(foldl'(\a c->fromIntegral(fromEnum c-fromEnum '0')+a*10::Integer)0).words).tail.lines

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}
{-# LANGUAGE BangPatterns, ViewPatterns, TupleSections, OverloadedStrings #-}
import Control.Applicative
import Control.Monad
import Control.Monad.ST
import Data.Array.Base
import Data.Array.ST (runSTUArray, STArray)
import Data.Bits
import qualified Data.ByteString.Char8 as B
import Data.Char
import Data.Function
import Data.Int
import Data.List
import qualified Data.Map as M
import qualified Data.Set as S
import Data.STRef
import GHC.Arr (Array, Ix, unsafeIndex)
bool :: a -> a -> Bool -> a
bool t f b=if b then t else f
readInt :: B.ByteString -> Int
readInt bs = case B.readInt bs of Just (n,_) -> n
rep, rev :: Monad m => Int -> (Int -> m ()) -> m ()
rep !n f=go 0 where go !i=when(i<n)$f i>>go(i+1)
rev !n f=go(n-1)where go !i=when(i>=0)$f i>>go(i-1)
{-# INLINE rep #-}
{-# INLINE rev #-}
modifyArray :: (MArray a e m, Ix i) => a i e -> i -> (e -> e) -> m ()
modifyArray a i f=readArray a i>>=writeArray a i.f
{-# INLINE modifyArray #-}
unsafeModify :: (MArray a e m, Ix i) => a i e -> Int -> (e -> e) -> m ()
unsafeModify a i f=unsafeRead a i>>=unsafeWrite a i.f
{-# INLINE unsafeModify #-}

main :: IO ()
main = do
  [n,m] <- map read.words <$> getLine
  xs <- sort.map (fromIntegral.readInt).B.words <$> B.getLine
  ys <- sort.map (fromIntegral.readInt).B.words <$> B.getLine
  print $ solve n m xs ys

solve :: Int -> Int -> [Int64] -> [Int64] -> Int64
solve n m xs ys = minimum $ map calc bds
   where
     bds = maximum ys : xs :: [Int64]
     !arrX = listArray (0,n-1) xs :: UArray Int Int64
     !arrY = listArray (0,m-1) ys :: UArray Int Int64
     !sumX = listArray (0,n-1) $ scanl1 (+) xs :: UArray Int Int64
     !sumY = listArray (0,m-1) $ scanr1 (+) ys :: UArray Int Int64
     calc bd = calcX bd + calcY bd
      where
        calcX bd
          | bd <= unsafeAt arrX 0 = 0
          | otherwise = fromIntegral (i+1) * bd - unsafeAt sumX i
          where
            i = upperBound (\i->unsafeAt arrX i < bd) 0 (n-1)
        calcY bd
          | unsafeAt arrY (m-1) <= bd = 0
          | otherwise = unsafeAt sumY j - bd * (fromIntegral $ m-1-j+1)
           where
             j = lowerBound (\j->unsafeAt arrY j > bd) 0 (m-1)

lowerBound :: (Integral i) => (i -> Bool) -> i -> i -> i
lowerBound p low high = go low high
   where
     go !low !high
       | high <= low = high
       | p mid       = go low       mid
       | otherwise   = go (mid + 1) high
      where
        mid = (low + high) `quot` 2
{-# INLINE lowerBound #-}

upperBound :: (Integral i) => (i -> Bool) -> i -> i -> i
upperBound p low high = go low high
   where
     go !low !high
       | high <= low = low
       | p mid       = go mid high
       | otherwise   = go low (mid - 1)
      where
        mid = (low + high + 1) `quot` 2
{-# INLINE upperBound #-}

import Data.List
z[] _=[]
z _ []=[]
z(a:as)(b:bs)|b>a=(b-a):z as bs|1>0=[]
s[a,b]=foldl'(+)0$z(sort a)(reverse$sort b)
main=interact$show.s.map(map(foldl'(\a c->fromEnum c-fromEnum '0'+a*10)0).words).tail.lines


main = do
	[n, m] <- fmap (map read . words) getLine
	putStrLn $ show $ gao n m

gao n m = r1 + r2 + 2 * (i' - r') where
	r1 = min n ((m + 1) `div` 2)
	r2 = max 0 $ pred $ min n ((m + 4) `div` 4)
--	sq = [i * i | i <- [1 .. n]]
	r' = sum $ zipWith (-) [1 ..] $ tail $ scanl (\i j -> until (\k -> k * k >= j) succ i) 0 [i * i - m | i <- [1 .. n]]
	i' = sum $ map (min m) [i * i | i <- [1 .. n]]

main = do
	[n, m] <- fmap (map read . words) getLine
	putStrLn $ show $ gao n m

gao n m = r1 + r2 + 2 * (i' - r') where
	r1 = min n ((m + 1) `div` 2)
	r2 = max 0 $ pred $ min n ((m + 4) `div` 4)
	r' = sum $ zipWith (-) [1 ..] $ tail $ scanl (\i j -> until ((>=j) . (^2)) succ i) 0 [i * i - m | i <- [1 .. n]]
	i' = sum $ map (min m) [i * i | i <- [1 .. n]]

main = do getLine >>= putStr . show . gao . map read . words

gao [n, m] = r1 + r2 + 2 * (i' - r') where
	r1 = min n ((m + 1) `div` 2)
	r2 = max 0 $ pred $ min n ((m + 4) `div` 4)
	r' = sum $ zipWith (-) [1 ..] $ tail $ scanl (\i j -> until ((>=j) . (^2)) succ i) 0 [i * i - m | i <- [1 .. n]]
	i' = sum $ map (min m) [i * i | i <- [1 .. n]]

main = do
	[n, m] <- fmap (map read . words) getLine
	putStrLn $ show $ gao n m

gao n m = r1 + r2 + 2 * (i' - r') where
	r1 = min n ((m + 1) `div` 2)
	r2 = min n (m `div` 4)
	sq = [i * i | i <- [1 .. n]]
	r' = sum $ zipWith (-) [1 ..] $ tail $ scanl (\i j -> until (\k -> k * k >= j) succ i) 0 $ map (flip (-) m) sq
	i' = sum $ map (min m) sq


import qualified Data.ByteString.Lazy.Char8 as P
import qualified Data.ByteString.Builder as B
import System.IO

import Control.Monad
import Control.Monad.Trans.State
import Control.Monad.Trans.Class
import Data.List
import Data.Semigroup

prune :: [Int] -> [Int]
prune (x : ys@(y : zs))
  | x >= y    = prune (x : zs)
  | otherwise = x : prune ys
prune xs = xs

solve :: [Int] -> [Int]
solve li = let
  qs = prune li
  psegs = reverse $ scanl (\(_, rli) q -> span (< q) rli) ([], li) qs
  in  concat $ snd (head psegs) : map fst psegs

main :: IO ()
main = (P.getContents >>=) . evalStateT $ do
  [t] <- readInts
  replicateM_ t $ do
    _ <- readInts
    p <- readInts
    putInts $ solve p

type SIO = StateT P.ByteString IO

readLine :: SIO P.ByteString
readLine = do
  (out, next) <- P.break (<= '\r') <$> get
  put $ P.dropWhile (<= '\r') next
  pure out

readInts :: SIO [Int]
readInts = do
  currLine <- readLine
  pure [x | Just (x, _) <- P.readInt <$> P.words currLine]

putInts :: [Int] -> SIO ()
putInts li = let
  outBuilder = foldr (<>) (B.char7 '\n')
    $ intersperse (B.char7 ' ') (map B.intDec li)
  in  lift $ B.hPutBuilder stdout outBuilder

{-# LANGUAGE OverloadedStrings, FunctionalDependencies, FlexibleContexts,
    ConstraintKinds, BangPatterns #-}

import Control.Applicative
import Control.Exception (assert)
import Control.Monad
import Control.Monad.State
import Data.Array.IArray
import Data.Array.Unboxed (UArray)
import Data.Bits
import qualified Data.ByteString.Lazy.Char8 as B
import Data.Char
import Data.Int
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IntMap
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
import Data.List
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Maybe
import Data.Monoid
import Data.Ord
import Data.Ratio
import Data.Sequence (Seq)
import qualified Data.Sequence as Seq
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Traversable
import System.IO

------

type Hopper r = State [r]

type HopperT r = StateT [r]

runHopper :: Hopper r a -> [r] -> (a, [r])
runHopper = runState

evalHopper :: Hopper r a -> [r] -> a
evalHopper = evalState

runHopperT :: HopperT r m a -> [r] -> m (a, [r])
runHopperT = runStateT

evalHopperT :: Monad m => HopperT r m a -> [r] -> m a
evalHopperT = evalStateT

type MonadHopper r m = MonadState [r] m

pop :: MonadHopper r m => m (Maybe r)
pop = state f
  where
    f :: [r] -> (Maybe r, [r])
    f [] = (Nothing, [])
    f (q:qs) = (Just q, qs)

popJust :: MonadHopper r m => m r
popJust = fromJust <$> pop

------

popInt :: MonadHopper B.ByteString m => m Int
popInt = fst . fromJust . B.readInt <$> popJust

popInts :: MonadHopper B.ByteString m => m [Int]
popInts = map (fst . fromJust . B.readInt) . B.words <$> popJust

popInteger :: MonadHopper B.ByteString m => m Integer
popInteger = fst . fromJust . B.readInteger <$> popJust

popIntegers :: MonadHopper B.ByteString m => m [Integer]
popIntegers = map (fst . fromJust . B.readInteger) . B.words <$> popJust

------

dropCR :: B.ByteString -> B.ByteString
dropCR s | not (B.null s) && B.last s == '\r' = B.init s
         | otherwise = s

bshow :: Show a => a -> B.ByteString
bshow = B.pack . show

main = B.interact go
  where
    go :: B.ByteString -> B.ByteString
    go = evalHopper run . map dropCR . B.lines

------

run :: Hopper B.ByteString B.ByteString
run = do
  t <- popInt
  B.unlines <$> replicateM t run1

run1 = do
  n <- popInt
  ps <- popInts
  return $ B.unwords $ map bshow (solve n ps)

solve n ps0 = solve' (IntSet.fromDistinctAscList [1 .. n]) (reverse ps0)
  where
    solve' _ [] = []
    solve' s ps =
      let i = fromJust $ elemIndex (IntSet.findMax s) ps
          (qs, rs) = splitAt (i + 1) ps
          s' = IntSet.difference s (IntSet.fromList qs) in
        reverse qs ++ solve' s' rs


{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}

import safe Control.Arrow ((>>>))
import qualified Data.Text as T
import qualified Data.Text.IO as TI
import Data.List
import Data.Function
import Data.Maybe

main :: IO ()
main = TI.getContents >>= (T.lines >>> drop 1 >>> tcio >>> (mapM_ putStrLn)) where
  tcio :: [T.Text] -> [String]
  tcio [] = []
  tcio (nm : rest) = ((:[]).itoline . solve . linestoiss) (nm:(take 1 rest)) ++ tcio (drop 1 rest)
  linetoi t = f 0 t where f n t = if (T.null) t then n else f (10*n + fromIntegral(on (-) fromEnum (T.head t) '0') ) (T.tail t) ;
  linetois = (map linetoi).(T.words); linestoiss = map linetois
  itoline = show; istoline = unwords . (map itoline); isstolines = map istoline

solve :: [[Integer]] -> Integer
solve [[_,x], qs] = sum $ map (\(q,t)->q*t) $ concat $ map fst $ unfoldr f (zip qs (repeat 1), True) where
  f ([],_) = Nothing
  f qtsc@(qts,can) = Just (qtsc, (reverse qts1,can1)) where
    (qts1,can1) = foldl' g ([],can) qts
    g (qts,can) (q,t)
      | can && q `mod` x == 0 = ((q `div` x, t*x):qts,True)
      | otherwise = (qts, False)

{-# LANGUAGE DeriveFunctor       #-}
{-# LANGUAGE GADTs               #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE ScopedTypeVariables #-}

import           Control.Arrow              ((>>>))
import           Control.Monad              (guard, MonadPlus (mplus), foldM_, forM_,
                                             join, replicateM_)
import qualified Control.Monad.ST.Strict    as ST
import qualified Control.Monad.State.Strict as S
import qualified Data.Array                 as A
import qualified Data.Array.MArray.Safe     as MA
import qualified Data.Array.ST.Safe         as STA
import           Data.Bits
import qualified Data.ByteString.Char8      as B8
import           Data.Char
import           Data.Functor               ((<&>))
import           Data.IORef                 (modifyIORef, newIORef, readIORef)
import           Data.Int
import qualified Data.IntMap                as IM
import           Data.List                  (elemIndices, find, findIndices,
                                             intersect, isPrefixOf, nub, sort,
                                             sortOn)
import qualified Data.Map                   as M
import           Data.Maybe                 (fromJust, fromMaybe, isJust)
import qualified Data.Set                   as S
import           Data.Traversable           (forM)
import           GHC.IO.Handle              (hDuplicateTo)
import           System.IO                  (IOMode (ReadMode, WriteMode),
                                             openFile, stdin, stdout)
import           Text.Printf                (printf)

-- import Debug.Trace (trace)
-- debug = flip trace

readIntB8 :: B8.ByteString -> Int
readIntB8 = B8.readInt >>> (fst . fromJust)

readIntegerB8 :: B8.ByteString -> Integer
readIntegerB8 = B8.readInteger >>> (fst . fromJust)

solve :: Int -> Int64 -> [Int64] -> Int64
solve n x as = answer
    where
        xs = takeWhile (<= (1000000000 :: Int64)) $ iterate (* x) x
        answer = sum $ as ++ do
            currX <- xs
            let prevX = currX `div` x
            guard $ all (\a -> a `mod` prevX == 0) as
            a <- takeWhile (\a -> a `mod` currX  == 0) as
            return a

main :: IO ()
main = do
    t <- B8.getLine <&> readIntB8
    replicateM_ t $ do
        [n, x] <- B8.getLine <&> map readIntB8 . B8.words
        as <- B8.getLine <&> map (\e -> (fromInteger (readIntegerB8 e)) :: Int64) . B8.words
        let answer = solve n ((fromIntegral x) :: Int64) as
        printf "%lld\n" answer

import Control.Arrow
import Control.Monad
import Data.Char
--import Debug.Trace
import System.IO
import Text.Read

main :: IO ()
main = do
  t <- readLn :: IO Integer
  forM_ [1 .. t] handleCase

handleCase :: Integer -> IO ()
handleCase c = do
  [n, x] <- fmap (map read . words) getLine :: IO [Integer]
  a <- fmap (map read . words) getLine :: IO [Integer]
  print $ solve x a
  return ()

stp :: Integer -> [Integer] -> [Integer]
stp x a = map (`div` x) a'
  where
    a' = takeWhile (\i -> i `mod` x == 0) a

takeWhileIncl :: (a -> Bool) -> [a] -> [a]
takeWhileIncl f [] = []
takeWhileIncl f (x:xs)
  | f x = x:takeWhileIncl f xs
  | otherwise = [x]

solve :: Integer -> [Integer] -> Integer
solve x a =
  let allNums = takeWhileIncl (\x -> length x == length a) . iterate (stp x) $ a
      withPowers = zip allNums (iterate (* x) 1)
      result = sum $ map (\(a, b) -> sum a * b) withPowers
   in result

import Control.Monad
import Data.List
import Text.Printf

readArray :: Read a => IO [a]
readArray = fmap (map read . words) getLine

readInts :: IO [Integer]
readInts = readArray

valuation :: Integer -> Integer -> Integer
valuation x a
  | a `mod` x == 0 = 1 + (valuation x $ a `div` x)
  | otherwise = 1

argmin :: [Integer] -> (Int, Integer)
argmin [a] = (0, a)
argmin (a:as) =
  let (i, b) = argmin as in
  if b < a then (i + 1, b) else (0, a)

solve :: Integer -> [Integer] -> Integer
solve x a =
  let (i, v) = argmin $ map (valuation x) a in
  (v + 1) * (sum $ take i a) + v * (sum $ drop i a)

solveCase :: IO String
solveCase = do
  (n:x:[]) <- readInts
  fmap (show . solve x) readInts

main :: IO ()
main = readLn >>= flip replicateM_ (solveCase >>= putStrLn)

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
--repeat
import safe Control.Arrow ((>>>))
import qualified Data.Text as T
import qualified Data.Text.IO as TI
import Data.List
import Data.Function
import Data.Maybe

main :: IO ()
main = TI.getContents >>= (T.lines >>> drop 1 >>> tcio >>> (mapM_ putStrLn)) where
  tcio :: [T.Text] -> [String]
  tcio [] = []
  tcio (nm : rest) = ((:[]).itoline . solve . linestoiss) (nm:(take 1 rest)) ++ tcio (drop 1 rest)
  linetoi t = f 0 t where f n t = if (T.null) t then n else f (10*n + fromIntegral(on (-) fromEnum (T.head t) '0') ) (T.tail t) ;
  linetois = (map linetoi).(T.words); linestoiss = map linetois
  itoline = show; istoline = unwords . (map itoline); isstolines = map istoline

solve :: [[Integer]] -> Integer
solve [[_,x], qs] = sum $ map (\(q,t)->q*t) $ concat $ map fst $ unfoldr f (zip qs (repeat 1), True) where
  f ([],_) = Nothing
  f qtsc@(qts,can) = Just (qtsc, (reverse qts1,can1)) where
    (qts1,can1) = foldl' g ([],can) qts
    g (qts,can) (q,t)
      | can && q `mod` x == 0 = ((q `div` x, t*x):qts,True)
      | otherwise = (qts, False)

import Control.Monad
import Data.Int

readInts :: IO [Int64]
readInts = map read . words <$> getLine

runSeveral :: IO () -> IO ()
runSeveral act = do
  t <- read <$> getLine
  replicateM_ t act

main :: IO ()
main = runSeveral $ do
  [_n, x] <- readInts
  a <- readInts
  let
    ct w q = case  quotRem q x  of
      (ans, 0) -> ct (w+1) ans
      _ -> w
    allCts = map (ct 1) a
    passes = minimum allCts
    (pref, suff) = span (> passes) allCts
  print . sum . zipWith (*) a $ (passes + 1 <$ pref) ++ (passes <$ suff)

import Control.Monad
import Data.Int
import Data.List

readInts :: IO [Int64]
readInts = map read . words <$> getLine

runSeveral :: IO () -> IO ()
runSeveral act = do
  t <- read <$> getLine
  replicateM_ t act

main :: IO ()
main = runSeveral $ do
  [_n, x] <- readInts
  a <- readInts
  let
    r (ct, q) = case  quotRem q x  of
      (ans, 0) -> Just (x*ct, ans)
      _ -> Nothing
    clean (Just v : vs) = Just (v, vs)
    clean _ = Nothing
    a_stop = zip (repeat 1) a ++ unfoldr clean (map r a_stop)
  print . sum $ map (uncurry (*)) a_stop

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}

import safe Control.Arrow ((>>>))
import qualified Data.Text as T
import qualified Data.Text.IO as TI
import Data.List
import Data.Function
import Data.Maybe

main :: IO ()
main = TI.getContents >>= (T.lines >>> drop 1 >>> tcio >>> (mapM_ putStrLn)) where
  tcio :: [T.Text] -> [String]
  tcio [] = []
  tcio (nm : rest) = ((:[]).itoline . solve . linestoiss) (nm:(take 1 rest)) ++ tcio (drop 1 rest)
  linetoi t = f 0 t where f n t = if (T.null) t then n else f (10*n + fromIntegral(on (-) fromEnum (T.head t) '0') ) (T.tail t) ;
  linetois = (map linetoi).(T.words); linestoiss = map linetois
  itoline = show; istoline = unwords . (map itoline); isstolines = map istoline

solve :: [[Integer]] -> Integer
solve [[_,x], qs] = sum $ map (\(q,t)->q*t) $ concat $ map fst $ unfoldr f (zip qs (repeat 1), True) where
  f ([],_) = Nothing
  f qtsc@(qts,can) = Just (qtsc, (reverse qts1,can1)) where
    (qts1,can1) = foldr g ([],can) qts
    g (q,t) (qts,can)
      | can && q `mod` x == 0 = ((q `div` x, t*x):qts,True)
      | otherwise = (qts, False)

import Control.Monad
import Data.Int
import Data.List

readInts :: IO [Int64]
readInts = map read . words <$> getLine

runSeveral :: IO () -> IO ()
runSeveral act = do
  t <- read <$> getLine
  replicateM_ t act

main :: IO ()
main = runSeveral $ do
  [_n, x] <- readInts
  a <- readInts
  let
    r (ct, q) = case  quotRem q x  of
      (ans, 0) -> Just (2*ct, ans)
      _ -> Nothing
    clean (Just v : vs) = Just (v, vs)
    clean _ = Nothing
    a_stop = zip (repeat 1) a ++ unfoldr clean (map r a_stop)
  print . sum $ map (uncurry (*)) a_stop

{-# LANGUAGE DeriveFunctor       #-}
{-# LANGUAGE GADTs               #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE ScopedTypeVariables #-}

import           Control.Arrow              ((>>>))
import           Control.Monad              (guard, MonadPlus (mplus), foldM_, forM_,
                                             join, replicateM_)
import qualified Control.Monad.ST.Strict    as ST
import qualified Control.Monad.State.Strict as S
import qualified Data.Array                 as A
import qualified Data.Array.MArray.Safe     as MA
import qualified Data.Array.ST.Safe         as STA
import           Data.Bits
import qualified Data.ByteString.Char8      as B8
import           Data.Char
import           Data.Functor               ((<&>))
import           Data.IORef                 (modifyIORef, newIORef, readIORef)
import           Data.Int
import qualified Data.IntMap                as IM
import           Data.List                  (elemIndices, find, findIndices,
                                             intersect, isPrefixOf, nub, sort,
                                             sortOn)
import qualified Data.Map                   as M
import           Data.Maybe                 (fromJust, fromMaybe, isJust)
import qualified Data.Set                   as S
import           Data.Traversable           (forM)
import           GHC.IO.Handle              (hDuplicateTo)
import           System.IO                  (IOMode (ReadMode, WriteMode),
                                             openFile, stdin, stdout)
import           Text.Printf                (printf)

-- import Debug.Trace (trace)
-- debug = flip trace

readIntB8 :: B8.ByteString -> Int
readIntB8 = B8.readInt >>> (fst . fromJust)

readIntegerB8 :: B8.ByteString -> Integer
readIntegerB8 = B8.readInteger >>> (fst . fromJust)

solve :: Int -> Int64 -> [Int64] -> Int64
solve n x as = answer
    where
        xs = iterate (* x) x
        cnts = [fromIntegral . (+1) . length $ takeWhile (\x -> a `mod` x == 0) xs | a <- as]
        answer = sum . zipWith (*) as $ scanl1 min cnts

main :: IO ()
main = do
    t <- B8.getLine <&> readIntB8
    replicateM_ t $ do
        [n, x] <- B8.getLine <&> map readIntB8 . B8.words
        as <- B8.getLine <&> map (\e -> (fromInteger (readIntegerB8 e)) :: Int64) . B8.words
        let answer = solve n ((fromIntegral x) :: Int64) as
        printf "%lld\n" answer

import Control.Arrow
import Control.Monad
import Data.Char
import Data.Maybe (isJust)
import System.IO
import Text.Read

main :: IO ()
main = do
  t <- readLn :: IO Integer
  forM_ [1 .. t] handleCase

handleCase :: Integer -> IO ()
handleCase c = do
  [n, x] <- fmap (map read . words) getLine :: IO [Integer]
  a <- fmap (map read . words) getLine :: IO [Integer]
  print  $ solve x a
  return ()

stp :: Integer -> [Integer] -> Maybe [Integer]
stp x a
  | null a' = Nothing
  | otherwise = Just $ map (`div` x) a'
  where a' = takeWhile (\i -> i `mod` x == 0) a

solve :: Integer -> [Integer] -> Integer
solve x a =
  let
    Just allNums = sequence . takeWhile  isJust . iterate (>>= stp x) $ Just a
    withPowers = zip allNums (iterate (* x) 1)
    result = sum $ map (\ (a, b) -> sum a * b) withPowers
  in result