using System;
using System.Collections.Generic;
using System.Text;
class A
{
const int INF = 1000 * 1000 * 1000 + 7;
struct VMType
{
public int cpu;
public int mem;
public int price;
public VMType(int cpu, int mem, int price)
{
this.cpu = cpu;
this.mem = mem;
this.price = price;
}
}
static List<VMType> vmTypes = new List<VMType>();
static int FindVmType(int cpu, int mem)
{
int bestPrice = INF;
int index = -1;
for (int i = 0; i < vmTypes.Count; i++)
{
if (vmTypes[i].cpu >= cpu && vmTypes[i].mem >= mem && vmTypes[i].price < bestPrice)
{
bestPrice = vmTypes[i].price;
index = i;
}
}
return index;
}
static void Main(string[] args)
{
var input = Console.ReadLine().Split();
int m = int.Parse(input[0]);
int d = int.Parse(input[1]);
for (int i = 0; i < m; i++)
{
input = Console.ReadLine().Split();
int cpu = int.Parse(input[0]);
int mem = int.Parse(input[1]);
int price = int.Parse(input[2]);
vmTypes.Add(new VMType(cpu, mem, price));
}
List<int> vmsToShutdown = new List<int>();
Dictionary<int, List<int>> containersToAllocate = new Dictionary<int, List<int>>();
int t = int.Parse(Console.ReadLine());
for (int j = 0; j < t; j++)
{
input = Console.ReadLine().Split();
int e = int.Parse(input[0]);
if (e == -1) return; // :(
int cnt = 1;
if (e == 0) cnt = int.Parse(input[1]);
List<int> containersToShutdown = new List<int>();
List<Tuple<int, int>> vmsToCreate = new List<Tuple<int, int>>();
for (int k = 0; k < e; k++)
{
input = Console.ReadLine().Split();
int type = int.Parse(input[0]);
if (type == 1)
{
int id = int.Parse(input[1]);
int cpu = int.Parse(input[2]);
int mem = int.Parse(input[3]);
vmsToCreate.Add(new Tuple<int, int>(id, FindVmType(cpu, mem)));
if (!containersToAllocate.ContainsKey(j + d))
{
containersToAllocate[j + d] = new List<int>();
}
containersToAllocate[j + d].Add(id);
}
else if (type == 2)
{
int id = int.Parse(input[1]);
containersToShutdown.Add(id);
}
}
var sb = new StringBuilder();
j--;
for (int it = 0; it < cnt; it++)
{
j++;
sb.Append((vmsToCreate.Count + vmsToShutdown.Count + (containersToAllocate.ContainsKey(j) ? containersToAllocate[j].Count : 0)).ToString() + "\n");
foreach (var vm in vmsToCreate)
{
sb.Append($"1 {vm.Item1} {vm.Item2 + 1}\n");
}
foreach (var id in vmsToShutdown)
{
sb.Append($"2 {id}\n");
}
if (containersToAllocate.ContainsKey(j))
{
var v = containersToAllocate[j];
foreach (var cid in v)
{
sb.Append($"3 {cid} {cid}\n");
}
}
vmsToShutdown = new List<int>(containersToShutdown);
containersToShutdown.Clear();
vmsToCreate.Clear();
}
Console.Write(sb.ToString());
Console.Out.Flush();
}
int eEnd = int.Parse(Console.ReadLine());
if (eEnd == 0)
{
// :)
}
else
{
// :(
}
}
}