






from __future__ import annotations

from sys import maxsize
from typing import Generic, TypeVar

UpperCamelCase        =				TypeVar("T")




def 				A		(					lowercase__  :  int  )		-> int:
	return (position - 1) // 2




def 				A		(					lowercase__  :  int  )		-> int:
	return (2 * position) + 1




def 				A		(					lowercase__  :  int  )		-> int:
	return (2 * position) + 2

class        lowerCAmelCase_				(      Generic[T]	):







				"""simple docstring"""




				def __init__( self     :Any     ):
					UpperCamelCase__ :list[tuple[T, int]]						=      []
					UpperCamelCase__ :dict[T, int]						=      {}
					UpperCamelCase__ :int						=      0







				def __len__( self     :Dict     ):
					return self.elements







				def __repr__( self     :Tuple     ):
					return str(self.heap     )







				def 				__a   ( self     :List[str]     ):
					# Check if the priority queue is empty
					return self.elements == 0







				def 				__a   ( self     :List[str]  ,     lowerCamelCase__     :T  ,     lowerCamelCase__     :int     ):
					# Add an element with given priority to the queue
					self.heap.append((elem, weight)     )
					UpperCamelCase__ :Dict						=      self.elements
					self.elements += 1
					self._bubble_up(lowerCamelCase__     )







				def 				__a   ( self     :Tuple     ):
					# Remove and return the element with lowest weight (highest priority)
					if self.elements > 1:
						self._swap_nodes(0  ,     self.elements - 1     )
					UpperCamelCase__				,		UpperCamelCase__ :Tuple						=      self.heap.pop()
					del self.position_map[elem]
					self.elements -= 1
					if self.elements > 0:
						UpperCamelCase__				,		UpperCamelCase__ :int						=      self.heap[0]
						self._bubble_down(lowerCamelCase__     )
					return elem







				def 				__a   ( self     :Union[str, Any]  ,     lowerCamelCase__     :T  ,     lowerCamelCase__     :int     ):
					# Update the weight of the given key
					UpperCamelCase__ :Optional[Any]						=      self.position_map[elem]
					UpperCamelCase__ :Dict						=      (elem, weight)
					if position > 0:
						UpperCamelCase__ :Optional[Any]						=      get_parent_position(lowerCamelCase__     )
						UpperCamelCase__				,		UpperCamelCase__ :Optional[Any]						=      self.heap[parent_position]
						if parent_weight > weight:
							self._bubble_up(lowerCamelCase__     )
						else:
							self._bubble_down(lowerCamelCase__     )
					else:
						self._bubble_down(lowerCamelCase__     )







				def 				__a   ( self     :Optional[int]  ,     lowerCamelCase__     :T     ):
					# Place a node at the proper position (upward movement) [to be used internally
					# only]
					UpperCamelCase__ :int						=      self.position_map[elem]
					if curr_pos == 0:
						return None
					UpperCamelCase__ :Any						=      get_parent_position(lowerCamelCase__     )
					UpperCamelCase__				,		UpperCamelCase__ :str						=      self.heap[curr_pos]
					UpperCamelCase__				,		UpperCamelCase__ :List[str]						=      self.heap[parent_position]
					if parent_weight > weight:
						self._swap_nodes(lowerCamelCase__  ,     lowerCamelCase__     )
						return self._bubble_up(lowerCamelCase__     )
					return None







				def 				__a   ( self     :Dict  ,     lowerCamelCase__     :T     ):
					# Place a node at the proper position (downward movement) [to be used
					# internally only]
					UpperCamelCase__ :Dict						=      self.position_map[elem]
					UpperCamelCase__				,		UpperCamelCase__ :Tuple						=      self.heap[curr_pos]
					UpperCamelCase__ :Optional[int]						=      get_child_left_position(lowerCamelCase__     )
					UpperCamelCase__ :int						=      get_child_right_position(lowerCamelCase__     )
					if child_left_position < self.elements and child_right_position < self.elements:
						UpperCamelCase__				,		UpperCamelCase__ :int						=      self.heap[child_left_position]
						UpperCamelCase__				,		UpperCamelCase__ :Optional[int]						=      self.heap[child_right_position]
						if child_right_weight < child_left_weight and child_right_weight < weight:
							self._swap_nodes(lowerCamelCase__  ,     lowerCamelCase__     )
							return self._bubble_down(lowerCamelCase__     )
					if child_left_position < self.elements:
						UpperCamelCase__				,		UpperCamelCase__ :Optional[Any]						=      self.heap[child_left_position]
						if child_left_weight < weight:
							self._swap_nodes(lowerCamelCase__  ,     lowerCamelCase__     )
							return self._bubble_down(lowerCamelCase__     )
					else:
						return None
					if child_right_position < self.elements:
						UpperCamelCase__				,		UpperCamelCase__ :Union[str, Any]						=      self.heap[child_right_position]
						if child_right_weight < weight:
							self._swap_nodes(lowerCamelCase__  ,     lowerCamelCase__     )
							return self._bubble_down(lowerCamelCase__     )
					return None







				def 				__a   ( self     :List[Any]  ,     lowerCamelCase__     :int  ,     lowerCamelCase__     :int     ):
					# Swap the nodes at the given positions
					UpperCamelCase__ :Optional[int]						=      self.heap[nodea_pos][0]
					UpperCamelCase__ :Optional[int]						=      self.heap[nodea_pos][0]
					UpperCamelCase__				,		UpperCamelCase__ :Optional[int]						=      (
					    self.heap[nodea_pos],
					    self.heap[nodea_pos],
					)
					UpperCamelCase__ :Dict						=      nodea_pos
					UpperCamelCase__ :Any						=      nodea_pos

class        lowerCAmelCase_				(      Generic[T]	):







				"""simple docstring"""




				def __init__( self     :str     ):
					UpperCamelCase__ :dict[T, dict[T, int]]						=      {}
					UpperCamelCase__ :int						=      0







				def __repr__( self     :int     ):
					return str(self.connections     )







				def __len__( self     :Dict     ):
					return self.nodes







				def 				__a   ( self     :List[Any]  ,     lowerCamelCase__     :T     ):
					# Add a node in the graph if it is not in the graph
					if node not in self.connections:
						UpperCamelCase__ :Optional[int]						=      {}
						self.nodes += 1







				def 				__a   ( self     :Union[str, Any]  ,     lowerCamelCase__     :T  ,     lowerCamelCase__     :T  ,     lowerCamelCase__     :int     ):
					# Add an edge between 2 nodes in the graph
					self.add_node(lowerCamelCase__     )
					self.add_node(lowerCamelCase__     )
					UpperCamelCase__ :Tuple						=      weight
					UpperCamelCase__ :int						=      weight




def 				A		(					lowercase__  :  GraphUndirectedWeighted[T] ,		)		-> tuple[dict[T, int], dict[T, T | None]]:
	UpperCamelCase__ :dict[T, int]						=      {node: maxsize for node in graph.connections}
	UpperCamelCase__ :dict[T, T | None]						=      {node: None for node in graph.connections}

	UpperCamelCase__ :MinPriorityQueue[T]						=      MinPriorityQueue()
	for node, weight in dist.items():
		priority_queue.push(lowercase__ ,		lowercase__  )

	if priority_queue.is_empty():
		return dist, parent

	# initialization
	UpperCamelCase__ :Optional[Any]						=      priority_queue.extract_min()
	UpperCamelCase__ :List[str]						=      0
	for neighbour in graph.connections[node]:
		if dist[neighbour] > dist[node] + graph.connections[node][neighbour]:
			UpperCamelCase__ :str						=      dist[node] + graph.connections[node][neighbour]
			priority_queue.update_key(lowercase__ ,		dist[neighbour]  )
			UpperCamelCase__ :int						=      node

    # running prim's algorithm
	while not priority_queue.is_empty():
		UpperCamelCase__ :int						=      priority_queue.extract_min()
		for neighbour in graph.connections[node]:
			if dist[neighbour] > dist[node] + graph.connections[node][neighbour]:
				UpperCamelCase__ :int						=      dist[node] + graph.connections[node][neighbour]
				priority_queue.update_key(lowercase__ ,		dist[neighbour]  )
				UpperCamelCase__ :List[Any]						=      node
	return dist, parent


_UpperCAmelCase						:  int									=			range(2, 20 + 1)
_UpperCAmelCase						:  Dict									=			[10**k for k in range(ks[-1] + 1)]
_UpperCAmelCase						:  dict[int, dict[int, list[list[int]]]]									=			{}





def    __lowerCamelCase      (					UpperCamelCase__  ,				UpperCamelCase__  ,				UpperCamelCase__  ,				UpperCamelCase__ ):







							'''simple docstring'''






							snake_case_     							=       sum(a_i[j] for j in range(UpperCamelCase__  ,				len(UpperCamelCase__ ) ) )
							snake_case_     							=       sum(a_i[j] * base[j] for j in range(min(len(UpperCamelCase__ )  ,				UpperCamelCase__ ) ) )

							snake_case_							,       snake_case_     							=       0, 0
							snake_case_     							=       n - i

							snake_case_     							=       memo.get(UpperCamelCase__ )

							if sub_memo is not None:
														snake_case_     							=       sub_memo.get(UpperCamelCase__ )

														if jumps is not None and len(UpperCamelCase__ ) > 0:
																					# find and make the largest jump without going over
																					snake_case_     							=       -1
																					for _k in range(len(UpperCamelCase__ ) - 1  ,				-1  ,				-1 ):
																												if jumps[_k][2] <= k and jumps[_k][1] <= max_dn:
																																			snake_case_     							=       _k
																																			break

																					if max_jump >= 0:
																												snake_case_							,       snake_case_							,       snake_case_     							=       jumps[max_jump]
																												# since the difference between jumps is cached, add c
																												snake_case_     							=       diff + c
																												for j in range(min(UpperCamelCase__  ,				len(UpperCamelCase__ ) ) ):
																																			snake_case_							,       snake_case_     							=       divmod(UpperCamelCase__  ,				10 )
																												if new_c > 0:
																																			add(UpperCamelCase__  ,				UpperCamelCase__  ,				UpperCamelCase__ )

														else:
																					snake_case_     							=       []
							else:
														snake_case_     							=       {c: []}
														snake_case_     							=       sub_memo

							if dn >= max_dn or c + diff >= base[k]:
														return diff, dn

							if k > ks[0]:
														while True:
																					# keep doing smaller jumps
																					snake_case_							,       snake_case_     							=       next_term(UpperCamelCase__  ,				k - 1  ,				i + dn  ,				UpperCamelCase__ )
																					diff += _diff
																					dn += terms_jumped

																					if dn >= max_dn or c + diff >= base[k]:
																												break
							else:
														# would be too small a jump, just compute sequential terms instead
														snake_case_							,       snake_case_     							=       compute(UpperCamelCase__  ,				UpperCamelCase__  ,				i + dn  ,				UpperCamelCase__ )
														diff += _diff
														dn += terms_jumped

							snake_case_     							=       sub_memo[c]

							# keep jumps sorted by # of terms skipped
							snake_case_     							=       0
							while j < len(UpperCamelCase__ ):
														if jumps[j][1] > dn:
																					break
														j += 1

							# cache the jump for this value digitsum(b) and c
							sub_memo[c].insert(UpperCamelCase__  ,				(diff, dn, k) )
							return (diff, dn)





def    __lowerCamelCase      (					UpperCamelCase__  ,				UpperCamelCase__  ,				UpperCamelCase__  ,				UpperCamelCase__ ):







							'''simple docstring'''






							if i >= n:
														return 0, i
							if k > len(UpperCamelCase__ ):
														a_i.extend([0 for _ in range(k - len(UpperCamelCase__ ) )] )

							# note: a_i -> b * 10^k + c
							# ds_b -> digitsum(b)
							# ds_c -> digitsum(c)
							snake_case_     							=       i
							snake_case_							,       snake_case_							,       snake_case_     							=       0, 0, 0
							for j in range(len(UpperCamelCase__ ) ):
														if j >= k:
																					ds_b += a_i[j]
														else:
																					ds_c += a_i[j]

							while i < n:
														i += 1
														snake_case_     							=       ds_c + ds_b
														diff += addend
														snake_case_     							=       0
														for j in range(UpperCamelCase__ ):
																					snake_case_     							=       a_i[j] + addend
																					snake_case_							,       snake_case_     							=       divmod(UpperCamelCase__  ,				10 )

																					ds_c += a_i[j]

														if addend > 0:
																					break

							if addend > 0:
														add(UpperCamelCase__  ,				UpperCamelCase__  ,				UpperCamelCase__ )
							return diff, i - start_i





def    __lowerCamelCase      (					UpperCamelCase__  ,				UpperCamelCase__  ,				UpperCamelCase__ ):







							'''simple docstring'''






							for j in range(UpperCamelCase__  ,				len(UpperCamelCase__ ) ):
														snake_case_     							=       digits[j] + addend
														if s >= 10:
																					snake_case_							,       snake_case_     							=       divmod(UpperCamelCase__  ,				10 )
																					snake_case_     							=       addend // 10 + quotient
														else:
																					snake_case_     							=       s
																					snake_case_     							=       addend // 10

														if addend == 0:
																					break

							while addend > 0:
														snake_case_							,       snake_case_     							=       divmod(UpperCamelCase__  ,				10 )
														digits.append(UpperCamelCase__ )





def    __lowerCamelCase      (					UpperCamelCase__ = 10**15 ):







							'''simple docstring'''






							snake_case_     							=       [1]
							snake_case_     							=       1
							snake_case_     							=       0
							while True:
														snake_case_							,       snake_case_     							=       next_term(UpperCamelCase__  ,				20  ,				i + dn  ,				UpperCamelCase__ )
														dn += terms_jumped
														if dn == n - i:
																					break

							snake_case_     							=       0
							for j in range(len(UpperCamelCase__ ) ):
														a_n += digits[j] * 10**j
							return a_n


if __name__ == "__main__":
					print(F'''{solution() = }''')




