#include <stdio.h>

void road(int Froad[][1001],int A,int B,int *row);

int check(int Froad[][1001],int X,int Y,int row);

```
int main(void)
{
int T,N,M,A,B,X,Y,Q,i,j,row,col,Froad[110][1001];
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&M);
scanf("%d%d",&Froad[0][1],&Froad[0][2]);
Froad[0][0] = 2;
row = 1;
M -=1;
while(M--)
{
scanf("%d%d",&A,&B);
road(Froad,A,B,&row);
}
scanf("%d",&Q);
while(Q--)
{
scanf("%d%d",&X,&Y);
if(X==Y)
printf("YO\n");
else
if(check(Froad,X,Y,row)==1)
printf("YO\n");
else
printf("NO\n");
}
}
}
void road(int Froad[][1001],int A,int B,int *row)
{
int i,j;
for(i=0;i<*row;i++)
for(j=1;j<=Froad[i][0];j++)
{
if(A == Froad[i][j])
{
Froad[i][0] +=1;
Froad[i][Froad[i][0]] = B;
return ;
}
if(B == Froad[i][j])
{
Froad[i][0] +=1;
Froad[i][Froad[i][0]] = A;
return ;
}
}
Froad[*row][0] =2;
Froad[*row][1] = A;
Froad[*row][2] = B;
*row +=1;
return ;
}
int check(int Froad[][1001],int X,int Y,int row)
{
int flag,i,j;
for(i=0;i<row;i++)
{
flag =0;
for(j=1;j<=Froad[i][0];j++)
{
if(X == Froad[i][j]||Y == Froad[i][j])
{
if(flag==0)
flag=1;
else
return flag;
}
}
}
return flag=0;
}
```