#include <vector>
#include <iostream>
#include <cstdio>
using namespace std;
#define N 5000
vector<int> v[N];
int vis[N];
int step[N];
int ans;
void dfs(int x,int t)
{
vis[x]=1;
step[x]=t;
int i;
for(i=0;i<v[x].size();i++)
{
int k=v[x][i];
if(!vis[k])
dfs(k,t+1);
else
{
int temp=step[x]-step[k]+1;
if(temp>ans)ans=temp;
}
}
}
int main()
{
int ca;
scanf("%d",&ca);
int n,m;
while(ca--)
{
scanf("%d%d",&n,&m);
int i,j,k;
for(i=0;i<=n;i++)
{
vis[i]=0;
v[i].clear();
}
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
v[a].push_back(b);
v[b].push_back(a);
}
ans=0;
for(i=1;i<=n;i++)
{
if(!vis[i])
{
dfs(i,1);
}
}
if(ans>=3)
printf("%d\n",ans);
else
printf("0\n");
}
return 0;
}
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
int len;
#define MAX 4450
int head[MAX];
int step[MAX];
struct node
{
int v;
int next;
} E[MAX *MAX];
int num;
void init()
{
num = 0;
len = 0;//note
memset(head, -1, sizeof(head));
memset(step, -1, sizeof(step));
}
void add(int s, int t)
{
E[num].v = t;
E[num].next = head[s];
head[s] = num++;
}
void dfs(int cur, int pre)
{
int i;
for(i = head[cur]; i != -1; i = E[i].next)
{
int v = E[i].v;
if(v == pre)
continue;
if(step[v] == -1)
{
step[v] = step[cur] + 1;
dfs(v, cur);
}
else
{
if(step[cur] + 1 - step[v] > len)
len = step[cur] + 1 - step[v];
}
}
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, m;
scanf("%d%d", &n, &m);
init();
int i;
int st, ed;
for(i = 1; i <= m; i++)
{
scanf("%d%d", &st, &ed);
add(st, ed);
add(ed, st);
}
step[1] = 0;
dfs(1, 1);
printf("%d\n", len);
}
}