Bananas are the favoured food of monkeys.
In the forest, there is a Banana Company that provides bananas from different places.
The company has two lists.
The first list records the types of bananas preferred by different monkeys, and the second one records the types of bananas from different places.
Now, the supplier wants to know, whether a monkey can accept at least one type of bananas from a place.
Remenber that, there could be more than one types of bananas from a place, and there also could be more than one types of bananas of a monkey's preference.
Input Format
The first line contains an integer TTT, indicating that there are TTT test cases.
For each test case, the first line contains two integers NNN and MMM, representing the length of the first and the second lists respectively.
In the each line of following NNN lines, two positive integers i,ji, ji,j indicate that the iii-th monkey favours the jjj-th type of banana.
In the each line of following MMM lines, two positive integers j,kj, kj,k indicate that the jjj-th type of banana could be find in the kkk-th place.
All integers of the input are less than or equal to 505050.
Output Format
For each test case, output all the pairs x,yx, yx,y that the xxx-the monkey can accept at least one type of bananas from the yyy-th place.
These pairs should be outputted as ascending order. That is say that a pair of x,yx, yx,y which owns a smaller xxx should be output first.
If two pairs own the same xxx, output the one who has a smaller yyy first.
And there should be an empty line after each test case.
样例输入
1 6 4 1 1 1 2 2 1 2 3 3 3 4 1 1 1 1 3 2 2 3 3
样例输出
1 1 1 2 1 3 2 1 2 3 3 3 4 1 4 3
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct mtb
{
int mnum;
int bnum;
}mtb;
mtb a[55];
typedef struct btr{
int bnum;
int rnum;
}btr;
btr b[55];
typedef struct M{
int mnum;
int rnum;
}M;
M F[1100];
int cmp(M p1, M p2)
{
if (p1.mnum < p2.mnum)
{
return 1;
}
if (p1.mnum == p2.mnum && p1.rnum < p2.rnum)
{
return 1;
}
return 0;
}
int x=0;
int main()
{
int T;
int list1,list2;
cin>>T;
while(T--)
{
cin>>list1>>list2;
for(int i=0;i<list1;i++)
{
cin>>a[i].mnum;
cin>>a[i].bnum;
}
for(int j=0;j<list2;j++)
{
cin>>b[j].bnum;
cin>>b[j].rnum;
}
for(int k=0;k<list1;k++)
{
for(int q=0;q<list2;q++)
{
if(a[k].bnum == b[q].bnum)
{
F[x].mnum=a[k].mnum;
F[x].rnum=b[q].rnum;
x++;
}
}
}
sort(F,F+x,cmp);
for(int t=1;t<=x;t++){
if(!(F[t].mnum==F[t-1].mnum && F[t].rnum==F[t-1].rnum))
cout<<F[t-1].mnum<<" "<<F[t-1].rnum<<endl;
}
cout<<endl;
x=0;
}
}