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

public class TestsGen implements Runnable {
	public static void main(String[] args) {
		new TestsGen().run();
	}

	public int done = 0;
	ArrayList<String> data = new ArrayList<String>();

	final int LARGE = 200;
	final int SMALL = LARGE / 10;
	final int MAXDELTA = 10;
	final int MAXRES = 1000;
	final long MINIP = 167772161;
	final int MAXNAMELEN = 20;

	public class Test {
		int n;
		int m;
		HashSet<String> usedNames;
		int[] leader;
		long lastIP;
		HashMap<String, Contestant> users;
		ArrayList<Contest> contests;
		ArrayList<String> names;

		public Test(int maxN, int maxM) {
			n = nextInt(maxN / 2, maxN);
			leader = new int[n];
			names = new ArrayList<String>();
			m = nextInt(maxM / 2, maxM);
			usedNames = new HashSet<String>();
			lastIP = MINIP;
			users = new HashMap<String, TestsGen.Test.Contestant>();
			contests = new ArrayList<TestsGen.Test.Contest>();
		}

		public String getNewName() {
			String res = "";
			do {
				int len = nextInt(1, MAXNAMELEN);
				for (int i = 0; i < len; i++) {
					res += (char) ('a' + nextInt(0, 25));
				}
			} while (usedNames.contains(res));
			usedNames.add(res);
			return res;
		}

		public class Contestant {
			String name;
			long ip;
			int rating;
			int finalRating;

			public Contestant() {
				name = getNewName();
				ip = lastIP;
				lastIP++;
				rating = nextInt(0, MAXDELTA);
				finalRating = rating;
			}

			public void update(int delta) {
				finalRating += delta;
				finalRating = Math.min(MAXDELTA, finalRating);
				finalRating = Math.max(0, finalRating);
			}
		}

		public class Result {
			String name;
			int delta;
			int currentResult;
			boolean isDouble;

			public Result(String name) {
				this.name = name;
				isDouble = false;
				this.delta = nextInt(-MAXDELTA, MAXDELTA);
				this.currentResult = nextInt(0, MAXRES);
			}
		}

		public class Contest {
			int k;
			ArrayList<Result> results;

			public Contest() {
				k = 0;
				results = new ArrayList<TestsGen.Test.Result>();
			}

			public boolean contains(String name) {
				for (int i = 0; i < k; i++) {
					if (results.get(i).name.equals(name)) {
						return true;
					}
				}
				return false;
			}

			public void insert(String name, int finalRating, int finalLevel) {
				Result nr = new Result(name);
				nr.isDouble = true;
				nr.delta = finalRating - users.get(name).rating;
				int low = 0;
				boolean highFreeCount = true;
				int high = MAXRES;
				// !!!!
				for (int i = 0; i < k; i++) {
					if (users.get(results.get(i).name).finalRating < finalLevel) {
						low = Math.max(low, results.get(i).currentResult);
					} else if (users.get(results.get(i).name).finalRating == finalLevel) {
						if (highFreeCount) {
							high = Math.max(high, results.get(i).currentResult);
						}
						if (results.get(i).isDouble) {
							high = Math.min(high, results.get(i).currentResult);
							highFreeCount = false;
						}
					}
				}
				if (high - 1 <= low) {
					for (int i = 0; i < k; i++) {
						if (users.get(results.get(i).name).finalRating == finalLevel
								&& !results.get(i).isDouble) {
							results.get(i).currentResult = low + 2;
							high = low + 2;
							break;
						}
					}
				}
				nr.currentResult = high - 1;
				this.k++;
				results.add(nr);
			}
		}

		public int not(int rating) {
			int val = nextInt(0, MAXDELTA);
			while (val == rating) {
				val = nextInt(0, MAXDELTA);
			}
			return val;
		}

		public void lift(String name) {
			for (int i = m - 1; i >= 0; i--) {
				if (contests.get(i).contains(name)) {
					for (int j = 0; j < contests.get(i).k; j++) {
						if (contests.get(i).results.get(j).name.equals(name)) {
							contests.get(i).results.get(j).delta = MAXDELTA;
						}
					}
				}
			}
		}

		public void insert(String doubleName, String origName, int finalRating,
				int finalLevel) {
			String toSeek = origName;
			if (origName.charAt(0) == '!') {
				toSeek = origName.substring(1);
				for (int i = m - 1; i >= 0; i--) {
					if (contests.get(i).contains(toSeek)) {
						contests.get(i).insert(doubleName, finalRating,
								finalLevel);
						break;
					}
				}
			} else {
				for (int i = m - 1; i >= 0; i--) {
					if (!contests.get(i).contains(toSeek)) {
						contests.get(i).insert(doubleName, finalRating,
								finalLevel);
						break;
					}
				}
			}

		}

		public void createDouble(String name, int code) {
			Contestant other = new Contestant();
			if (code != 3) {
				other.ip = users.get(name).ip;
			}
			n++;
			users.put(other.name, other);
			names.add(other.name);
			if (users.get(name).finalRating == 0) {
				lift(name);
			}
			if (code == 2) {
				insert(other.name, name, users.get(name).finalRating,
						users.get(name).finalRating);
			} else {
				if (code == 1) {
					insert(other.name, '!' + name,
							users.get(name).finalRating - 1,
							users.get(name).finalRating);
				} else if (code == 4) {
					insert(other.name, name, users.get(name).finalRating - 1,
							not(users.get(name).finalRating));
				} else {
					insert(other.name, name, users.get(name).finalRating - 1,
							users.get(name).finalRating);
				}
			}
		}

		public int get(int val) {
			if (leader[val] == val) {
				return val;
			}
			int ans = get(leader[val]);
			leader[val] = ans;
			return ans;
		}

		public void setOneIP() {
			for (int i = 0; i < n; i++) {
				users.get(names.get(i)).ip = lastIP;
			}
		}

		public void makeItPlenty() {
			if (m != LARGE) {
				m++;
				Contest nc = new Contest();
				nc.k = n / 2;
				for (int i = 0; i < n / 2; i++) {
					Result nr = new Result(names.get(i));
					nr.delta = -10;
					nc.results.add(nr);
				}
				contests.add(nc);
			}
		}

		public void genMax() {
			n = LARGE;
			m = LARGE;
			for (int i = 0; i < n - 2; i++) {
				Contestant nc = new Contestant();
				nc.rating = 1;
				names.add(nc.name);
				users.put(nc.name, nc);
			}
			Contestant nc = new Contestant();
			nc.name = "zero";
			nc.rating = 0;
			names.add(nc.name);
			users.put(nc.name, nc);

			nc = new Contestant();
			nc.name = "deus";
			nc.rating = 1;
			names.add(nc.name);
			users.put(nc.name, nc);

			Contest first = new Contest();
			first.k = 2;
			Result zeroRes = new Result("zero");
			zeroRes.delta = 0;
			zeroRes.currentResult = 10;
			first.results.add(zeroRes);

			Result deusRes = new Result("deus");
			deusRes.delta = 0;
			deusRes.currentResult = 100;
			first.results.add(deusRes);

			Contest second = new Contest();
			second.k = LARGE - 2;
			for (int i = 0; i < n - 2; i++) {
				Result nr = new Result(names.get(i));
				nr.delta = 0;
				second.results.add(nr);
			}

			for (int i = 0; i < LARGE / 2; i++) {
				contests.add(first);
				contests.add(second);
			}
			this.setOneIP();
		}

		public void genMax2(int or) {
			n = LARGE;
			m = LARGE;
			for (int i = 0; i < n - 1; i++) {
				Contestant nc = new Contestant();
				nc.rating = 1;
				names.add(nc.name);
				users.put(nc.name, nc);
			}

			Contestant nc = new Contestant();
			nc.name = "deus";
			nc.rating = or;
			names.add(nc.name);
			users.put(nc.name, nc);

			for (int i = 0; i < (n - 1) / 2; i++) {
				users.get(names.get(i)).rating = 0;
			}

			Contest first = new Contest();
			first.k = (n - 1) / 2 + 1;
			Result deusRes = new Result("deus");
			deusRes.delta = 0;
			deusRes.currentResult = 100;
			first.results.add(deusRes);
			for (int i = 0; i < (n - 1) / 2; i++) {
				Result nr = new Result(names.get(i));
				nr.delta = 0;
				nr.currentResult = 10;
				first.results.add(nr);
			}

			Contest second = new Contest();
			second.k = LARGE - (n - 1) / 2;
			for (int i = (n - 1) / 2; i < n; i++) {
				Result nr = new Result(names.get(i));
				nr.delta = 0;
				second.results.add(nr);
			}

			for (int i = 0; i < LARGE / 2; i++) {
				contests.add(first);
				contests.add(second);
			}
			this.setOneIP();
		}

		public void makeRandomTest(int maxN) {

			for (int i = 0; i < n; i++) {
				Contestant nc = new Contestant();
				names.add(nc.name);
				users.put(nc.name, nc);
			}
			for (int i = 0; i < m; i++) {
				Contest randomContest = new Contest();
				int k = nextInt(n / 3, 2 * n / 3);
				randomContest.k = k;
				int left = n;
				for (int j = 0; j < n; j++) {
					leader[j] = j;
				}
				for (int j = 0; j < k; j++) {
					int pos = nextInt(0, left - 1);
					pos = get(pos);
					if (pos != n - 1) {
						leader[pos] = pos + 1;
					} else {
						leader[pos] = 0;
					}
					left--;
					randomContest.results.add(new TestsGen.Test.Result(names
							.get(pos)));
				}
				contests.add(randomContest);
			}
			for (int i = 0; i < m; i++) {
				for (int j = 0; j < contests.get(i).k; j++) {
					users.get(contests.get(i).results.get(j).name).update(
							contests.get(i).results.get(j).delta);
				}
			}
			for (int i = 0; i < (maxN - n); i++) {
				int sus = nextInt(0, n - 1);
				int code = 0;
				// if (nextInt(0, 2) == 0) {
				code = nextInt(0, 4);
				// }
				createDouble(names.get(sus), code);
			}
		}

		public void dump() {
			data.add("" + n + " " + m);
			for (int i = 0; i < n; i++) {
				data.add(names.get(i) + " " + users.get(names.get(i)).rating);
			}
			for (int i = 0; i < m; i++) {
				data.add("" + contests.get(i).k);
				for (int j = 0; j < contests.get(i).k; j++) {
					Result curRes = contests.get(i).results.get(j);
					curRes.delta = Math.min(curRes.delta, MAXDELTA);
					curRes.delta = Math.max(curRes.delta, -MAXDELTA);
					data.add(curRes.name + " " + curRes.currentResult + " "
							+ curRes.delta + " "
							+ getNormalIP(users.get(curRes.name).ip));
				}
			}
		}

		public String getNormalIP(long a) {
			String ans = "";
			for (int i = 0; i < 4; i++) {
				ans = a % 256 + ans;
				a /= 256;
				if (i != 3) {
					ans = '.' + ans;
				}
			}
			return ans;
		}
	}

	public void print() throws IOException {
		done++;
		System.err.println(done);
		PrintWriter out;
		if (done < 10)
			out = new PrintWriter("0" + String.valueOf(done));
		else
			out = new PrintWriter(String.valueOf(done));

		for (int i = 0; i < data.size(); i++)
			out.println(data.get(i));

		out.close();
		data.clear();
	}

	public int nextInt(int low, int high) {
		int base = high - low + 1;
		int val = rnd.nextInt();
		val %= base;
		while (val < 0)
			val += base;
		val += low;
		return val;
	}

	Random rnd;

	public void genSample() {
		data.add("3 2");
		data.add("Petya 1");
		data.add("Vasya 2");
		data.add("Gena 3");
		data.add("2");
		data.add("Vasya 2 -2 10.0.0.1");
		data.add("Petya 10 2 10.0.0.2");
		data.add("2");
		data.add("Gena 100 0 10.0.0.1");
		data.add("Petya 10 0 10.0.0.2");
	}

	public void genNarrow1() {
		data.add("2 1");
		data.add("Petya 10");
		data.add("Gena 3");
		data.add("1");
		data.add("Gena 1000 0 10.0.0.1");
	}

	public void genNarrow2() {
		data.add("2 2");
		data.add("Petya 10");
		data.add("Gena 3");
		data.add("1");
		data.add("Gena 1000 0 10.0.0.1");
		data.add("1");
		data.add("Petya 1000 0 10.0.0.1");
	}

	public void run() {
		try {
			rnd = new Random(93475);

			genSample();
			print();

			genNarrow2();
			print();

			genNarrow1();
			print();

			{
				Test t = new Test(LARGE, LARGE);
				t.genMax();
				t.dump();
				print();
			}

			{
				Test t = new Test(LARGE, LARGE);
				t.genMax2(1);
				t.dump();
				print();
			}

			{
				Test t = new Test(LARGE, LARGE);
				t.genMax2(2);
				t.dump();
				print();
			}

			for (int i = 0; i < 5; i++) {
				Test t = new Test(SMALL, SMALL);
				t.makeRandomTest(SMALL);
				t.dump();
				print();
			}

			for (int i = 0; i < 5; i++) {
				Test t = new Test(LARGE, LARGE);
				t.makeRandomTest(LARGE);
				t.dump();
				print();
			}

			rnd = new Random(39463);

			for (int i = 0; i < 15; i++) {
				Test t = new Test(LARGE, LARGE);
				t.makeRandomTest(LARGE);
				t.setOneIP();
				t.makeItPlenty();
				t.dump();
				print();
			}
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
}
