// #define _GLIBCXX_DEBUG #include // clang-format off std::ostream&operator<<(std::ostream&os,std::int8_t x){return os<<(int)x;} std::ostream&operator<<(std::ostream&os,std::uint8_t x){return os<<(int)x;} std::ostream&operator<<(std::ostream&os,const __int128_t &v){if(!v)os<<"0";__int128_t tmp=v<0?(os<<"-",-v):v;std::string s;while(tmp)s+='0'+(tmp%10),tmp/=10;return std::reverse(s.begin(),s.end()),os< template constexpr inline Int mod_inv(Int a, Int mod) { static_assert(std::is_signed_v); Int x= 1, y= 0, b= mod; for (Int q= 0, z= 0; b;) z= x, x= y, y= z - y * (q= a / b), z= a, a= b, b= z - b * q; return assert(a == 1), x < 0 ? mod - (-x) % mod : x % mod; } namespace math_internal { using namespace std; using u8= unsigned char; using u32= unsigned; using i64= long long; using u64= unsigned long long; using u128= __uint128_t; #define CE constexpr #define IL inline #define NORM \ if (n >= mod) n-= mod; \ return n #define PLUS(U, M) \ CE IL U plus(U l, U r) const { \ if (l+= r; l >= M) l-= M; \ return l; \ } #define DIFF(U, C, M) \ CE IL U diff(U l, U r) const { \ if (l-= r; l >> C) l+= M; \ return l; \ } #define SGN(U) \ static CE IL U set(U n) { return n; } \ static CE IL U get(U n) { return n; } \ static CE IL U norm(U n) { return n; } template struct MP_Mo { u_t mod; CE MP_Mo(): mod(0), iv(0), r2(0) {} CE MP_Mo(u_t m): mod(m), iv(inv(m)), r2(-du_t(mod) % mod) {} CE IL u_t mul(u_t l, u_t r) const { return reduce(du_t(l) * r); } PLUS(u_t, mod << 1) DIFF(u_t, A, mod << 1) CE IL u_t set(u_t n) const { return mul(n, r2); } CE IL u_t get(u_t n) const { n= reduce(n); NORM; } CE IL u_t norm(u_t n) const { NORM; } private: u_t iv, r2; static CE u_t inv(u_t n, int e= 6, u_t x= 1) { return e ? inv(n, e - 1, x * (2 - x * n)) : x; } CE IL u_t reduce(const du_t &w) const { return u_t(w >> B) + mod - ((du_t(u_t(w) * iv) * mod) >> B); } }; struct MP_Na { u32 mod; CE MP_Na(): mod(0){}; CE MP_Na(u32 m): mod(m) {} CE IL u32 mul(u32 l, u32 r) const { return u64(l) * r % mod; } PLUS(u32, mod) DIFF(u32, 31, mod) SGN(u32) }; struct MP_Br { // mod < 2^31 u32 mod; CE MP_Br(): mod(0), s(0), x(0) {} CE MP_Br(u32 m): mod(m), s(95 - __builtin_clz(m - 1)), x(((u128(1) << s) + m - 1) / m) {} CE IL u32 mul(u32 l, u32 r) const { return rem(u64(l) * r); } PLUS(u32, mod) DIFF(u32, 31, mod) SGN(u32) private: u8 s; u64 x; CE IL u64 quo(u64 n) const { return (u128(x) * n) >> s; } CE IL u32 rem(u64 n) const { return n - quo(n) * mod; } }; struct MP_Br2 { // 2^20 < mod <= 2^41 u64 mod; CE MP_Br2(): mod(0), x(0) {} CE MP_Br2(u64 m): mod(m), x((u128(1) << 84) / m) {} CE IL u64 mul(u64 l, u64 r) const { return rem(u128(l) * r); } PLUS(u64, mod << 1) DIFF(u64, 63, mod << 1) static CE IL u64 set(u64 n) { return n; } CE IL u64 get(u64 n) const { NORM; } CE IL u64 norm(u64 n) const { NORM; } private: u64 x; CE IL u128 quo(const u128 &n) const { return (n * x) >> 84; } CE IL u64 rem(const u128 &n) const { return n - quo(n) * mod; } }; struct MP_D2B1 { u8 s; u64 mod, d, v; CE MP_D2B1(): s(0), mod(0), d(0), v(0) {} CE MP_D2B1(u64 m): s(__builtin_clzll(m)), mod(m), d(m << s), v(u128(-1) / d) {} CE IL u64 mul(u64 l, u64 r) const { return rem((u128(l) * r) << s) >> s; } PLUS(u64, mod) DIFF(u64, 63, mod) SGN(u64) private: CE IL u64 rem(const u128 &u) const { u128 q= (u >> 64) * v + u; u64 r= u64(u) - (q >> 64) * d - d; if (r > u64(q)) r+= d; if (r >= d) r-= d; return r; } }; template CE u_t pow(u_t x, u64 k, const MP &md) { for (u_t ret= md.set(1);; x= md.mul(x, x)) if (k & 1 ? ret= md.mul(ret, x) : 0; !(k>>= 1)) return ret; } #undef NORM #undef PLUS #undef DIFF #undef SGN #undef CE } namespace math_internal { struct m_b {}; struct s_b: m_b {}; } template constexpr bool is_modint_v= std::is_base_of_v; template constexpr bool is_staticmodint_v= std::is_base_of_v; namespace math_internal { #define CE constexpr template struct SB: s_b { protected: static CE MP md= MP(MOD); }; template struct MInt: public B { using Uint= U; static CE inline auto mod() { return B::md.mod; } CE MInt(): x(0) {} template && !is_same_v, nullptr_t> = nullptr> CE MInt(T v): x(B::md.set(v.val() % B::md.mod)) {} CE MInt(__int128_t n): x(B::md.set((n < 0 ? ((n= (-n) % B::md.mod) ? B::md.mod - n : n) : n % B::md.mod))) {} CE MInt operator-() const { return MInt() - *this; } #define FUNC(name, op) \ CE MInt name const { \ MInt ret; \ ret.x= op; \ return ret; \ } FUNC(operator+(const MInt& r), B::md.plus(x, r.x)) FUNC(operator-(const MInt& r), B::md.diff(x, r.x)) FUNC(operator*(const MInt& r), B::md.mul(x, r.x)) FUNC(pow(u64 k), math_internal::pow(x, k, B::md)) #undef FUNC CE MInt operator/(const MInt& r) const { return *this * r.inv(); } CE MInt& operator+=(const MInt& r) { return *this= *this + r; } CE MInt& operator-=(const MInt& r) { return *this= *this - r; } CE MInt& operator*=(const MInt& r) { return *this= *this * r; } CE MInt& operator/=(const MInt& r) { return *this= *this / r; } CE bool operator==(const MInt& r) const { return B::md.norm(x) == B::md.norm(r.x); } CE bool operator!=(const MInt& r) const { return !(*this == r); } CE bool operator<(const MInt& r) const { return B::md.norm(x) < B::md.norm(r.x); } CE inline MInt inv() const { return mod_inv(val(), B::md.mod); } CE inline Uint val() const { return B::md.get(x); } friend ostream& operator<<(ostream& os, const MInt& r) { return os << r.val(); } friend istream& operator>>(istream& is, MInt& r) { i64 v; return is >> v, r= MInt(v), is; } private: Uint x; }; template using ModInt= conditional_t < (MOD < (1 << 30)) & MOD, MInt, MOD>>, conditional_t < (MOD < (1ull << 62)) & MOD, MInt, MOD>>, conditional_t>, conditional_t>, conditional_t>, MInt>>>>>>; #undef CE } using math_internal::ModInt; template mod_t get_inv(int n) { static_assert(is_modint_v); static const auto m= mod_t::mod(); static mod_t dat[LM]; static int l= 1; if (l == 1) dat[l++]= 1; while (l <= n) dat[l++]= dat[m % l] * (m - m / l); return dat[n]; } class UnionFind { std::vector par; public: UnionFind(int n): par(n, -1) {} bool unite(int u, int v) { if ((u= root(u)) == (v= root(v))) return false; if (par[u] > par[v]) std::swap(u, v); return par[u]+= par[v], par[v]= u, true; } bool same(int u, int v) { return root(u) == root(v); } int root(int u) { return par[u] < 0 ? u : par[u]= root(par[u]); } int size(int u) { return -par[root(u)]; } }; template struct ListRange { using Iterator= typename std::vector::const_iterator; Iterator bg, ed; Iterator begin() const { return bg; } Iterator end() const { return ed; } size_t size() const { return std::distance(bg, ed); } const T &operator[](int i) const { return bg[i]; } }; template class CsrArray { std::vector csr; std::vector pos; public: CsrArray()= default; CsrArray(const std::vector &c, const std::vector &p): csr(c), pos(p) {} size_t size() const { return pos.size() - 1; } const ListRange operator[](int i) const { return {csr.cbegin() + pos[i], csr.cbegin() + pos[i + 1]}; } }; template class Tree { template struct Edge_B { int to; T cost; operator int() const { return to; } }; template struct Edge_B { int to; operator int() const { return to; } }; using Edge= Edge_B; using C= std::conditional_t, std::nullptr_t, Cost>; std::vector, std::pair, std::tuple>> es; std::vector g; std::vector P, PP, D, I, L, R, pos; std::vector DW, W; public: Tree(int n): P(n, -2) {} template std::enable_if_t, void> add_edge(int u, int v) { es.emplace_back(u, v), es.emplace_back(v, u); } template std::enable_if_t, void> add_edge(int u, int v, T c) { es.emplace_back(u, v, c), es.emplace_back(v, u, c); } template , std::is_convertible>, std::nullptr_t> = nullptr> void add_edge(int u, int v, T c, U d) /* c:u->v, d:v->u */ { es.emplace_back(u, v, c), es.emplace_back(v, u, d); } void build(int root= 0) { size_t n= P.size(); I.resize(n), PP.resize(n), std::iota(PP.begin(), PP.end(), 0), D.assign(n, 0), L.assign(n, 0), R.assign(n, 0), pos.resize(n + 1), g.resize(es.size()); for (const auto &e: es) ++pos[std::get<0>(e)]; std::partial_sum(pos.begin(), pos.end(), pos.begin()); if constexpr (std::is_void_v) for (const auto &[f, t]: es) g[--pos[f]]= {t}; else for (const auto &[f, t, c]: es) g[--pos[f]]= {t, c}; auto f= [&, i= 0, v= 0, t= 0](int r) mutable { for (P[r]= -1, I[t++]= r; i < t; ++i) for (int u: operator[](v= I[i])) if (P[v] != u) P[I[t++]= u]= v; }; f(root); for (size_t r= 0; r < n; ++r) if (P[r] == -2) f(r); std::vector Z(n, 1), nx(n, -1); for (int i= n, v; i--;) { if (P[v= I[i]] == -1) continue; if (Z[P[v]]+= Z[v]; nx[P[v]] == -1) nx[P[v]]= v; if (Z[nx[P[v]]] < Z[v]) nx[P[v]]= v; } for (int v: I) if (nx[v] != -1) PP[nx[v]]= v; for (int v: I) if (P[v] != -1) PP[v]= PP[PP[v]], D[v]= D[P[v]] + 1; for (int i= n; i--;) L[I[i]]= i; for (int v: I) { int ir= R[v]= L[v] + Z[v]; for (int u: operator[](v)) if (u != P[v] && u != nx[v]) L[u]= ir-= Z[u]; if (nx[v] != -1) L[nx[v]]= L[v] + 1; } if constexpr (weight) { DW.resize(n), W.resize(n); for (int v: I) for (auto &[u, c]: operator[](v)) { if (u != P[v]) DW[u]= DW[v] + c; else W[v]= c; } } for (int i= n; i--;) I[L[i]]= i; } size_t size() const { return P.size(); } const ListRange operator[](int v) const { return {g.cbegin() + pos[v], g.cbegin() + pos[v + 1]}; } int depth(int v) const { return D[v]; } C depth_w(int v) const { static_assert(weight, "\'depth_w\' is not available"); return DW[v]; } int to_seq(int v) const { return L[v]; } int to_node(int i) const { return I[i]; } int parent(int v) const { return P[v]; } int root(int v) const { for (v= PP[v];; v= PP[P[v]]) if (P[v] == -1) return v; } bool connected(int u, int v) const { return root(u) == root(v); } int lca(int u, int v) const { for (;; v= P[PP[v]]) { if (L[u] > L[v]) std::swap(u, v); if (PP[u] == PP[v]) return u; } } int la(int v, int k) const { assert(k <= D[v]); for (int u;; k-= L[v] - L[u] + 1, v= P[u]) if (L[v] - k >= L[u= PP[v]]) return I[L[v] - k]; } int la_w(int v, C w) const { static_assert(weight, "\'la_w\' is not available"); for (C c;; w-= c) { int u= PP[v]; c= DW[v] - DW[u] + W[u]; if (w < c) { int ok= L[v], ng= L[u] - 1; while (ok - ng > 1) { if (int m= (ok + ng) / 2; DW[v] - DW[I[m]] <= w) ok= m; else ng= m; } return I[ok]; } if (v= P[u]; v == -1) return u; } } int jump(int u, int v, int k) const { if (!k) return u; if (u == v) return -1; if (k == 1) return in_subtree(v, u) ? la(v, D[v] - D[u] - 1) : P[u]; int w= lca(u, v), d_uw= D[u] - D[w], d_vw= D[v] - D[w]; return k > d_uw + d_vw ? -1 : k <= d_uw ? la(u, k) : la(v, d_uw + d_vw - k); } int jump_w(int u, int v, C w) const { static_assert(weight, "\'jump_w\' is not available"); if (u == v) return u; int z= lca(u, v); C d_uz= DW[u] - DW[z], d_vz= DW[v] - DW[z]; return w >= d_uz + d_vz ? v : w <= d_uz ? la_w(u, w) : la_w(v, d_uz + d_vz - w); } int dist(int u, int v) const { return D[u] + D[v] - D[lca(u, v)] * 2; } C dist_w(int u, int v) const { static_assert(weight, "\'dist_w\' is not available"); return DW[u] + DW[v] - DW[lca(u, v)] * 2; } // u is in v bool in_subtree(int u, int v) const { return L[v] <= L[u] && L[u] < R[v]; } int subtree_size(int v) const { return R[v] - L[v]; } // half-open interval std::array subtree(int v) const { return std::array{L[v], R[v]}; } // sequence of closed intervals template std::vector> path(int u, int v) const { std::vector> up, down; while (PP[u] != PP[v]) { if (L[u] < L[v]) down.emplace_back(std::array{L[PP[v]], L[v]}), v= P[PP[v]]; else up.emplace_back(std::array{L[u], L[PP[u]]}), u= P[PP[u]]; } if (L[u] < L[v]) down.emplace_back(std::array{L[u] + edge, L[v]}); else if (L[v] + edge <= L[u]) up.emplace_back(std::array{L[u], L[v] + edge}); return up.insert(up.end(), down.rbegin(), down.rend()), up; } }; template static constexpr bool tuple_like_v= false; template static constexpr bool tuple_like_v> = true; template static constexpr bool tuple_like_v> = true; template static constexpr bool tuple_like_v> = true; template auto to_tuple(const T &t) { if constexpr (tuple_like_v) return std::apply([](auto &&...x) { return std::make_tuple(x...); }, t); } template auto forward_tuple(const T &t) { if constexpr (tuple_like_v) return std::apply([](auto &&...x) { return std::forward_as_tuple(x...); }, t); } template static constexpr bool array_like_v= false; template static constexpr bool array_like_v> = true; template static constexpr bool array_like_v> = std::is_convertible_v; template static constexpr bool array_like_v> = true; template static constexpr bool array_like_v> = array_like_v> && std::is_convertible_v; template auto to_array(const T &t) { if constexpr (array_like_v) return std::apply([](auto &&...x) { return std::array{x...}; }, t); } template using to_tuple_t= decltype(to_tuple(T())); template using to_array_t= decltype(to_array(T())); template class SegmentTree_2D { public: using T= typename M::T; using Pos= std::array; std::vector xs; std::vector yxs; std::vector id, tol; std::vector val; template using canbe_Pos= std::is_convertible, std::tuple>; template using canbe_PosV= std::is_convertible, std::tuple>; template static constexpr bool canbe_Pos_and_T_v= std::conjunction_v, std::is_convertible>; int sz; inline int x2i(pos_t x) const { return std::lower_bound(xs.begin(), xs.end(), x) - xs.begin(); } inline int y2i(pos_t y) const { return std::lower_bound(yxs.begin(), yxs.end(), Pos{y, 0}, [](const Pos &a, const Pos &b) { return a[0] < b[0]; }) - yxs.begin(); } inline int xy2i(pos_t x, pos_t y) const { Pos p{y, x}; auto it= std::lower_bound(yxs.begin(), yxs.end(), p); return assert(p == *it), it - yxs.begin(); } template inline auto get_(const P &p) { if constexpr (z) return std::get(p); else return std::get(p.first); } template inline void build(const XYW *xyw, int n, const T &v= M::ti()) { xs.resize(n), yxs.resize(n); for (int i= n; i--;) xs[i]= get_(xyw[i]); std::sort(xs.begin(), xs.end()), xs.erase(std::unique(xs.begin(), xs.end()), xs.end()), id.resize((sz= 1 << (32 - __builtin_clz(xs.size()))) * 2 + 1); std::vector ix(n), ord(n); for (int i= n; i--;) ix[i]= x2i(get_(xyw[i])); for (int i: ix) for (i+= sz; i; i>>= 1) ++id[i + 1]; for (int i= 1, e= sz * 2; i < e; ++i) id[i + 1]+= id[i]; val.assign(id.back() * 2, M::ti()), tol.resize(id[sz] + 1), std::iota(ord.begin(), ord.end(), 0), std::sort(ord.begin(), ord.end(), [&](int i, int j) { return get_(xyw[i]) == get_(xyw[j]) ? get_(xyw[i]) < get_(xyw[j]) : get_(xyw[i]) < get_(xyw[j]); }); for (int i= n; i--;) yxs[i]= {get_(xyw[ord[i]]), get_(xyw[ord[i]])}; std::vector ptr= id; for (int r: ord) for (int i= ix[r] + sz, j= -1; i; j= i, i>>= 1) { int p= ptr[i]++; if constexpr (z) { if constexpr (std::tuple_size_v == 3) val[id[i + 1] + p]= std::get<2>(xyw[r]); else val[id[i + 1] + p]= v; } else val[id[i + 1] + p]= xyw[r].second; if (j != -1) tol[p + 1]= !(j & 1); } for (int i= 1, e= id[sz]; i < e; ++i) tol[i + 1]+= tol[i]; for (int i= 0, e= sz * 2; i < e; ++i) { auto dat= val.begin() + id[i] * 2; for (int j= id[i + 1] - id[i]; --j > 0;) dat[j]= M::op(dat[j * 2], dat[j * 2 + 1]); } } inline T fold(int i, int a, int b) const { int n= id[i + 1] - id[i]; T ret= M::ti(); auto dat= val.begin() + id[i] * 2; for (a+= n, b+= n; a < b; a>>= 1, b>>= 1) { if (a & 1) ret= M::op(ret, dat[a++]); if (b & 1) ret= M::op(dat[--b], ret); } return ret; } template inline void seti(int i, int j, T v) { auto dat= val.begin() + id[i] * 2; j+= id[i + 1] - id[i]; if constexpr (z) dat[j]= v; else dat[j]= M::op(dat[j], v); for (; j;) j>>= 1, dat[j]= M::op(dat[2 * j], dat[2 * j + 1]); } template inline void set_(pos_t x, pos_t y, T v) { for (int i= 1, p= xy2i(x, y);;) { if (seti(i, p - id[i], v); i >= sz) break; if (int lc= tol[p] - tol[id[i]], rc= (p - id[i]) - lc; tol[p + 1] - tol[p]) p= id[2 * i] + lc, i= 2 * i; else p= id[2 * i + 1] + rc, i= 2 * i + 1; } } public: template , canbe_PosV

>>> SegmentTree_2D(const P *p, size_t n) { build<1>(p, n); } template , canbe_PosV

>>> SegmentTree_2D(const std::vector

&p): SegmentTree_2D(p.data(), p.size()) {} template ::value>> SegmentTree_2D(const std::set

&p): SegmentTree_2D(std::vector(p.begin(), p.end())) {} template >> SegmentTree_2D(const P *p, size_t n, const U &v) { build<1>(p, n, v); } template >> SegmentTree_2D(const std::vector

&p, const U &v): SegmentTree_2D(p.data(), p.size(), v) {} template >> SegmentTree_2D(const std::set

&p, const U &v): SegmentTree_2D(std::vector(p.begin(), p.end()), v) {} template >> SegmentTree_2D(const std::pair *p, size_t n) { build<0>(p, n); } template >> SegmentTree_2D(const std::vector> &p): SegmentTree_2D(p.data(), p.size()) {} template >> SegmentTree_2D(const std::map &p): SegmentTree_2D(std::vector(p.begin(), p.end())) {} // [l,r) x [u,d) T fold(pos_t l, pos_t r, pos_t u, pos_t d) const { T ret= M::ti(); int L= x2i(l), R= x2i(r); auto dfs= [&](auto &dfs, int i, int a, int b, int c, int d) -> void { if (c == d || R <= a || b <= L) return; if (L <= a && b <= R) return ret= M::op(ret, fold(i, c, d)), void(); int m= (a + b) / 2, ac= tol[id[i] + c] - tol[id[i]], bc= c - ac, ad= tol[id[i] + d] - tol[id[i]], bd= d - ad; dfs(dfs, i * 2, a, m, ac, ad), dfs(dfs, i * 2 + 1, m, b, bc, bd); }; return dfs(dfs, 1, 0, sz, y2i(u), y2i(d)), ret; } void set(pos_t x, pos_t y, T v) { set_<1>(x, y, v); } void mul(pos_t x, pos_t y, T v) { set_<0>(x, y, v); } T get(pos_t x, pos_t y) const { return val[xy2i(x, y) + id[2]]; } }; using namespace std; struct RMQ { using T= int; static T ti() { return 0x7fffffff; } static T op(T a, T b) { return min(a, b); } }; signed main() { cin.tie(0); ios::sync_with_stdio(0); using Mint= ModInt<1000000007>; int N, M; cin >> N >> M; vector> es; vector used(M); UnionFind uf(N); Mint w= 1; Tree tree(N); for (int i= 0; i < M; ++i) { int A, B; cin >> A >> B, --A, --B; es.emplace_back(A, B); w+= w; if (uf.unite(A, B)) { used[i]= true; tree.add_edge(A, B, w); } } tree.build(); vector> xyw; for (int i= 0; i < M; ++i) { if (used[i]) continue; auto [A, B]= es[i]; int a= tree.to_seq(A), b= tree.to_seq(B); if (a > b) swap(a, b); xyw.push_back({a, b, i}); } SegmentTree_2D seg(xyw); int Q; cin >> Q; while (Q--) { int u, v, e; cin >> u >> v >> e, --u, --v, --e; auto [x, y]= es[e]; if (tree.parent(y) == x) swap(x, y); bool u_in= tree.in_subtree(u, x); if (!used[e] || u_in == tree.in_subtree(v, x)) { cout << tree.dist_w(u, v) << '\n'; continue; } auto [l, r]= tree.subtree(x); int i= min(seg.fold(0, l, l, r), seg.fold(l, r, r, N)); if (i > M) { cout << -1 << '\n'; continue; } auto [p, q]= es[i]; if (!u_in) swap(u, v); if (tree.in_subtree(q, x)) swap(p, q); cout << tree.dist_w(u, p) + tree.dist_w(v, q) + Mint(2).pow(i + 1) << '\n'; } return 0; }