#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e4 + 7, N = 5e2 + 7, MOD = 1e9 + 7, Mod = 1e9 + 6;
long long n, m, q, hx, hy, par[N], h[N], ans, l, r;
pair<int, int> a[maxN];
bool v1[maxN], v2[maxN], vis[N];
vector<int> e[N];
int find_par(int x) {
  if (par[x] != x) par[x] = find_par(par[x]);
  return par[x];
}
void merge(int x, int y) {
  if (h[x] <= h[y]) {
    par[x] = y;
    h[y] = max(h[y], h[x] + 1);
  } else
    par[y] = x, h[x] = max(h[x], h[y] + 1);
}
void dfs(int s) {
  vis[s] = true;
  for (int i = 0; i < e[s].size(); i++) {
    if (!vis[e[s][i]]) dfs(e[s][i]);
  }
}
void clear() {
  for (int i = 1; i <= n; i++) e[i].clear(), vis[i] = false;
}
int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) cin >> a[i].first >> a[i].second;
  for (int i = 1; i <= n; i++) par[i] = i, h[i] = 0;
  for (int i = 1; i <= m; i++) {
    hx = find_par(a[i].first);
    hy = find_par(a[i].second);
    if (hx != hy) merge(hx, hy), v1[i] = true;
  }
  for (int i = 1; i <= n; i++) par[i] = i, h[i] = 0;
  for (int i = m; i >= 1; i--) {
    hx = find_par(a[i].first);
    hy = find_par(a[i].second);
    if (hx != hy) merge(hx, hy), v2[i] = true;
  }
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> l >> r;
    l--;
    r++;
    clear();
    ans = 0;
    for (int j = 1; j <= l; j++) {
      if (v1[j])
        e[a[j].first].push_back(a[j].second),
            e[a[j].second].push_back(a[j].first);
    }
    for (int j = m; j >= r; j--) {
      if (v2[j])
        e[a[j].first].push_back(a[j].second),
            e[a[j].second].push_back(a[j].first);
    }
    for (int j = 1; j <= n; j++) {
      if (!vis[j]) dfs(j), ans++;
    }
    cout << ans << "\n";
  }
  return cout << '\n', 0;
}
