(*
   build sequence a0, a1, ... , an
   a0 = 0,
   1)ai = a(i-1)+x or 2)ai = a(i-1)-y
   ai <= B

   maximise sum

   want all ai to be as close to B as possible
   - do 1) up to B then (2) and (1) alternating

   a0, a1, a2, ... , ak, a(k+1), ... , an

   0, x, 2*x, ... , k*x where k*x <= B, k*x-y, ... , an

   sum = (x+2*x+...+k*x) + floor((n-k)/2)*(k*x-y+k*x) + (k*x-y if n-k is not even)

   n=5, B=100, x=1, y=30

    0, 1, 2, 3, 4, 5 = 5/2(5+1) =15

   n=7, B=1e8, x=1e8, y=1e8

   0, 1e8, 0, 1e8, 0, 1e8, 0 = 7

   k = 1e8 / 1e8 = 1 < n
   s1 = (1e8*1*2/2)=1e8
   s2 = 6*(1e8)/2 = 3*1e8
   n-k is even so 4*1e8

   n=4, B=1, x=7, y=3

   0, -3, 4 -> PROBLEM!

   k = 1 / 7 = 0
   s1 = 0
   s2 = (4)*(2*0-3)/2=-6

   add +x until no longer possible
   need to -y until we can +x
   add +x
   need to -y until we can +x
   repeat O(n) complexity

   add +x k1 times
   subtract y k2 times
   add x
   subtract y k2 times
   O(1) complexity if we calculate k1 and k2

   k1*x-k2*y + x<=B
   (k1+1)*x-B<=k2*y
   ((k1+1)*x-B)/y <= k2, since k2 isn't necessarily constant we have to do O(n)

   e.g 0, -3, -6, 1, -2, -5, -8, 0 (goes from 2 to 3)

   n=5, B=100, x=1, y=30

   0 + alternate_sum 100 1 30 1 4
   0 + 1 + alternate_sum ... 2 3
   0 + 1 + 2 + ... 3 2
   0 + 1 + 2 + 3 + ... 4 1
   0 + 1 + 2 + 3 + 4 + ... 5 0
   0 + 1 + 2 + 3 + 4 + 5

    *)

let read_int () = Scanf.scanf " %d" (fun x -> x);;
let read_float () = Scanf.scanf " %f" (fun x -> x);;

let rec alternate_sum _B x y current = function
  | 0 -> current
  | n ->
    if (current +. x<=_B) then current +. alternate_sum _B x y (current +. x) (n-1)
    else current +. alternate_sum _B x y (current -. y) (n-1)
;;

let run_case = function
  | () ->
    let n = read_int () in
    let _B = read_float () in
    let x = read_float () in
    let y = read_float () in
    alternate_sum _B x y 0.0 n
;;

let print_big_float_as_int = fun x ->
   if (x!='.') then print_char (x)
;;

let rec iter_cases = function
  | 0 -> 0
  | n ->
    let sum = run_case () in
    (*let _ = String.iter print_big_float_as_int (string_of_float sum) in*)
    Printf.printf "%.0f" sum;
    print_newline ();
    iter_cases (n-1)
;;

let t = read_int () in
iter_cases t ;;

let n = int_of_string (input_line stdin) in
for i = 0 to n-1 do
  let n, b, x, y = Scanf.sscanf (input_line stdin) "%d  %d  %d  %d" (fun n b x y -> (n,b,x,y)) in
  let tot= ref (Int64.zero) in
  let curr = ref (Int64.zero) in
  for j = 1 to n do
    if Int64.add (!tot) (Int64.of_int x) <= Int64.of_int b then
      (tot := Int64.add (!tot) (Int64.of_int x) ; curr := Int64.add (!curr) (!tot))
    else (tot := Int64.add (!tot) (Int64.of_int (-y)) ; curr := Int64.add (!curr) (!tot)) ;
  done;
  print_endline (Int64.to_string !curr )
done;
;;

(*
   build sequence a0, a1, ... , an
   a0 = 0,
   1)ai = a(i-1)+x or 2)ai = a(i-1)-y
   ai <= B

   maximise sum

   want all ai to be as close to B as possible
   - do 1) up to B then (2) and (1) alternating

   a0, a1, a2, ... , ak, a(k+1), ... , an

   0, x, 2*x, ... , k*x where k*x <= B, k*x-y, ... , an

   sum = (x+2*x+...+k*x) + floor((n-k)/2)*(k*x-y+k*x) + (k*x-y if n-k is not even)

   n=5, B=100, x=1, y=30

    0, 1, 2, 3, 4, 5 = 5/2(5+1) =15

   n=7, B=1e8, x=1e8, y=1e8

   0, 1e8, 0, 1e8, 0, 1e8, 0 = 7

   k = 1e8 / 1e8 = 1 < n
   s1 = (1e8*1*2/2)=1e8
   s2 = 6*(1e8)/2 = 3*1e8
   n-k is even so 4*1e8

   n=4, B=1, x=7, y=3

   0, -3, 4 -> PROBLEM!

   k = 1 / 7 = 0
   s1 = 0
   s2 = (4)*(2*0-3)/2=-6

   add +x until no longer possible
   need to -y until we can +x
   add +x
   need to -y until we can +x
   repeat O(n) complexity

   add +x k1 times
   subtract y k2 times
   add x
   subtract y k2 times
   O(1) complexity if we calculate k1 and k2

   k1*x-k2*y + x<=B
   (k1+1)*x-B<=k2*y
   ((k1+1)*x-B)/y <= k2, since k2 isn't necessarily constant we have to do O(n)

   e.g 0, -3, -6, 1, -2, -5, -8, 0 (goes from 2 to 3)

   n=5, B=100, x=1, y=30

   0 + alternate_sum 100 1 30 1 4
   0 + 1 + alternate_sum ... 2 3
   0 + 1 + 2 + ... 3 2
   0 + 1 + 2 + 3 + ... 4 1
   0 + 1 + 2 + 3 + 4 + ... 5 0
   0 + 1 + 2 + 3 + 4 + 5

    *)

let read_int () = Scanf.scanf " %d" (fun x -> x);;
let read_float () = Scanf.scanf " %f" (fun x -> x);;

let rec alternate_sum _B x y current = function
  | 0 -> current
  | n ->
    if (current +. x<=_B) then current +. alternate_sum _B x y (current +. x) (n-1)
    else current +. alternate_sum _B x y (current -. y) (n-1)
;;

let run_case = function
  | () ->
    let n = read_int () in
    let _B = read_float () in
    let x = read_float () in
    let y = read_float () in
    alternate_sum _B x y 0.0 n
;;

let print_big_float_as_int = fun x ->
    if (x!='.') then print_char (x)
;;

let rec print_big_float_as_int2 = function
| current_float, current_order ->
  if (current_float <= float_of_int max_int) then current_float, current_order
  else print_big_float_as_int2 ((current_float/.10.0), (current_order+1))
;;
let rec print_order = function
  | 0 -> ()
  | n ->
    print_int 0;
    print_order (n-1)
;;
let rec iter_cases = function
  | 0 -> 0
  | n ->
    let sum = run_case () in
    (* let _ = String.iter print_big_float_as_int (string_of_float sum) in *)
    let float_int, order = print_big_float_as_int2 (sum, 0) in
    print_int (int_of_float float_int);
    print_order order;
    print_newline ();
    iter_cases (n-1)
;;

let t = read_int () in
iter_cases t ;;

(*
   build sequence a0, a1, ... , an
   a0 = 0,
   1)ai = a(i-1)+x or 2)ai = a(i-1)-y
   ai <= B

   maximise sum

   want all ai to be as close to B as possible
   - do 1) up to B then (2) and (1) alternating

   a0, a1, a2, ... , ak, a(k+1), ... , an

   0, x, 2*x, ... , k*x where k*x <= B, k*x-y, ... , an

   sum = (x+2*x+...+k*x) + floor((n-k)/2)*(k*x-y+k*x) + (k*x-y if n-k is not even)

   n=5, B=100, x=1, y=30

    0, 1, 2, 3, 4, 5 = 5/2(5+1) =15

   n=7, B=1e8, x=1e8, y=1e8

   0, 1e8, 0, 1e8, 0, 1e8, 0 = 7

   k = 1e8 / 1e8 = 1 < n
   s1 = (1e8*1*2/2)=1e8
   s2 = 6*(1e8)/2 = 3*1e8
   n-k is even so 4*1e8

   n=4, B=1, x=7, y=3

   0, -3, 4 -> PROBLEM!

   k = 1 / 7 = 0
   s1 = 0
   s2 = (4)*(2*0-3)/2=-6

   add +x until no longer possible
   need to -y until we can +x
   add +x
   need to -y until we can +x
   repeat O(n) complexity

   add +x k1 times
   subtract y k2 times
   add x
   subtract y k2 times
   O(1) complexity if we calculate k1 and k2

   k1*x-k2*y + x<=B
   (k1+1)*x-B<=k2*y
   ((k1+1)*x-B)/y <= k2, since k2 isn't necessarily constant we have to do O(n)

   e.g 0, -3, -6, 1, -2, -5, -8, 0 (goes from 2 to 3)

   n=5, B=100, x=1, y=30

   0 + alternate_sum 100 1 30 1 4
   0 + 1 + alternate_sum ... 2 3
   0 + 1 + 2 + ... 3 2
   0 + 1 + 2 + 3 + ... 4 1
   0 + 1 + 2 + 3 + 4 + ... 5 0
   0 + 1 + 2 + 3 + 4 + 5

    *)

let read_int () = Scanf.scanf " %d" (fun x -> x);;
let read_float () = Scanf.scanf " %f" (fun x -> x);;

let rec alternate_sum _B x y current = function
  | 0 -> current
  | n ->
    if (current +. x<=_B) then current +. alternate_sum _B x y (current +. x) (n-1)
    else current +. alternate_sum _B x y (current -. y) (n-1)
;;

let run_case = function
  | () ->
    let n = read_int () in
    let _B = read_float () in
    let x = read_float () in
    let y = read_float () in
    alternate_sum _B x y 0.0 n
;;

let print_big_float_as_int = fun x ->
   if (x!='.') then print_char (x)
;;

let rec iter_cases = function
  | 0 -> 0
  | n ->
    let sum = run_case () in
    let _ = String.iter print_big_float_as_int (string_of_float sum) in
    print_newline ();
    iter_cases (n-1)
;;

let t = read_int () in
iter_cases t ;;

(*
   build sequence a0, a1, ... , an
   a0 = 0,
   1)ai = a(i-1)+x or 2)ai = a(i-1)-y
   ai <= B

   maximise sum

   want all ai to be as close to B as possible
   - do 1) up to B then (2) and (1) alternating

   a0, a1, a2, ... , ak, a(k+1), ... , an

   0, x, 2*x, ... , k*x where k*x <= B, k*x-y, ... , an

   sum = (x+2*x+...+k*x) + floor((n-k)/2)*(k*x-y+k*x) + (k*x-y if n-k is not even)

   n=5, B=100, x=1, y=30

    0, 1, 2, 3, 4, 5 = 5/2(5+1) =15

   n=7, B=1e8, x=1e8, y=1e8

   0, 1e8, 0, 1e8, 0, 1e8, 0 = 7

   k = 1e8 / 1e8 = 1 < n
   s1 = (1e8*1*2/2)=1e8
   s2 = 6*(1e8)/2 = 3*1e8
   n-k is even so 4*1e8

   n=4, B=1, x=7, y=3

   0, -3, 4 -> PROBLEM!

   k = 1 / 7 = 0
   s1 = 0
   s2 = (4)*(2*0-3)/2=-6

   add +x until no longer possible
   need to -y until we can +x
   add +x
   need to -y until we can +x
   repeat O(n) complexity

   add +x k1 times
   subtract y k2 times
   add x
   subtract y k2 times
   O(1) complexity if we calculate k1 and k2

   k1*x-k2*y + x<=B
   (k1+1)*x-B<=k2*y
   ((k1+1)*x-B)/y <= k2, since k2 isn't necessarily constant we have to do O(n)

   e.g 0, -3, -6, 1, -2, -5, -8, 0 (goes from 2 to 3)

   n=5, B=100, x=1, y=30

   0 + alternate_sum 100 1 30 1 4
   0 + 1 + alternate_sum ... 2 3
   0 + 1 + 2 + ... 3 2
   0 + 1 + 2 + 3 + ... 4 1
   0 + 1 + 2 + 3 + 4 + ... 5 0
   0 + 1 + 2 + 3 + 4 + 5

    *)

let read_int () = Scanf.scanf " %d" (fun x -> x);;
let read_float () = Scanf.scanf " %f" (fun x -> x);;

let rec alternate_sum _B x y current = function
  | 0 -> current
  | n ->
    if (current +. x<=_B) then current +. alternate_sum _B x y (current +. x) (n-1)
    else current +. alternate_sum _B x y (current -. y) (n-1)
;;

let run_case = function
  | () ->
    let n = read_int () in
    let _B = read_float () in
    let x = read_float () in
    let y = read_float () in
    alternate_sum _B x y 0.0 n
;;

let rec iter_cases = function
  | 0 -> 0
  | n ->
    print_float (run_case ());
    print_newline ();
    iter_cases (n-1)
;;

let t = read_int () in
iter_cases t ;;


Scanf.(Array.(scanf " %d %d" @@ fun x y ->
  let map = make_matrix 1000 1000 false in
  let rooks = make 666 (0,0) in
  init 666 (fun i ->
    scanf " %d %d" @@ fun x y ->
      (map.(x).(y) <- true; rooks.(i) <- x,y)
  ) |> ignore;
  let x,y = ref x,ref y in
  let rec move p q =
    if map.(!x+p).(!y+q) then (
      move p 0
    ) else (
      x := !x + p;
      y := !y + q;
      Printf.printf "%d %d\n%!" !x !y;
      let i,u,v = scanf " %d %d %d" @@ fun p q r -> p,q,r in
      if i=0 && u=0 && v=0 then exit 0 (* WA *)
      else if i= -1 && u= -1 && v= -1 then exit 0 (* AC *)
      else (
        let pu,pv = rooks.(i-1) in
        map.(pu).(pv) <- false;
        map.( u).( v) <- true;
        rooks.(i-1) <- u,v;
        Printf.eprintf "%d: %d,%d --> %d %d\n%!" i pu pv u v;
      )
    )
  in
  while !x <> 500 || !y <> 500 do
    if !x < 500 then move 1 0
    else if !x > 500 then move (-1) 0
    else if !y < 500 then move 0 1
    else if !y > 500 then move 0 (-1)
  done;
  let tr,tl,bl,br = fold_left (fun (p,q,r,s) (x,y) ->
    match x>500, y>500 with
    (* t/f: right/left, top/bottom *)
    |  true, true -> (p+1,q,r,s)
    | false, true -> (p,q+1,r,s)
    | false,false -> (p,q,r+1,s)
    |  true,false -> (p,q,r,s+1)
  ) (0,0,0,0) rooks
  in
  Printf.eprintf "%d; %d; %d; %d\n%!" tr tl bl br;
  let mn = fold_left min br [|tr;tl;bl|] in
  ignore @@ init 499 @@ fun _ ->
    if mn=tr then move (-1) (-1)
    else if mn=tl then move 1 (-1)
    else if mn=bl then move 1 1
    else move (-1) 1
))

open Printf
open String

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let rec exists i j f = (i<=j) && ((f i) || exists (i+1) j f)
let minf i j f = fold (i+1) j (fun k a -> min (f k) a) (f i)

let nrooks = (* 6 *) 666
let (maxx,maxy) = (* (9,9) *) (999,999)
let (cx,cy) = ((maxx+1)/2, (maxy+1)/2)

let split_string s =
  let s = trim s in

  let n = String.length s in
  let li = fold 0 (n-1)
    (fun i ac -> if s.[i] = ' ' then i::ac else ac) []
  in

  let rec chop prev li ac =
    match li with [] -> List.rev ac
      | i::tail ->
	chop i tail ((sub s (prev+1) (i-prev-1))::ac)
  in

  chop (-1) (List.rev (n::li)) []

let read_ints () =
  let c = split_string (read_line()) in
  let c = List.map (fun s -> int_of_string s) c in
  Array.of_list c

let () =
  let rook = Array.make (nrooks+1) (0,0) in

  let line1 = read_ints() in
  let (kx, ky) = (line1.(0), line1.(1)) in

  for i=1 to nrooks do
    let linei = read_ints() in
    rook.(i) <- (linei.(0), linei.(1));
  done;

  let in_check (kx,ky) =
    (* return true if this king is in check *)
    exists 1 nrooks (fun i ->
      let (x,y) = rook.(i) in
      x = kx || y = ky
    )
  in

  let collide (kx,ky) =
  (* return true if the king is on top of a rook *)
    exists 1 nrooks (fun i -> rook.(i) = (kx,ky))
  in

  let dirs = [(-1,-1);(-1,0);(-1,1);(0,-1);(0,1);(1,-1);(1,0);(1,1)] in

  let rec move_to_goal (kx,ky) (goalx,goaly) = if kx = goalx && ky = goaly then true else (
    (* assume (kx,ky) has already been checked for being in check, or in a collision *)
(*    printf "move_to_goal (%d,%d) (%d,%d)\n" kx ky goalx goaly; *)
    let check_dirs = List.filter (fun (dx,dy) ->
      let (nx,ny) = (kx+dx, ky+dy) in
      nx >= 1 && ny >= 1 && nx<=maxx && ny <= maxy && in_check (nx,ny) && (not (collide (nx,ny)))
    ) dirs in

    let (terminal,kx,ky) =
      if check_dirs <> [] then (
	let (dx,dy) = List.hd check_dirs in
	let (kx,ky) = (kx+dx, ky+dy) in
	printf "%d %d\n" kx ky;
	(true,kx,ky) (* found a winning move *)
      ) else (
	let dx = Pervasives.compare goalx kx in
	let dy = Pervasives.compare goaly ky in
	let (kx,ky) = (kx+dx, ky+dy) in
	printf "%d %d\n" kx ky;
	(in_check (kx,ky), kx, ky)
      )
    in

    if terminal then false else (
      (* wait for a move from the other guy *)
      let line = read_ints() in
      let (k,x,y) = (line.(0), line.(1), line.(2)) in
      rook.(k) <- (x,y);
      move_to_goal (kx,ky) (goalx,goaly)
    )
  ) in

  if move_to_goal (kx,ky) (cx,cy) then (
    let count = Array.make_matrix 2 2 0 in
    for i=1 to nrooks do
      let wx = if fst rook.(i) < cx then 0 else 1 in
      let wy = if snd rook.(i) < cy then 0 else 1 in
      count.(wx).(wy) <- count.(wx).(wy) + 1
    done;
    let (_, i,j) =
      minf 0 1 (fun i ->
	minf 0 1 (fun j ->
	  (count.(i).(j), i, j)
	)
      )
    in
    let goalx = [|0;maxx|].(1-i) in
    let goaly = [|0;maxy|].(1-j) in

    if move_to_goal (cx,cy) (goalx, goaly) then failwith "bad" else ()
  ) else (
    (* the game is already over *)
  )

Scanf.(Array.(scanf " %d %d" @@ fun x y ->
  let map = make_matrix 1000 1000 false in
  let rooks = make 666 (0,0) in
  init 666 (fun i ->
    scanf " %d %d" @@
      fun x y -> map.(x).(y) <- true;
      rooks.(i) <- x,y) |> ignore;
  let x,y = ref x,ref y in
  let rec move p q =
    if map.(!x+p).(!y+q) then (
      move p 0
    ) else (
      x := !x + p;
      y := !y + q;
      Printf.printf "%d %d\n" !x !y; flush stdout;
      let i,u,v = scanf " %d %d %d" @@ fun p q r -> p,q,r in
      if i=0 && u=0 && v=0 then exit 0 (* WA *)
      else if i= -1 && u= -1 && v= -1 then exit 0 (* AC *)
      else (
        let pu,pv = rooks.(i-1) in
        map.(pu).(pv) <- false;
        map.( u).( v) <- true;
        rooks.(i-1) <- u,v;
        Printf.eprintf "%d: %d,%d --> %d %d\n" i pu pv u v; flush stderr
      )
    )
  in
  while !x <> 499 || !y <> 499 do
    if !x < 499 then move 1 0
    else if !x > 499 then move (-1) 0
    else if !y < 499 then move 0 1
    else if !y > 499 then move 0 (-1)
  done;
  let tr,tl,bl,br = fold_left (fun (p,q,r,s) (x,y) ->
    match x>499, y>499 with
    (* t/f: right/left, top/bottom *)
    |  true, true -> (p+1,q,r,s)
    | false, true -> (p,q+1,r,s)
    | false,false -> (p,q,r+1,s)
    |  true,false -> (p,q,r,s+1)
  ) (0,0,0,0) rooks
  in
  let mn = fold_left min br [|tr;tl;bl|] in
  ignore @@ init 499 @@ fun _ ->
    if mn=tr then move (-1) (-1)
    else if mn=tl then move 1 (-1)
    else if mn=bl then move 1 1
    else move (-1) 1
))

(* 11:35 *)
open Printf
open String

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let rec exists i j f = (i<=j) && ((f i) || exists (i+1) j f)
let minf i j f = fold (i+1) j (fun k a -> min (f k) a) (f i)

let split_string s =
  let s = trim s in

  let n = String.length s in
  let li = fold 0 (n-1)
    (fun i ac -> if s.[i] = ' ' then i::ac else ac) []
  in

  let rec chop prev li ac =
    match li with [] -> List.rev ac
      | i::tail ->
	chop i tail ((sub s (prev+1) (i-prev-1))::ac)
  in

  chop (-1) (List.rev (n::li)) []

let read_ints () =
  let c = split_string (read_line()) in
  let c = List.map (fun s -> int_of_string s) c in
  Array.of_list c

let () =
  let nrooks = 666 in
  let rook = Array.make (nrooks+1) (0,0) in

  let line1 = read_ints() in
  let (kx, ky) = (line1.(0), line1.(1)) in

  for i=1 to nrooks do
    let linei = read_ints() in
    rook.(i) <- (linei.(0), linei.(1));
  done;

  let in_check (kx,ky) =
    (* return true if this king is in check *)
    exists 1 nrooks (fun i ->
      let (x,y) = rook.(i) in
      x = kx || y = ky
    )
  in

  let collide (kx,ky) =
  (* return true if the king is on top of a rook *)
    exists 1 nrooks (fun i -> rook.(i) = (kx,ky))
  in

  let dirs = [(-1,-1);(-1,0);(-1,1);(0,-1);(0,1);(1,-1);(1,0);(1,1)] in

  let rec move_to_goal (kx,ky) (goalx,goaly) = if kx = goalx && ky = goaly then true else (
  (* assume (kx,ky) has already been checked for being in check, or in a collision *)
    let check_dirs = List.filter (fun (dx,dy) ->
      let (nx,ny) = (kx+dx, ky+dy) in
      nx >= 1 && ny >= 1 && nx<=999 && ny <= 999 && in_check (nx,ny) && (not (collide (nx,ny)))
    ) dirs in

    let terminal =
      if check_dirs <> [] then (
	let (dx,dy) = List.hd check_dirs in
	printf "%d %d\n" (kx+dx) (ky+dy);
	true (* found a winning move *)
      ) else (
	let dx = Pervasives.compare goalx kx in
	let dy = Pervasives.compare goaly ky in
	let (kx,ky) = (kx+dx, ky+dy) in
	printf "%d %d\n" kx ky;
	in_check (kx,ky)
      )
    in

    if terminal then false else (
      (* wait for a move from the other guy *)
      let line = read_ints() in
      let (k,x,y) = (line.(0), line.(1), line.(2)) in
      rook.(k) <- (x,y);
      move_to_goal (kx,ky) (goalx,goaly)
    )
  ) in

  if move_to_goal (kx,ky) (500,500) then (
    let count = Array.make_matrix 2 2 0 in
    for i=1 to nrooks do
      let wx = if fst rook.(i) < 500 then 0 else 1 in
      let wy = if snd rook.(i) < 500 then 0 else 1 in
      count.(wx).(wy) <- count.(wx).(wy) + 1
    done;
    let (_, i,j) =
      minf 0 1 (fun i ->
	minf 0 1 (fun j ->
	  (count.(i).(j), i, j)
	)
      )
    in
    let goalx = [|0;999|].(1-i) in
    let goaly = [|0;999|].(1-j) in

    if move_to_goal (500,500) (goalx, goaly) then () else failwith "bad"
  ) else (
    (* the game is already over *)
  )

(* 11:35 *)
open Printf
open String

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let rec exists i j f = (i<=j) && ((f i) || exists (i+1) j f)
let minf i j f = fold (i+1) j (fun k a -> min (f k) a) (f i)

let split_string s =
  let s = trim s in

  let n = String.length s in
  let li = fold 0 (n-1)
    (fun i ac -> if s.[i] = ' ' then i::ac else ac) []
  in

  let rec chop prev li ac =
    match li with [] -> List.rev ac
      | i::tail ->
	chop i tail ((sub s (prev+1) (i-prev-1))::ac)
  in

  chop (-1) (List.rev (n::li)) []

let read_ints () =
  let c = split_string (read_line()) in
  let c = List.map (fun s -> int_of_string s) c in
  Array.of_list c

let () =
  let nrooks = 666 in
  let rook = Array.make (nrooks+1) (0,0) in

  let line1 = read_ints() in
  let (kx, ky) = (line1.(0), line1.(1)) in

  for i=1 to nrooks do
    let linei = read_ints() in
    rook.(i) <- (linei.(0), linei.(1));
  done;

  let in_check (kx,ky) =
    (* return true if this king is in check *)
    exists 1 nrooks (fun i ->
      let (x,y) = rook.(i) in
      x = kx || y = ky
    )
  in

  let collide (kx,ky) =
  (* return true if the king is on top of a rook *)
    exists 1 nrooks (fun i -> rook.(i) = (kx,ky))
  in

  let dirs = [(-1,-1);(-1,0);(-1,1);(0,-1);(0,1);(1,-1);(1,0);(1,1)] in

  let rec move_to_goal (kx,ky) (goalx,goaly) = if kx = goalx && ky = goaly then true else (
  (* assume (kx,ky) has already been checked for being in check, or in a collision *)
    let check_dirs = List.filter (fun (dx,dy) ->
      let (nx,ny) = (kx+dx, ky+dy) in
      nx >= 0 && ny >= 0 && nx<=999 && ny <= 999 && in_check (nx,ny) && (not (collide (nx,ny)))
    ) dirs in

    let terminal =
      if check_dirs <> [] then (
	let (dx,dy) = List.hd check_dirs in
	printf "%d %d\n" (kx+dx) (ky+dy);
	true (* found a winning move *)
      ) else (
	let dx = Pervasives.compare goalx kx in
	let dy = Pervasives.compare goaly ky in
	let (kx,ky) = (kx+dx, ky+dy) in
	printf "%d %d\n" kx ky;
	in_check (kx,ky)
      )
    in

    if terminal then false else (
      (* wait for a move from the other guy *)
      let line = read_ints() in
      let (k,x,y) = (line.(0), line.(1), line.(2)) in
      rook.(k) <- (x,y);
      move_to_goal (kx,ky) (goalx,goaly)
    )
  ) in

  if move_to_goal (kx,ky) (500,500) then (
    let count = Array.make_matrix 2 2 0 in
    for i=1 to nrooks do
      let wx = if fst rook.(i) < 500 then 0 else 1 in
      let wy = if snd rook.(i) < 500 then 0 else 1 in
      count.(wx).(wy) <- count.(wx).(wy) + 1
    done;
    let (_, i,j) =
      minf 0 1 (fun i ->
	minf 0 1 (fun j ->
	  (count.(i).(j), i, j)
	)
      )
    in
    let goalx = [|0;999|].(1-i) in
    let goaly = [|0;999|].(1-j) in

    if move_to_goal (500,500) (goalx, goaly) then () else failwith "bad"
  ) else (
    (* the game is already over *)
  )


open Stack
open Array

type node = {
  l : int;
  r : int;
  x : int;
  left : node;
  right : node
}

let rec dummy = { l = 0; r = -1; x = -1; left = dummy; right = dummy }

let build_min a =
  let next l a a' =
    let rec next i i' =
     if i > l - 1 then i'
     else if i = l - 1 then (a'.(i') <- a.(i); i' + 1)
     else
       let left = a.(i) and right = a.(i + 1) in
       a'.(i') <- { l = left.l; r = right.r; x = min left.x right.x; left; right };
       next (i + 2) (i' + 1)
    in
    next
  in
  let rec build l a =
    match l with
    | 0 -> dummy
    | 1 -> a.(0)
    | l ->
      let a' = make (l / 2 + 1) dummy in
      let l' = next l a a' 0 0 in
      build l' a'
  in
  let a = mapi (fun i x -> { l = i; r = i; x; left = dummy; right = dummy}) a in
  build (length a) a

let many = 10000000

let find_min root l r =
  let rec find root =
    if root.r < l || root.l > r then many
    else if l <= root.l && root.r <= r then root.x
    else min (find root.left) (find root.right)
  in
  find root

let search compare return arr =
  let rec search l r =
    if r = l + 1 then Some (match return with `Left -> l | `Right -> r)
    else
      let m = l + (r - l) / 2 in
      match compare arr.(m) with
      | `Eq -> Some m
      | `Lt -> search m r
      | `Gt -> search l m
  in
  let l = 0 and r = length arr - 1 in
  if r < l then None
  else
    let cl = compare arr.(l) and cr = compare arr.(r) in
    if cl = `Gt then (if return = `Right then Some l else None)
    else if cr = `Lt then (if return = `Left then Some r else None)
    else if cl = `Eq then Some l
    else if cr = `Eq then Some r
    else search l r

let cmp (a : int) b = if a < b then `Lt else if a > b then `Gt else `Eq

let () =
  let open Scanf in
  scanf "%d %d\n" @@ fun n m ->
  let ns = init n (fun i -> scanf "%d " @@ fun x -> x, i) in
  sort compare ns;
  let lrs = ref [] in
  iteri (fun i (x, j) -> if i < n - 1 && x = fst ns.(i + 1) then lrs := (j, snd ns.(i + 1)) :: !lrs) ns;
  let lrs = of_list !lrs in
  sort compare lrs;
  let s = Stack.create () in
  iter
    (fun (_, r as lr) ->
       while not (is_empty s) && snd (top s) > r do ignore (pop s) done;
       push lr s)
    lrs;
  let lrs = init (Stack.length s) (fun _ -> pop s) in
  (let l = length lrs in
   for i = 0 to l / 2 - 1 do let t = lrs.(l - i - 1) in lrs.(l - i - 1) <- lrs.(i); lrs.(i) <- t done);
  let search_left l' = search (fun (l, _) -> cmp l l') `Right lrs in
  let search_right r' = search (fun (_, r) -> cmp r r') `Left lrs in
  let root = build_min @@ map (fun (l, r) -> r - l) lrs in
  for _ = 1 to m do
    scanf "%d %d\n" @@ fun l r ->
    match search_left (l - 1), search_right (r - 1) with
    | Some l, Some r ->
      Printf.printf "%d\n" (let r = find_min root l r in if r = many then -1 else r)
    | _ -> Printf.printf "-1\n"
  done

(*[@@@warning "-30"]*)

type node = {
  l : int;
  r : int;
  x : int;
  left : node;
  right : node
}

let rec dummy = { l = 0; r = -1; x = -1; left = dummy; right = dummy }

let build_min a =
  let next l a a' =
    let rec next i i' =
     if i > l - 1 then i'
     else if i = l - 1 then begin a'.(i') <- a.(i); i' + 1 end
     else begin
       let left = a.(i) and right = a.(i + 1) in
       a'.(i') <- { l = left.l; r = right.r; x = min left.x right.x; left; right };
       next (i + 2) (i' + 1)
     end
    in
    next
  in
  let rec build l a =
    match l with
    | 0 -> dummy
    | 1 -> a.(0)
    | l ->
      let a' = Array.make (l / 2 + 1) dummy in
      let l' = next l a a' 0 0 in
      build l' a'
  in
  let a = Array.mapi (fun i x -> { l = i; r = i; x; left = dummy; right = dummy}) a in
  build (Array.length a) a

let many = 10000000

let find_min root l r =
  let rec find root =
    if root.r < l || root.l > r then many
    else if l <= root.l && root.r <= r then root.x
    else
      min (find root.left) (find root.right)
  in
  find root

type cmp = Lt | Gt | Eq

type return = Left | Right

let search compare return arr =
  let rec search l r =
    if r = l + 1 then Some (match return with Left -> l | Right -> r)
    else
      let m = l + (r - l) / 2 in
      match compare arr.(m) with
      | Eq -> Some m
      | Lt -> search m r
      | Gt -> search l m
  in
  let l = 0 and r = Array.length arr - 1 in
  if r < l then None
  else
    let cl = compare arr.(l) and cr = compare arr.(r) in
    if cl = Gt then (if return = Right then Some l else None)
    else if cr = Lt then (if return = Left then Some r else None)
    else if cl = Eq then Some l
    else if cr = Eq then Some r
    else search l r

let cmp (a : int) b = if a < b then Lt else if a > b then Gt else Eq

let () =
  let open Scanf in
  scanf "%d %d\n" @@ fun n m ->
  let ns = Array.(init n (fun i -> scanf "%d " @@ fun x -> x, i)) in
  Array.sort compare ns;
  let lrs = ref [] in
  Array.iteri (fun i (x, j) -> if i < n - 1 && x = fst ns.(i + 1) then lrs := (j, snd ns.(i + 1)) :: !lrs) ns;
  let lrs = Array.of_list !lrs in
  Array.sort compare lrs;
  let s = Stack.create () in
  Array.iter
    (fun (_, r as lr) ->
       while not (Stack.is_empty s) && snd (Stack.top s) > r do ignore (Stack.pop s) done;
       Stack.push lr s)
    lrs;
  let lrs = Array.init (Stack.length s) (fun _ -> Stack.pop s) in
  (let l = Array.length lrs in
   for i = 0 to l / 2 - 1 do let t = lrs.(l - i - 1) in lrs.(l - i - 1) <- lrs.(i); lrs.(i) <- t done);
  let search_left l' = search (fun (l, _) -> cmp l l') Right lrs in
  let search_right r' = search (fun (_, r) -> cmp r r') Left lrs in
  let root = build_min @@ Array.map (fun (l, r) -> r - l) lrs in
  for _ = 1 to m do
    scanf "%d %d\n" @@ fun l r ->
    match search_left (l - 1), search_right (r - 1) with
    | Some l, Some r ->
      Printf.printf "%d\n" (let r = find_min root l r in if r = many then -1 else r)
    | _ -> Printf.printf "-1\n"
  done

open Stack
open Array

type node = {
  l : int;
  r : int;
  x : int;
  left : node;
  right : node
}

let rec dummy = { l = 0; r = -1; x = -1; left = dummy; right = dummy }

let build_min a =
  let next l a a' =
    let rec next i i' =
     if i > l - 1 then i'
     else if i = l - 1 then (a'.(i') <- a.(i); i' + 1)
     else
       let left = a.(i) and right = a.(i + 1) in
       a'.(i') <- { l = left.l; r = right.r; x = min left.x right.x; left; right };
       next (i + 2) (i' + 1)
    in
    next
  in
  let rec build l a =
    match l with
    | 0 -> dummy
    | 1 -> a.(0)
    | l ->
      let a' = make (l / 2 + 1) dummy in
      let l' = next l a a' 0 0 in
      build l' a'
  in
  let a = mapi (fun i x -> { l = i; r = i; x; left = dummy; right = dummy}) a in
  build (length a) a

let many = 10000000

let find_min root l r =
  let rec find root =
    if root.r < l || root.l > r then many
    else if l <= root.l && root.r <= r then root.x
    else min (find root.left) (find root.right)
  in
  find root

let () =
  let open Scanf in
  scanf "%d %d\n" @@ fun n m ->
  let ns = init n (fun i -> scanf "%d " @@ fun x -> x, i) in
  sort compare ns;
  let lrs = ref [] in
  iteri (fun i (x, j) -> if i < n - 1 && x = fst ns.(i + 1) then lrs := (j, snd ns.(i + 1)) :: !lrs) ns;
  let lrs = of_list !lrs in
  sort compare lrs;
  let s = Stack.create () in
  iter
    (fun (_, r as lr) ->
       while not (is_empty s) && snd (top s) > r do ignore (pop s) done;
       push lr s)
    lrs;
  let lrs = init (Stack.length s) (fun _ -> pop s) in
  let l = length lrs in
  for i = 0 to l / 2 - 1 do let t = lrs.(l - i - 1) in lrs.(l - i - 1) <- lrs.(i); lrs.(i) <- t done;
  let ls, rs = make n ~-1, make n ~-1 in
  ignore @@ fold_left (fun (p, i) (l, _) -> for j = p + 1 to l do ls.(j) <- i done; l, i + 1) (~-1, 0) lrs;
  ignore @@ fold_right (fun (_, r) (p, i) -> for j = r to p - 1 do rs.(j) <- i done; r, i - 1) lrs (n, l - 1);
  let root = build_min @@ map (fun (l, r) -> r - l) lrs in
  for _ = 1 to m do
    scanf "%d %d\n" @@ fun l r ->
    let l = ls.(l - 1) and r = rs.(r - 1) in
    if l >= 0 && r >= 0 then Printf.printf "%d\n" (let r = find_min root l r in if r = many then -1 else r)
    else Printf.printf "-1\n"
  done

open Stack
open Array

let upd_min a n i (v : int) =
  let rec upd p l r =
    if l <= i && i <= r then begin
      if v < a.(p) then a.(p) <- v;
      if r - l >= 1 then
        let m = l + (r - l) / 2 in
        upd (p lsl 1 + 1) l m;
        upd ((p + 1) lsl 1) (m + 1) r
    end
  in
  upd 0 0 (n - 1)

let big_n = 10000000

let find_min a n l r =
  let rec find p l' r' =
    if r' < l || l' > r then big_n
    else if l <= l' && r' <= r then a.(p)
    else if r' - l' >= 1 then
      let m = l' + (r' - l') / 2 in
      min (find (p lsl 1 + 1) l' m) (find ((p + 1) lsl 1) (m + 1) r')
    else big_n
  in
  find 0 0 (n - 1)

let () =
  let open Scanf in
  scanf "%d %d\n" @@ fun n m ->
  let ns = init n (fun i -> scanf "%d " @@ fun x -> x, i) in
  sort compare ns;
  let lrs = ref [] in
  iteri (fun i (x, j) -> if i < n - 1 && x = fst ns.(i + 1) then lrs := (j, snd ns.(i + 1)) :: !lrs) ns;
  let lrs = of_list !lrs in
  sort compare lrs;
  let s = Stack.create () in
  iter
    (fun (_, r as lr) ->
       while not (is_empty s) && snd (top s) > r do ignore (pop s) done;
       push lr s)
    lrs;
  let lrs = init (Stack.length s) (fun _ -> pop s) in
  let len = length lrs in
  for i = 0 to len / 2 - 1 do let t = lrs.(len - i - 1) in lrs.(len - i - 1) <- lrs.(i); lrs.(i) <- t done;
  let ls, rs = make n ~-1, make n ~-1 in
  ignore @@ fold_left (fun (p, i) (l, _) -> for j = p + 1 to l do ls.(j) <- i done; l, i + 1) (~-1, 0) lrs;
  ignore @@ fold_right (fun (_, r) (p, i) -> for j = r to p - 1 do rs.(j) <- i done; r, i - 1) lrs (n, len - 1);
  let a = make (n * 3 + 1) big_n in
  iteri (fun i (l, r) -> upd_min a len i (r - l)) lrs;
  for _ = 1 to m do
    scanf "%d %d\n" @@ fun l r ->
    let l = ls.(l - 1) and r = rs.(r - 1) in
    if l >= 0 && r >= 0 then Printf.printf "%d\n" (let r = find_min a len l r in if r = big_n then -1 else r)
    else Printf.printf "-1\n"
  done

type sub = {
  mutable bl : node;
  mutable br : node;
  mutable tr : node
}
and node =
  | Empty
  | Node of int ref * sub

type tree = { mutable root : node; size : int }

let empty = 0, -1, 0, -1

let split (lmin, lmax, rmin, rmax) =
  let lmid = lmin  + (lmax - lmin) / 2 in
  let rmid = rmin + (rmax - rmin) / 2 in
  if lmin < lmax && rmin < rmax then
    (lmin, lmid, rmin, rmid),
    (lmin, lmid, rmid + 1, rmax),
    (lmid + 1, lmax, rmid + 1, rmax)
  else if lmin < lmax then
    (lmin, lmid, rmin, rmax),
    empty,
    (lmid + 1, lmax, rmin, rmax)
  else if rmin < rmax then
    (lmin, lmax, rmin, rmid),
    (lmin, lmax, rmid + 1, rmax),
    empty
  else
    empty, empty, empty

let add ({ root; size } as t) l r =
  let dist = r - l in
  let rec add (lmin, lmax, rmin, rmax as rect) t =
    if lmin <= l && l <= lmax && rmin <= r && r <= rmax then
      match t with
      | Empty ->
        let r = { bl = Empty; br = Empty; tr = Empty } in
        add' rect r;
        Node (ref dist, r)
      | Node (min, r) as node ->
        add' rect r;
        if dist < !min then min := dist;
        node
    else t
  and add' rect r =
    let bl, br, tr = split rect in
    r.bl <- add bl r.bl;
    r.br <- add br r.br;
    r.tr <- add tr r.tr
  in
  t.root <- add (0, size - 1, 0, size - 1) root

let none = max_int

let min { root; size } l r =
  let rec min (lmin, lmax, rmin, rmax as rect) =
    function
    | Empty -> none
    | _ when lmin > lmax || rmin > rmax -> none
    | Node (m, _)
      when l <= lmin && lmax <= r && l <= rmin && rmax <= r -> !m
    | Node _
      when r < lmin || l > lmax || r < rmin || l > rmax -> none
    | Node (_, rct) ->
      let bl, br, tr = split rect in
      let bl = min bl rct.bl and br = min br rct.br and tr = min tr rct.tr in
      let br = if bl < br then bl else br in
      if br < tr then br else tr
  in
  min (0, size - 1, 0, size - 1) root

type element = { v : int64; i : int }

let () =
  let open Scanf in
  scanf "%d %d\n" @@ fun n m ->
  let seq = Array.init n (fun i -> scanf "%Ld " @@ fun v -> { v; i }) in
  Array.sort
    (fun e1 e2 ->
       let r = Int64.compare e1.v e2.v in
       if r <> 0 then r else compare e1.i e2.i)
    seq;
  let t = { root = Empty; size = n } in
  Array.iteri (fun i v -> if i < n - 1 && v.v = seq.(i + 1).v then add t v.i seq.(i + 1).i) seq;
  for _ = 1 to m do
    scanf "%d %d\n" @@ fun l r -> Printf.printf "%d\n" @@ let m = min t (l - 1) (r - 1) in if m = none then -1 else m
  done

type sub = {
  mutable bl : node;
  mutable br : node;
  mutable tr : node
}
and node =
  | Empty
  | Node of int ref * sub

type tree = { mutable root : node; size : int }

let empty = 0, -1, 0, -1

let split (lmin, lmax, rmin, rmax) =
  let lmid = lmin  + (lmax - lmin) / 2 in
  let rmid = rmin + (rmax - rmin) / 2 in
  if lmin < lmax && rmin < rmax then
    (lmin, lmid, rmin, rmid),
    (lmin, lmid, rmid + 1, rmax),
    (lmid + 1, lmax, rmid + 1, rmax)
  else if lmin < lmax then
    (lmin, lmid, rmin, rmax),
    (lmid + 1, lmax, rmin, rmax),
    empty
  else if rmin < rmax then
    (lmin, lmax, rmin, rmid),
    (lmin, lmax, rmid + 1, rmax),
    empty
  else
    empty, empty, empty

let add ({ root; size } as t) l r =
  let dist = r - l in
  let rec add (lmin, lmax, rmin, rmax as rect) t =
    if lmin <= l && l <= lmax && rmin <= r && r <= rmax then
      match t with
      | Empty ->
        let r = { bl = Empty; br = Empty; tr = Empty } in
        add' rect r;
        Node (ref dist, r)
      | Node (min, r) as node ->
        add' rect r;
        if dist < !min then min := dist;
        node
    else t
  and add' rect r =
    let bl, br, tr = split rect in
    r.bl <- add bl r.bl;
    r.br <- add br r.br;
    r.tr <- add tr r.tr
  in
  t.root <- add (0, size - 1, 0, size - 1) root

let none = max_int

let min { root; size } l r =
  let rec min (lmin, lmax, rmin, rmax as rect) =
    function
    | Empty -> none
    | Node (m, _)
      when l <= lmin && lmax <= r && l <= rmin && rmax <= r -> !m
    | Node _
      when r < lmin || l > lmax || r < rmin || l > rmax -> none
    | Node (_, rct) ->
      let bl, br, tr = split rect in
      let bl = min bl rct.bl and br = min br rct.br and tr = min tr rct.tr in
      let br = if bl < br then bl else br in
      if br < tr then br else tr
  in
  min (0, size - 1, 0, size - 1) root

type element = { v : int; i : int }

let () =
  let open Scanf in
  scanf "%d %d\n" @@ fun n m ->
  let seq = Array.init n (fun i -> scanf "%d " @@ fun v -> { v; i }) in
  Array.sort compare seq;
  let t = { root = Empty; size = n } in
  Array.iteri (fun i v -> if i < n - 1 && v.v = seq.(i + 1).v then add t v.i seq.(i + 1).i) seq;
  for _ = 1 to m do
    scanf "%d %d\n" @@ fun l r -> Printf.printf "%d\n" @@ let m = min t (l - 1) (r - 1) in if m = none then -1 else m
  done

type sub = {
  mutable bl : node;
  mutable br : node;
  mutable tl : node;
  mutable tr : node
}
and node =
  | Empty
  | Node of int * sub

type tree = { mutable root : node; size : int }

let split (lmin, lmax, rmin, rmax) =
  let lmid = lmin  + (lmax - lmin) / 2 in
  let rmid = rmin + (rmax - rmin) / 2 in
  let empty = 0, -1, 0, -1 in
  if lmin < lmax && rmin < rmax then
    (lmin, lmid, rmin, rmid),
    (lmin, lmid, rmid + 1, rmax),
    (lmid + 1, lmax, rmin, rmid),
    (lmid + 1, lmax, rmid + 1, rmax)
  else if lmin < lmax then
    (lmin, lmid, rmin, rmax),
    empty,
    (lmid + 1, lmax, rmin, rmax),
    empty
  else if rmin < rmax then
    (lmin, lmax, rmin, rmid),
    (lmin, lmax, rmid + 1, rmax),
    empty,
    empty
  else
    empty, empty, empty, empty

let inside l r (lmin, lmax, rmin, rmax) =
  lmin <= l && l <= lmax && rmin <= r && r <= rmax

let add ({ root; size } as t) l r =
  let dist = r - l in
  let rec add rect =
    if inside l r rect then
      let add r =
        let bl, br, tl, tr = split rect in
        r.bl <- add bl r.bl;
        r.br <- add br r.br;
        r.tl <- add tl r.tl;
        r.tr <- add tr r.tr
      in
      function
      | Empty ->
        let r = { bl = Empty; br = Empty; tl = Empty; tr = Empty } in
        add r;
        Node (dist, r)
      | Node (min, r) as node ->
        add r;
        if dist < min then Node (dist, r) else node
    else fun x -> x
  in
  t.root <- add (0, size - 1, 0, size - 1) root

let none = -1

let min4 x y z k =
  let min x y = if x = none then y else if y = none then x else if x < y then x else y in
  List.fold_left min none [x; y; z; k]

let min { root; size } l r =
  let rec min (lmin, lmax, rmin, rmax as rect) =
    function
    | Empty -> none
    | Node _
      when not (inside l l rect || inside l r rect || inside r l rect || inside r r rect) -> none
    | Node (m, rct) ->
      if l <= lmin && lmax <= r && l <= rmin && rmax <= r then m
      else
        let bl, br, tl, tr = split rect in
        min4 (min bl rct.bl) (min br rct.br) (min tl rct.tl) (min tr rct.tr)
  in
  min (0, size - 1, 0, size - 1) root

type element = { v : int; i : int }

let () =
  let open Scanf in
  scanf "%d %d\n" @@ fun n m ->
  let seq = Array.init n (fun i -> scanf "%d " @@ fun v -> { v; i }) in
  Array.sort compare seq;
  let t = { root = Empty; size = n } in
  Array.iteri (fun i v -> if i < n - 1 && v.v = seq.(i + 1).v then add t v.i seq.(i + 1).i) seq;
  for _ = 1 to m do
    scanf "%d %d\n" @@ fun l r -> Printf.printf "%d\n" @@ min t (l - 1) (r - 1)
  done

type cmp = Le | Gt

let cmp a b =
  if a <= b then Le
  else Gt

type node = {
  l : int;
  r : int;
  mutable ll : quadr;
  mutable lr : quadr;
  mutable rl : quadr;
  mutable rr : quadr;
}
and quadr = Some of node | None

let rec merge a b =
  match a, b with
  | None, None -> None
  | Some _, None -> a
  | None, Some _ -> b
  | Some p, Some c ->
    let lp, rp, lc, rc, p, c, r =
      match cmp (abs (p.r - p.l)) (abs (c.r - c.l)) with
      | Le -> p.l, p.l, c.l, c.l, a, b, p
      | Gt -> c.l, c.r, p.l, p.r, b, a, c
    in
    begin match cmp lp lc, cmp rp rc with
    | Le, Le -> r.ll <- merge r.ll c
    | Le, Gt -> r.lr <- merge r.lr c
    | Gt, Le -> r.rl <- merge r.rl c
    | Gt, Gt -> r.rr <- merge r.rr c
    end;
    p

let add (l, r) t = merge t (Some { l; r; ll = None; lr = None; rl = None; rr = None })

let rec find t ((l, r) as e) =
  match t with
  | None -> -1
  | Some { l = l'; r = r'; ll; lr; rl; rr } ->
    let try2 a b =
      let a, b = find a e, find b e in
      match cmp 0 a, cmp 0 b, cmp a b with
      | Le, Le, Le -> a
      | Le, Le, Gt -> b
      | Le, Gt, _ -> a
      | Gt, Le, _ -> b
      | Gt, Gt, _ -> -1
    in
    match cmp l l', cmp r' r, cmp l' r, cmp l r' with
    | Le, Le, _, _ -> abs (r' - l')
    | Le, Gt, Le, _ -> try2 lr rr
    | Le, Gt, Gt, _ -> find rr e
    | Gt, Le, _, Le -> try2 ll lr
    | Gt, Le, _, Gt -> find ll e
    | Gt, Gt, _, _ -> find lr e

let (@@) a b = a b

exception End of quadr

let () =
  Scanf.scanf "%d %d\n" @@ fun n m ->
  let a = Array.init n (fun i -> Scanf.scanf " %d" @@ fun x -> (x, i)) in
  Array.sort (fun (a, i) (a', i') -> let r = compare a a' in if r <> 0 then r else compare i i') a;
  let t = Array.make (n - 1) ((0, 0), n) in
  Array.iteri
    (fun i (c, ci) ->
       if i > 0 then
         let p, pi = a.(i - 1) in
         if p = c then
           t.(i - 1) <- (pi, ci), Random.int n)
    a;
  Array.sort (fun (_, k) (_, k') -> compare k k') t;
  let t = try Array.fold_left (fun t (e, k)-> if k < n then add e t else raise (End t)) None t with End t -> t in
  for i = 1 to m do
    Scanf.scanf " %d %d" @@ fun l r -> Printf.printf "%d\n" @@ find t (l - 1, r - 1)
  done

type sub = {
  mutable bl : node;
  mutable br : node;
  mutable tr : node
}
and node =
  | Empty
  | Node of int ref * sub

type tree = { mutable root : node; size : int }

let empty = 0, -1, 0, -1

let split (lmin, lmax, rmin, rmax) =
  let lmid = lmin  + (lmax - lmin) / 2 in
  let rmid = rmin + (rmax - rmin) / 2 in
  if lmin < lmax && rmin < rmax then
    (lmin, lmid, rmin, rmid),
    (lmin, lmid, rmid + 1, rmax),
    (lmid + 1, lmax, rmid + 1, rmax)
  else if lmin < lmax then
    (lmin, lmid, rmin, rmax),
    empty,
    (lmid + 1, lmax, rmin, rmax)
  else if rmin < rmax then
    (lmin, lmax, rmin, rmid),
    (lmin, lmax, rmid + 1, rmax),
    empty
  else
    empty, empty, empty

let add ({ root; size } as t) l r =
  let dist = r - l in
  let rec add (lmin, lmax, rmin, rmax as rect) t =
    if lmin <= l && l <= lmax && rmin <= r && r <= rmax then
      match t with
      | Empty ->
        let r = { bl = Empty; br = Empty; tr = Empty } in
        add' rect r;
        Node (ref dist, r)
      | Node (min, r) as node ->
        add' rect r;
        if dist < !min then min := dist;
        node
    else t
  and add' rect r =
    let bl, br, tr = split rect in
    r.bl <- add bl r.bl;
    r.br <- add br r.br;
    r.tr <- add tr r.tr
  in
  t.root <- add (0, size - 1, 0, size - 1) root

let none = max_int

let min { root; size } l r =
  let rec min (lmin, lmax, rmin, rmax as rect) =
    function
    | Empty -> none
    | Node (m, _)
      when l <= lmin && lmax <= r && l <= rmin && rmax <= r -> !m
    | Node _
      when r < lmin || l > lmax || r < rmin || l > rmax -> none
    | Node (_, rct) ->
      let bl, br, tr = split rect in
      let bl = min bl rct.bl and br = min br rct.br and tr = min tr rct.tr in
      let br = if bl < br then bl else br in
      if br < tr then br else tr
  in
  min (0, size - 1, 0, size - 1) root

type element = { v : int64; i : int }

let () =
  let open Scanf in
  scanf "%d %d\n" @@ fun n m ->
  let seq = Array.init n (fun i -> scanf "%Ld " @@ fun v -> { v; i }) in
  Array.sort compare seq;
  let t = { root = Empty; size = n } in
  Array.iteri (fun i v -> if i < n - 1 && v.v = seq.(i + 1).v then add t v.i seq.(i + 1).i) seq;
  for _ = 1 to m do
    scanf "%d %d\n" @@ fun l r -> Printf.printf "%d\n" @@ let m = min t (l - 1) (r - 1) in if m = none then -1 else m
  done

(*[@@@warning "-30"]*)

type node = {
          left   : node option;
  mutable right  : node option;
  mutable parent : node option;
          l      : int;
          r      : int;
          olca   : olca
}
and olca = {
          uf       : uf;
  mutable ancestor : node option;
  mutable colored  : bool
}
and uf = {
  mutable parent   : node option;
  mutable rank     : int
}

module H = Hashtbl.Make(struct type t = node let equal = (==) let hash n = n.l end)

let olca () = { uf = { parent = None; rank = 0 }; ancestor = None; colored = false }

let rec add curr l r =
  match curr with
  | Some curr when curr.r - curr.l < r - l ->
    let olca = olca () in
    begin match curr.right with
    | Some right as left ->
      let node = Some { left; right = None; parent = Some curr; l; r; olca } in
      right.parent <- node;
      curr.right <- node;
      node
    | None ->
      let node = Some { left = None; right = None; parent = Some curr; l; r; olca } in
      curr.right <- node;
      node
    end
  | Some curr ->
    begin match curr.parent with
    | Some _ as p -> add p l r
    | None ->
      let node = Some { left = Some curr; right = None; parent = None; l; r; olca = olca () } in
      curr.parent <- node;
      node
    end
  | None -> Some { left = None; right = None; parent = None; l; r; olca = olca () }

exception Empty_tree

let of_some = function Some x -> x | None -> raise Empty_tree

let tarjan_olca queries =
  let init n =
     n.olca.uf.parent <- Some n;
     n.olca.uf.rank   <- 0
  in
  let rec find n =
    match n.olca.uf.parent with
    | Some n' when n' == n -> n
    | Some n' ->
      let p = find n' in
      n.olca.uf.parent <- Some p;
      p
    | None -> assert false
  in
  let union u v =
    let u, v = find u, find v in
    if u.olca.uf.rank > v.olca.uf.rank then
      v.olca.uf.parent <- Some u
    else if u.olca.uf.rank < v.olca.uf.rank then
      u.olca.uf.parent <- Some v
    else if u != v then begin
      v.olca.uf.parent <- Some u;
      u.olca.uf.rank <- u.olca.uf.rank + 1
    end
  in
  let rec tarjan u =
     init u;
     u.olca.ancestor <- Some u;
     let recur v =
       tarjan v;
       union u v;
       (find u).olca.ancestor <- Some u
     in
     let handle =
       match u.parent with
       | None -> (function Some v -> recur v | None -> ())
       | Some v' -> function Some v -> if v != v' then recur v | None -> ()
     in
     handle u.left;
     handle u.right;
     u.olca.colored <- true;
     try
       H.iter
         (fun v a ->
            if v.olca.colored then
              let r = of_some (find v).olca.ancestor in
              a := r;
              H.find (H.find queries v) u := r)
         (H.find queries u)
     with
     | Not_found -> ()
  in
  tarjan

type cmp = Lt | Gt | Eq

let search compare return arr =
  let rec search l r =
    if r = l + 1 then Some (return (arr.(l), arr.(r)))
    else
      let m = l + (r - l) / 2 in
      match compare arr.(m) with
      | Eq -> Some arr.(m)
      | Lt -> search m r
      | Gt -> search l m
  in
  let l = 0 and r = Array.length arr - 1 in
  if compare arr.(l) = Gt || compare arr.(r) = Lt then None
  else if compare arr.(l) = Eq then Some arr.(l)
  else if compare arr.(r) = Eq then Some arr.(r)
  else search l r

let () =
  let open Scanf in
  scanf "%d %d\n" @@ fun n m ->
  let ns = Array.init n (fun i -> scanf "%d " @@ fun x -> x, i) in
  Array.sort compare ns;
  let lrs = ref [] in
  Array.iteri (fun i (x, j) -> if i < n - 1 && x = fst ns.(i + 1) then lrs := (j, snd ns.(i + 1)) :: !lrs) ns;
  let lrs = Array.of_list !lrs in
  Array.sort compare lrs;
  let lrs' = ref [] in
  try
    let root =
      let r =
        of_some @@ Array.fold_left (fun root (l, r) -> let r = add root l r in lrs' := of_some r :: !lrs'; r) None lrs
      in
      let rec root r = match r.parent with None -> r | Some p -> root p in
      root r
    in
    let lrs' = Array.of_list @@ List.rev !lrs' in
    let queries, queries_list = H.create 32, ref [] in
    let add_query u v =
      H.replace
        (try H.find queries u with Not_found -> let h = H.create 16 in H.replace queries u h; h)
        v
        (ref root)
    in
    let add_query l r u v = add_query u v; add_query v u; queries_list := Some (l, r, u, v) :: !queries_list in
    let cmp a b = if a < b then Lt else if b < a then Gt else Eq in
    let search_left l' = search (fun { l; _ } -> cmp l l') snd lrs' in
    let search_right r' = search (fun { r; _ } -> cmp r r') fst lrs' in
    for _ = 1 to m do
      scanf "%d %d\n" @@ fun l r ->
      match search_left (l - 1), search_right (r - 1) with
      | Some u, Some v -> add_query (l - 1) (r - 1) u v
      | _ -> queries_list := None :: !queries_list
    done;
    tarjan_olca queries root;
    List.iter Printf.(
        function
        | None -> printf "-1\n"
        | Some (l', r', u, v) ->
          let { l; r; _ } = !H.(find (find queries u) v) in
          if l' <= l && r <= r' then printf "%d\n" (r - l)
          else printf "-1\n")
      (List.rev !queries_list)
  with
  | Empty_tree ->  for _ = 1 to m do Printf.printf "-1\n" done

(*[@@@warning "-30"]*)

type node = {
  l : int;
  r : int;
  x : int;
  left : node option;
  right : node option
}

let build f l =
  let rec next acc =
    function
    | [] -> List.rev acc
    | [x] -> List.rev (x :: acc)
    | x :: y :: xs ->
      next ({ l = x.l; r = y.r; x = f x.x y.x; left = Some x; right = Some y } :: acc) xs
  in
  let next = next [] in
  let rec build =
    function
    | [] -> { l = 0; r = -1; x = -1; left = None; right = None }
    | [root] -> root
    | layer -> build (next layer)
  in
  build @@ List.mapi (fun i x -> { l = i; r = i; x; left = None; right = None}) l

let find f root l r =
  let rec find root =
    if l <= root.l && root.r <= r then [root.x]
    else if root.r < l || root.l > r then []
    else
      let map = function None -> [] | Some t -> find t in
      let l = map root.left @ map root.right in
      match l with
      | [] -> []
      | _ :: _ -> List.[fold_left f (hd l) (tl l)]
  in
  find root

let build_min = build min and find_min = find min

type cmp = Lt | Gt | Eq

let search compare return arr =
  let rec search l r =
    if r = l + 1 then Some (return (l, r))
    else
      let m = l + (r - l) / 2 in
      match compare arr.(m) with
      | Eq -> Some m
      | Lt -> search m r
      | Gt -> search l m
  in
  let l = 0 and r = Array.length arr - 1 in
  if r < l || compare arr.(l) = Gt || compare arr.(r) = Lt then None
  else if compare arr.(l) = Eq then Some l
  else if compare arr.(r) = Eq then Some r
  else search l r

let () =
  let open Scanf in
  scanf "%d %d\n" @@ fun n m ->
  let ns = Array.(init n (fun i -> scanf "%d " @@ fun x -> x, i)) in
  Array.sort compare ns;
  let lrs = ref [] in
  Array.iteri (fun i (x, j) -> if i < n - 1 && x = fst ns.(i + 1) then lrs := (j, snd ns.(i + 1)) :: !lrs) ns;
  let lrs = Array.of_list !lrs in
  Array.sort compare lrs;
  let s = Stack.create () in
  Array.iter
    (fun (_, r as lr) ->
       while not (Stack.is_empty s) && snd (Stack.top s) > r do ignore (Stack.pop s) done;
       Stack.push lr s)
    lrs;
  let lrs =
    Array.(init (Stack.length s) (fun _ -> Stack.pop s) |>
           to_list |>
           List.rev |>
           of_list)
  in
  let cmp a b = if a < b then Lt else if a > b then Gt else Eq in
  let search_left l' = search (fun (l, _) -> cmp l l') snd lrs in
  let search_right r' = search (fun (_, r) -> cmp r r') fst lrs in
  let root = build_min Array.(to_list @@ map (fun (l, r) -> r - l) lrs) in
  for _ = 1 to m do
    scanf "%d %d\n" @@ fun l r ->
    match search_left (l - 1), search_right (r - 1) with
    | Some l, Some r ->
      Printf.printf "%d\n" @@
      begin match find_min root l r with
      | [] -> -1
      | [x] -> x
      | l -> List.(fold_left min (hd l) (tl l))
      end
    | _ -> Printf.printf "-1\n"
  done


open Printf
open Char
open Scanf
module LL = Int64
let ( !! )  x  = LL.to_int x
let ( ++ ) x y = LL.add x y
let ( -- ) x y = LL.sub x y
let ( ** ) x y = LL.mul x y
let ( // ) x y = LL.div x y
let ( %% ) x y = LL.rem x y
let ( >> ) x y = (LL.compare x y) > 0
let ( << ) x y = (LL.compare x y) < 0
let ( == ) x y = (LL.compare x y) = 0
let rd_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let rd_ll _ = bscanf Scanning.stdin " %Ld " (fun x -> x)
let rd_arr n = Array.init n rd_ll
let rd_str _ = bscanf Scanning.stdin " %s " (fun x -> x)
let rd_mat n m = Array.init n rd_str

let diag i j n = (i - j + n) mod n
let c2i ch = int_of_char ch - int_of_char '0'
let solve n mat =
  let s = ref 0 in
  let cnt = Array.init n (fun _ -> 0) in
  for i=0 to n-1 do
    for j=0 to n-1 do
      let d = diag i j n in
      let c = cnt.(d) in
      let x = c2i mat.(i).[j] in
      cnt.(d) <- (c + x);
      s := !s + x;
    done;
  done;
  let best = (Array.fold_right max cnt 0) in
  let offD = !s - best in
    n - best + offD

let () =
  let t = rd_int () in
  for i=1 to t do
    let n = rd_int () in
    let mat = rd_mat n n in
    printf "%d\n" (solve n mat)
  done;

open Printf
open Char
open Scanf
module LL = Int64
let ( !! )  x  = LL.to_int x
let ( ++ ) x y = LL.add x y
let ( -- ) x y = LL.sub x y
let ( ** ) x y = LL.mul x y
let ( // ) x y = LL.div x y
let ( %% ) x y = LL.rem x y
let ( >> ) x y = (LL.compare x y) > 0
let ( << ) x y = (LL.compare x y) < 0
let ( == ) x y = (LL.compare x y) = 0
let rd_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let rd_ll _ = bscanf Scanning.stdin " %Ld " (fun x -> x)
let rd_arr n = Array.init n rd_ll
let rd_str _ = bscanf Scanning.stdin " %s " (fun x -> x)
let rd_mat n m = Array.init n rd_str

let diag i j n = (i - j + n) mod n
let c2i ch = int_of_char ch - int_of_char '0'
let solve n mat =
  let cnt = Array.init n (fun _ -> 0) in
  let rec loop ij sum =
    match ij with
    | (-1, _) -> sum
    | (i, j) ->
      let d = diag i j n in
      let c = cnt.(d) in
      let x = c2i mat.(i).[j] in
      let di = if j-1 < 0 then 1 else 0 in
      cnt.(d) <- (c + x);
      loop (i-di, (j-1+n) mod n) (sum + x)
  in
    let s = loop (n-1, n-1) 0 in
    let onD  = (Array.fold_right max cnt 0) in
    let offD = s - onD in
    n - onD + offD

let () =
  let t = rd_int () in
  for i=1 to t do
    let n = rd_int () in
    let mat = rd_mat n n in
    printf "%d\n" (solve n mat)
  done;


(* OCaml template *)
open Printf
open Scanf

(* Redefine operators for Int32 *)
(*
let ( + ) = Int32.add;;
let ( - ) = Int32.sub;;
let ( * ) = Int32.mul;;
let ( / ) = Int32.div;;
let ( % ) = Int32.rem;;
*)

(* Redefine operators for Int64 *)
let ( + ) = Int64.add;;
let ( - ) = Int64.sub;;
let ( * ) = Int64.mul;;
let ( / ) = Int64.div;;
let ( % ) = Int64.rem;;

(* Input *)
let read_int32 _ = bscanf Scanning.stdin "%ld " @@ fun x -> x;;
let read_int64 _ = bscanf Scanning.stdin "%Ld " @@ fun x -> x;;

(* Output *)
let print_int32 n = printf "%ld " n;;
let print_int32_endl n = printf "%ld\n" n;;
let print_int64 n = printf "%Ld " n;;
let print_int64_endl n = printf "%Ld\n" n;;

(* Utility functions *)
let comp f g x = f @@ g x;;
let rec fac n = if n = 0L then 1L else n * fac (n - 1L);;
let rec fib n = if n < 2L then n else fib (n - 2L) + fib (n - 1L);;
let sum = List.fold_left (+) 0L;;

(* Solve test case *)
let solve l r =
	if r < 2L * l then
		r - l
	else
		(r - 1L) / 2L;;

(* Main *)
let t = read_int ();;

for t' = 1 to t do
	let l = read_int64 () in
		let r = read_int64 () in
			print_int64_endl @@ solve l r
done

open Printf
open Scanf
let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let read_pair _ = bscanf Scanning.stdin " %d %d " (fun x y -> (x,y))

let () =
  let cases = read_int () in
  for case = 1 to cases do
    let (a, b) = read_pair () in
    let c = b/2+1 in
    let result =
    if c >= a then b mod c
    else b mod a
    in
      printf "%d\n" result
  done


let (+) = Int64.add
let ( * ) = Int64.mul
let ( - ) = Int64.sub
let two = Int64.one + Int64.one
let one = Int64.one
let zero = Int64.zero

let solve =
  let n = Scanf.scanf "%Ld\n" (fun n -> n) in
  let readPoints() = Scanf.scanf "%Ld %Ld\n" (fun x y -> (x,y)) in
  let rec readInput i (accX,accY) =
    match i with
      | i when i = n -> (List.rev accX,List.rev accY)
      | i -> let (x,y) = readPoints() in
          readInput (i+one) ((x::accX),(y::accY))
  in
  let rec sumpoints lst sum sqsum i acc =
    match lst with
      | [] -> acc
      | x::xs -> let newval = ((i-one)*(x*x)-two*x*sum+sqsum) in
          sumpoints xs (sum+x) (sqsum+(x*x)) (i+one) (acc+newval)
  in
  let xs,ys = readInput zero ([],[]) in
    Printf.printf "%Ld\n" ((sumpoints xs zero zero one zero)+(sumpoints ys zero zero one zero))
;;

let (+) = Int64.add
let ( * ) = Int64.mul
let ( - ) = Int64.sub
let two = Int64.one + Int64.one
let one = Int64.one
let zero = Int64.zero

let solve =
  let n = Scanf.scanf "%Ld\n" (fun n -> n) in
  let readPoints() = Scanf.scanf "%Ld %Ld\n" (fun x y -> (x,y)) in
  let rec readInput i (accX,accY) =
    match i with
      | i when i = n -> (List.rev accX,List.rev accY)
      | i -> let (x,y) = readPoints() in
          readInput (i+one) ((x::accX),(y::accY))
  in
  let rec sumpoints lst sum sqsum i acc =
    match lst with
      | [] -> acc
      | x::xs -> let newval = ((i-one)*(x*x)-(two*x*sum)+sqsum) in
          sumpoints xs (sum+x) (sqsum+(x*x)) (i+one) (acc+newval)
  in
  let xs,ys = readInput zero ([],[]) in
    Printf.printf "%Ld\n" ((sumpoints xs zero zero one zero)+(sumpoints ys zero zero one zero))
;;

let solve =
  let n = Scanf.scanf "%d\n" (fun n -> n) in
  let readPoints() = Scanf.scanf "%d %d\n" (fun x y -> (x,y)) in
  let rec readInput i (accX,accY) =
    match i with
      | i when i = n -> (List.rev accX,List.rev accY)
      | i -> let (x,y) = readPoints() in
          readInput (i+1) ((x::accX),(y::accY))
  in
  let rec sumpoints lst sum sqsum i acc =
    match lst with
      | [] -> acc
      | x::xs -> let newval = ((i-1)*(x*x)-2*x*sum+sqsum) in
          sumpoints xs (sum+x) (sqsum+(x*x)) (i+1) (acc+newval)
  in
  let xs,ys = readInput 0 ([],[]) in
    Printf.printf "%d\n" ((sumpoints xs 0 0 1 0)+(sumpoints ys 0 0 1 0))
;;

let solve =
    let n = Scanf.scanf "%d\n" (fun n -> n) in
    let readPoints() = Scanf.scanf "%d %d\n" (fun x y -> (x,y)) in
    let rec readInput i (accX,accY) =
        match i with
            | i when i = n -> (List.rev accX,List.rev accY)
            | i -> let (x,y) = readPoints() in
                    readInput (i+1) ((x::accX),(y::accY))
    in
    let rec sumpoints lst sum sqsum i acc =
    match lst with
        | [] -> acc
        | x::xs -> let newval = ((i-1)*(x*x)-2*x*sum+sqsum) in
                    sumpoints xs (sum+x) (sqsum+(x*x)) (i+1) (acc+newval)
    in
    let xs,ys = readInput 0 ([],[]) in
    let result = (sumpoints xs 0 0 1 0)+(sumpoints ys 0 0 1 0) in
      Printf.printf "%d\n" result
;;


let solve a b =
  let rec loop lst1 lst2 cnt =
    match lst1 with
      | [] -> if cnt <= 2 then true else false
      | h :: t ->
	if h = List.hd lst2 then loop t (List.tl lst2) cnt
	else loop t (List.tl lst2) (cnt+1)
  in
  if List.sort compare a = List.sort compare b && loop a b 0 then "YES"
  else "NO"

let _ =
  let a = (Scanf.scanf " %s" (Str.split (Str.regexp ""))) in
  let b = (Scanf.scanf " %s" (Str.split (Str.regexp ""))) in
  Printf.printf "%s\n" (solve a b)

let a = read_line () in
let b = read_line () in
let x, y = ref [], ref [] in
let na = String.length a in
let nb = String.length b in
for i = 0 to min na nb - 1 do
  if a.[i] <> b.[i] then begin
    x := a.[i] :: !x;
    y := b.[i] :: !y
  end
done;
let m = List.length !x in
let sort = List.sort (fun a b -> Char.code a - Char.code b) in
print_endline (if na = nb && m = 2 && sort !x = sort !y
  then "YES" else "NO")

open String;;
let diff s a b =
        let l = ref [] in
         for i = 0 to length a - 1 do
                if a.[i] <> b.[i] then
                        l := (i) :: (!l)
                done;
        !l;;

let swap a h k =
        let n = a.[h] in
        a.[h] <- a.[k];
        a.[k] <- n;;

let ch l a b= match l with
        [] -> false
        |h:: t -> match t with
                [] -> false
                |k :: [] -> swap a h k;a = b
                |t :: k -> false
;;
let main _ =
        let a  = Scanf.scanf "%s\n" (fun a -> a) in
        let b  = Scanf.scanf "%s\n" (fun a -> a) in
           if length a <> length b
           then print_endline "NO"
           else
                        let l = diff [] a b in
                                if (ch l a b)
                                then print_endline "YES"
                                else print_endline "NO"
;;
main ();;

let solve a b =
  let sorted_a = List.sort compare a in
  let sorted_b = List.sort compare b in
  if sorted_a = sorted_b then "YES" else "NO"

let _ =
  let a = (Scanf.scanf " %s" (Str.split (Str.regexp ""))) in
  let b = (Scanf.scanf " %s" (Str.split (Str.regexp ""))) in
  Printf.printf "%s\n" (solve a b)


let foldli f acc s =
  let n = String.length s in
  let rec go i acc =
    if i >= n then
      acc
    else
      go (i+1) (f i acc s.[i])
  in go 0 acc

let () =
  let a = read_line () in
  let opt,cnt,_,_ = foldli (fun i (opt,cnt,b,s) c ->
    if c = '(' then
      (opt,cnt,b,i::s)
    else if s = [] then
      (opt,cnt,i,[])
    else
      let s' = List.tl s in
      let opt' = i - (if s' = [] then b else List.hd s') in
      if opt' > opt then
        (opt',1,b,s')
      else if opt' = opt then
        (opt,cnt+1,b,s')
      else
        (opt,cnt,b,s')
  ) (0,1,-1,[]) a in
  Printf.printf "%d %d\n" opt cnt

(* Utils
   --------------------------------------------------------------------------- *)

let read_string () =
  Scanf.bscanf Scanf.Scanning.stdib " %s " (fun x -> x)

(* Solution
   --------------------------------------------------------------------------- *)

let solve s =
  let stk = Stack.create () in

  let b = ref (-1) in
  let max = ref 0 in
  let max_ct = ref 1 in

  for i = 0 to String.length s - 1 do
    try
      match s.[i] with
        | '(' -> Stack.push i stk
        | ')' ->
          let top = Stack.pop stk in
          let len = i - (if Stack.is_empty stk then !b else Stack.top stk) in
          match compare !max len with
            | 1 -> ()
            | 0 -> incr max_ct
            | _ -> max := len; max_ct := 1

    with Stack.Empty -> b := i
  done;

  (!max, !max_ct)

let _ =
  let (len, count) = solve (read_string ()) in
  Printf.printf "%d %d" len count

(* Utils
   --------------------------------------------------------------------------- *)

let read_string () =
  Scanf.bscanf Scanf.Scanning.stdib " %s " (fun x -> x)

let explode s =
  let rec exp i l =
    if i < 0 then l else exp (i - 1) (s.[i] :: l) in
  exp (String.length s - 1) []

(* Solution
   --------------------------------------------------------------------------- *)

let rec rev acc = function
  | '(' :: xs -> rev (')' :: acc) xs
  | ')' :: xs -> rev ('(' :: acc) xs
  | x   :: xs -> rev (x   :: acc) xs
  | [] -> acc

let merge (l1, t1) (l2, t2)  =
  match (compare l1 l2) with
    | 1 -> (l1, t1)
    | 0 when l1 = 0 -> (0, 1)
    | 0 -> (l1, t1 + t2)
    | _ -> (l2, t2)

let rec count b ct res = function
  | [] -> if b = 0 then merge res (ct, 1) else res
  | '(' :: xs ->
      count (b + 1) (ct + 1) res xs
  | _   :: xs ->
    if b > 0 then
      count (b - 1) (ct + 1) res xs
    else
      count 0 0 (merge res (ct, 1)) xs

let solve s =
  let s1 = ')' :: explode s in
  let s2 = rev [] s1 in
  merge (count 0 0 (0, 1) s1) (count 0 0 (0, 1) s2)

let _ =
  let (len, count) = solve (read_string ()) in
  Printf.printf "%d %d" len count

(* Utils
   --------------------------------------------------------------------------- *)

let read_string () =
  Scanf.bscanf Scanf.Scanning.stdib " %s " (fun x -> x)

let explode s =
  let rec exp i l =
    if i < 0 then l else exp (i - 1) (s.[i] :: l) in
  exp (String.length s - 1) []

(* Solution
   --------------------------------------------------------------------------- *)

let rec rev acc = function
  | '(' :: xs -> rev (')' :: acc) xs
  | ')' :: xs -> rev ('(' :: acc) xs
  | x   :: xs -> rev (x   :: acc) xs
  | [] -> acc

let merge (l1, t1) (l2, t2)  =
  match (compare l1 l2) with
    | 1 -> (l1, t1)
    | 0 when l1 = 0 -> (0, 1)
    | 0 -> (l1, t1 + t2)
    | _ -> (l2, t2)

let rec count b ct = function
  | [] -> if b = 0 then (ct, 1) else (0, 1)
  | '(' :: xs -> count (b + 1) (ct + 1) xs
  | _   :: xs ->
    if b > 0 then
      count (b - 1) (ct + 1) xs
    else
      merge (count 0 0 xs) (ct, 1)

let solve s =
  let s1 = ')' :: explode s in
  let s2 = rev [] s1 in
  merge (count 0 0 s1) (count 0 0 s2)

let _ =
  let (len, count) = solve (read_string ()) in
  Printf.printf "%d %d" len count

(* Utils
   --------------------------------------------------------------------------- *)

let read_string () =
  Scanf.bscanf Scanf.Scanning.stdib " %s " (fun x -> x)

(* Solution
   --------------------------------------------------------------------------- *)

let srev s1 =
  let s2 = String.copy s1 in
  let len = String.length s1 - 1 in
  for i = 0 to len do
    s2.[i] <- match s1.[len - i] with
      | '(' -> ')'
      | ')' -> '('
  done;
  s2

let merge (l1, t1) (l2, t2)  =
  match (compare l1 l2) with
    |  1 -> (l1, t1)
    |  0 when l1 = 0 -> (0, 1)
    |  0 -> (l1, t1 + t2)
    | -1 -> (l2, t2)

let count s =
  let rec iter b ct i =
    try
      match s.[i] with
        | '(' -> iter (b + 1) (ct + 1) (i + 1)
        | ')' ->
          if b > 0 then
            iter (b - 1) (ct + 1) (i + 1)
          else
            merge (iter 0 0 (i + 1)) (ct, 1)

    with Invalid_argument _ ->
      if b = 0 then (ct, 1) else (0, 1)

  in iter 0 0 0

let solve s =
  let s1 = ")" ^ s in
  let s2 = srev s1 in
  merge (count s1) (count s2)

let _ =
  let (len, count) = solve (read_string ()) in
  Printf.printf "%d %d" len count

(* Utils
   --------------------------------------------------------------------------- *)

let read_string () =
  Scanf.bscanf Scanf.Scanning.stdib " %s " (fun x -> x)

(* Solution
   --------------------------------------------------------------------------- *)

let solve s =
  let stk = Stack.create () in

  let b = ref 0 in
  let max = ref 0 in
  let max_ct = ref 1 in

  for i = 0 to String.length s - 1 do
    try
      match s.[i] with
        | '(' -> Stack.push i stk
        | ')' ->
          let top = Stack.pop stk in
          let len = if Stack.is_empty stk then i - !b else i - top in
          match compare !max len with
            | 1 -> ()
            | 0 -> incr max_ct
            | _ -> max := len; max_ct := 1

    with Stack.Empty -> b := i
  done;

  (!max, !max_ct)

let _ =
  let (len, count) = solve (read_string ()) in
  Printf.printf "%d %d" (len+1) count

(* Utils
   --------------------------------------------------------------------------- *)

let read_string () =
  Scanf.bscanf Scanf.Scanning.stdib " %s " (fun x -> x)

let explode s =
  let rec exp i l =
    if i < 0 then l else exp (i - 1) (s.[i] :: l) in
  exp (String.length s - 1) []

(* Solution
   --------------------------------------------------------------------------- *)

let rec rev acc = function
  | '(' :: xs -> rev (')' :: acc) xs
  | ')' :: xs -> rev ('(' :: acc) xs
  | [] -> acc

let merge (l1, t1) (l2, t2)  =
  match (compare l1 l2) with
    |  1 -> (l1, t1)
    |  0 when l1 = 0 -> (0, 1)
    |  0 -> (l1, t1 + t2)
    | -1 -> (l2, t2)

let rec count b ct = function
  | [] -> if b = 0 then (ct, 1) else (0, 1)
  | '(' :: xs -> count (b + 1) (ct + 1) xs
  | ')' :: xs ->
    if b > 0 then
      count (b - 1) (ct + 1) xs
    else
      merge (count 0 0 xs) (ct, 1)

let solve s =
  let s1 = ')' :: explode s in
  let s2 = rev [] s1 in
  merge (count 0 0 s1) (count 0 0 s2)

let _ =
  let (len, count) = solve (read_string ()) in
  Printf.printf "%d %d" len count

(* Utils
   --------------------------------------------------------------------------- *)

let read_string () =
  Scanf.bscanf Scanf.Scanning.stdib " %s " (fun x -> x)

let explode s =
  let rec exp i l =
    if i < 0 then l else exp (i - 1) (s.[i] :: l) in
  exp (String.length s - 1) []

(* Solution
   --------------------------------------------------------------------------- *)

let rec rev acc = function
  | '(' :: xs -> rev (')' :: acc) xs
  | ')' :: xs -> rev ('(' :: acc) xs
  | [] -> acc

let merge (l1, t1) (l2, t2)  =
  match (compare l1 l2) with
    |  1 -> (l1, t1)
    |  0 when l1 = 0 -> (0, 1)
    |  0 -> (l1, t1 + t2)
    | -1 -> (l2, t2)

let rec count b ct = function
  | [] -> if b = 0 then (ct, 1) else (0, 1)
  | '(' :: xs -> count (b + 1) (ct + 1) xs
  | ')' :: xs ->
    if b > 0 then
      count (b - 1) (ct + 1) xs
    else
      merge (count 0 0 xs) (ct, 1)

let solve s =
  let s1 = explode s in
  let s2 = rev [] s1 in
  merge (count 0 0 s1) (count 0 0 s2)

let _ =
  let (len, count) = solve (read_string ()) in
  Printf.printf "%d %d" len count

(* Utils
   --------------------------------------------------------------------------- *)

let read_string () =
  Scanf.bscanf Scanf.Scanning.stdib " %s " (fun x -> x)

(* Solution
   --------------------------------------------------------------------------- *)

let solve s =
  let stk = Stack.create () in

  let max = ref 0 in
  let max_ct = ref 1 in

  for i = 0 to String.length s - 1 do
    try
      match s.[i] with
        | '(' -> Stack.push i stk
        | ')' ->
          let len = i - Stack.pop stk + 1 in
          match compare !max len with
            | 1 -> ()
            | 0 -> incr max_ct
            | _ -> max := len; max_ct := 1

    with Stack.Empty -> ()
  done;

  (!max, !max_ct)

let _ =
  let (len, count) = solve (read_string ()) in
  Printf.printf "%d %d" len count


open Char;;
let n = read_int () and kol = ref 0 and answer = ref 0;;
let a = Array.create n "";;
let check c x =
begin
	kol := 0;
	for i = 0 to (String.length x) - 1 do
		if String.get x i = c then kol := !kol + 1
	done;
	!kol;
end;;
for i = 0 to n - 1 do let ss = read_line() in Array.set a i ss done;;
for i = 1 to 26 do
	for j = 1 to 26 do
		if i <> j then
		begin
			let ans = ref 0 in
			begin
				for k = 0 to n - 1 do
					if (check (chr (i + 96)) a.(k)) + (check (chr (j + 96)) a.(k)) = String.length a.(k) then ans := !ans + String.length a.(k);
				done;
				answer := max !answer !ans;
			end
		end
	done
done;;
print_int !answer;;

open Printf
open Scanf

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_string () = bscanf Scanning.stdib " %s " (fun x -> x)
let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let maxf i j f = fold (i+1) j (fun k a -> max (f k) a) (f i)
let rec forall i j f = (i>j) || ((f i) && forall (i+1) j f)
let n2l n = char_of_int ((int_of_char 'a') + n)
let rec sum i j f ac = if i>j then ac else sum (i+1) j f (ac + (f i))

let () =
  let n = read_int () in
  let word = Array.init n (fun _ -> read_string()) in

  let word_contains_only w c d =
    let n = String.length w in
    forall 0 (n-1) (fun i -> w.[i] = c || w.[i] = d)
  in

  let answer = maxf 0 25 (fun i ->
    maxf 0 25 (fun j ->
      let c = n2l i in
      let d = n2l j in
      sum 0 (n-1) (fun k ->
	if word_contains_only word.(k) c d then String.length word.(k) else 0
      ) 0
    )
  ) in

  printf "%d\n" answer


module Pset = Set.Make (struct type t = int*int let compare = compare end)

let read_int () = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x)
let read_pair () = let u = read_int() in (u,read_int())

let () =
  let n = read_int() in

  let color = [|ref Pset.empty; ref Pset.empty|] in

  let () = for i=1 to n do
    let c = read_int() in
    let s = read_int() in
      color.(c) := Pset.add (s,i) (!(color.(c)));
  done in

  let step (set0, size0, set1, size1) =
    let i = if size0 = 1 then 0 else if size1 = 1 then 1 else
      ( let (m0,_) = Pset.min_elt set0 in
	let (m1,_) = Pset.min_elt set1 in
	  if m0 > m1 then 0 else 1
      )
    in
      (* i is the one which stays, 1-i is removed *)
    let (set0, size0, set1, size1) = if i=0 then (set0, size0, set1, size1) else (set1, size1, set0, size0) in
    let (s0,i0) = Pset.min_elt set0 in
    let set0 = Pset.remove (s0,i0) set0 in
    let (s1,i1) = Pset.min_elt set1 in
    let set1 = Pset.remove (s1,i1) set1 in

      Printf.printf "%d %d %d\n" i0 i1 s1;

      let set0 = Pset.add (s0-s1, i0) set0 in
	(set0, size0, set1, size1-1)
  in

  let rec loop sets i = if i=n-1 then () else loop (step sets) (i+1) in

    loop (!(color.(0)), Pset.cardinal !(color.(0)), !(color.(1)), Pset.cardinal !(color.(1))) 0

(* Codeforces 260D BlackWhiteTree ??? *)

open String;;
open Array;;

let gr () = Scanf.scanf " %d" (fun i -> i);;
let grf () = Scanf.scanf " %f" (fun i -> i);;
let grl () = Scanf.scanf " %Ld" (fun i -> i);;
let rdln() = input_line Pervasives.stdin;;
(* let spli s = Str.split (Str.regexp " ") s;; *)
let printlisti li = List.iter (fun i -> (print_int i; print_string " ")) li;;
let printarri ai = Array.iter (fun i -> (print_int i; print_string " ")) ai;;
let debug = false;;

let rec readlist n acc = match n with
	| 0 -> List.rev acc
	| _ -> readlist (n -1) (gr() :: acc);;

let rec readprs idx n acc = match n with
	| 0 -> List.rev acc
	| _ -> let c = gr() in
			let v = gr() in
			readprs (idx +1) (n -1) ((c, v, idx) :: acc);;

let rec split1 li la lb = match li with
	| [] -> (la, lb)
	| h :: t ->
			let (c, _, _) = h in
			match c with
			| 0 -> split1 t (h :: la) lb
			| 1 -> split1 t la (h :: lb);;

let sortfun a b =
	let (ca, va, ia) = a in
	let (cb, vb, ib) = b in
	(va - vb);;

let rec combine_witha lasta lb acc = match lb with
| [] -> acc
| hb :: tb ->
					let (_, va, ia) = lasta in
					let (_, vb, ib) = hb in
					combine_witha lasta tb ((ia, ib, vb) :: acc);;

let rec combine_withb la lastb acc = match la with
| [] -> acc
| ha :: ta ->
					let (_, va, ia) = ha in
					let (_, vb, ib) = lastb in
					combine_withb ta lastb ((ia, ib, va) :: acc);;

let rec combine la lb acc = match la with
	| [] -> acc
	| ha :: [] -> combine_witha ha lb acc
	| ha :: ta -> match lb with
			| [] -> acc
			| hb :: [] -> combine_withb la hb acc
			| hb :: tb ->
					let (_, va, ia) = ha in
					let (_, vb, ib) = hb in
					if va <= vb then
						combine ta ((1, (vb - va), ib) :: tb) ((ia, ib, va) :: acc)
					else
						combine ((0, (va - vb), ia) :: ta) tb ((ia, ib, vb) :: acc);;

let rec pricomb co =
	let (ia, ib, w) = co in
	begin
		print_int ia; print_string " ";
		print_int ib; print_string " ";
		print_int w;
		print_newline()
	end;;

let n = gr();;

let main () =
	let prs = readprs 1 n [] in
	let (ula, ulb) = split1 prs [] [] in
	let la = List.sort sortfun ula in
	let lb = List.sort sortfun ulb in
	let comb = combine la lb [] in
	List.iter pricomb comb;;

main();;

let read_int () = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x) ;;

let n = read_int () ;;

let nodes =
    let rec loop i f =
        if i > n then f []
        else let ci = read_int () and si = read_int ()
        in loop (i + 1) (fun l -> f ((i, ci, si) :: l))
    in loop 1 (fun x -> x) ;;

let white = List.filter (fun (i, c, s) -> c = 0) nodes ;;
let black = List.filter (fun (i, c, s) -> c = 1) nodes ;;

let tree =
    let rec build_tree u cu su w b tree =
        if w = [] && b = [] then tree
        else let (node, color, sum, white, black, edge) =
            let (v, cv, sv), nw, nb, opposite_nodes =
                if cu = 0 then let t = List.tl b in (List.hd b), w, t, t
                else let t = List.tl w in (List.hd w), t, b, t
            in if su > sv || su = sv && opposite_nodes <> []
                then (u, cu, su - sv, nw, nb, (u, v, sv))
            else (v, cv, sv - su, nw, nb, (u, v, su))
        in build_tree node color sum white black (edge :: tree)
    in let (u, c, s) = List.hd white
    in build_tree u c s (List.tl white) black [] ;;

let rec print_tree = function
    | [] -> ()
    | (u, v, w) :: t -> Printf.printf "%d %d %d\n" u v w ; print_tree t
in print_tree tree ;;

(* Codeforces 260D BlackWhiteTree works *)

open String;;
open Array;;

let gr () = Scanf.scanf " %d" (fun i -> i);;
let grf () = Scanf.scanf " %f" (fun i -> i);;
let grl () = Scanf.scanf " %Ld" (fun i -> i);;
let rdln() = input_line Pervasives.stdin;;
(* let spli s = Str.split (Str.regexp " ") s;; *)
let printlisti li = List.iter (fun i -> (print_int i; print_string " ")) li;;
let printarri ai = Array.iter (fun i -> (print_int i; print_string " ")) ai;;
let debug = false;;

let rec readlist n acc = match n with
	| 0 -> List.rev acc
	| _ -> readlist (n -1) (gr() :: acc);;

let rec readprs idx n acc = match n with
	| 0 -> List.rev acc
	| _ -> let c = gr() in
			let v = gr() in
			readprs (idx +1) (n -1) ((c, v, idx) :: acc);;

let rec split1 li la lb = match li with
	| [] -> (la, lb)
	| h :: t ->
			let (c, _, _) = h in
			match c with
			| 0 -> split1 t (h :: la) lb
			| 1 -> split1 t la (h :: lb);;

let sortfun a b =
	let (ca, va, ia) = a in
	let (cb, vb, ib) = b in
	(va - vb);;

let rec combine la lb acc = match la with
	| [] -> acc
	| ha :: ta -> match lb with
			| [] -> acc
			| hb :: tb ->
					let (_, va, ia) = ha in
					let (_, vb, ib) = hb in
					if va <= vb then
						combine ta ((1, (vb - va), ib) :: tb) ((ia, ib, va) :: acc)
					else
						combine ((0, (va - vb), ia) :: ta) tb ((ia, ib, vb) :: acc);;

let rec pricomb co =
	let (ia, ib, w) = co in
	begin
		print_int ia; print_string " ";
		print_int ib; print_string " ";
		print_int w;
		print_newline()
	end;;

let n = gr();;

let main () =
	let prs = readprs 1 n [] in
	let (ula, ulb) = split1 prs [] [] in
	let la = List.sort sortfun ula in
	let lb = List.sort sortfun ulb in
	let comb = combine la lb [] in
	List.iter pricomb comb;;

main();;


let n = int_of_string (input_line stdin) in
for i = 0 to n-1 do
  let a = input_line stdin in
  let tab = Array.make 26 (-1) in
  let rep = ref true in
  for i = 0 to String.length a - 1 do
    let lett = a.[i] in
    let num = Char.code lett - 97 in
    if tab.(num) = -2 then begin rep := false end ;
    if !rep && (tab.(num) <> -1) then begin
      let last = tab.(num) in
      for j = 0 to 25 do
        if tab.(j) <> -1 && tab.(j) <> -2 && num<>j && tab.(j) < last then rep := false ;
        if tab.(j) = -1 then tab.(j) <- (-2)
      done;
    end ; tab.(num) <- i ;

  done;
  if !rep then print_endline "YES" else print_endline "NO"
done;;

module CharOrd = struct
  type t = char

  let compare x y = if x < y then -1 else if x > y then 1 else 0
end

module CharSet = Set.Make (CharOrd)

let solve () =
  let str = read_line () in
  let n = String.length str in
  let c = ref CharSet.empty in
  let rec find_k i =
    if i >= n then n
    else if CharSet.exists (fun x -> x == String.get str i) !c then i
    else (
      c := CharSet.add (String.get str i) !c;
      find_k (i + 1))
  in
  let k = find_k 0 in
  let rec is_ok i =
    if i >= n then true
    else if String.get str i = String.get str (i - k) then is_ok (i + 1)
    else false
  in
  if is_ok k then "YES" else "NO"

let () =
  let t = read_int () in
  for i = 0 to t - 1 do
    print_endline (solve ())
  done

module CharOrd = struct
  type t = char

  let compare x y = if x < y then -1 else if x > y then 1 else 0
end

module CharSet = Set.Make (CharOrd)

let solve () =
  let str = read_line () in
  let n = String.length str in
  let c = ref CharSet.empty in
  let rec find_k i =
    if i >= n then n - 1
    else if CharSet.exists (fun x -> x == String.get str i) !c then i
    else (
      c := CharSet.add (String.get str i) !c;
      find_k (i + 1))
  in
  let k = find_k 0 in
  let rec is_ok i =
    if i >= n then true
    else if String.get str i = String.get str (i - k) then is_ok (i + 1)
    else false
  in
  if is_ok k then "YES" else "NO"

let () =
  let t = read_int () in
  for i = 0 to t - 1 do
    print_endline (solve ())
  done

let n = int_of_string (input_line stdin) in
for i = 0 to n-1 do
  let a = input_line stdin in
let tab = Array.make 26 (-1) in
  let rep = ref true in
  for i = 0 to String.length a - 1 do
    let lett = a.[i] in
    let num = Char.code lett - 97 in
    if !rep && (tab.(num) <> -1) then begin
      let last = tab.(num) in
      for j = 0 to 25 do
        if tab.(j) <> -1 && tab.(j) < last then rep := false
      done;

    end ; tab.(num) <- i ;

  done;
  if !rep then print_endline "YES" else print_endline "NO"
done;;


open Printf
open Scanf

let rec gcd a b = if a=0 then b else gcd (b mod a) a

let ( -- ) (a,b) (c,d) = (a-c,b-d)

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_pair () = let u = read_int() in (u,read_int())
let () =
  let n = read_int () in
  let gun = read_pair () in
  let point = Array.init n (fun _ -> (read_pair()) -- gun) in

  let h = Hashtbl.create 10 in

  let increment t =
    let c = try Hashtbl.find h t with Not_found -> 0 in
    Hashtbl.replace h t (c+1)
  in

  for i=0 to n-1 do
    let (x,y) = point.(i) in
    let g = gcd (abs x) (abs y) in
    let (x,y) = (x/g, y/g) in
    let (x,y) = min (x,y) (-x,-y) in
    increment (x,y);
  done;

  printf "%d\n" (Hashtbl.length h)

(* test if points a, b, c are colinear *)
let colinear (ax, ay) (bx, by) (cx, cy) =
  (ax - cx) * (by - cy) = (ay - cy) * (bx - cx)

let () = Scanf.scanf "%d %d %d\n" (fun n x0 y0 ->
  let colinear = colinear (x0, y0) in
  let points = Array.init n (fun _ ->
    Scanf.scanf "%d %d\n" (fun x y -> (x, y)))
  in
  let rays = ref [points.(0)] in
  Array.iter (fun p ->
    (* check each ray to see if it hits this point *)
    let rec aux = function
      | [] -> false
      | q :: ps -> if colinear p q then true else aux ps
    in
    if not (aux !rays) then
      rays := p :: !rays
  ) points;
  Printf.printf "%d\n" (List.length !rays)
)


let n = Scanf.scanf " %d" (fun x -> x) in

let rec names n =
    if n == 0
    then []
    else Scanf.scanf " %s" (fun x -> x::(names (n - 1)))
in

let sides names =
    let rec helper names acc =
        match names with
        | [] -> acc
        | hd::tl ->
            match hd with
            | "Tetrahedron" -> helper tl (acc + 4)
            | "Cube" -> helper tl (acc + 6)
            | "Octahedron" -> helper tl (acc + 8)
            | "Dodecahedron" -> helper tl (acc + 12)
            | "Icosahedron" -> helper tl (acc + 20)
            | _ -> 0
    in helper names 0
in

Printf.printf "%d\n" (sides (names n))

let make_list () =
    let n = Pervasives.read_int () in
    let rec loop i list =
        if i < n
        then
            let shape = Pervasives.read_line () in
            loop (i + 1) (shape :: list)
        else
            list
    in loop 0 []

let counter count shape =
    match shape with
    | "Tetrahedron" -> count + 4
    | "Cube" -> count + 6
    | "Octahedron" -> count + 8
    | "Dodecahedron" -> count + 12
    | "Icosahedron" -> count + 20

let () =
    let shapes = make_list () in
    let count = List.fold_left counter 0 shapes in
    Pervasives.print_int count

let counter shape count =
    match shape with
    | "Tetrahedron" -> count + 4
    | "Cube" -> count + 6
    | "Octahedron" -> count + 8
    | "Dodecahedron" -> count + 12
    | "Icosahedron" -> count + 20

let () =
    let n = Pervasives.read_int () in
    let rec loop i count =
        if i < n
        then
            let shape = Pervasives.read_line () in
            loop (i + 1) (counter shape count)
        else
            Pervasives.print_int count
    in loop 0 0

let main () =
  let gr () = Scanf.scanf "%d" (fun i -> i) in
  let gs () = Scanf.scanf "\n%s" (fun i -> i) in
  let n = gr () in
  let rec solve cnt ans =
      match cnt with
      | 0 -> ans
      | cnt ->
          let line = gs() in
          let sides = match line with
          | "Tetrahedron" -> 4
          | "Cube" -> 6
          | "Octahedron" -> 8
          | "Dodecahedron" -> 12
          | "Icosahedron" -> 20
          | somestr -> Printf.printf "!!!%s\n" (somestr); 0
          in
          solve (cnt - 1) (ans + sides)
  in
  Printf.printf "%d\n" (solve n 0);;
let _ = main();;

open Printf
open Scanf

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let sum i j f = fold i j (fun i a -> (f i) + a) 0

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_string () = bscanf Scanning.stdib " %s " (fun x -> x)
let () =
  let n = read_int () in

  let answer = sum 0 (n-1) (fun _ ->
    match read_string() with
      | "Tetrahedron" -> 4
      | "Cube" -> 6
      | "Octahedron" -> 8
      | "Dodecahedron" -> 12
      | "Icosahedron" -> 20
      | _ -> failwith "bad input"
  ) in

  printf "%d\n" answer