#include <bits/stdc++.h>
using namespace std;
const int N = 10000 + 5, LOG = 9 + 1;
struct DSU {
 public:
  int n = 500;
  vector<int> rep, sz;
  DSU() {
    rep.resize(n + 1), sz.resize(n + 1);
    for (int i = 1; i <= n; ++i) rep[i] = i, sz[i] = 1;
  }
  int find(int x) {
    if (x == rep[x]) return x;
    return rep[x] = find(rep[x]);
  }
  void unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y) return;
    if (sz[x] < sz[y]) swap(x, y);
    sz[x] += sz[y];
    rep[y] = x;
  }
} L[N], R[N];
pair<int, int> e[N];
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    cin >> e[i].first >> e[i].second;
  }
  for (int i = 1; i <= m; ++i)
    L[i] = L[i - 1], L[i].unite(e[i].first, e[i].second);
  for (int i = m; i >= 1; --i)
    R[i] = R[i + 1], R[i].unite(e[i].first, e[i].second);
  int k;
  cin >> k;
  while (k--) {
    int l, r;
    cin >> l >> r;
    DSU res = L[l - 1];
    for (int i = 1; i <= n; ++i)
      if (R[r + 1].find(i) != i) res.unite(i, R[r + 1].find(i));
    int nb = 0;
    for (int i = 1; i <= n; ++i) nb += (res.find(i) == i);
    cout << nb << '\n';
  }
}
