using System;
using System.Collections.Generic;
using System.IO;
internal struct Query
{
public int Id;
public long Cpu;
public long Mem;
public long Qto;
public int Type;
}
internal struct Instance
{
public int Id;
public int Level;
public int B;
public int[] Nodes;
public int Primary;
}
internal struct ServerModel
{
public int Id;
public long Cpu;
public long Mem;
public long Cost;
}
internal struct Dispatch
{
public int InstanceId;
public int QueryId;
public int NodeId;
public long Amount;
public Dispatch(int instanceId, int queryId, int nodeId, long amount)
{
InstanceId = instanceId;
QueryId = queryId;
NodeId = nodeId;
Amount = amount;
}
}
internal sealed class FastScanner
{
private readonly Stream _stream;
private readonly byte[] _buffer;
private int _len;
private int _ptr;
public FastScanner()
{
_stream = Console.OpenStandardInput();
_buffer = new byte[1 << 16];
_len = 0;
_ptr = 0;
}
private int Read()
{
if (_ptr >= _len)
{
_len = _stream.Read(_buffer, 0, _buffer.Length);
_ptr = 0;
if (_len == 0)
{
return -1;
}
}
return _buffer[_ptr++];
}
public bool TryReadInt(out int value)
{
long parsed;
if (!TryReadLong(out parsed))
{
value = 0;
return false;
}
value = (int)parsed;
return true;
}
public bool TryReadLong(out long value)
{
int c;
do
{
c = Read();
if (c == -1)
{
value = 0;
return false;
}
} while (c <= 32);
int sign = 1;
if (c == '-')
{
sign = -1;
c = Read();
}
long result = 0;
while (c >= '0' && c <= '9')
{
result = result * 10 + (c - '0');
c = Read();
if (c == -1)
{
break;
}
}
value = result * sign;
return true;
}
}
internal static class Program
{
private static long MakeKey(int instanceId, int queryId)
{
return ((long)instanceId << 32) ^ (uint)queryId;
}
private static void Main()
{
var fs = new FastScanner();
if (!fs.TryReadInt(out int k))
{
return;
}
var queries = new Query[k];
var queryIds = new HashSet<int>(k * 2 + 1);
for (int i = 0; i < k; ++i)
{
fs.TryReadInt(out queries[i].Id);
fs.TryReadLong(out queries[i].Cpu);
fs.TryReadLong(out queries[i].Mem);
fs.TryReadLong(out queries[i].Qto);
fs.TryReadInt(out queries[i].Type);
queryIds.Add(queries[i].Id);
}
fs.TryReadInt(out int m);
var instances = new Instance[m];
var primaryByInstance = new Dictionary<int, int>(m * 2 + 1);
var initialNodes = new List<int>(1000);
for (int i = 0; i < m; ++i)
{
fs.TryReadInt(out instances[i].Id);
fs.TryReadInt(out instances[i].Level);
fs.TryReadInt(out instances[i].B);
instances[i].Nodes = new int[instances[i].B];
for (int j = 0; j < instances[i].B; ++j)
{
fs.TryReadInt(out instances[i].Nodes[j]);
initialNodes.Add(instances[i].Nodes[j]);
}
instances[i].Primary = instances[i].Nodes[0];
primaryByInstance[instances[i].Id] = instances[i].Primary;
}
fs.TryReadInt(out int n);
var models = new ServerModel[n];
for (int i = 0; i < n; ++i)
{
fs.TryReadInt(out models[i].Id);
fs.TryReadLong(out models[i].Cpu);
fs.TryReadLong(out models[i].Mem);
fs.TryReadLong(out models[i].Cost);
}
fs.TryReadLong(out long cpuMin);
fs.TryReadLong(out long memMin);
fs.TryReadLong(out long cpuMax);
fs.TryReadLong(out long memMax);
fs.TryReadLong(out long gCpu);
fs.TryReadLong(out long gMem);
fs.TryReadInt(out int tLu);
fs.TryReadInt(out int tCu);
fs.TryReadInt(out int tSo);
_ = tLu;
_ = tCu;
_ = tSo;
for (int i = 0; i < 9; ++i)
{
fs.TryReadInt(out int dummy);
_ = dummy;
}
fs.TryReadInt(out int tTotal);
long targetCpu = cpuMax;
long targetMem = memMax;
if (targetCpu < cpuMin)
{
targetCpu = cpuMin;
}
if (targetMem < memMin)
{
targetMem = memMin;
}
if (targetCpu > cpuMin)
{
long steps = (targetCpu - cpuMin) / gCpu;
targetCpu = cpuMin + steps * gCpu;
}
if (targetMem > memMin)
{
long steps = (targetMem - memMin) / gMem;
targetMem = memMin + steps * gMem;
}
int modelIdForNode = -1;
long bestCost = 1L << 62;
for (int i = 0; i < models.Length; ++i)
{
if (models[i].Cpu >= targetCpu && models[i].Mem >= targetMem)
{
if (models[i].Cost < bestCost)
{
bestCost = models[i].Cost;
modelIdForNode = models[i].Id;
}
}
}
if (modelIdForNode == -1)
{
targetCpu = cpuMin;
targetMem = memMin;
for (int i = 0; i < models.Length; ++i)
{
if (models[i].Cpu >= targetCpu && models[i].Mem >= targetMem)
{
if (models[i].Cost < bestCost)
{
bestCost = models[i].Cost;
modelIdForNode = models[i].Id;
}
}
}
}
if (modelIdForNode == -1)
{
return;
}
long dCpu = targetCpu - cpuMin;
long dMem = targetMem - memMin;
bool doInitialScale = (dCpu != 0 || dMem != 0);
var serverByNode = new Dictionary<int, int>(initialNodes.Count * 2 + 1);
int nextServerId = 1000000000;
for (int i = 0; i < initialNodes.Count; ++i)
{
serverByNode[initialNodes[i]] = nextServerId++;
}
var output = new StreamWriter(Console.OpenStandardOutput())
{
AutoFlush = false
};
output.WriteLine(initialNodes.Count);
for (int i = 0; i < initialNodes.Count; ++i)
{
int nodeId = initialNodes[i];
output.WriteLine($"{serverByNode[nodeId]} {modelIdForNode}");
}
output.WriteLine(initialNodes.Count);
for (int i = 0; i < initialNodes.Count; ++i)
{
int nodeId = initialNodes[i];
output.WriteLine($"{nodeId} {serverByNode[nodeId]}");
}
output.Flush();
if (!fs.TryReadInt(out int verdict) || verdict == -1)
{
return;
}
var middleware = new Dictionary<long, long>(1 << 16);
for (int t = 1; t <= tTotal; ++t)
{
if (!fs.TryReadInt(out int rt))
{
return;
}
for (int i = 0; i < rt; ++i)
{
fs.TryReadInt(out int instanceId);
fs.TryReadInt(out int queryId);
fs.TryReadLong(out long qps);
long key = MakeKey(instanceId, queryId);
if (middleware.TryGetValue(key, out long old))
{
middleware[key] = old + qps;
}
else
{
middleware[key] = qps;
}
}
int cntNew = 0;
int cntDel = 0;
int cntLocal = (t == 1 && doInitialScale) ? initialNodes.Count : 0;
int cntCross = 0;
int cntOut = 0;
output.WriteLine($"{cntNew} {cntDel} {cntLocal} {cntCross} {cntOut}");
if (cntLocal > 0)
{
for (int i = 0; i < initialNodes.Count; ++i)
{
output.WriteLine($"{initialNodes[i]} {dCpu} {dMem}");
}
}
var dispatches = new List<Dispatch>(middleware.Count);
foreach (var pair in middleware)
{
long amount = pair.Value;
if (amount <= 0)
{
continue;
}
long key = pair.Key;
int instanceId = (int)(key >> 32);
int queryId = (int)(key & 0xFFFFFFFFL);
int primaryNode = primaryByInstance[instanceId];
dispatches.Add(new Dispatch(instanceId, queryId, primaryNode, amount));
}
output.WriteLine(dispatches.Count);
for (int i = 0; i < dispatches.Count; ++i)
{
var d = dispatches[i];
output.WriteLine($"{d.InstanceId} {d.QueryId} {d.NodeId} {d.Amount}");
}
middleware.Clear();
output.Flush();
if (!fs.TryReadInt(out verdict) || verdict == -1)
{
return;
}
}
}
}