#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define iter(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++)
#define REP(p,a,b) for(int p=a;p<b;p++)
#define pb(f) push_back(f)
#define mkp(a,b) make_pair(a,b)
#define fst first
#define snd second
#define pii pair<int,int>
#define ins(a) insert(a)
#define maxV 100005
ll visited[maxV];
int parent[maxV];
ll longest;
vector<int> adj[maxV];
vector<int> adj1[maxV];
ll disc[maxV];
ll low[maxV];
ll best[maxV][2];
set<pii> ans;
ll cnt;
struct subset
{
int parent;
int rank;
}subsets[maxV];
void initialize()
{
memset(visited,0,sizeof(visited));
memset(parent,-1,sizeof(parent));
}
void initializer(int V)
{
REP(i,1,V+1)
{
subsets[i].parent=i;
subsets[i].rank=0;
}
}
void bridge(int u)
{
static ll time = 0;
visited[u] = 1;
disc[u] = low[u] = ++time;
iter(i,adj[u])
{
int v = *i;
if (!visited[v])
{
parent[v] = u;
bridge(v);
low[u]=min(low[u],low[v]);
if (low[v] > disc[u])
cnt++,ans.insert(mkp(u,v)),ans.insert(mkp(v,u));
}
else if (v != parent[u])
low[u]=min(low[u],disc[v]);
}
}
void dfs(int v)
{
visited[v]=1;
int n=adj1[v].size();
for (int i=0;i<n;i++)
{
int u=adj1[v][i];
if (visited[u])
continue;
dfs(u);
if (best[u][0]+1>best[v][0])
{
best[v][1]=best[v][0];
best[v][0]=best[u][0]+1;
}
else if (best[u][0]+1>best[v][1])
best[v][1]=best[u][0]+1;
}
longest=max(longest,best[v][0]+best[v][1]);
}
int find(int x)
{
if(subsets[x].parent!=x)
subsets[x].parent=find(subsets[x].parent);
return subsets[x].parent;
}
void Union(int x,int y)
{
int xroot=find(x);
int yroot=find(y);
if(subsets[xroot].rank>subsets[yroot].rank)
subsets[yroot].parent=xroot;
else if(subsets[xroot].rank<subsets[yroot].rank)
subsets[xroot].parent=yroot;
else
subsets[xroot].parent=yroot,subsets[yroot].rank++;
}
void DSU(int n)
{
REP(i,1,n+1)
{
REP(j,0,adj[i].size())
{
if(ans.find(pii(i,adj[i][j]))==ans.end())
{
int xroot=find(i);
int yroot=find(adj[i][j]);
if(xroot!=yroot)
Union(xroot,yroot);
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t,a,b,n,m;
cin>>t;
while(t--)
{
ll maxi=-1;
cnt=0;
cin>>n>>m;
REP(i,0,maxV)
adj[i].clear(),adj1[i].clear(),best[i][0]=best[i][1]=0;
ans.clear();
REP(i,0,m)
{
cin>>a>>b;
adj[a].pb(b);
adj[b].pb(a);
}
initialize();
REP(i,1,n+1)
if(!visited[i])
bridge(i);
initializer(n);
longest=0;
DSU(n);
iter(i,ans)
{
pii p;
p.fst=i->fst;
p.snd=i->snd;
if(find(p.fst)!=find(p.snd))
adj1[find(p.fst)].pb(find(p.snd));
}
memset(visited,0,sizeof(visited));
for (int i=1;i<=n;i++)
{
if (!visited[i])
dfs(i);
}
cout<<cnt-longest<<endl;
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIGxsIGxvbmcgbG9uZyBpbnQKI2RlZmluZSBpdGVyKGksYSkgZm9yKCB0eXBlb2YoYS5iZWdpbigpKSBpPWEuYmVnaW4oKTtpIT1hLmVuZCgpO2krKykKI2RlZmluZSBSRVAocCxhLGIpIGZvcihpbnQgcD1hO3A8YjtwKyspCiNkZWZpbmUgcGIoZikgcHVzaF9iYWNrKGYpCiNkZWZpbmUgbWtwKGEsYikgbWFrZV9wYWlyKGEsYikKI2RlZmluZSBmc3QgZmlyc3QKI2RlZmluZSBzbmQgc2Vjb25kCiNkZWZpbmUgcGlpIHBhaXI8aW50LGludD4KI2RlZmluZSBpbnMoYSkgaW5zZXJ0KGEpCiNkZWZpbmUgbWF4ViAxMDAwMDUKCmxsIHZpc2l0ZWRbbWF4Vl07CmludCBwYXJlbnRbbWF4Vl07CmxsIGxvbmdlc3Q7CnZlY3RvcjxpbnQ+IGFkalttYXhWXTsKdmVjdG9yPGludD4gYWRqMVttYXhWXTsKbGwgZGlzY1ttYXhWXTsKbGwgbG93W21heFZdOwpsbCBiZXN0W21heFZdWzJdOwpzZXQ8cGlpPiBhbnM7CmxsIGNudDsKCnN0cnVjdCBzdWJzZXQKewogICAgaW50IHBhcmVudDsKICAgIGludCByYW5rOwp9c3Vic2V0c1ttYXhWXTsKCnZvaWQgaW5pdGlhbGl6ZSgpCnsKICAgIG1lbXNldCh2aXNpdGVkLDAsc2l6ZW9mKHZpc2l0ZWQpKTsKICAgIG1lbXNldChwYXJlbnQsLTEsc2l6ZW9mKHBhcmVudCkpOwp9Cgp2b2lkIGluaXRpYWxpemVyKGludCBWKQp7CiAgICBSRVAoaSwxLFYrMSkKICAgIHsKICAgICAgICBzdWJzZXRzW2ldLnBhcmVudD1pOwogICAgICAgIHN1YnNldHNbaV0ucmFuaz0wOwogICAgfQp9Cgp2b2lkIGJyaWRnZShpbnQgdSkKewogICAgc3RhdGljIGxsIHRpbWUgPSAwOwogICAgdmlzaXRlZFt1XSA9IDE7CiAgICBkaXNjW3VdID0gbG93W3VdID0gKyt0aW1lOwogICAgaXRlcihpLGFkalt1XSkKICAgIHsKICAgICAgICBpbnQgdiA9ICppOwogICAgICAgIGlmICghdmlzaXRlZFt2XSkKICAgICAgICB7CiAgICAgICAgICAgIHBhcmVudFt2XSA9IHU7CiAgICAgICAgICAgIGJyaWRnZSh2KTsKICAgICAgICAgICAgbG93W3VdPW1pbihsb3dbdV0sbG93W3ZdKTsKICAgICAgICAgICAgaWYgKGxvd1t2XSA+IGRpc2NbdV0pCiAgICAgICAgICAgICAgY250KyssYW5zLmluc2VydChta3AodSx2KSksYW5zLmluc2VydChta3Aodix1KSk7CiAgICAgICAgfQogICAgICAgIGVsc2UgaWYgKHYgIT0gcGFyZW50W3VdKQogICAgICAgICAgICBsb3dbdV09bWluKGxvd1t1XSxkaXNjW3ZdKTsKICAgIH0KfQoKdm9pZCBkZnMoaW50IHY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4cm9vdF0ucGFyZW50PXlyb290LHN1YnNldHNbeXJvb3RdLnJhbmsrKzsKfQoKdm9pZCBEU1UoaW50IG4pCnsKICAgIFJFUChpLDEsbisxKQogICAgewogICAgICAgIFJFUChqLDAsYWRqW2ldLnNpemUoKSkKICAgICAgICB7CiAgICAgICAgICAgIGlmKGFucy5maW5kKHBpaShpLGFkaltpXVtqXSkpPT1hbnMuZW5kKCkpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGludCB4cm9vdD1maW5kKGkpOwogICAgICAgICAgICAgICAgaW50IHlyb290PWZpbmQoYWRqW2ldW2pdKTsKICAgICAgICAgICAgICAgIGlmKHhyb290IT15cm9vdCkKICAgICAgICAgICAgICAgICAgICBVbmlvbih4cm9vdCx5cm9vdCk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9Cn0KCmludCBtYWluKCkgewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBjaW4udGllKE5VTEwpOwogICAgaW50IHQsYSxiLG4sbTsKICAgIGNpbj4+dDsKICAgIHdoaWxlKHQtLSkKICAgIHsKICAgICAgICBsbCBtYXhpPS0xOwogICAgICAgIGNudD0wOwogICAgICAgIGNpbj4+bj4+bTsKICAgICAgICBSRVAoaSwwLG1heFYpCiAgICAgICAgICAgIGFkaltpXS5jbGVhcigpLGFkajFbaV0uY2xlYXIoKSxiZXN0W2ldWzBdPWJlc3RbaV1bMV09MDsKICAgICAgICBhbnMuY2xlYXIoKTsKICAgICAgICBSRVAoaSwwLG0pCiAgICAgICAgewogICAgICAgICAgICBjaW4+PmE+PmI7CiAgICAgICAgICAgIGFkalthXS5wYihiKTsKICAgICAgICAgICAgYWRqW2JdLnBiKGEpOwogICAgICAgIH0KICAgICAgICBpbml0aWFsaXplKCk7CiAgICAgICAgUkVQKGksMSxuKzEpCiAgICAgICAgaWYoIXZpc2l0ZWRbaV0pCiAgICAgICAgICAgIGJyaWRnZShpKTsKICAgICAgICBpbml0aWFsaXplcihuKTsKICAgICAgICBsb25nZXN0PTA7CiAgICAgICAgRFNVKG4pOwogICAgICAgIGl0ZXIoaSxhbnMpCiAgICAgICAgewogICAgICAgICAgICBwaWkgcDsKICAgICAgICAgICAgcC5mc3Q9aS0+ZnN0OwogICAgICAgICAgICBwLnNuZD1pLT5zbmQ7CiAgICAgICAgICAgIGlmKGZpbmQocC5mc3QpIT1maW5kKHAuc25kKSkKICAgICAgICAgICAgICAgIGFkajFbZmluZChwLmZzdCldLnBiKGZpbmQocC5zbmQpKTsKICAgICAgICB9CiAgICAgICAgbWVtc2V0KHZpc2l0ZWQsMCxzaXplb2YodmlzaXRlZCkpOwogICAgICAgIGZvciAoaW50IGk9MTtpPD1uO2krKykKICAgICAgICB7CiAgICAgICAgICAgIGlmICghdmlzaXRlZFtpXSkKICAgICAgICAgICAgICAgIGRmcyhpKTsKICAgICAgICB9CiAgICAgICAgY291dDw8Y250LWxvbmdlc3Q8PGVuZGw7CiAgICB9CiAgICByZXR1cm4gMDsKfQ==