// adam es eva eggyut.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <vector>
using namespace std;
struct adat
{
long long kezd, veg;
};
int n, i, A, E,a,b,j;
vector<pair<long long, long long>>megold;
vector<adat>eva,adam;
int main()
{
cin >> n;
cin >> E;
eva.resize(E + 1);
for (i = 1; i <= E; ++i)
{
cin >> eva[i].kezd >> eva[i].veg;
}
cin >> A;
adam.resize(A + 1);
for (i = 1; i <= A; ++i)
{
cin >> adam[i].kezd >> adam[i].veg;
}
i = 1;
j = 1;
while (i <= E && j <= A)
{
if (eva[i].veg <= adam[j].kezd) ++i;
else if (adam[j].veg <= eva[i].kezd) ++j;
else if (eva[i].kezd <= adam[j].kezd)
{
if (eva[i].veg >= adam[j].veg)
{
megold.push_back({ adam[j].kezd, adam[j].veg });
++j;
}
else
{
megold.push_back({ adam[j].kezd, eva[i].veg });
++i;
}
}
else
{
if (eva[i].veg <= adam[j].veg)
{
megold.push_back({ eva[i].kezd, eva[i].veg });
++i;
}
else
{
megold.push_back({ eva[i].kezd, adam[j].veg });
++j;
}
}
}
cout << megold.size() << "\n";
for (auto& e : megold)
{
cout << e.first << " " <<e.second<<"\n";
}
return 0;
}
/*
100
3
8 10
11 14
18 19
3
9 13
15 17
19 20
*/
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file