"""simple docstring"""







import unittest

import numpy as np
import timeout_decorator  # noqa

from transformers import OPTConfig, is_flax_available
from transformers.testing_utils import require_flax, require_sentencepiece, slow

from ...generation.test_flax_utils import FlaxGenerationTesterMixin
from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor


if is_flax_available():
						import os

						# The slow tests are often failing with OOM error on GPU
						# This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed
						# but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html
						UpperCamelCase_       :       Dict							   =		"platform"

						import jax
						import jax.numpy as jnp

						from transformers import FlaxOPTForCausalLM, FlaxOPTModel, GPTaTokenizer


def   a__      (		UpperCAmelCase__       :			Any		,			UpperCAmelCase__       :			List[Any]		,			UpperCAmelCase__       :			List[Any]=None		,			UpperCAmelCase__       :			Dict=None			)->	Tuple:







						if attention_mask is None:
												UpperCAmelCase__						=		np.where(input_ids != config.pad_token_id		,			1		,			0			)
						return {
						    "input_ids": input_ids,
						    "attention_mask": attention_mask,
						}


@require_flax
class 			_lowercase		:

				def __init__(     self       :    Any       ,    _lowercase       :    List[Any]       ,    _lowercase       :    Tuple=1_3       ,    _lowercase       :    Optional[int]=7       ,    _lowercase       :    str=True       ,    _lowercase       :    str=False       ,    _lowercase       :    List[Any]=9_9       ,    _lowercase       :    Optional[Any]=1_6       ,    _lowercase       :    Union[str, Any]=2       ,    _lowercase       :    List[Any]=4       ,    _lowercase       :    Tuple=4       ,    _lowercase       :    Any="gelu"       ,    _lowercase       :    Any=0.1       ,    _lowercase       :    Tuple=0.1       ,    _lowercase       :    List[str]=2_0       ,    _lowercase       :    int=2       ,    _lowercase       :    Optional[int]=1       ,    _lowercase       :    Dict=0       ,    _lowercase       :    Tuple=1_6       ,    _lowercase       :    int=1_6       ,    _lowercase       :    int=0.02       ,    ):


										'''simple docstring'''
										UpperCAmelCase__						=		parent
										UpperCAmelCase__						=		batch_size
										UpperCAmelCase__						=		seq_length
										UpperCAmelCase__						=		is_training
										UpperCAmelCase__						=		use_labels
										UpperCAmelCase__						=		vocab_size
										UpperCAmelCase__						=		hidden_size
										UpperCAmelCase__						=		num_hidden_layers
										UpperCAmelCase__						=		num_attention_heads
										UpperCAmelCase__						=		intermediate_size
										UpperCAmelCase__						=		hidden_act
										UpperCAmelCase__						=		hidden_dropout_prob
										UpperCAmelCase__						=		attention_probs_dropout_prob
										UpperCAmelCase__						=		max_position_embeddings
										UpperCAmelCase__						=		eos_token_id
										UpperCAmelCase__						=		pad_token_id
										UpperCAmelCase__						=		bos_token_id
										UpperCAmelCase__						=		embed_dim
										UpperCAmelCase__						=		word_embed_proj_dim
										UpperCAmelCase__						=		initializer_range
										UpperCAmelCase__						=		False

				def        A (     self       :    Tuple     ):


										'''simple docstring'''
										UpperCAmelCase__						=		np.clip(ids_tensor([self.batch_size, self.seq_length - 1]       ,    self.vocab_size     )       ,    3       ,    self.vocab_size     )
										UpperCAmelCase__						=		np.concatenate((input_ids, 2 * np.ones((self.batch_size, 1)       ,    dtype=np.intaa     ))       ,    -1     )

										UpperCAmelCase__						=		OPTConfig(
										    vocab_size=self.vocab_size       ,    hidden_size=self.hidden_size       ,    num_hidden_layers=self.num_hidden_layers       ,    num_attention_heads=self.num_attention_heads       ,    ffn_dim=self.intermediate_size       ,    dropout=self.hidden_dropout_prob       ,    attention_dropout=self.attention_probs_dropout_prob       ,    max_position_embeddings=self.max_position_embeddings       ,    eos_token_id=self.eos_token_id       ,    bos_token_id=self.bos_token_id       ,    pad_token_id=self.pad_token_id       ,    embed_dim=self.embed_dim       ,    is_encoder_decoder=a_       ,    word_embed_proj_dim=self.word_embed_proj_dim       ,    initializer_range=self.initializer_range       ,    use_cache=a_       ,    )
										UpperCAmelCase__						=		prepare_opt_inputs_dict(a_       ,    a_     )
										return config, inputs_dict

				def        A (     self       :    Any     ):


										'''simple docstring'''
										UpperCAmelCase__	,      UpperCAmelCase__						=		self.prepare_config_and_inputs()
										return config, inputs_dict

				def        A (     self       :    Any       ,    _lowercase       :    Optional[Any]       ,    _lowercase       :    List[str]       ,    _lowercase       :    Optional[Any]     ):


										'''simple docstring'''
										UpperCAmelCase__						=		2_0
										UpperCAmelCase__						=		model_class_name(a_     )

										UpperCAmelCase__						=		inputs_dict["""input_ids"""]
										UpperCAmelCase__						=		inputs_dict["""attention_mask"""]

										UpperCAmelCase__						=		model.init_cache(input_ids.shape[0]       ,    a_     )
										UpperCAmelCase__						=		jnp.ones((input_ids.shape[0], max_length)       ,    dtype="""i4"""     )

										UpperCAmelCase__						=		jnp.broadcast_to(
										    jnp.arange(input_ids.shape[-1] - 1     )[None, :]       ,    (input_ids.shape[0], input_ids.shape[-1] - 1)       ,    )
										UpperCAmelCase__						=		model(
										    input_ids[:, :-1]       ,    attention_mask=a_       ,    past_key_values=a_       ,    position_ids=a_       ,    )

										UpperCAmelCase__						=		jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]       ,    dtype="""i4"""     )
										UpperCAmelCase__						=		model(
										    input_ids[:, -1:]       ,    attention_mask=a_       ,    past_key_values=outputs_cache.past_key_values       ,    position_ids=a_       ,    )

										UpperCAmelCase__						=		model(a_     )

										UpperCAmelCase__						=		np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])     )     )
										self.parent.assertTrue(diff < 1e-3       ,    msg=f'Max diff is {diff}'     )

				def        A (     self       :    Optional[Any]       ,    _lowercase       :    Optional[Any]       ,    _lowercase       :    str       ,    _lowercase       :    str     ):


										'''simple docstring'''
										UpperCAmelCase__						=		2_0
										UpperCAmelCase__						=		model_class_name(a_     )

										UpperCAmelCase__	,      UpperCAmelCase__						=		(
										    inputs_dict["""input_ids"""],
										    inputs_dict["""attention_mask"""],
										)

										UpperCAmelCase__						=		jnp.concatenate(
										    [
										        attention_mask,
										        jnp.zeros((attention_mask.shape[0], max_length - attention_mask.shape[1])     ),
										    ]       ,    axis=-1       ,    )

										UpperCAmelCase__						=		model.init_cache(input_ids.shape[0]       ,    a_     )
										UpperCAmelCase__						=		jnp.broadcast_to(
										    jnp.arange(input_ids.shape[-1] - 1     )[None, :]       ,    (input_ids.shape[0], input_ids.shape[-1] - 1)       ,    )

										UpperCAmelCase__						=		model(
										    input_ids[:, :-1]       ,    attention_mask=a_       ,    past_key_values=a_       ,    position_ids=a_       ,    )
										UpperCAmelCase__						=		jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]       ,    dtype="""i4"""     )
										UpperCAmelCase__						=		model(
										    input_ids[:, -1:]       ,    past_key_values=outputs_cache.past_key_values       ,    attention_mask=a_       ,    position_ids=a_       ,    )

										UpperCAmelCase__						=		model(a_       ,    attention_mask=a_     )

										UpperCAmelCase__						=		np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])     )     )
										self.parent.assertTrue(diff < 1e-3       ,    msg=f'Max diff is {diff}'     )


@require_flax
class 			_lowercase		(   SCREAMING_SNAKE_CASE__      ,      unittest.TestCase      ,      SCREAMING_SNAKE_CASE__  ):
				lowerCamelCase__						=       (FlaxOPTModel, FlaxOPTForCausalLM) if is_flax_available() else ()
				lowerCamelCase__						=       () if is_flax_available() else ()

				def        A (     self       :    str     ):


										'''simple docstring'''
										UpperCAmelCase__						=		FlaxOPTModelTester(self     )

				def        A (     self       :    Any     ):


										'''simple docstring'''
										UpperCAmelCase__	,      UpperCAmelCase__						=		self.model_tester.prepare_config_and_inputs()
										for model_class in self.all_model_classes:
																self.model_tester.check_use_cache_forward(a_       ,    a_       ,    a_     )

				def        A (     self       :    Optional[int]     ):


										'''simple docstring'''
										UpperCAmelCase__	,      UpperCAmelCase__						=		self.model_tester.prepare_config_and_inputs()
										for model_class in self.all_model_classes:
																self.model_tester.check_use_cache_forward_with_attn_mask(a_       ,    a_       ,    a_     )

				@slow
				def        A (     self       :    List[Any]     ):


										'''simple docstring'''



										for model_class_name in self.all_model_classes:
																UpperCAmelCase__						=		model_class_name.from_pretrained("""facebook/opt-125m"""     )
																UpperCAmelCase__						=		np.ones((1, 1)     ) * model.config.eos_token_id
																UpperCAmelCase__						=		model(a_     )
																self.assertIsNotNone(a_     )


@require_sentencepiece
@require_flax
class 			_lowercase		(   unittest.TestCase  ):

				@slow
				def        A (     self       :    Dict     ):


										'''simple docstring'''
										UpperCAmelCase__						=		FlaxOPTModel.from_pretrained("""facebook/opt-350m"""     )
										UpperCAmelCase__						=		jnp.array([[0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2]]     )
										UpperCAmelCase__						=		model(input_ids=a_     ).last_hidden_state
										UpperCAmelCase__						=		(1, 1_1, 5_1_2)
										self.assertEqual(output.shape       ,    a_     )
										UpperCAmelCase__						=		jnp.array(
										    [[-0.2867, -1.9256, -0.3062], [-1.2711, -0.1337, -0.1897], [0.4109, 0.1187, -1.3142]]     )
										self.assertTrue(jnp.allclose(output[:, :3, :3]       ,    a_       ,    atol=4e-2     )     )


@require_flax
@slow
class 			_lowercase		(   unittest.TestCase  ):

				def        A (     self       :    Optional[Any]     ):


										'''simple docstring'''
										super().setUp()
										UpperCAmelCase__						=		"""facebook/opt-350m"""

				def        A (     self       :    str     ):


										'''simple docstring'''
										UpperCAmelCase__						=		FlaxOPTForCausalLM.from_pretrained(self.path_model     )
										UpperCAmelCase__						=		GPTaTokenizer.from_pretrained(self.path_model     )

										UpperCAmelCase__						=		[
										    """Today is a beautiful day and I want to""",
										    """In the city of""",
										    """Paris is the capital of France and""",
										    """Computers and mobile phones have taken""",
										]
										# verify that prompt without BOS token is identical to Metaseq -> add_special_tokens=False
										UpperCAmelCase__						=		tokenizer(a_       ,    return_tensors="""jax"""       ,    padding=a_       ,    add_special_tokens=a_     )
										UpperCAmelCase__						=		model(inputs.input_ids       ,    attention_mask=inputs.attention_mask     )[0].mean(axis=-1     )
										UpperCAmelCase__						=		jnp.array(
										    [
										        [1.3851, -13.8923, -10.5229, -10.7533, -0.2309, -10.2384, -0.5365, -9.0947, -5.1670],
										        [-4.7073, -10.6276, -3.9415, -21.5242, -0.2822, -0.2822, -0.2822, -0.2822, -0.2822],
										        [0.6247, -3.4229, -8.9179, -1.4297, -14.1650, 1.4146, -9.0218, -0.2703, -0.2703],
										        [6.4783, -1.9913, -10.7926, -2.3336, 1.5092, -0.9974, -6.8213, 1.3477, 1.3477],
										    ]     )
										self.assertTrue(jnp.allclose(a_       ,    a_       ,    atol=4e-2     )     )

										UpperCAmelCase__						=		jax.jit(a_     )
										UpperCAmelCase__						=		model(inputs.input_ids       ,    attention_mask=inputs.attention_mask     )[0].mean(axis=-1     )
										self.assertTrue(jnp.allclose(a_       ,    a_       ,    atol=4e-2     )     )


@require_flax
@slow
class 			_lowercase		(   unittest.TestCase  ):

				@property
				def        A (     self       :    Any     ):


										'''simple docstring'''
										return [
										    "Today is a beautiful day and I want",
										    "In the city of",
										    "Paris is the capital of France and",
										    "Computers and mobile phones have taken",
										]

				def        A (     self       :    Union[str, Any]     ):


										'''simple docstring'''
										UpperCAmelCase__						=		"""facebook/opt-125m"""

										UpperCAmelCase__						=		[
										    """Today is a beautiful day and I want to""",
										    """In the city of New York, the city""",
										    """Paris is the capital of France and the capital""",
										    """Computers and mobile phones have taken over the""",
										]

										UpperCAmelCase__						=		[]

										UpperCAmelCase__						=		FlaxOPTForCausalLM.from_pretrained(a_     )
										UpperCAmelCase__						=		GPTaTokenizer.from_pretrained(a_     )

										for prompt in self.prompts:
																UpperCAmelCase__						=		tokenizer(a_       ,    return_tensors="""jax"""     ).input_ids

																UpperCAmelCase__						=		model.generate(a_       ,    max_length=1_0     )
																UpperCAmelCase__						=		generated_ids[0]

																UpperCAmelCase__						=		tokenizer.batch_decode(a_       ,    skip_special_tokens=a_     )
																predicted_outputs += generated_string

										self.assertListEqual(a_       ,    a_     )

				def        A (     self       :    Any     ):


										'''simple docstring'''
										UpperCAmelCase__						=		"""facebook/opt-350m"""

										UpperCAmelCase__						=		[
										    """Today is a beautiful day and I want to""",
										    """In the city of San Francisco, the city""",
										    """Paris is the capital of France and the capital""",
										    """Computers and mobile phones have taken over the""",
										]

										UpperCAmelCase__						=		[]
										UpperCAmelCase__						=		FlaxOPTForCausalLM.from_pretrained(a_     )
										UpperCAmelCase__						=		GPTaTokenizer.from_pretrained(a_     )

										for prompt in self.prompts:
																UpperCAmelCase__						=		tokenizer(a_       ,    return_tensors="""jax"""     ).input_ids

																UpperCAmelCase__						=		model.generate(a_       ,    max_length=1_0     )
																UpperCAmelCase__						=		generated_ids[0]

																UpperCAmelCase__						=		tokenizer.batch_decode(a_       ,    skip_special_tokens=a_     )
																predicted_outputs += generated_string

										self.assertListEqual(a_       ,    a_     )

				def        A (     self       :    Optional[int]     ):


										'''simple docstring'''
										UpperCAmelCase__						=		"""facebook/opt-125m"""
										UpperCAmelCase__						=		[
										    """Today is a beautiful day and I want to thank""",
										    """In the city of Rome Canaver Canaver Canaver Canaver""",
										]
										UpperCAmelCase__						=		FlaxOPTForCausalLM.from_pretrained(a_     )
										UpperCAmelCase__						=		GPTaTokenizer.from_pretrained(a_     )
										UpperCAmelCase__						=		tokenizer(
										    [
										        """Today is a beautiful day and I want to""",
										        """In the city of""",
										    ]       ,    return_tensors="""jax"""       ,    padding=a_       ,    )

										UpperCAmelCase__						=		jax.jit(model.generate     )

										UpperCAmelCase__						=		jit_generate(inputs["""input_ids"""]       ,    attention_mask=inputs["""attention_mask"""]     ).sequences

										UpperCAmelCase__						=		tokenizer.batch_decode(a_       ,    skip_special_tokens=a_     )

										self.assertIsNotNone(a_       ,    a_     )

				def        A (     self       :    Any     ):


										'''simple docstring'''
										UpperCAmelCase__						=		"""facebook/opt-350m"""

										UpperCAmelCase__						=		GPTaTokenizer.from_pretrained(a_     )
										UpperCAmelCase__						=		FlaxOPTForCausalLM.from_pretrained(a_     )

										UpperCAmelCase__						=		"""left"""

										# use different length sentences to test batching
										UpperCAmelCase__						=		[
										    """Hello, my dog is a little""",
										    """Today, I""",
										]

										UpperCAmelCase__						=		tokenizer(a_       ,    return_tensors="""jax"""       ,    padding=a_     )
										UpperCAmelCase__						=		inputs["""input_ids"""]

										UpperCAmelCase__						=		model.generate(input_ids=a_       ,    attention_mask=inputs["""attention_mask"""]       ,    trace=a_     )

										UpperCAmelCase__						=		tokenizer(sentences[0]       ,    return_tensors="""jax"""     ).input_ids
										UpperCAmelCase__						=		model.generate(input_ids=a_     )

										UpperCAmelCase__						=		inputs_non_padded.shape[-1] - inputs["""attention_mask"""][-1].sum()
										UpperCAmelCase__						=		tokenizer(sentences[1]       ,    return_tensors="""jax"""     ).input_ids
										UpperCAmelCase__						=		model.generate(input_ids=a_       ,    max_length=model.config.max_length - num_paddings     )

										UpperCAmelCase__						=		tokenizer.batch_decode(outputs[0]       ,    skip_special_tokens=a_     )
										UpperCAmelCase__						=		tokenizer.decode(output_non_padded[0][0]       ,    skip_special_tokens=a_     )
										UpperCAmelCase__						=		tokenizer.decode(output_padded[0][0]       ,    skip_special_tokens=a_     )

										UpperCAmelCase__						=		[
										    """Hello, my dog is a little bit of a dork.\nI'm a little bit""",
										    """Today, I was in the middle of a conversation with a friend about the""",
										]
										self.assertListEqual(a_       ,    a_     )
										self.assertListEqual(a_       ,    [non_padded_sentence, padded_sentence]     )


"""simple docstring"""







from collections import deque


def   a__      (		UpperCAmelCase__       :			Tuple			)->	Optional[Any]:
						return input(UpperCAmelCase__			).strip().split(""" """			)


def   a__      (		UpperCAmelCase__       :			int		,			UpperCAmelCase__       :			int			)->	dict[int, list[int]]:
						UpperCAmelCase__						=		{}
						for i in range(UpperCAmelCase__			):
												UpperCAmelCase__						=		[]

						for e in range(UpperCAmelCase__			):
												UpperCAmelCase__	,      UpperCAmelCase__						=		(int(UpperCAmelCase__			) for i in _input(f'Edge {e + 1}: <node1> <node2> '			))
												graph[x].append(UpperCAmelCase__			)
						return graph


def   a__      (		UpperCAmelCase__       :			int		,			UpperCAmelCase__       :			int			)->	dict[int, list[int]]:
						UpperCAmelCase__						=		{}
						for i in range(UpperCAmelCase__			):
												UpperCAmelCase__						=		[]

						for e in range(UpperCAmelCase__			):
												UpperCAmelCase__	,      UpperCAmelCase__						=		(int(UpperCAmelCase__			) for i in _input(f'Edge {e + 1}: <node1> <node2> '			))
												graph[x].append(UpperCAmelCase__			)
												graph[y].append(UpperCAmelCase__			)
						return graph


def   a__      (		UpperCAmelCase__       :			int		,			UpperCAmelCase__       :			int			)->	dict[int, list[tuple[int, int]]]:
						UpperCAmelCase__						=		{}
						for i in range(UpperCAmelCase__			):
												UpperCAmelCase__						=		[]

						for e in range(UpperCAmelCase__			):
												UpperCAmelCase__	,      UpperCAmelCase__	,      UpperCAmelCase__						=		(int(UpperCAmelCase__			) for i in _input(f'Edge {e + 1}: <node1> <node2> <weight> '			))
												graph[x].append((y, w)			)
												graph[y].append((x, w)			)
						return graph


if __name__ == "__main__":
						UpperCamelCase_   ,						UpperCamelCase_       :       Any							   =		(int(i) for i in _input("""Number of nodes and edges: """))

						UpperCamelCase_       :       Union[str, Any]							   =		int(
						    _input(
						        """Press 1 or 2 or 3 \n"""
						        """1. Unweighted directed \n"""
						        """2. Unweighted undirected \n"""
						        """3. Weighted undirected \n"""
						    )[0]
						)

						UpperCamelCase_       :       Optional[int]							   =		{
						    1: initialize_unweighted_directed_graph,
						    2: initialize_unweighted_undirected_graph,
						    3: initialize_weighted_undirected_graph,
						}[graph_choice](n, m)


def   a__      (		UpperCAmelCase__       :			str		,			UpperCAmelCase__       :			Any			)->	Union[str, Any]:
						UpperCAmelCase__	,      UpperCAmelCase__						=		{s}, [s]
						print(UpperCAmelCase__			)
						while _s:
												UpperCAmelCase__						=		0
												for i in g[_s[-1]]:
																		if i not in vis:
																								_s.append(UpperCAmelCase__			)
																								vis.add(UpperCAmelCase__			)
																								UpperCAmelCase__						=		1
																								print(UpperCAmelCase__			)
																								break
												if not flag:
																		_s.pop()


def   a__      (		UpperCAmelCase__       :			Union[str, Any]		,			UpperCAmelCase__       :			int			)->	Union[str, Any]:
						UpperCAmelCase__	,      UpperCAmelCase__						=		{s}, deque([s]			)
						print(UpperCAmelCase__			)
						while q:
												UpperCAmelCase__						=		q.popleft()
												for v in g[u]:
																		if v not in vis:
																								vis.add(UpperCAmelCase__			)
																								q.append(UpperCAmelCase__			)
																								print(UpperCAmelCase__			)


def   a__      (		UpperCAmelCase__       :			Optional[Any]		,			UpperCAmelCase__       :			Any			)->	Optional[Any]:
						UpperCAmelCase__	,      UpperCAmelCase__	,      UpperCAmelCase__						=		{s: 0}, set(), {s: 0}
						while True:
												if len(UpperCAmelCase__			) == len(UpperCAmelCase__			) - 1:
																		break
												UpperCAmelCase__						=		1_0_0_0_0_0
												for i in dist:
																		if i not in known and dist[i] < mini:
																								UpperCAmelCase__						=		dist[i]
																								UpperCAmelCase__						=		i
												known.add(UpperCAmelCase__			)
												for v in g[u]:
																		if v[0] not in known and dist[u] + v[1] < dist.get(v[0]		,			1_0_0_0_0_0			):
																								UpperCAmelCase__						=		dist[u] + v[1]
																								UpperCAmelCase__						=		u
						for i in dist:
												if i != s:
																		print(dist[i]			)


def   a__      (		UpperCAmelCase__       :			Any		,			UpperCAmelCase__       :			int=None		,			UpperCAmelCase__       :			List[str]=None			)->	Dict:
						if q is None:
												UpperCAmelCase__						=		[1]
						if ind is None:
												UpperCAmelCase__						=		[0] * (len(UpperCAmelCase__			) + 1)  # SInce oth Index is ignored
												for u in g:
																		for v in g[u]:
																								ind[v] += 1
												UpperCAmelCase__						=		deque()
												for i in g:
																		if ind[i] == 0:
																								q.append(UpperCAmelCase__			)
						if len(UpperCAmelCase__			) == 0:
												return
						UpperCAmelCase__						=		q.popleft()
						print(UpperCAmelCase__			)
						for w in g[v]:
												ind[w] -= 1
												if ind[w] == 0:
																		q.append(UpperCAmelCase__			)
						topo(UpperCAmelCase__		,			UpperCAmelCase__		,			UpperCAmelCase__			)


def   a__      (		)->	List[Any]:
						UpperCAmelCase__						=		input().strip()
						UpperCAmelCase__						=		[]
						for _ in range(UpperCAmelCase__			):
												a.append(map(UpperCAmelCase__		,			input().strip().split()			)			)
						return a, n


def   a__      (		UpperCAmelCase__       :			str			)->	str:
						((UpperCAmelCase__)	,      (UpperCAmelCase__))						=		a_and_n
						UpperCAmelCase__						=		list(UpperCAmelCase__			)
						UpperCAmelCase__						=		[[0] * n for i in range(UpperCAmelCase__			)]
						for k in range(UpperCAmelCase__			):
												for i in range(UpperCAmelCase__			):
																		for j in range(UpperCAmelCase__			):
																								if dist[i][j] > dist[i][k] + dist[k][j]:
																														UpperCAmelCase__						=		dist[i][k] + dist[k][j]
																														UpperCAmelCase__						=		k
						print(UpperCAmelCase__			)


def   a__      (		UpperCAmelCase__       :			Optional[int]		,			UpperCAmelCase__       :			int			)->	Optional[int]:
						UpperCAmelCase__	,      UpperCAmelCase__	,      UpperCAmelCase__						=		{s: 0}, set(), {s: 0}
						while True:
												if len(UpperCAmelCase__			) == len(UpperCAmelCase__			) - 1:
																		break
												UpperCAmelCase__						=		1_0_0_0_0_0
												for i in dist:
																		if i not in known and dist[i] < mini:
																								UpperCAmelCase__						=		dist[i]
																								UpperCAmelCase__						=		i
												known.add(UpperCAmelCase__			)
												for v in g[u]:
																		if v[0] not in known and v[1] < dist.get(v[0]		,			1_0_0_0_0_0			):
																								UpperCAmelCase__						=		v[1]
																								UpperCAmelCase__						=		u
						return dist


def   a__      (		)->	Union[str, Any]:
						UpperCAmelCase__	,      UpperCAmelCase__						=		map(UpperCAmelCase__		,			input().split(""" """			)			)
						UpperCAmelCase__						=		[]
						for _ in range(UpperCAmelCase__			):
												edges.append(map(UpperCAmelCase__		,			input().split(""" """			)			)			)
						return edges, n


def   a__      (		UpperCAmelCase__       :			Tuple			)->	int:
						# Sort edges on the basis of distance
						((UpperCAmelCase__)	,      (UpperCAmelCase__))						=		e_and_n
						e.sort(reverse=UpperCAmelCase__		,			key=lambda UpperCAmelCase__			: x[2]			)
						UpperCAmelCase__						=		[{i} for i in range(1		,			n + 1			)]
						while True:
												if len(UpperCAmelCase__			) == 1:
																		break
												print(UpperCAmelCase__			)
												UpperCAmelCase__						=		e.pop()
												for i in range(len(UpperCAmelCase__			)			):
																		if x[0] in s[i]:
																								break
												for j in range(len(UpperCAmelCase__			)			):
																		if x[1] in s[j]:
																								if i == j:
																														break
																								s[j].update(s[i]			)
																								s.pop(UpperCAmelCase__			)
																								break







def   a__      (		UpperCAmelCase__       :			Union[str, Any]			)->	Dict:
						UpperCAmelCase__						=		[]
						for node in graph:
												if not graph[node]:
																		isolated.append(UpperCAmelCase__			)
						return isolated

