#include <bits/stdc++.h>
using namespace std;
struct UnionFind {
  vector<int> data;
  UnionFind(int size = 510) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
  void print() {
    for (int i = 1; i <= 6; i++) cout << root(i) << " ";
    cout << endl;
  }
};
UnionFind ufrtl[10001], ufltr[10001];
int edge[10001][2];
int main() {
  int n, m, l, r, q;
  cin >> n >> m;
  for (int i = 0; i < m; i++) cin >> edge[i][0] >> edge[i][1];
  for (int i = 0; i < m; i++) {
    ufltr[i + 1] = ufltr[i];
    ufltr[i + 1].unionSet(edge[i][0], edge[i][1]);
  }
  for (int i = m - 1; i >= 0; i--) {
    ufrtl[i] = ufrtl[i + 1];
    ufrtl[i].unionSet(edge[i][0], edge[i][1]);
  }
  cin >> q;
  while (q--) {
    cin >> l >> r;
    int ans = 0;
    UnionFind uf = ufltr[l - 1];
    for (int j = 1; j <= n; j++) uf.unionSet(j, ufrtl[r].root(j));
    for (int j = 1; j <= n; j++)
      if (uf.root(j) == j) ans++;
    cout << ans << endl;
  }
  return 0;
}
