#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<stack>
#include<string>
using namespace std;
const int maxn=100010;
int dfn[maxn],low[maxn],Stack[maxn],head[maxn],vis[maxn],Dfn[maxn];
int bridge[maxn],father[maxn];
int cnt,index,e,n,m;
struct E
{
int from,to,next;
} edge[maxn*8];
void init()
{
index=cnt=e=0;
memset(vis,0,sizeof(vis));
memset(bridge,0,sizeof(bridge));
}
int cmp(int a,int b)
{
return a>b;
}
void Insert(int x,int y)
{
edge[e].from=x;
edge[e].to=y;
edge[e].next=head[x];
head[x]=e++;
}
void tarjan(int u,int father)
{
dfn[u]=low[u]=index++;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].to;
if(father==v)continue;
if(!dfn[v])
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>dfn[u])
{
bridge[cnt++]=i;
}
}
else low[u]=min(low[u],dfn[v]);
}
}
int main()
{
int i,j,x,y;
int t,sum;
char str[2][200];
scanf("%d",&t);
while(t--)
{
init();
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
sum=1;
map<string,int>mp;
map<int,string>mmp;
for(i=0;i<m;i++)
{
scanf("%s%s",str[0],str[1]);
if(mp[str[0]]==0)
{
mp[str[0]]=sum;
mmp[sum]=str[0];
sum++;
}
if(mp[str[1]]==0)
{
mp[str[1]]=sum;
mmp[sum]=str[1];
sum++;
}
x=mp[str[0]];
y=mp[str[1]];
Insert(x,y);
Insert(y,x);
}
memset(dfn,0,sizeof(dfn));
tarjan(1,-1);
printf("%d\n",cnt);
sort(bridge,bridge+cnt,cmp);
for(j=0; j<cnt; j++)
{
i=bridge[j];
i=i/2*2;
x=edge[i].from;
y=edge[i].to;
if(x!=y)
printf("%s %s\n",mmp[edge[i].from].c_str(),mmp[edge[i].to].c_str());
}
}
}