var
a,b,c,d:array[1..100002] of longint;
k,y,i,j,t,n,m,sum:integer;
begin
assign(input,'cowboys.in'); reset(input);
readln(t);
for y:=1 to t do begin
readln(n,m);
if (m=0) or (n=0) then begin
writeln(0);
end else begin
for i:=1 to m do
readln(a[i],b[i]);
for i:=1 to m do
if b[i]<a[i] then begin k:=a[i]; a[i]:=b[i]; b[i]:=k; end;
for i:=1 to n do c[i]:=0;
for i:=1 to n do begin
if c[i]=0 then begin
c[i]:=i;
for j:=1 to m do
if a[j]=i then c[b[j]]:=i;
end; end;
for i:=1 to m do
if c[a[i]]<c[b[i]] then c[b[i]]:=c[a[i]];
for i:=1 to m do
if c[b[i]]<c[a[i]] then c[a[i]]:=c[b[i]];
for i:=1 to n do
inc(d[c[i]]);
sum:=0;
for i:=1 to n do
if d[i]>1 then sum:=sum+1;
writeln(sum);
end; end;
end.

