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

public class TestGen {

    final static Random rnd = new Random(213);

    public static void main(String[] args) throws Exception {
        new TestGen().generate();
    }

    int MAXT = 100;
    int MAXN = 500;
    int MAXM = 500;
    int MAXCOST = 1000_000;
    int MAXK = 100000;

    PrintWriter out;
    int countTests = 0;

    class Road {
        int cost;
        int fr, to;

        public Road(int cost, int fr, int to) {
            super();
            this.cost = cost;
            this.fr = fr;
            this.to = to;
        }

    }

    class Event {
        int where;
        int when;

        public Event(int where, int when) {
            super();
            this.where = where;
            this.when = when;
        }

    }

    class Test {
        int n, m, k;
        ArrayList<Road>[] g;
        Event[] events;

        Test(int n, int m, int k) {
            this.n = n;
            this.m = m;
            this.k = k;
            g = new ArrayList[n];
            for (int i = 0; i < n; i++) {
                g[i] = new ArrayList<>();
            }
            events = new Event[m];
        }
    }

    ArrayList<Test> genSample() {
        ArrayList<Test> res = new ArrayList<>();
		{
        Test t = new Test(2, 3, 1);
        t.events[0] = new Event(0, 10);
        t.events[1] = new Event(1, 18);
        t.events[2] = new Event(0, 20);
        t.g[0].add(new Road(5, 0, 1));
        t.g[1].add(new Road(5, 1, 0));
        res.add(t);
		}
		{
        Test t = new Test(2, 3, 2);
        t.events[0] = new Event(0, 10);
        t.events[1] = new Event(1, 18);
        t.events[2] = new Event(0, 20);
        t.g[0].add(new Road(5, 0, 1));
        t.g[1].add(new Road(5, 1, 0));
		t.g[0].add(new Road(1, 0, 1));
        t.g[1].add(new Road(1, 1, 0));
        res.add(t);
		}
        return res;
    }

    ArrayList<Test> genRandom(int testN, int n, int m, int k, int MAXTIME) {
        ArrayList<Test> res = new ArrayList<>();
        System.err.println(n);
        for (int i = 0; i < testN; i++) {
            Test t = new Test(n, m, k);
            for (int j = 0; j < m; j++) {
                t.events[j] = new Event(rnd.nextInt(n), rnd.nextInt(MAXTIME)+1);
            }
            for (int j = 0; j < k; j++) {
                int fr = rnd.nextInt(n);
                int to = rnd.nextInt(n);
                while (to == fr)
                    to = rnd.nextInt(n);
                int cost = 1 + rnd.nextInt(MAXTIME);
                t.g[fr].add(new Road(cost, fr, to));
                t.g[to].add(new Road(cost, to, fr));
            }
            res.add(t);
        }
        return res;
    }

    void generate() {
        printTest(genSample());
        printTest(genRandom(100, MAXN / 100, MAXM / 100, 10, 100));
        printTest(genRandom(100, MAXN / 100, MAXM / 100, 100, 100));
        printTest(genRandom(100, MAXN / 100, MAXM / 100, 1000, 100));
        printTest(genRandom(100, MAXN / 100, MAXM / 100, MAXK / 1000, MAXCOST));
        printTest(genRandom(100, MAXN / 100, MAXM / 100, MAXK / 100, 10));
        printTest(genRandom(10, MAXN / 10, MAXM / 10, MAXK / 10, 100));
        printTest(genRandom(3, MAXN / 3, MAXM / 3, MAXK / 3, 100));
        printTest(genRandom(1, MAXN / 1, MAXM / 1, MAXK / 1, 100));
        printTest(genRandom(1, MAXN / 1, MAXM / 1, MAXK / 1, MAXCOST));
        printTest(genRandom(1, MAXN / 1, MAXM / 1, MAXK / 1, MAXCOST / 10));
    }

    void printTest(ArrayList<Test> ts) {
        try {
            countTests++;
            String fileName = "../tests/";
            fileName += String.format("%02d", countTests);
            out = new PrintWriter(fileName);

            out.println(ts.size());
            for (Test t : ts) {
                out.println(t.n + " " + t.m + " " + t.k);
                for (int i = 0; i < t.m; i++) {
                    out.println(t.events[i].when + " " + (t.events[i].where+1));
                }
                for (int i = 0; i < t.n; i++)
                    for (Road r : t.g[i])
                        if (r.to > i)
                            out.println((i + 1) + " " + (r.to + 1) + " "
                                    + r.cost);
            }

            System.out.println("Done printing test #" + countTests);
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
