#include <bits/stdc++.h>
using namespace std;
int a[500005], b[500005], c[500005], par[500005], ans[500005], n, m, q,
    par1[500005];
vector<pair<int, int> > v[500005];
int f(int i) {
  if (par[i] != i) par[i] = f(par[i]);
  return par[i];
}
int f1(int i) {
  if (par1[i] != i) par1[i] = f1(par1[i]);
  return par1[i];
}
void get(int i, int j, int k) {
  if (ans[v[i][j].first] == 0) return;
  for (int l = j; l <= k; l++) {
    int id = v[i][l].second;
    int qu = v[i][l].first;
    int x = a[id], y = b[id];
    x = f(x);
    y = f(y);
    if (x == y) {
      ans[qu] = 0;
      return;
    }
    par1[x] = x;
    par1[y] = y;
  }
  for (int l = j; l <= k; l++) {
    int id = v[i][l].second;
    int qu = v[i][l].first;
    int x = a[id], y = b[id];
    x = f(x);
    y = f(y);
    x = f1(x);
    y = f1(y);
    if (x == y) {
      ans[qu] = 0;
      return;
    }
    par1[y] = x;
  }
}
vector<pair<int, int> > ed[500005];
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> a[i] >> b[i] >> c[i];
    ed[c[i]].push_back(make_pair(a[i], b[i]));
  }
  for (int i = 1; i <= n; i++) par[i] = i;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    ans[i] = 1;
    int sz;
    cin >> sz;
    for (int j = 0; j <= sz - 1; j++) {
      int x;
      cin >> x;
      v[c[x]].push_back(make_pair(i, x));
    }
  }
  for (int i = 0; i <= 500004; i++) {
    sort(v[i].begin(), v[i].end());
    for (int j = 0; j < v[i].size(); j++) {
      int k = j;
      while (k < v[i].size() - 1 && v[i][j].first == v[i][k + 1].first) k++;
      get(i, j, k);
      j = k;
    }
    for (int j = 0; j < ed[i].size(); j++) {
      int x = ed[i][j].first, y = ed[i][j].second;
      par[f(y)] = f(x);
    }
  }
  for (int i = 1; i <= q; i++) {
    if (ans[i])
      cout << "YES\n";
    else
      cout << "NO\n";
  }
  return 0;
}
