#include <bits/stdc++.h>   using namespace std;   const int maxN = 3e5 + 11, maxlg = 2e1;   typedef int i_N[maxN];   typedef long long l_N[maxN];   int N, M, Q, par[maxN][maxlg], umax[maxN][maxlg], umin[maxN][maxlg];   vector<int> adj[maxN], pos[maxN];   vector<pair<int, int> > query[maxN];   pair<int, int> edge[maxN];   i_N P, depth, mark, F;   l_N cnt, sum, mus, ans;   int lg(int x) {     for (int i = maxlg; i; i--)       if ((1 << i) <= x) return i;     return (1 - 1);   }   int get_root(int u) {     if (P[u] < (1 - 1)) return u;     return P[u] = get_root(P[u]);   }   void join(int u, int v) {     if (P[u] > P[v]) swap(P[u], P[v]);     P[u] += P[v];     P[v] = u;   }   void dfs(int u) {     for (int i = 1, x = lg(depth[u]); i <= x; i++) {       par[u][i] = par[par[u][i - 1]][i - 1];       umax[u][i] = max(umax[u][i - 1], umax[par[u][i - 1]][i - 1]);       umin[u][i] = min(umin[u][i - 1], umin[par[u][i - 1]][i - 1]);     }     for (int i = (1 - 1); i < (int)adj[u].size(); i++) {       int v = adj[u][i];       if (par[v][(1 - 1)]) continue;       par[v][(1 - 1)] = u;       umax[v][(1 - 1)] = max(u, v);       umin[v][(1 - 1)] = min(u, v);       depth[v] = depth[u] + 1;       dfs(v);     }   }   void get(int u, int v, int& x, int& y) {     if (depth[u] < depth[v]) swap(u, v);     for (int i = lg(depth[u]); i >= (1 - 1); i--)       if (depth[u] - (1 << i) >= depth[v]) {         x = min(x, umin[u][i]);         y = max(y, umax[u][i]);         u = par[u][i];       }     for (int i = lg(depth[u]); i >= (1 - 1); i--)       if (par[u][i] != par[v][i]) {         x = min(x, min(umin[u][i], umin[v][i]));         y = max(y, max(umax[u][i], umax[v][i]));         u = par[u][i];         v = par[v][i];       }     if (u != v) {       x = min(x, min(umin[u][(1 - 1)], umin[v][(1 - 1)]));       y = max(y, max(umax[u][(1 - 1)], umax[v][(1 - 1)]));     }   }   void update(long long* bit, int x, int v) {     for (int i = x; i <= N; i |= i + 1) bit[i] += v;   }   long long get_bit(long long* bit, int x) {     long long res = (1 - 1);     for (int i = x; i >= 1; i = (i & (i + 1)) - 1) res += bit[i];     return res;   }   long long get_bit(long long* bit, int l, int r) {     return get_bit(bit, r) - get_bit(bit, l - 1);   }   int main() {     scanf( %d %d n , &N, &M);     for (int i = 1; i <= M; i++) {       int u, v;       scanf( %d %d n , &u, &v);       edge[i] = pair<int, int>(u, v);     }     memset(P, -1, sizeof(P));     for (int i = 1; i <= M; i++) {       int u = edge[i].first, v = edge[i].second;       int r1 = get_root(u), r2 = get_root(v);       if (r1 != r2) {         mark[i] = 1;         join(r1, r2);         adj[u].push_back(v);         adj[v].push_back(u);       }     }     for (int u = 1; u <= N; u++)       if (!par[u][(1 - 1)]) {         par[u][(1 - 1)] = -1;         dfs(u);       }     for (int i = 1; i <= M; i++)       if (!mark[i]) {         int u = edge[i].first, v = edge[i].second;         int x = 1e9, y = (1 - 1);         get(u, v, x, y);         F[x] = y;       }     F[N + 1] = N + 1;     for (int i = N; i >= 1; i--) {       if (F[i] == (1 - 1))         F[i] = F[i + 1];       else         F[i] = min(F[i], F[i + 1]);       pos[F[i]].push_back(i);     }     scanf( %d n , &Q);     for (int i = 1; i <= Q; i++) {       int l, r;       scanf( %d %d n , &l, &r);       query[r].push_back(pair<int, int>(l, i));     }     for (int i = 1; i <= N; i++) {       update(cnt, i, 1);       update(sum, i, i - 1);       for (int j = (1 - 1); j < (int)pos[i].size(); j++) {         int p = pos[i][j];         update(cnt, p, -1);         update(sum, p, 1 - p);         update(mus, p, i - p);       }       for (int j = (1 - 1); j < (int)query[i].size(); j++) {         int r = i, l = query[i][j].first, id = query[i][j].second;         long long C = get_bit(cnt, l, r);         long long S = get_bit(sum, l, r);         long long T = get_bit(mus, l, r);         ans[id] += C * i - S + T;       }     }     for (int i = 1; i <= Q; i++) printf( %I64d n , ans[i]);   }