#include <bits/stdc++.h>
using namespace std;
struct UnionFind {
  vector<int> data;
  UnionFind() : data(502, -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)]; }
};
UnionFind v1[10002];
UnionFind v2[10002];
vector<pair<int, int> > v;
int main() {
  int n, m, x, t, y, k;
  scanf("%d%d", &n, &m);
  UnionFind u1, u2;
  v1[0] = u1;
  v2[0] = u2;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    v.push_back(make_pair(x - 1, y - 1));
    u1.unionSet(x - 1, y - 1);
    v1[i + 1] = u1;
  }
  for (int i = v.size() - 1; i >= 0; i--) {
    u2.unionSet(v[i].first, v[i].second);
    v2[v.size() - i] = u2;
  }
  cin >> k;
  while (k--) {
    scanf("%d%d", &x, &y);
    u1 = v1[x - 1];
    for (int i = 0; i < n; i++) {
      u1.unionSet(i, v2[m - y].root(i));
    }
    int ans = 0;
    for (int i = 0; i < n; i++) ans += (u1.root(i) == i);
    cout << ans << endl;
  }
  return 0;
}
