// Author: Grzegorz Herman // O(m lg m + q lg^2 m) #include #include #include #include #include using namespace std; struct node { static node null; node * parent; node * child[2]; bool flip; int weight; node * best; node() : parent(&null), child{ &null, &null }, flip(false), weight(0), best(this) { } node(node const &) = delete; void unflip() { if (!flip) return; child[0]->flip ^= true; child[1]->flip ^= true; swap(child[0], child[1]); flip = false; } void setchild(int d, node * newchild) { child[d] = newchild; newchild->parent = this; best = this; for (int d=0; d<2; ++d) if (child[d]->best->weight > best->weight) best = child[d]->best; } int rotate(node * & parent) { parent = this->parent; parent->unflip(); this->unflip(); for (int d=0; d<2; ++d) if (parent->child[d] == this) { node * grand = parent->parent; parent->setchild(d, this->child[1-d]); this->setchild(1-d, parent); for (int dd=0; dd<2; ++dd) if (grand->child[dd] == parent) grand->child[dd] = this; this->parent = grand; return d; } return -1; } void splay() { int lastd = -1; node * lastp; node * curp; while (true) { int d = rotate(curp); if (d < 0) break; if (d == lastd) { lastp->rotate(lastp); lastd = -1; } else lastd = d; lastp = curp; } } void access(node * newchild = &null) { splay(); setchild(1, newchild); if (parent != &null) parent->access(this); } void evert() { access(); splay(); flip = true; access(); } }; node node::null; struct edge : node { node * u; node * v; }; struct vft { vector>> data; vft(int m) : data(m, vector>(1, make_pair(-1,0))) { } void update(int t, int i, int v) { for (; i < (int)data.size(); i |= i+1) { int w = data[i].back().second + v; if (data[i].back().first == t) data[i].back().second = w; else data[i].emplace_back(t, w); } } int prefsum(int t, int i) const { int sum = 0; for (; i >= 0; i = (i&(i+1))-1) sum += (lower_bound(data[i].begin(), data[i].end(), make_pair(t+1, 0)) - 1)->second; return sum; } }; void testcase() { int n, m; cin >> n >> m; vector>> I(m); for (int i=0; i> I[i].second.first >> I[i].second.second >> I[i].first; sort(I.begin(), I.end(), greater>>()); node V[n]; edge E[m]; vft T(m), S(m); for (int i=0; iweight = I[i].first; e->u = &V[I[i].second.first-1]; e->v = &V[I[i].second.second-1]; e->u->evert(); e->v->evert(); if (e->u->parent != &node::null) { e->u->access(); edge * f = (edge *) e->v->best; f->evert(); f->u->evert(); f->parent = &node::null; f->v->evert(); f->parent = &node::null; //cerr << "remove weight " << f->weight << endl; T.update(i, f-E, -f->weight); S.update(i, f-E, -1); } e->u->evert(); e->u->parent = e; e->v->evert(); e->v->parent = e; //cerr << "add weight " << e->weight << endl; T.update(i, e-E, +e->weight); S.update(i, e-E, +1); } int q; int t = 0; for (cin >> q; q; --q) { int l, h; cin >> l >> h; l -= t; h -= t; l = upper_bound(I.begin(), I.end(), make_pair(l, make_pair(0, 0)), greater>>()) - I.begin(); h = upper_bound(I.begin(), I.end(), make_pair(h+1, make_pair(0, 0)), greater>>()) - I.begin(); t = T.prefsum(l-1, m-1) - T.prefsum(l-1, h-1); //int s = S.prefsum(l-1, m-1) - S.prefsum(l-1, h-1); // cout << n-s << " " << t << endl; cout << t << endl; } } int main() { int z; for (cin >> z; z; --z) testcase(); return 0; }