Company: Meesho_17july
Difficulty: medium
Large Dam Problem Description A large dam has N floodgates, numbered 0 to N-1 . The control system keeps a record of floodgate operations: each entry in the record opens a pathway between two distinct floodgates, letting water flow directly between them from that day onward. The record is a list of K logs. Each log is [day, door_A, door_B] : on that day , a pathway was opened between floodgate door_A and floodgate door_B . Once a pathway is opened it is never closed, so every pathway recorded on a day at or before some day D is in service on day D . Two floodgates are considered connected on a given day if water can travel between them, either through a pathway opened directly between them or by passing through other floodgates along pathways that are in service. Find the earliest day on which every floodgate in the dam is connected to every other one. If the floodgates never all become connected, print -1 . Input Format The first line contains a single integer T , the number of test c