using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Program
{
sealed class FastScanner
{
private readonly System.IO.Stream _stream = Console.OpenStandardInput();
private readonly byte[] _buf = new byte[1 << 16];
private int _len, _ptr;
private byte Read()
{
if (_ptr >= _len)
{
_len = _stream.Read(_buf, 0, _buf.Length);
_ptr = 0;
if (_len <= 0) return 0;
}
return _buf[_ptr++];
}
public string Next()
{
var sb = new StringBuilder();
byte c;
do { c = Read(); } while (c <= 32);
while (c > 32)
{
sb.Append((char)c);
c = Read();
}
return sb.ToString();
}
public int NextInt() => int.Parse(Next());
public long NextLong() => long.Parse(Next());
}
public static void Main()
{
var fs = new FastScanner();
var sb = new StringBuilder();
long N = fs.NextLong();
long M = fs.NextLong();
long K = fs.NextLong();
var cost = new long[N];
var loc = new int[N];
var preconditions = new Dictionary<(int, int), int>();
for (int i = 0; i < N; ++i)
{
cost[i] = fs.NextLong();
loc[i] = fs.NextInt();
long L = fs.NextLong();
for (int j = 0; j < L; ++j)
{
int s = fs.NextInt();
preconditions[(i, s)] = 0;
}
}
var w = new long[M];
var flowVerts = new List<int>[(int)M];
for (int i = 0; i < M; ++i)
{
w[i] = fs.NextLong();
long L = fs.NextLong();
var list = new List<int>((int)L);
for (int j = 0; j < L; ++j) list.Add(fs.NextInt());
flowVerts[i] = list;
}
var pipelines = new List<int>[(int)K];
var cntFlows = new int[M];
var first = Enumerable.Repeat(-1, (int)N).ToArray();
var last = Enumerable.Repeat(-1, (int)N).ToArray();
for (int i = 0; i < K; ++i)
{
long L = fs.NextLong();
var pipe = new List<int>((int)L);
var walk = new List<int>();
for (int j = 0; j < L; ++j)
{
int f = fs.NextInt();
pipe.Add(f);
cntFlows[f]++;
walk.AddRange(flowVerts[f]);
}
pipelines[i] = pipe;
int sz = walk.Count;
for (int j = 0; j < sz; ++j) last[walk[j]] = j;
for (int j = sz - 1; j >= 0; --j) first[walk[j]] = j;
var keys = preconditions.Keys.ToList();
foreach (var key in keys)
{
int u = key.Item1;
int v = key.Item2;
if (last[u] != -1 && last[v] != -1 && last[u] > first[v])
preconditions[key] = preconditions[key] + 1;
}
for (int j = 0; j < sz; ++j) last[walk[j]] = first[walk[j]] = -1;
}
var idx = new List<int>();
var cntW = new int[M];
for (int i = 0; i < K; ++i)
{
bool ok = true;
foreach (var f in pipelines[i])
{
cntW[f]++;
ok &= cntFlows[f] - cntW[f] >= w[f];
}
long L = pipelines[i].Count;
var walk = new List<int>();
for (int j = 0; j < L; ++j)
{
int f = pipelines[i][j];
walk.AddRange(flowVerts[f]);
}
int sz = walk.Count;
for (int j = 0; j < sz; ++j) last[walk[j]] = j;
for (int j = sz - 1; j >= 0; --j) first[walk[j]] = j;
foreach (var kv in preconditions)
{
var key = kv.Key;
int u = key.Item1, v = key.Item2;
if (last[u] != -1 && last[v] != -1 && last[u] > first[v])
ok &= kv.Value > 1;
}
if (!ok)
{
idx.Add(i);
foreach (var f in pipelines[i]) cntW[f] = 0;
}
else
{
foreach (var f in pipelines[i])
{
cntFlows[f] -= cntW[f];
cntW[f] = 0;
}
var keys = preconditions.Keys.ToList();
foreach (var key in keys)
{
int u = key.Item1, v = key.Item2;
if (last[u] != -1 && last[v] != -1 && last[u] > first[v])
preconditions[key] = preconditions[key] - 1;
}
}
for (int j = 0; j < sz; ++j) last[walk[j]] = first[walk[j]] = -1;
}
sb.Append(idx.Count).Append('\n');
for (int t = 0; t < idx.Count; ++t)
{
if (t > 0) sb.Append(' ');
sb.Append(idx[t]);
}
sb.Append('\n');
sb.Append(idx.Count).Append('\n');
foreach (var i in idx)
{
sb.Append(pipelines[i].Count);
foreach (var f in pipelines[i]) sb.Append(' ').Append(f);
sb.Append('\n');
}
Console.Write(sb.ToString());
}
}