#include <bits/stdc++.h>
using namespace std;
const int T = 100005;
int x, y, raod, kk, n, m, i, j, k, l, r;
int size[T], P[T], fix[T], a[T], b[T];
vector<int> g[T], V, G[T];
int B[10005][505], A[10005][505];
int get(int x) {
  if (P[x] == x)
    return x;
  else
    return (P[x] = get(P[x]));
}
void unite(int a, int b) {
  a = get(a);
  b = get(b);
  if (size[a] > size[b]) swap(a, b);
  P[a] = b;
}
int gett(int num, int x) {
  if (A[num][x] == x) return x;
  return (A[num][x] = gett(num, A[num][x]));
}
int gett2(int num, int x) {
  if (B[num][x] == x) return x;
  return (B[num][x] = gett2(num, B[num][x]));
}
int go(int l, int r) {
  int i, j;
  int ans = 0;
  for (j = 1; j <= n; j++) P[j] = j, size[j] = 1;
  for (j = 1; j <= n; j++) unite(j, A[l - 1][j]);
  for (j = 1; j <= n; j++) unite(j, B[r + 1][j]);
  for (j = 1; j <= n; j++)
    if (P[j] == j) ans++;
  return ans;
}
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) scanf("%d%d", &a[i], &b[i]);
  for (j = 1; j <= n; j++) {
    A[0][j] = j;
    B[m + 1][j] = j;
  }
  for (i = 1; i <= m; i++) {
    for (j = 1; j <= n; j++) A[i][j] = A[i - 1][j];
    x = gett(i, a[i]);
    y = gett(i, b[i]);
    if (x == y) continue;
    if (rand() & 1) swap(x, y);
    A[i][x] = y;
  }
  for (i = m; i >= 1; i--) {
    for (j = 1; j <= n; j++) B[i][j] = B[i + 1][j];
    x = gett2(i, a[i]);
    y = gett2(i, b[i]);
    if (x == y) continue;
    if (rand() & 1) swap(x, y);
    B[i][x] = y;
  }
  scanf("%d", &kk);
  while (kk--) scanf("%d%d", &l, &r), printf("%d\n", go(l, r));
}
