input: from typing import List
  
  def optimized_travel_cost(n: int, edges: List[List[int]], request: List[List[int]]) -> List[int]:
      """
      Consider an undirected weighted graph with n nodes labeled from 0 to n - 1.
  
      Given the integer n and an array edges, where edges[i] = [u_i, v_i, w_i] signifies an edge between nodes u_i and v_i with a weight of w_i.
  
      A walk on this graph comprises a sequence of nodes and edges. This walk begins and concludes at a node, with each edge connecting the preceding node to the following node. Note that a walk can traverse the same edge or node multiple times.
  
      The cost of a walk from node u to node v is determined by the bitwise AND of the weights of the edges in the walk. Specifically, if the sequence of edge weights in the walk is w_0, w_1, w_2, ..., w_k, the cost is equal to w_0 & w_1 & w_2 & ... & w_k, where & represents the bitwise AND operation.
  
      Additionally, given a 2D array request, where request[i] = [s_i, t_i], for each request, determine the minimum cost of the walk commencing at node s_i and terminating at node t_i. If no such walk exists, return -1 for that request.
  
      Return an array res, where res[i] indicates the minimum cost of a walk for request i.
  
      Examples:
      >>> n = 5, edges = [[0,1,7],[1,3,7],[1,2,1]], request = [[0,3],[3,4]]
      [1,-1]
      >>> n = 3, edges = [[0,2,7],[0,1,15],[1,2,6],[1,2,1]], request = [[1,2]]
      [0]
  
      Constraints:
      2 <= n <= 10^5
      0 <= edges.length <= 10^5
      edges[i].length == 3
      0 <= u_i, v_i <= n - 1
      u_i != v_i
      0 <= w_i <= 10^5
      1 <= request.length <= 10^5
      request[i].length == 2
      0 <= s_i, t_i <= n - 1
      s_i != t_i
      """
output:     import collections
      import heapq
  
      graph = collections.defaultdict(list)
      for u, v, w in edges:
          graph[u].append((v, w))
          graph[v].append((u, w))
  
      def min_cost(u, v):
          pq = [(float('inf'), u)]
          visited = set()
  
          while pq:
              current_cost, node = heapq.heappop(pq)
              if node in visited:
                  continue
              visited.add(node)
              if node == v:
                  return current_cost
              
              for neighbor, weight in graph[node]:
                  if neighbor not in visited:
                      cost = current_cost & weight if current_cost != float('inf') else weight
                      heapq.heappush(pq, (cost, neighbor))
          
          return -1
  
      res = []
      for s, t in request:
          res.append(min_cost(s, t))
          
      return res