#include <bits/stdc++.h>
using namespace std;
struct par {
  int x, y, c, idx;
  bool operator<(const par &other) const {
    if (c < other.c) return true;
    if (c > other.c) return false;
    if (x < other.x) return true;
    if (x > other.x) return false;
    return y < other.y;
  }
  bool operator==(const par &other) const {
    return x == other.x and y == other.y and c == other.c;
  }
} A[500005], B[500005], F[500005];
int T[500005];
namespace std {
template <>
struct hash<par> {
  size_t operator()(const par &obj) const {
    return hash<int>()(obj.x) + hash<int>()(obj.y) + hash<int>()(obj.c);
  }
};
}  // namespace std
template <class T>
struct magic {
  vector<T> stuff;
  bool sorted = false;
  void insert(T obj) { stuff.push_back(obj); }
  bool contains(T obj) {
    if (!sorted) {
      sort(stuff.begin(), stuff.end());
      sorted = true;
    }
    int mide = stuff.size();
    int x = 0, y = mide - 1;
    int mid;
    while (x < y) {
      mid = (x + y) / 2;
      if (stuff[mid] < obj) {
        x = mid + 1;
      }
    }
    if (stuff[mid] < obj or obj < stuff[mid]) return false;
    return true;
  }
  bool containsDuplicates() {
    if (!sorted) {
      sort(stuff.begin(), stuff.end());
      sorted = true;
    }
    for (int i = 0; i < stuff.size() - 1; i++) {
      if (stuff[i] == stuff[i + 1]) {
        return true;
      }
    }
    return false;
  }
  void clear() {
    stuff.clear();
    sorted = false;
  }
};
int ctos, edges, Q[500005];
int uf[500005];
bool P[500005];
void init(void) {
  for (int i = 0; i <= 500000; i++) uf[i] = -1;
}
int root(int x) {
  int r = x;
  while (uf[r] >= 0) r = uf[r];
  while (uf[x] >= 0) {
    int tmp = uf[x];
    uf[x] = r;
    x = tmp;
  }
  return r;
}
void join(int x, int y) {
  x = root(x);
  y = root(y);
  if (x != y) {
    if (uf[x] < uf[y]) {
      uf[x] += uf[y];
      uf[y] = x;
    } else {
      uf[y] += uf[x];
      uf[x] = y;
    }
  }
}
int queries;
int son;
vector<int> L[500005];
int hay;
bool Z[500005];
bool YT[500005];
void aslo(int dnde) {
  if (Z[dnde]) return;
  Z[dnde] = true;
  ++hay;
  for (int i = 0; i < L[dnde].size(); i++) {
    aslo(L[dnde][i]);
  }
}
int main() {
  ios::sync_with_stdio(false);
  init();
  cin >> ctos >> edges;
  for (int i = 1; i <= edges; i++) {
    cin >> A[i].x >> A[i].y >> A[i].c;
    A[i].idx = i;
    B[i] = A[i];
  }
  sort(B + 1, B + edges + 1);
  for (int i = 1; i <= edges; i++) {
    if (i == 1 or B[i].c != B[i - 1].c) {
      for (int e = i; e <= edges and B[e].c == B[i].c; e++) {
        F[B[e].idx].x = root(B[e].x);
        F[B[e].idx].y = root(B[e].y);
        F[B[e].idx].c = B[e].c;
        F[B[e].idx].idx = B[e].idx;
        if (F[B[e].idx].x != F[B[e].idx].y) {
          P[B[e].idx] = true;
        }
      }
      for (int e = i; e <= edges and B[e].c == B[i].c; e++)
        join(B[e].x, B[e].y);
    }
  }
  sort(F + 1, F + edges + 1);
  int team = 0;
  for (int i = 1; i <= edges; i++) {
    if (i == 1 or !(F[i] == F[i - 1])) {
      ++team;
    }
    T[F[i].idx] = team;
  }
  cin >> queries;
  while (queries--) {
    cin >> son;
    bool sip = true;
    for (int i = 1; i <= son; i++) {
      cin >> Q[i];
      int es = Q[i];
      if (!P[es] or A[es].x == A[es].y or YT[T[es]]) sip = false;
      YT[T[es]] = true;
    }
    if (!sip) {
      for (int i = 1; i <= son; i++) {
        int es = Q[i];
        YT[T[es]] = false;
      }
      cout << "NO\n";
      continue;
    }
    for (int i = 1; i <= son; i++) {
      int es = Q[i];
      L[A[es].x].push_back(A[es].y);
      L[A[es].y].push_back(A[es].x);
    }
    int res = 0;
    for (int i = 1; i <= son; i++) {
      int es = Q[i];
      hay = 0;
      if (!Z[A[es].x]) {
        aslo(A[es].x);
        res = res + hay - 1;
      }
    }
    if (son == res) {
      cout << "YES\n";
    } else {
      cout << "NO\n";
    }
    for (int i = 1; i <= son; i++) {
      int es = Q[i];
      YT[T[es]] = false;
      Z[A[es].x] = false;
      Z[A[es].y] = false;
      L[A[es].x].clear();
      L[A[es].y].clear();
    }
  }
  return 0;
}
