import java.io.*;
import java.util.*;

public class TestGen {

    static PrintWriter out;
    static Random rnd = new Random(645624);
    static int tNumber;
    static void printTest(int n, int m, int l, int r, ArrayList<Integer> a, ArrayList<Integer> b) throws IOException {
        if (tNumber < 10)
            out = new PrintWriter("../tests/0" + tNumber);
        else
            out = new PrintWriter("../tests/" + tNumber);
        tNumber++;               
        out.println(n + " " + m);
        out.println(l + " " + r);
        for (int i = 0; i < a.size(); i+=2)
            out.println(a.get(i) + " " + a.get(i + 1));

        for (int i = 0; i < b.size(); i+=2)
            out.println(b.get(i) + " " + b.get(i + 1));

        out.close();
    }
    static void genSample() throws FileNotFoundException {
        out = new PrintWriter("../tests/01");
        out.println("3 2\r\n0 5\r\n0 1\r\n2 3\r\n4 5\r\n0 1\r\n2 3");
        out.close();
        out = new PrintWriter("../tests/02");
        out.println("2 3\r\n0 7\r\n1 3\r\n6 7\r\n1 2\r\n3 5\r\n6 7");
        out.close();
    }
    static int randFromTo(int l, int r) {
        return rnd.nextInt(r - l + 1) + l;      
    }
    static ArrayList<Integer> genSegs(int n, int maxD) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        int now = randFromTo(-maxD, maxD);
        list.add(now);
        for (int i = 0; i < 2 * n - 1; i++)
            list.add(randFromTo(list.get(list.size() - 1) + 1, list.get(list.size() - 1) + maxD));
        return list;                
    }

	static final int MX = (int)1e9;

	static void genMaxTest1() throws IOException {
	 	int n = 1, m = 1, l = -MX, r = MX;
		ArrayList<Integer> a = new ArrayList<Integer>();
		ArrayList<Integer> b = new ArrayList<Integer>();
		a.add(-MX);
		a.add(MX);
		b.add(-MX);
		b.add(MX);
		printTest(n, m, l, r, a, b);
	}

	static void genMaxTest2() throws IOException {
	 	int n = 300, m = 300, l = -MX, r = MX;
		ArrayList<Integer> a = new ArrayList<Integer>();
		ArrayList<Integer> b = new ArrayList<Integer>();
		int len = (2 * MX) / 600;
		int cur = -MX;
		for (int i = 0; i < n; i++) {
		 	a.add(cur);
			a.add(cur + len);
			b.add(cur);
			b.add(cur + len);
			cur += 2 * len;
		}
		printTest(n, m, l, r, a, b);
	}

    public static void main(String[] args) throws IOException {
        genSample();
        tNumber = 3;
        for (int i = 0; i < 5; i++) {
            int n = randFromTo(5, 10);
            int m = randFromTo(5, 10);
            ArrayList<Integer> a = genSegs(n, randFromTo(1, 15));
            ArrayList<Integer> b = genSegs(m, randFromTo(1, 15));
            int l = Math.min(a.get(0), b.get(0));
            int r = Math.max(a.get(a.size() - 1), b.get(b.size() - 1));
            if (randFromTo(0, 4) != 0) {
                l -= randFromTo(0, 30);
                r += randFromTo(0, 30);         
            }
            printTest(n, m, l, r, a, b);
        }
        {
            int n = randFromTo(20, 50);
            int m = randFromTo(20, 50);
            ArrayList<Integer> a = genSegs(n, randFromTo(1, 100));
            ArrayList<Integer> b = genSegs(m, randFromTo(1, 100));
            int l = -1000000000;
            int r = Math.max(a.get(a.size() - 1), b.get(b.size() - 1));
            printTest(n, m, l, r, a, b);
        }
        {
            int n = randFromTo(20, 50);
            int m = randFromTo(20, 50);
            ArrayList<Integer> a = genSegs(n, randFromTo(1, 1000));
            ArrayList<Integer> b = genSegs(m, randFromTo(1, 1000));
            int l = Math.min(a.get(0), b.get(0));
            int r = 1000000000;
            printTest(n, m, l, r, a, b);
        }
        for (int i = 0; i < 10; i++) {
            int n = randFromTo(70, 100);
            int m = randFromTo(70, 100);
            ArrayList<Integer> a = genSegs(n, randFromTo(1, 1000000));
            ArrayList<Integer> b = genSegs(m, randFromTo(1, 1000000));
            int l = Math.min(a.get(0), b.get(0));
            int r = Math.max(a.get(a.size() - 1), b.get(b.size() - 1));
            if (randFromTo(0, 4) != 0) {
                l -= randFromTo(0, 1000000);
                r += randFromTo(0, 1000000);            
            }
            printTest(n, m, l, r, a, b);
        }

        for (int i = 0; i < 5; i++) {
            int n = randFromTo(100, 200);
            int m = randFromTo(100, 200);
            ArrayList<Integer> a = genSegs(n, randFromTo(1, 1000000));
            ArrayList<Integer> b = genSegs(m, randFromTo(1, 1000000));
            int l = Math.min(a.get(0), b.get(0));
            int r = Math.max(a.get(a.size() - 1), b.get(b.size() - 1));
            if (randFromTo(0, 4) != 0) {
                l -= randFromTo(0, 1000000);
                r += randFromTo(0, 1000000);            
            }
            printTest(n, m, l, r, a, b);
        }

        for (int i = 0; i < 15; i++) {
            int n = randFromTo(250, 300);
            int m = randFromTo(250, 300);
            ArrayList<Integer> a = genSegs(n, randFromTo(1, 1000000));
            ArrayList<Integer> b = genSegs(m, randFromTo(1, 1000000));
            int l = Math.min(a.get(0), b.get(0));
            int r = Math.max(a.get(a.size() - 1), b.get(b.size() - 1));
            if (randFromTo(0, 4) != 0) {
                l -= randFromTo(0, 1000000);
                r += randFromTo(0, 1000000);            
            }
            printTest(n, m, l, r, a, b);
        }

        for (int i = 0; i < 10; i++) {
            int n = randFromTo(200, 300);
            int m = randFromTo(200, 300);
            ArrayList<Integer> a = genSegs(n, randFromTo(1, 1000));
            ArrayList<Integer> b = genSegs(m, randFromTo(1, 1000));
            int l = Math.min(a.get(0), b.get(0));
            int r = Math.max(a.get(a.size() - 1), b.get(b.size() - 1));
            if (randFromTo(0, 4) != 0) {
                l -= randFromTo(0, 100);
                r += randFromTo(0, 100);            
            }
            printTest(n, m, l, r, a, b);
        }

		genMaxTest1();
		genMaxTest2();
    }
}
