#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 99;
struct edge {
  int u;
  int v;
  int an;
};
int n, m, q, x, y, k, l, sz, ans, b[N], par[N], mark[N], vis[N], a[N][2];
map<pair<int, int>, int> mv;
pair<int, int> p[N];
vector<edge> e[N];
vector<int> g[N];
void clear(int x) {
  mv.clear();
  for (int i = 0; i < x; i++)
    g[a[b[i]][0]].clear(), g[a[b[i]][1]].clear(),
        vis[a[b[i]][0]] = vis[a[b[i]][1]] = 0;
}
int Get_par(int u) {
  if (par[u] < 0) return u;
  return par[u] = Get_par(par[u]);
}
void merge(int u, int v) {
  if (u == v) return;
  if (par[u] > par[v]) swap(u, v);
  par[u] += par[v], par[v] = u;
}
void dfs(int x, int par) {
  vis[x] = 1;
  for (int i = 0; i < g[x].size(); i++)
    if (vis[g[x][i]] && g[x][i] != par) ans = 0;
  for (int i = 0; i < g[x].size(); i++)
    if (!vis[g[x][i]]) dfs(g[x][i], x);
}
int main() {
  fill(par, par + N, -1);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &a[i][0], &a[i][1]);
    ;
    scanf("%I64d", &l);
    ;
    e[l].push_back(edge());
    sz = e[l].size() - 1;
    e[l][sz].u = a[i][0], e[l][sz].v = a[i][1], e[l][sz].an = i;
  }
  for (int i = 1; i < N; i++) {
    for (int j = 0; j < e[i].size(); j++) {
      p[e[i][j].an] = make_pair(Get_par(e[i][j].u), Get_par(e[i][j].v));
      if (Get_par(e[i][j].u) != Get_par(e[i][j].v)) mark[e[i][j].an] = 1;
    }
    for (int j = 0; j < e[i].size(); j++) {
      merge(Get_par(e[i][j].u), Get_par(e[i][j].v));
      if (p[e[i][j].an].first < p[e[i][j].an].second)
        swap(p[e[i][j].an].first, p[e[i][j].an].second);
    }
  }
  cin >> q;
  for (int io = 0; io < q; io++) {
    ans = 1;
    scanf("%d", &k);
    ;
    for (int i = 0; i < k; i++) {
      scanf("%d", &x);
      ;
      --x, b[i] = x;
      if (mv[p[x]])
        ans = 0;
      else
        mv[p[x]] = 1;
      if (!mark[x]) ans = 0;
      g[a[x][0]].push_back(a[x][1]), g[a[x][1]].push_back(a[x][0]);
    }
    for (int i = 0; i < k; i++)
      if (!vis[a[b[i]][0]]) dfs(a[b[i]][0], 0);
    clear(k);
    cout << (ans ? "YES" : "NO") << '\n';
  }
}
