For a connected undirected weighted graph *G*, MST (minimum spanning tree) is a subgraph of *G* that contains all of *G*'s vertices, is a tree, and sum of its edges is minimum possible.

You are given a graph *G*. If you run a MST algorithm on graph it would give you only one MST and it causes other edges to become jealous. You are given some queries, each query contains a set of edges of graph *G*, and you should determine whether there is a MST containing all these edges or not.
The first line contains two integers *n*, *m* (2  ≤ *n*, *m*  ≤ 5·10<sup class="upper-index">5</sup>, *n* - 1 ≤ *m*) — the number of vertices and edges in the graph and the number of queries.

The *i*-th of the next *m* lines contains three integers *u*<sub class="lower-index">*i*</sub>, *v*<sub class="lower-index">*i*</sub>, *w*<sub class="lower-index">*i*</sub> (*u*<sub class="lower-index">*i*</sub> ≠ *v*<sub class="lower-index">*i*</sub>, 1 ≤ *w*<sub class="lower-index">*i*</sub> ≤ 5·10<sup class="upper-index">5</sup>) — the endpoints and weight of the *i*-th edge. There can be more than one edges between two vertices. It's guaranteed that the given graph is connected.

The next line contains a single integer *q* (1 ≤ *q* ≤ 5·10<sup class="upper-index">5</sup>) — the number of queries.

*q* lines follow, the *i*-th of them contains the *i*-th query. It starts with an integer *k*<sub class="lower-index">*i*</sub> (1 ≤ *k*<sub class="lower-index">*i*</sub> ≤ *n* - 1) — the size of edges subset and continues with *k*<sub class="lower-index">*i*</sub> distinct space-separated integers from 1 to *m* — the indices of the edges. It is guaranteed that the sum of *k*<sub class="lower-index">*i*</sub> for 1 ≤ *i* ≤ *q* does not exceed 5·10<sup class="upper-index">5</sup>.
This is the graph of sample:

Weight of minimum spanning tree on this graph is 6.

MST with edges (1, 3, 4, 6), contains all of edges from the first query, so answer on the first query is "YES".

Edges from the second query form a cycle of length 3, so there is no spanning tree including these three edges. Thus, answer is "NO".
import sys

from collections import defaultdict


# Class to represent a graph


class Graph:

    def __init__(self, vertices):
        self.V = vertices  # No. of vertices
        self.nonSortedGraph = []  # default dictionary
        self.graph = []

        # to store graph

    # function to add an edge to graph
    def addEdge(self, u, v, w):
        self.nonSortedGraph.append([u-1, v-1, w])

    # A utility function to find set of an element i
    # (uses path compression technique)
    def find(self, parent, i):
        if parent[i] == i:
            return i
        return self.find(parent, parent[i])

    # A function that does union of two sets of x and y
    # (uses union by rank)
    def union(self, parent, rank, x, y):
        xroot = self.find(parent, x)
        yroot = self.find(parent, y)

        # Attach smaller rank tree under root of
        # high rank tree (Union by Rank)
        if rank[xroot] < rank[yroot]:
            parent[xroot] = yroot
        elif rank[xroot] > rank[yroot]:
            parent[yroot] = xroot

        # If ranks are same, then make one as root
        # and increment its rank by one
        else:
            parent[yroot] = xroot
            rank[xroot] += 1

    # The main function to construct MST using Kruskal's
    # algorithm
    def KruskalMST(self,edges):
        qEdgesObjects = list(map(lambda x: self.nonSortedGraph[x - 1], edges))
        qEdgesObjects.sort(key=lambda x: x[2], reverse=True)
        result = []  # This will store the resultant MST

        # An index variable, used for sorted edges
        i = 0

        # An index variable, used for result[]
        e = 0

        # Step 1:  Sort all the edges in
        # non-decreasing order of their
        # weight.  If we are not allowed to change the
        # given graph, we can create a copy of graph
        # it's sorted:)


        parent = []
        rank = []

        # Create V subsets with single elements
        for node in range(self.V):
            parent.append(node)
            rank.append(0)

        # Number of edges to be taken is equal to V-1
        while e < self.V - 1:
            if not len(qEdgesObjects):
                return True
            flag = False
            # Step 2: Pick the smallest edge and increment
            # the index for next iteration
            u, v, w = self.graph[i]
            if w == qEdgesObjects[-1][2]:
                u, v, w = qEdgesObjects.pop()
                i = i-1
                flag = True
            i = i + 1
            x = self.find(parent, u)
            y = self.find(parent, v)

            # If including this edge does't
            #  cause cycle, include it in result
            #  and increment the indexof result
            # for next edge
            if x != y:
                e = e + 1
                result.append([u, v, w])
                self.union(parent, rank, x, y)
            elif flag:
                return False
            # Else discard the edge
        if not len(qEdgesObjects):
            return True
        return False
        minimumCost = 0
        for u, v, weight in result:
            minimumCost += weight




def get_ints(): return list(map(int, sys.stdin.readline().strip().split()))


# class MstGraph:
#     def __init__(self, verticesSize):
#         self.vertices = list(map(lambda x: Component(), range(1, verticesSize + 1)))
#         self.value = 0
#
#     def addEdge(self, edge):
#         if self.vertices[edge.v1 - 1].getFinalPointer() == self.vertices[edge.v2 - 1].getFinalPointer():
#             return False
#         finalComponent = Component()
#         self.vertices[edge.v1 - 1].setFinalPointer(finalComponent)
#         self.vertices[edge.v2 - 1].setFinalPointer(finalComponent)
#         self.value += edge.value
#         return True
#
#     def isMST(self):
#         head = None
#         for vertex in self.vertices:
#             if head is None:
#                 head = vertex.getFinalPointer()
#             else:
#                 if vertex.getFinalPointer() != head:
#                     return False
#         return True
#
#
# class Component:
#     def __init__(self):
#         self.pointer = None
#
#     def point(self, component):
#         self.pointer = component
#
#     def getFinalPointer(self):
#         if self.pointer is None:
#             return self
#         return self.pointer.getFinalPointer()
#
#     def setFinalPointer(self, component):
#         if self.pointer is None:
#             self.pointer = component
#         else:
#             self.pointer.setFinalPointer(component)
#
#
# class Graph:
#     def __init__(self, verticesSize, edgesSize):
#         self.edges = []
#
#     def addEdgesWithVertex(self, vertexIndex1, vertexIndex2, edgeValue):
#         edge = Edge(vertexIndex1, vertexIndex2, edgeValue)
#         self.edges.append(edge)
#
#     def getSortedEdges(self):
#         copiedEdges = self.edges.copy()
#         copiedEdges.sort(key=lambda x: x.value)
#         return copiedEdges
#
#
# class Edge:
#     def __init__(self, v1, v2, value):
#         self.v1 = v1
#         self.v2 = v2
#         self.value = value


n, m = get_ints()
graph = Graph(n)
for i in range(m):
    edgeProperties = get_ints()
    graph.addEdge(edgeProperties[0], edgeProperties[1], edgeProperties[2])
q = int(input())
qEdges = []

for i in range(q):
    qEdges.append(get_ints()[1:])
# sortedEdges = graph.getSortedEdges()


# def mstValue(mstGraph=MstGraph(n)):
#     for j in range(len(sortedEdges)):
#         edge = sortedEdges[j]
#         mstGraph.addEdge(edge)
#     # print(mstGraph.value)
#     return mstGraph.value


# def canBeInMST(edges):
#     mstGraph = Graph(n)
#
#     qEdgesObjects = list(map(lambda x: graph.edges[x - 1], edges))
#     qEdgesObjects.sort(key=lambda x: x.value, reverse=True)
#
#     for j in range(len(sortedEdges)):
#         flag = False
#         edge = sortedEdges[j]
#         if not len(qEdgesObjects):
#             return True
#         if edge.value == qEdgesObjects[-1].value:
#             reachedQEdge = qEdgesObjects.pop()
#             flag = True
#         if flag:
#             if not mstGraph.addEdge(reachedQEdge):
#                 return False
#         mstGraph.addEdge(edge)
#     if not mstGraph.isMST():
#         return False
#     if len(qEdgesObjects):
#         print("error")
#         return False
#     else:
#         return True
graph.graph = sorted(graph.nonSortedGraph,key=lambda item: item[2])
for i in range(q):
    if graph.KruskalMST(qEdges[i]):
        print("YES")
    else:
        print("NO")
