#include <bits/stdc++.h>
using namespace std;
const int MAX = 100 * 100 + 5, MAXN = 505;
int n;
struct dsu {
  int parent[MAXN], sz[MAXN], cmp;
  vector<pair<int, int> > e;
  dsu() {
    cmp = n;
    for (int i = 0; i < MAXN; i++) {
      parent[i] = i;
      sz[i] = 1;
    }
  }
  int find(int x) {
    if (x == parent[x]) return x;
    int t = find(parent[x]);
    parent[x] = t;
    return t;
  }
  void merge(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (sz[x] < sz[y]) swap(x, y);
    e.push_back(make_pair(x, y));
    cmp--;
    sz[x] += sz[y];
    parent[y] = x;
  }
} dsu1[MAX], dsu2[MAX];
pair<int, int> p[MAX];
int main() {
  int m, k;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> p[i].first >> p[i].second;
    p[i].first--;
    p[i].second--;
  }
  for (int i = 1; i <= m; i++) {
    dsu1[i] = dsu1[i - 1];
    dsu1[i].merge(p[i - 1].first, p[i - 1].second);
    int j = m - i + 1;
    dsu2[j] = dsu2[j + 1];
    dsu2[j].merge(p[j - 1].first, p[j - 1].second);
  }
  cin >> k;
  for (int i = 0; i < k; i++) {
    int l, r;
    cin >> l >> r;
    dsu dsu3 = dsu1[l - 1];
    for (int i = 0; i < dsu2[r + 1].e.size(); i++)
      dsu3.merge(dsu2[r + 1].e[i].first, dsu2[r + 1].e[i].second);
    cout << dsu3.cmp + n << endl;
  }
  return 0;
}
