

'''simple docstring'''


from collections import deque
from math import floor
from random import random
from time import time



class 					lowercase						:


							def __init__(			self)							->				Optional[int]:
								UpperCAmelCase_		:      Union[str, Any]       	=					{}


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case=1)							->				int:
								if self.graph.get(_snake_case):
									if self.graph[u].count([w, v]) == 0:
										self.graph[u].append([w, v])
								else:
									UpperCAmelCase_		:      Optional[Any]       	=					[[w, v]]
								if not self.graph.get(_snake_case):
									UpperCAmelCase_		:      List[str]       	=					[]


							def 						_snake_case							(			self)							->				List[str]:
								return list(self.graph)


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case)							->				Union[str, Any]:
								if self.graph.get(_snake_case):
									for _ in self.graph[u]:
										if _[1] == v:
											self.graph[u].remove(_snake_case)


							def 						_snake_case							(			self    ,			_snake_case=-2    ,			_snake_case=-1)							->				List[str]:
								if s == d:
									return []
								UpperCAmelCase_		:      Any       	=					[]
								UpperCAmelCase_		:      Optional[int]       	=					[]
								if s == -2:
									UpperCAmelCase_		:      List[Any]       	=					list(self.graph)[0]
								stack.append(_snake_case)
								visited.append(_snake_case)
								UpperCAmelCase_		:      Tuple       	=					s

								while True:
									# check if there is any non isolated nodes
									if len(self.graph[s]) != 0:
										UpperCAmelCase_		:      Optional[int]       	=					s
										for node in self.graph[s]:
											if visited.count(node[1]) < 1:
												if node[1] == d:
													visited.append(_snake_case)
													return visited
												else:
													stack.append(node[1])
													visited.append(node[1])
													UpperCAmelCase_		:      Optional[Any]       	=					node[1]
													break

            # check if all the children are visited
									if s == ss:
										stack.pop()
										if len(_snake_case) != 0:
											UpperCAmelCase_		:      Union[str, Any]       	=					stack[len(_snake_case) - 1]
									else:
										UpperCAmelCase_		:      Optional[Any]       	=					ss

									# check if se have reached the starting point
									if len(_snake_case) == 0:
										return visited


							def 						_snake_case							(			self    ,			_snake_case=-1)							->				Tuple:
								if c == -1:
									UpperCAmelCase_		:      Dict       	=					floor(random() * 1_0000) + 10
								for i in range(_snake_case):
									# every vertex has max 100 edges
									for _ in range(floor(random() * 102) + 1):
										UpperCAmelCase_		:      Tuple       	=					floor(random() * c) + 1
										if n != i:
											self.add_pair(_snake_case    ,			_snake_case    ,			1)


							def 						_snake_case							(			self    ,			_snake_case=-2)							->				List[str]:
								UpperCAmelCase_		:      Any       	=					deque()
								UpperCAmelCase_		:      Dict       	=					[]
								if s == -2:
									UpperCAmelCase_		:      int       	=					list(self.graph)[0]
								d.append(_snake_case)
								visited.append(_snake_case)
								while d:
									UpperCAmelCase_		:      Optional[Any]       	=					d.popleft()
									if len(self.graph[s]) != 0:
										for node in self.graph[s]:
											if visited.count(node[1]) < 1:
												d.append(node[1])
												visited.append(node[1])
								return visited


							def 						_snake_case							(			self    ,			_snake_case)							->				Tuple:
								UpperCAmelCase_		:      Dict       	=					0
								for x in self.graph:
									for y in self.graph[x]:
										if y[1] == u:
											count += 1
								return count


							def 						_snake_case							(			self    ,			_snake_case)							->				Dict:
								return len(self.graph[u])


							def 						_snake_case							(			self    ,			_snake_case=-2)							->				Dict:
								UpperCAmelCase_		:      List[Any]       	=					[]
								UpperCAmelCase_		:      Any       	=					[]
								if s == -2:
									UpperCAmelCase_		:      Union[str, Any]       	=					list(self.graph)[0]
								stack.append(_snake_case)
								visited.append(_snake_case)
								UpperCAmelCase_		:      Any       	=					s
								UpperCAmelCase_		:      str       	=					[]

								while True:
									# check if there is any non isolated nodes
									if len(self.graph[s]) != 0:
										UpperCAmelCase_		:      Dict       	=					s
										for node in self.graph[s]:
											if visited.count(node[1]) < 1:
												stack.append(node[1])
												visited.append(node[1])
												UpperCAmelCase_		:      List[str]       	=					node[1]
												break

            # check if all the children are visited
									if s == ss:
										sorted_nodes.append(stack.pop())
										if len(_snake_case) != 0:
											UpperCAmelCase_		:      Optional[int]       	=					stack[len(_snake_case) - 1]
									else:
										UpperCAmelCase_		:      Any       	=					ss

									# check if se have reached the starting point
									if len(_snake_case) == 0:
										return sorted_nodes


							def 						_snake_case							(			self)							->				List[Any]:
								UpperCAmelCase_		:      Union[str, Any]       	=					[]
								UpperCAmelCase_		:      int       	=					[]
								UpperCAmelCase_		:      str       	=					list(self.graph)[0]
								stack.append(_snake_case)
								visited.append(_snake_case)
								UpperCAmelCase_		:      Tuple       	=					-2
								UpperCAmelCase_		:      Dict       	=					[]
								UpperCAmelCase_		:      Tuple       	=					s
								UpperCAmelCase_		:      Tuple       	=					False
								UpperCAmelCase_		:      str       	=					set()

								while True:
									# check if there is any non isolated nodes
									if len(self.graph[s]) != 0:
										UpperCAmelCase_		:      Any       	=					s
										for node in self.graph[s]:
											if (
											    visited.count(node[1]) > 0
											    and node[1] != parent
											    and indirect_parents.count(node[1]) > 0
											    and not on_the_way_back
											):
												UpperCAmelCase_		:      Optional[int]       	=					len(_snake_case) - 1
												while len_stack >= 0:
													if stack[len_stack] == node[1]:
														anticipating_nodes.add(node[1])
														break
													else:
														anticipating_nodes.add(stack[len_stack])
														len_stack -= 1
											if visited.count(node[1]) < 1:
												stack.append(node[1])
												visited.append(node[1])
												UpperCAmelCase_		:      Any       	=					node[1]
												break

            # check if all the children are visited
									if s == ss:
										stack.pop()
										UpperCAmelCase_		:      Optional[int]       	=					True
										if len(_snake_case) != 0:
											UpperCAmelCase_		:      str       	=					stack[len(_snake_case) - 1]
									else:
										UpperCAmelCase_		:      Union[str, Any]       	=					False
										indirect_parents.append(_snake_case)
										UpperCAmelCase_		:      Optional[int]       	=					s
										UpperCAmelCase_		:      Dict       	=					ss

									# check if se have reached the starting point
									if len(_snake_case) == 0:
										return list(_snake_case)


							def 						_snake_case							(			self)							->				List[str]:
								UpperCAmelCase_		:      Optional[Any]       	=					[]
								UpperCAmelCase_		:      Union[str, Any]       	=					[]
								UpperCAmelCase_		:      Union[str, Any]       	=					list(self.graph)[0]
								stack.append(_snake_case)
								visited.append(_snake_case)
								UpperCAmelCase_		:      Tuple       	=					-2
								UpperCAmelCase_		:      Optional[Any]       	=					[]
								UpperCAmelCase_		:      int       	=					s
								UpperCAmelCase_		:      Any       	=					False
								UpperCAmelCase_		:      Any       	=					set()

								while True:
									# check if there is any non isolated nodes
									if len(self.graph[s]) != 0:
										UpperCAmelCase_		:      List[Any]       	=					s
										for node in self.graph[s]:
											if (
											    visited.count(node[1]) > 0
											    and node[1] != parent
											    and indirect_parents.count(node[1]) > 0
											    and not on_the_way_back
											):
												UpperCAmelCase_		:      List[Any]       	=					len(_snake_case) - 1
												while len_stack_minus_one >= 0:
													if stack[len_stack_minus_one] == node[1]:
														anticipating_nodes.add(node[1])
														break
													else:
														return True
											if visited.count(node[1]) < 1:
												stack.append(node[1])
												visited.append(node[1])
												UpperCAmelCase_		:      Dict       	=					node[1]
												break

            # check if all the children are visited
									if s == ss:
										stack.pop()
										UpperCAmelCase_		:      Optional[Any]       	=					True
										if len(_snake_case) != 0:
											UpperCAmelCase_		:      str       	=					stack[len(_snake_case) - 1]
									else:
										UpperCAmelCase_		:      Union[str, Any]       	=					False
										indirect_parents.append(_snake_case)
										UpperCAmelCase_		:      Optional[Any]       	=					s
										UpperCAmelCase_		:      Optional[Any]       	=					ss

									# check if se have reached the starting point
									if len(_snake_case) == 0:
										return False


							def 						_snake_case							(			self    ,			_snake_case=-2    ,			_snake_case=-1)							->				Optional[int]:
								UpperCAmelCase_		:      str       	=					time()
								self.dfs(_snake_case    ,			_snake_case)
								UpperCAmelCase_		:      List[str]       	=					time()
								return end - begin





							def 						_snake_case							(			self    ,			_snake_case=-2)							->				List[str]:
								UpperCAmelCase_		:      Tuple       	=					time()
								self.bfs(_snake_case)
								UpperCAmelCase_		:      Union[str, Any]       	=					time()
								return end - begin



class 					lowercase						:


							def __init__(			self)							->				Dict:
								UpperCAmelCase_		:      Union[str, Any]       	=					{}


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case=1)							->				Dict:
								# check if the u exists
								if self.graph.get(_snake_case):
									# if there already is a edge
									if self.graph[u].count([w, v]) == 0:
										self.graph[u].append([w, v])
								else:
									# if u does not exist
									UpperCAmelCase_		:      Optional[Any]       	=					[[w, v]]
								# add the other way
								if self.graph.get(_snake_case):
									# if there already is a edge
									if self.graph[v].count([w, u]) == 0:
										self.graph[v].append([w, u])
								else:
									# if u does not exist
									UpperCAmelCase_		:      Tuple       	=					[[w, u]]


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case)							->				List[str]:
								if self.graph.get(_snake_case):
									for _ in self.graph[u]:
										if _[1] == v:
											self.graph[u].remove(_snake_case)
        # the other way round
								if self.graph.get(_snake_case):
									for _ in self.graph[v]:
										if _[1] == u:
											self.graph[v].remove(_snake_case)


							def 						_snake_case							(			self    ,			_snake_case=-2    ,			_snake_case=-1)							->				List[str]:
								if s == d:
									return []
								UpperCAmelCase_		:      Tuple       	=					[]
								UpperCAmelCase_		:      int       	=					[]
								if s == -2:
									UpperCAmelCase_		:      List[Any]       	=					list(self.graph)[0]
								stack.append(_snake_case)
								visited.append(_snake_case)
								UpperCAmelCase_		:      List[str]       	=					s

								while True:
									# check if there is any non isolated nodes
									if len(self.graph[s]) != 0:
										UpperCAmelCase_		:      List[Any]       	=					s
										for node in self.graph[s]:
											if visited.count(node[1]) < 1:
												if node[1] == d:
													visited.append(_snake_case)
													return visited
												else:
													stack.append(node[1])
													visited.append(node[1])
													UpperCAmelCase_		:      Optional[Any]       	=					node[1]
													break

            # check if all the children are visited
									if s == ss:
										stack.pop()
										if len(_snake_case) != 0:
											UpperCAmelCase_		:      Optional[Any]       	=					stack[len(_snake_case) - 1]
									else:
										UpperCAmelCase_		:      Dict       	=					ss

									# check if se have reached the starting point
									if len(_snake_case) == 0:
										return visited


							def 						_snake_case							(			self    ,			_snake_case=-1)							->				Tuple:
								if c == -1:
									UpperCAmelCase_		:      List[str]       	=					floor(random() * 1_0000) + 10
								for i in range(_snake_case):
									# every vertex has max 100 edges
									for _ in range(floor(random() * 102) + 1):
										UpperCAmelCase_		:      Dict       	=					floor(random() * c) + 1
										if n != i:
											self.add_pair(_snake_case    ,			_snake_case    ,			1)


							def 						_snake_case							(			self    ,			_snake_case=-2)							->				int:
								UpperCAmelCase_		:      str       	=					deque()
								UpperCAmelCase_		:      Any       	=					[]
								if s == -2:
									UpperCAmelCase_		:      Optional[int]       	=					list(self.graph)[0]
								d.append(_snake_case)
								visited.append(_snake_case)
								while d:
									UpperCAmelCase_		:      Tuple       	=					d.popleft()
									if len(self.graph[s]) != 0:
										for node in self.graph[s]:
											if visited.count(node[1]) < 1:
												d.append(node[1])
												visited.append(node[1])
								return visited


							def 						_snake_case							(			self    ,			_snake_case)							->				Optional[int]:
								return len(self.graph[u])


							def 						_snake_case							(			self)							->				int:
								UpperCAmelCase_		:      Union[str, Any]       	=					[]
								UpperCAmelCase_		:      Optional[int]       	=					[]
								UpperCAmelCase_		:      Union[str, Any]       	=					list(self.graph)[0]
								stack.append(_snake_case)
								visited.append(_snake_case)
								UpperCAmelCase_		:      Union[str, Any]       	=					-2
								UpperCAmelCase_		:      int       	=					[]
								UpperCAmelCase_		:      Dict       	=					s
								UpperCAmelCase_		:      Optional[int]       	=					False
								UpperCAmelCase_		:      Optional[Any]       	=					set()

								while True:
									# check if there is any non isolated nodes
									if len(self.graph[s]) != 0:
										UpperCAmelCase_		:      List[str]       	=					s
										for node in self.graph[s]:
											if (
											    visited.count(node[1]) > 0
											    and node[1] != parent
											    and indirect_parents.count(node[1]) > 0
											    and not on_the_way_back
											):
												UpperCAmelCase_		:      str       	=					len(_snake_case) - 1
												while len_stack >= 0:
													if stack[len_stack] == node[1]:
														anticipating_nodes.add(node[1])
														break
													else:
														anticipating_nodes.add(stack[len_stack])
														len_stack -= 1
											if visited.count(node[1]) < 1:
												stack.append(node[1])
												visited.append(node[1])
												UpperCAmelCase_		:      Union[str, Any]       	=					node[1]
												break

            # check if all the children are visited
									if s == ss:
										stack.pop()
										UpperCAmelCase_		:      List[Any]       	=					True
										if len(_snake_case) != 0:
											UpperCAmelCase_		:      List[Any]       	=					stack[len(_snake_case) - 1]
									else:
										UpperCAmelCase_		:      List[Any]       	=					False
										indirect_parents.append(_snake_case)
										UpperCAmelCase_		:      Tuple       	=					s
										UpperCAmelCase_		:      str       	=					ss

									# check if se have reached the starting point
									if len(_snake_case) == 0:
										return list(_snake_case)


							def 						_snake_case							(			self)							->				Optional[int]:
								UpperCAmelCase_		:      Optional[Any]       	=					[]
								UpperCAmelCase_		:      Union[str, Any]       	=					[]
								UpperCAmelCase_		:      Tuple       	=					list(self.graph)[0]
								stack.append(_snake_case)
								visited.append(_snake_case)
								UpperCAmelCase_		:      str       	=					-2
								UpperCAmelCase_		:      Optional[int]       	=					[]
								UpperCAmelCase_		:      int       	=					s
								UpperCAmelCase_		:      Any       	=					False
								UpperCAmelCase_		:      Union[str, Any]       	=					set()

								while True:
									# check if there is any non isolated nodes
									if len(self.graph[s]) != 0:
										UpperCAmelCase_		:      Any       	=					s
										for node in self.graph[s]:
											if (
											    visited.count(node[1]) > 0
											    and node[1] != parent
											    and indirect_parents.count(node[1]) > 0
											    and not on_the_way_back
											):
												UpperCAmelCase_		:      Optional[Any]       	=					len(_snake_case) - 1
												while len_stack_minus_one >= 0:
													if stack[len_stack_minus_one] == node[1]:
														anticipating_nodes.add(node[1])
														break
													else:
														return True
											if visited.count(node[1]) < 1:
												stack.append(node[1])
												visited.append(node[1])
												UpperCAmelCase_		:      int       	=					node[1]
												break

            # check if all the children are visited
									if s == ss:
										stack.pop()
										UpperCAmelCase_		:      int       	=					True
										if len(_snake_case) != 0:
											UpperCAmelCase_		:      Tuple       	=					stack[len(_snake_case) - 1]
									else:
										UpperCAmelCase_		:      Tuple       	=					False
										indirect_parents.append(_snake_case)
										UpperCAmelCase_		:      List[Any]       	=					s
										UpperCAmelCase_		:      Any       	=					ss

									# check if se have reached the starting point
									if len(_snake_case) == 0:
										return False


							def 						_snake_case							(			self)							->				Tuple:
								return list(self.graph)


							def 						_snake_case							(			self    ,			_snake_case=-2    ,			_snake_case=-1)							->				Optional[Any]:
								UpperCAmelCase_		:      List[Any]       	=					time()
								self.dfs(_snake_case    ,			_snake_case)
								UpperCAmelCase_		:      int       	=					time()
								return end - begin





							def 						_snake_case							(			self    ,			_snake_case=-2)							->				int:
								UpperCAmelCase_		:      Optional[Any]       	=					time()
								self.bfs(_snake_case)
								UpperCAmelCase_		:      str       	=					time()
								return end - begin









'''simple docstring'''


class 					lowercase						:


							def __init__(			self)							->				Dict:
								UpperCAmelCase_		:      List[str]       	=					0
								UpperCAmelCase_		:      str       	=					0
								UpperCAmelCase_		:      Union[str, Any]       	=					{}


							def 						_snake_case							(			self    ,			_snake_case)							->				Tuple:
								if vertex not in self.adjacency:
									UpperCAmelCase_		:      Tuple       	=					{}
									self.num_vertices += 1


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case)							->				Any:
								self.add_vertex(_snake_case)
								self.add_vertex(_snake_case)

								if head == tail:
									return

								UpperCAmelCase_		:      str       	=					weight
								UpperCAmelCase_		:      Optional[int]       	=					weight


							def 						_snake_case							(			self)							->				Union[str, Any]:
								UpperCAmelCase_		:      Dict       	=					self.get_edges()
								for edge in edges:
									UpperCAmelCase_	,    UpperCAmelCase_	,    UpperCAmelCase_		:      str       	=					edge
									edges.remove((tail, head, weight))
								for i in range(len(_snake_case)):
									UpperCAmelCase_		:      Union[str, Any]       	=					list(edges[i])

								edges.sort(key=lambda _snake_case: e[2])
								for i in range(len(_snake_case) - 1):
									if edges[i][2] >= edges[i + 1][2]:
										UpperCAmelCase_		:      Tuple       	=					edges[i][2] + 1
								for edge in edges:
									UpperCAmelCase_	,    UpperCAmelCase_	,    UpperCAmelCase_		:      List[Any]       	=					edge
									UpperCAmelCase_		:      Any       	=					weight
									UpperCAmelCase_		:      Union[str, Any]       	=					weight


							def __str__(			self)							->				str:
								UpperCAmelCase_		:      int       	=					''
								for tail in self.adjacency:
									for head in self.adjacency[tail]:
										UpperCAmelCase_		:      Dict       	=					self.adjacency[head][tail]
										string += F"""{head} -> {tail} == {weight}\n"""
								return string.rstrip('\n')


							def 						_snake_case							(			self)							->				str:
								UpperCAmelCase_		:      Optional[int]       	=					[]
								for tail in self.adjacency:
									for head in self.adjacency[tail]:
										output.append((tail, head, self.adjacency[head][tail]))
								return output


							def 						_snake_case							(			self)							->				Any:
								return self.adjacency.keys()


							@staticmethod
							def 						_snake_case							(			_snake_case=None    ,			_snake_case=None)							->				Dict:
								UpperCAmelCase_		:      List[str]       	=					Graph()
								if vertices is None:
									UpperCAmelCase_		:      Optional[Any]       	=					[]
								if edges is None:
									UpperCAmelCase_		:      int       	=					[]
								for vertex in vertices:
									g.add_vertex(_snake_case)
								for edge in edges:
									g.add_edge(*_snake_case)
								return g



							class 					lowercase						:


														def __init__(			self)							->				Dict:
															UpperCAmelCase_		:      Tuple       	=					{}
															UpperCAmelCase_		:      str       	=					{}


														def __len__(			self)							->				List[Any]:
															return len(self.parent)


														def 						_snake_case							(			self    ,			_snake_case)							->				Union[str, Any]:
															if item in self.parent:
																return self.find(_snake_case)

															UpperCAmelCase_		:      Any       	=					item
															UpperCAmelCase_		:      List[str]       	=					0
															return item


														def 						_snake_case							(			self    ,			_snake_case)							->				str:
															if item not in self.parent:
																return self.make_set(_snake_case)
															if item != self.parent[item]:
																UpperCAmelCase_		:      Dict       	=					self.find(self.parent[item])
															return self.parent[item]





														def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case)							->				int:
															UpperCAmelCase_		:      List[str]       	=					self.find(_snake_case)
															UpperCAmelCase_		:      List[str]       	=					self.find(_snake_case)

															if roota == roota:
																return roota

															if self.rank[roota] > self.rank[roota]:
																UpperCAmelCase_		:      int       	=					roota
																return roota

															if self.rank[roota] < self.rank[roota]:
																UpperCAmelCase_		:      Dict       	=					roota
																return roota

															if self.rank[roota] == self.rank[roota]:
																self.rank[roota] += 1
																UpperCAmelCase_		:      Any       	=					roota
																return roota
															return None


							@staticmethod
							def 						_snake_case							(			_snake_case)							->				int:
								UpperCAmelCase_		:      Optional[Any]       	=					graph.num_vertices

								UpperCAmelCase_		:      Optional[int]       	=					Graph.UnionFind()
								UpperCAmelCase_		:      int       	=					[]
								while num_components > 1:
									UpperCAmelCase_		:      Any       	=					{}
									for vertex in graph.get_vertices():
										UpperCAmelCase_		:      Optional[Any]       	=					-1

									UpperCAmelCase_		:      List[str]       	=					graph.get_edges()
									for edge in edges:
										UpperCAmelCase_	,    UpperCAmelCase_	,    UpperCAmelCase_		:      List[str]       	=					edge
										edges.remove((tail, head, weight))
									for edge in edges:
										UpperCAmelCase_	,    UpperCAmelCase_	,    UpperCAmelCase_		:      Union[str, Any]       	=					edge
										UpperCAmelCase_		:      Any       	=					union_find.find(_snake_case)
										UpperCAmelCase_		:      Dict       	=					union_find.find(_snake_case)
										if seta != seta:
											if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight:
												UpperCAmelCase_		:      List[str]       	=					[head, tail, weight]

											if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight:
												UpperCAmelCase_		:      str       	=					[head, tail, weight]
									for vertex in cheap_edge:
										if cheap_edge[vertex] != -1:
											UpperCAmelCase_	,    UpperCAmelCase_	,    UpperCAmelCase_		:      Any       	=					cheap_edge[vertex]
											if union_find.find(_snake_case) != union_find.find(_snake_case):
												union_find.union(_snake_case    ,			_snake_case)
												mst_edges.append(cheap_edge[vertex])
												UpperCAmelCase_		:      Tuple       	=					num_components - 1
								UpperCAmelCase_		:      int       	=					Graph.build(edges=_snake_case)
								return mst






