#include using namespace std; using lld = long long; #define st first #define nd second #define pb push_back typedef pair PII; typedef PII node_t; typedef int modify_t; node_t op_modify(node_t a, modify_t b) { return node_t(a.st+a.nd*b, a.nd); } modify_t op_buf(modify_t a, modify_t b) { return a+b; } node_t op_query(node_t a, node_t b) { return node_t(a.st+b.st, a.nd+b.nd); } const node_t NODE_INIT = node_t(0, 1); const modify_t MODIFY_NEUTRAL = 0; struct link_cut_tree { struct node { node *par, *child[2]; bool rev; modify_t buf; node_t val, res; }; vector _node; #define _left(x) x->child[0] #define _right(x) x->child[1] void init(int n) { // tworzy las n drzew jednowierzcholkowych _node.assign(n, node{NULL, {NULL, NULL}, 0, MODIFY_NEUTRAL, NODE_INIT, NODE_INIT}); } void link(int _a, int _b) { // ZALOZENIE: find_root(a) != find_root(b) node *a = &_node[_a], *b = &_node[_b]; _change_root(a); a->par = b; } void cut(int _a, int _b) { // ZALOZENIE: a-b musi byc istniejaca krawedzia node *a = &_node[_a], *b = &_node[_b]; _change_root(a); _access(b); _splay(b); _set_child(b, NULL, 0); a->par = NULL; } void modify(int _a, int _b, modify_t v) { // ZALOZENIE: find_root(a) == find_root(b) node *a = &_node[_a], *b = &_node[_b]; _change_root(a); _access(b); _splay(b); b->buf = op_buf(b->buf, v); } node_t query(int _a, int _b) { // ZALOZENIE: find_root(a) == find_root(b) node *a = &_node[_a], *b = &_node[_b]; _change_root(a); _access(b); _splay(b); return b->res; } int find_root(int _x) { node *x = &_node[_x]; _access(x); _splay(x); while(_left(x)) x = _left(x); _splay(x); return (int)(x-_node.data()); } void _change_root(node *x) { _access(x); _splay(x); x->rev ^= 1; } void _access(node *x) { for(node *cur=NULL;x;x=x->par) { _splay(x); _set_child(x, cur, 1); cur = x; } } bool _is_root(node *x) { return !x->par || (_right(x->par) != x && _left(x->par) != x); } void _push(node *x) { if(_left(x)) { _left(x)->rev ^= x->rev; _left(x)->buf = op_buf(_left(x)->buf, x->buf); } if(_right(x)) { _right(x)->rev ^= x->rev; _right(x)->buf = op_buf(_right(x)->buf, x->buf); } if(x->rev) { swap(_left(x), _right(x)); x->rev = 0; } x->val = op_modify(x->val, x->buf); x->buf = MODIFY_NEUTRAL; } void _update(node *x) { x->res = x->val; if(_left(x)) x->res = op_query(x->res, op_modify(_left(x)->res, _left(x)->buf)); if(_right(x)) x->res = op_query(x->res, op_modify(_right(x)->res, _right(x)->buf)); } void _set_child(node *x, node *c, int t) { _push(x); x->child[t] = c; if(c) c->par = x; _update(x); } #define _is_left(x) (x == _left(x->par)) void _rotate(node *x) { node *p = x->par; x->par = p->par; if(!_is_root(p)) _set_child(p->par, x, !_is_left(p)); int f = (x == _left(p)); _set_child(p, x->child[f], !f); _set_child(x, p, f); } void _splay(node *x) { static vector r; node *p; for(p=x;!_is_root(p);p=p->par) r.pb(p); for(r.pb(p);!r.empty();r.pop_back()) _push(r.back()); for(;!_is_root(x);_rotate(x)) if(!_is_root(x->par) && _is_left(x) == _is_left(x->par)) _rotate(x->par); } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); link_cut_tree tree; int n,m; cin >> n >> m; tree.init(n); vector e(m); for(auto& i : e) cin >> i.first >> i.second, i.first--, i.second--; vector res(m+1,0); for(int i=0,j=0;i pref(m+1,0); for(int i=1;i<=m;i++) pref[i] = pref[i-1] + res[i]-i+1; int q; cin >> q; while(q--) { int b,e; cin >> b >> e; int x = upper_bound(res.begin()+b,res.begin()+e+1,e)-res.begin()-1; cout << pref[x]-pref[b-1]+(lld) (e-x)*(lld) (e-x+1) / 2ll << "\n"; } return 0; }