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

public class Main545A {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskC solver = new TaskC();
		solver.solve(1, in, out);
		out.close();
	}
}

// Not finished
class TaskC {

	public void solve(int testNumber, InputReader in, PrintWriter out) {
		int n = in.nextInt();
		int[] isGood = new int[n];
		Arrays.fill(isGood, 1);
		for (int i = 0; i<n*n; i++) {
			if (in.nextInt()==1 || in.nextInt()==3) {
				isGood[i/n] = 0;
			}
			if (in.nextInt()==2 || in.nextInt() ==3) {
				isGood[i%n] = 0;
			}
		}
		out.println(Arrays.stream(isGood).sum());
		Arrays.stream(isGood).filter(k->k==1).forEach(kk -> out.print(kk+" "));
	}
}

class InputReader {
	public BufferedReader reader;
	public StringTokenizer tokenizer;

	public InputReader(InputStream stream) {
		reader = new BufferedReader(new InputStreamReader(stream), 32768);
		tokenizer = null;
	}

	public String next() {
		while (tokenizer == null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return tokenizer.nextToken();
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

}


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

public class Main545A {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskC solver = new TaskC();
		solver.solve(1, in, out);
		out.close();
	}
}

// Not finished
class TaskC {

	public void solve(int testNumber, InputReader in, PrintWriter out) {
		int n = in.nextInt();
		int[] isGood = new int[n];
		Arrays.fill(isGood, 1);
		for (int i = 0; i<n*n; i++) {
			int tmp = in.nextInt();
			if (tmp==1 || tmp==3) {
				isGood[i/n] = 0;
			}
			if (tmp==2 || tmp==3) {
				isGood[i%n] = 0;
			}
		}
		out.println(Arrays.stream(isGood).sum());
		IntStream.range(0, isGood.length)
			.filter(k->isGood[k]==1).forEach(id -> out.print(id+" "));
	}
}

class InputReader {
	public BufferedReader reader;
	public StringTokenizer tokenizer;

	public InputReader(InputStream stream) {
		reader = new BufferedReader(new InputStreamReader(stream), 32768);
		tokenizer = null;
	}

	public String next() {
		while (tokenizer == null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return tokenizer.nextToken();
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

}


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

public class Main545A {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskC solver = new TaskC();
		solver.solve(1, in, out);
		out.close();
	}
}

// Not finished
class TaskC {

	public void solve(int testNumber, InputReader in, PrintWriter out) {
		int n = in.nextInt();
		int[] isGood = new int[n];
		Arrays.fill(isGood, 1);
		for (int i = 0; i<n*n; i++) {
			int tmp = in.nextInt();
			if (tmp==1 || tmp==3) {
				isGood[i/n] = 0;
			}
			if (tmp==2 || tmp==3) {
				isGood[i%n] = 0;
			}
		}
		out.println(Arrays.stream(isGood).sum());
		IntStream.range(0, isGood.length)
			.filter(k->isGood[k]==1).forEach(id -> out.print((id+1)+" "));
	}
}

class InputReader {
	public BufferedReader reader;
	public StringTokenizer tokenizer;

	public InputReader(InputStream stream) {
		reader = new BufferedReader(new InputStreamReader(stream), 32768);
		tokenizer = null;
	}

	public String next() {
		while (tokenizer == null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return tokenizer.nextToken();
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <string>
#include <string.h>
#include <memory>
#include <map>
#include <queue>
using namespace std;
#define li long long
#define ld long double
#define mp make_pair
#define pb push_back
li n,m;
string word[100];
li dif[1000000];
queue <li> poz;
struct para
{
        li a1,b1,a2,b2;
        para(){l=0;}
        li tryed[200];
        li done[200];
		queue <li> och;
        void scan()
        {
                cin>>a1>>b1>>a2>>b2;
        }
        li to_first(li r)
        {
                return max(1LL, dif[r]-tryed[r]-a2-b1);
        }
        li to_second(li r)
        {
                return max(1LL, dif[r]-tryed[r]-a1-b2);
        }
};
para people[5000];
int main()
{
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
        li i,j,l,t,s,x1,y1, res=0, ans=0;
        cin>>n>>t;
        for(i=0;i<n;i++)
                people[i].scan();
        cin>>m;
        for(i=0;i<m;i++)
        {
			cin>>word[i];
			cin>>dif[i];
			poz.push(i);
        }
        j=m;
        i=0;
        li hod=0;
		while(!poz.empty())
        {
                li team=hod%n;
                li time=t;
				while(time>0 && !poz.empty())
                {
                        li cur;
                        if(hod%(2*n)>n || (hod%(2*n)==0 && hod!=0))
							cur=people[team].to_first(poz.front());
                        else
							cur=people[team].to_second(poz.front());
                        if(time>=cur)
                                {
									time-=cur;
									people[team].done[poz.front()]=1;
									people[team].och.push(poz.front());
									poz.pop();
                                }
                                else
                                {
									people[team].tryed[poz.front()]+=(cur-time);
									poz.push(poz.front());
									poz.pop();
									time=0;
                                }

                }
                hod++;
        }
        for(i=0;i<n;i++)
        {
			l=people[i].och.size();
			if(l==0)
				cout<<0<<'\n';
			else
			{
				cout<<l<<' ';
                while(!people[i].och.empty())
                {
					cout<<word[people[i].och.front()];

					if(people[i].och.size()!=1)
                                cout<<' ';
                        else
                                cout<<'\n';
					people[i].och.pop();
                }
			}
        }
        return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <string>
#include <string.h>
#include <memory>
#include <map>
#include <queue>
using namespace std;
#define li long long
#define ld long double
#define mp make_pair
#define pb push_back
li n,m,a1,b1,c1,k, res=0;

string word[100];
int dif[1000000];
queue <int> poz;
struct para
{
        int a1,b1,a2,b2,l;
        para(){l=0;}
        li tryed[200];
        li done[200];
		queue <li> och;
        void scan()
        {
                cin>>a1>>b1>>a2>>b2;
        }
        li to_first(li r)
        {
                return max(1LL, dif[r]-tryed[r]-a2-b1);
        }
        li to_second(li r)
        {
                return max(1LL, dif[r]-tryed[r]-a1-b2);
        }
};
para people[5000];
int main()
{
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
        li i,j,l,t,s,x1,y1, res=0, ans=0;
        cin>>n>>t;
        for(i=0;i<n;i++)
                people[i].scan();
        cin>>m;
        for(i=0;i<m;i++)
        {
			cin>>word[i];
			cin>>dif[i];
			poz.push(i);
        }
        j=m;
        i=0;
        li hod=0;
		while(!poz.empty())
        {
                li team=hod%n;
                li time=t;
				while(time>0 && !poz.empty())
                {
                        li cur;
                        if(hod%(2*n)>n)
							cur=people[team].to_first(poz.front());
                        else
							cur=people[team].to_second(poz.front());
                        if(time>=cur)
                                {
									time-=cur;
									people[team].done[poz.front()]=1;
									people[team].och.push(poz.front());
									poz.pop();
                                }
                                else
                                {
									people[team].tryed[poz.front()]+=(cur-time);
									poz.push(poz.front());
									poz.pop();
									time=0;
                                }

                }
                hod++;
        }
        for(i=0;i<n;i++)
        {
			l=people[i].och.size();
			if(l==0)
				cout<<0<<'\n';
			else
			{
				cout<<l<<' ';
                while(!people[i].och.empty())
                {
					cout<<word[people[i].och.front()];

					if(people[i].och.size()!=1)
                                cout<<' ';
                        else
                                cout<<'\n';
					people[i].och.pop();
                }
			}
        }
        return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <string>
#include <string.h>
#include <memory>
#include <map>
using namespace std;
#define li long long
#define ld long double
#define mp make_pair
#define pb push_back
li n,m,a1,b1,c1,k, res=0;
map <li, li> sw;
struct para
{
	int a1,b1,a2,b2,l;
	para(){l=0;}
	map <li, int> a;
	vector <li> done;
	void scan()
	{
		cin>>a1>>b1>>a2>>b2;
	}
	li to_first(li r)
	{
		li dif;
		if(a.find(r)!=a.end())
			dif=(*a.find(r)).second;
		else
			dif=(*sw.find(r)).second;
		return max(1LL, dif-a2-b1);
	}
	li to_second(li r)
	{
		li dif;
		if(a.find(r)!=a.end())
			dif=(*a.find(r)).second;
		else
			dif=(*sw.find(r)).second;
		return max(1LL, dif-a1-b2);
	}
};
para people[5000];
pair < li, li > word[10000000];
li hesh(string r)
{
	li p=57;
	li p1=999997;
	li res=0,j=1;
	for(int i=0;i<r.size();i++)
	{
		res*=p;
		res+=r[i];
		res%=p1;
	}
	return res;
}
string hes[1000000];
int main()
{
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	li i,j,l,t,s,x1,y1, res=0, ans=0;
	cin>>n>>t;
	for(i=0;i<n;i++)
		people[i].scan();
	cin>>m;
	for(i=0;i<m;i++)
	{
		string u;
		cin>>u>>word[i].second;
		li u1=hesh(u);
		word[i].first=u1;
		hes[u1]=u;
		sw.insert(mp(u1, word[i].second));
	}
	j=m;
	i=0;
	li hod=0;
	while(i<j)
	{
		li team=hod%n;
		li time=t;
		while(time>0 && i<j)
		{
			li cur;
			if(hod%(2*n)>=n)
				cur=people[team].to_first(word[i].first);
			else
				cur=people[team].to_second(word[i].first);
			if(time>=cur)
				{
					time-=cur;
					people[team].a.erase(word[i].first);
					i++;
					people[team].done.pb(word[i-1].first);
					people[team].l++;
				}
				else
				{
					people[team].a.insert(mp(word[i].first, cur-time));
					i++;
					word[j]=word[i-1];
					j++;
					time=0;
				}

		}
		hod++;
	}
	for(i=0;i<n;i++)
	{
		if(people[i].l==0)
		{
			cout<<0<<'\n';
			continue;
		}
		cout<<people[i].l<<' ';
		for(j=0;j<people[i].l;j++)
		{
			cout<<hes[people[i].done[j]];
			if(j!=people[i].l-1)
				cout<<' ';
			else
				cout<<'\n';
		}
	}
	return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <string>
#include <string.h>
#include <memory>
#include <map>
#include <queue>
using namespace std;
#define li long long
#define ld long double
#define mp make_pair
#define pb push_back
li n,m;
string word[100];
li dif[1000000];
queue <li> poz;
struct para
{
        li a1,b1,a2,b2;
        li tryed[200];
        li done[200];
		queue <li> och;
        void scan()
        {
                cin>>a1>>b1>>a2>>b2;
        }
        li to_first(li r)
        {
                return max(1LL, dif[r]-tryed[r]-a2-b1);
        }
        li to_second(li r)
        {
                return max(1LL, dif[r]-tryed[r]-a1-b2);
        }
};
para people[5000];
int main()
{
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
        li i,j,l,t,s,x1,y1, res=0, ans=0;
        cin>>n>>t;
        for(i=0;i<n;i++)
                people[i].scan();
        cin>>m;
        for(i=0;i<m;i++)
        {
			cin>>word[i];
			cin>>dif[i];
			poz.push(i);
        }
        j=m;
        i=0;
        li hod=0;
		while(!poz.empty())
        {
                li team=hod%n;
                li time=t;
				while(time>0 && !poz.empty())
                {
                        li cur;
                        if(hod%(2*n)>n || (hod%(2*n)==0 && hod!=0))
							cur=people[team].to_first(poz.front());
                        else
							cur=people[team].to_second(poz.front());
                        if(time>=cur)
                                {
									time-=cur;
									people[team].done[poz.front()]=1;
									people[team].och.push(poz.front());
									poz.pop();
                                }
                                else
                                {
									people[team].tryed[poz.front()]+=(cur-time);
									poz.push(poz.front());
									poz.pop();
									time=0;
                                }

                }
                hod++;
        }
        for(i=0;i<n;i++)
        {
			l=people[i].och.size();
			if(l==0)
				cout<<0<<'\n';
			else
			{
				cout<<l<<' ';
                while(!people[i].och.empty())
                {
					cout<<word[people[i].och.front()];

					if(people[i].och.size()!=1)
                                cout<<' ';
                        else
                                cout<<'\n';
					people[i].och.pop();
                }
			}
        }
        return 0;
}


#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <cstdlib>
#include <cstdio>
#include <stack>
#include <map>
#include <cmath>
#include <ctime>
#include <memory.h>
#include <fstream>
using namespace std;

#ifdef MYDEBUG
#pragma comment(linker, "/stack:1000000000")
#endif

typedef pair<int,int> pii;
typedef long long LL;
typedef unsigned long long ULL;

#define MAX(a,b) ((a>=b)?a:b)
#define MIN(a,b) ((a<=b)?a:b)
#define ABS(a) ((a<0)?-(a):a)
#define sz size()
#define mp make_pair
#define pb push_back
#define HAS(v,k) ((v).find(k)!=(v).end())
#define ALL(a) a.begin(),a.end()
#define CLR(a,b) memset(a,b,sizeof(a))
#define FOR(i,a,b) for(int i=(a),_b(b); i<_b; ++i)
#define RFOR(i,a,b) for(int i=(a)-1,_b(b); i>=_b; --i)
#define sqr(a) ((a)*(a))
#define V(t) vector<t>
#define VV(t) V(V(t))

// tell, get
int T[128][2],G[128][2],D[128][128],done[128],t,k,n,m;
VV(string) ans;
vector<pair<string,int> > mas;
int go(int p,int f,int &c)
{
	int res=0;
	int have=t;
	while(have>0)
	{
		int time=MAX(1,mas[c].second-(T[p][f]+G[p][(f+1)%2])-D[p][c]);
		if(time<=have)
		{
			++res;
			have-=time;
			ans[p].pb(mas[c].first);
			done[c]=1;
			if(k+res==m)return res;
			while(done[c])
				c=(c+1)%m;
		}
		else
		{
			D[p][c]+=time;
			c=(c+1)%m;
			break;
		}
	}
	return res;
}
int main()
{
#ifdef MYDEBUG
	freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	clock_t beg = clock();
#endif

	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);

	scanf("%d %d",&n,&t);
	FOR(i,0,n)
		scanf("%d %d %d %d",&T[i][0],&T[i][1],&G[i][0],&G[i][1]);
	ans.resize(n);
	scanf("%d",&m);
	mas.resize(m);
	FOR(i,0,m)
	{
		cin >>mas[i].first;
		scanf("%d",&mas[i].second);
	}
	k=0;
	int p=0,f=0,c=0;
	while(k<m)
	{
		while(done[c])
			c=(c+1)%m;
		k+=go(p,f,c);
		++p;
		if(p==n)
		{
			p=0;
			f=(f+1)%2;
		}
	}
	FOR(j,0,n)
	{
		printf("%d",ans[j].sz);
		FOR(i,0,ans[j].sz)
			cout <<" " <<ans[j][i];
		printf("\n");
	}
#ifdef MYDEBUG
    fprintf(stderr,"*** Total time: %.3lf ***\n",1.0*(clock()-beg)/CLOCKS_PER_SEC);
#endif
	return 0;
}


#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <cstdlib>
#include <cstdio>
#include <stack>
#include <map>
#include <cmath>
#include <ctime>
#include <memory.h>
#include <fstream>
using namespace std;

#ifdef MYDEBUG
#pragma comment(linker, "/stack:1000000000")
#endif

typedef pair<int,int> pii;
typedef long long LL;
typedef unsigned long long ULL;

#define MAX(a,b) ((a>=b)?a:b)
#define MIN(a,b) ((a<=b)?a:b)
#define ABS(a) ((a<0)?-(a):a)
#define sz size()
#define mp make_pair
#define pb push_back
#define HAS(v,k) ((v).find(k)!=(v).end())
#define ALL(a) a.begin(),a.end()
#define CLR(a,b) memset(a,b,sizeof(a))
#define FOR(i,a,b) for(int i=(a),_b(b); i<_b; ++i)
#define RFOR(i,a,b) for(int i=(a)-1,_b(b); i>=_b; --i)
#define sqr(a) ((a)*(a))
#define V(t) vector<t>
#define VV(t) V(V(t))

// tell, get
int T[128][2],G[128][2],D[128][128],done[128],t,k,n,m;
VV(string) ans;
vector<pair<string,int> > mas;
int go(int p,int f,int &c)
{
	int res=0;
	int have=t;
	while(have>0)
	{
		int time=MAX(1,mas[c].second-(T[p][f]+G[p][(f+1)%2])-D[p][c]);
		if(time<=have)
		{
			++res;
			have-=time;
			ans[p].pb(mas[c].first);
			done[c]=1;
			if(k+res==m)return res;
			while(done[c])
				c=(c+1)%m;
		}
		else
		{
			D[p][c]+=time;
			++c;
			break;
		}
	}
	return res;
}
int main()
{
#ifdef MYDEBUG
	freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	clock_t beg = clock();
#endif

	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);

	scanf("%d %d",&n,&t);
	FOR(i,0,n)
		scanf("%d %d %d %d",&T[i][0],&T[i][1],&G[i][0],&G[i][1]);
	ans.resize(n);
	scanf("%d",&m);
	mas.resize(m);
	FOR(i,0,m)
	{
		cin >>mas[i].first;
		scanf("%d",&mas[i].second);
	}
	k=0;
	int p=0,f=0,c=0;
	while(k<m)
	{
		while(done[c])
			c=(c+1)%m;
		k+=go(p,f,c);
		++p;
		if(p==n)
		{
			p=0;
			f=(f+1)%2;
		}
	}
	FOR(j,0,n)
	{
		printf("%d",ans[j].sz);
		FOR(i,0,ans[j].sz)
			cout <<" " <<ans[j][i];
		printf("\n");
	}
#ifdef MYDEBUG
    fprintf(stderr,"*** Total time: %.3lf ***\n",1.0*(clock()-beg)/CLOCKS_PER_SEC);
#endif
	return 0;
}


#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <cstdlib>
#include <cstdio>
#include <stack>
#include <map>
#include <cmath>
#include <ctime>
#include <memory.h>
#include <fstream>
using namespace std;

#ifdef MYDEBUG
#pragma comment(linker, "/stack:1000000000")
#endif

typedef pair<int,int> pii;
typedef long long LL;
typedef unsigned long long ULL;

#define MAX(a,b) ((a>=b)?a:b)
#define MIN(a,b) ((a<=b)?a:b)
#define ABS(a) ((a<0)?-(a):a)
#define sz size()
#define mp make_pair
#define pb push_back
#define HAS(v,k) ((v).find(k)!=(v).end())
#define ALL(a) a.begin(),a.end()
#define CLR(a,b) memset(a,b,sizeof(a))
#define FOR(i,a,b) for(int i=(a),_b(b); i<_b; ++i)
#define RFOR(i,a,b) for(int i=(a)-1,_b(b); i>=_b; --i)
#define sqr(a) ((a)*(a))
#define V(t) vector<t>
#define VV(t) V(V(t))

// tell, get
int T[128][2],G[128][2],D[128][128],done[128],t,k,n,m;
VV(string) ans;
vector<pair<string,int> > mas;
int go(int p,int f,int &c)
{
	int res=0;
	int have=t;
	while(have>0)
	{
		int time=MAX(1,mas[c].second-(T[p][f]+G[p][(f+1)%2])-D[p][c]);
		if(time<=have)
		{
			++res;
			have-=time;
			ans[p].pb(mas[c].first);
			done[c]=1;
			if(k+res==m)return res;
			while(done[c])
				c=(c+1)%m;
		}
		else
		{
			D[p][c]+=have;
			have=0;
			c=(c+1)%m;
			break;
		}
	}
	return res;
}
int main()
{
#ifdef MYDEBUG
	freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	clock_t beg = clock();
#endif

	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);

	scanf("%d %d",&n,&t);
	FOR(i,0,n)
		scanf("%d %d %d %d",&T[i][0],&T[i][1],&G[i][0],&G[i][1]);
	ans.resize(n);
	scanf("%d",&m);
	mas.resize(m);
	FOR(i,0,m)
	{
		cin >>mas[i].first;
		scanf("%d",&mas[i].second);
	}
	k=0;
	int p=0,f=0,c=0;
	while(k<m)
	{
		while(done[c])
			c=(c+1)%m;
		k+=go(p,f,c);
		++p;
		if(p==n)
		{
			p=0;
			f=(f+1)%2;
		}
	}
	FOR(j,0,n)
	{
		printf("%d",ans[j].sz);
		FOR(i,0,ans[j].sz)
			cout <<" " <<ans[j][i];
		printf("\n");
	}
#ifdef MYDEBUG
    fprintf(stderr,"*** Total time: %.3lf ***\n",1.0*(clock()-beg)/CLOCKS_PER_SEC);
#endif
	return 0;
}


#include <cstdio>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <memory.h>
#include <string>
#include <map>
#include <set>
#include <ctime>

using namespace std;

int a[2][102],b[2][102];

int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int n,t;
    cin >> n >> t;
    for(int i=0;i<n;i++)
        cin >> a[0][i] >> b[0][i] >> a[1][i] >> b[1][i];
    int m,c[102]={0};
    cin >> m;
    string word[102];
    for(int i=0;i<m;i++)
        cin >> word[i] >> c[i];
    bool ok[102]={0};
    int d[102][102]={0};
    int left=m,cur=0,player=0,lt=t;
    vector < vector <string> > ans(n);
    while(left)
    {
        while(ok[cur]) cur=(cur+1)%m;
        int team,num;
        if (player<n) team=player,num=0;
        else team=player-n,num=1;
        int time=max(1,c[cur]-(a[num][team]+b[num^1][team])-d[cur][team]);
        if (time<=lt)
        {
            ok[cur]=true;
//          cout << team << "   " << word[cur] << endl;
            ans[team].push_back(word[cur]);
            lt-=time; left--;
            if (time<lt) continue;
        }
        else d[cur][team]+=lt;
        lt=t; player=(player+1)%(2*n); cur=(cur+1)%m;
    }
    for(int i=0;i<n;i++)
    {
        cout << ans[i].size();
        for(int j=0;j<ans[i].size();j++)
            cout << " " << ans[i][j];
        cout << endl;
    }
    return 0;
}


#include <cstdio>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <memory.h>
#include <string>
#include <map>
#include <set>
#include <ctime>

using namespace std;

int a[2][102],b[2][102];

int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int n,t;
    cin >> n >> t;
    for(int i=0;i<n;i++)
        cin >> a[0][i] >> b[0][i] >> a[1][i] >> b[1][i];
    int m,c[102]={0};
    cin >> m;
    string word[102];
    for(int i=0;i<m;i++)
        cin >> word[i] >> c[i];
    bool ok[102]={0};
    int d[102][102]={0};
    int left=m,cur=0,player=0,lt=t;
    vector < vector <string> > ans(n);
    while(left)
    {
        while(ok[cur]) cur=(cur+1)%m;
        int team,num;
        if (player<n) team=player,num=0;
        else team=player-n,num=1;
        int time=max(1,c[cur]-(a[num][team]+b[num^1][team])-d[cur][team]);
        if (time<=lt)
        {
            ok[cur]=true;
//          cout << team << "   " << word[cur] << endl;
            ans[team].push_back(word[cur]);
            lt-=time; left--;
            if (lt) continue;
        }
        else d[cur][team]+=lt;
        lt=t; player=(player+1)%(2*n); cur=(cur+1)%m;
    }
    for(int i=0;i<n;i++)
    {
        cout << ans[i].size();
        for(int j=0;j<ans[i].size();j++)
            cout << " " << ans[i][j];
        cout << endl;
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include <queue>
#include <vector>
using namespace std;
typedef long long li;
typedef pair <int, int> pi;
#define mp make_pair
#define pb push_back
void solve ();
int main()
{
		freopen("input.txt","r",stdin);
#ifndef _DEBUG
	freopen("output.txt","w",stdout);
#endif
	 solve();
	return 0;
}
//#define int li
int n, t;
pair <int, int> skill[200][2];
int m;
pair < int, string > cards[200];
queue < int > q;
int d[200][200];
int points[200];
vector < string > solved[200];
pi newcur (pi cur)
{
	if ( cur.first<n-1 )
		return mp ( cur.first+1, cur.second );
	return mp (0, (cur.second+1)%2);
}
void solve()
{
	cin>>n>>t;
	for (int i=0; i<n; i++)
		cin>>skill[i][0].first>>skill[i][0].second>>skill[i][1].first>>skill[i][1].second;
	cin>>m;
	for (int i=0; i<m; i++)
	{
		cin>>cards[i].second>>cards[i].first;
		q.push(i);
	}
	pi cur=mp (0, 0);
	int curt=t;
	while (!q.empty())
	{
		int num=q.front();
		int timer=max(1, cards[num].first-skill[cur.first][cur.second].first-skill[cur.first][(cur.second+1)%2].second-d[cur.first][num] );
		if ( timer<curt )
		{
			curt-=timer;
			points[cur.first]++;
			solved[cur.first].pb ( cards[num].second );
			q.pop();
		}
		else
			if (timer==curt)
			{
				points[cur.first]++;
				solved[cur.first].pb ( cards[num].second );
				q.pop();
				curt=t;
				cur=newcur(cur);
			}
		else
		{
			d[cur.first][num]+=curt;
			curt=t;
			pi next=newcur(cur);
			q.pop();
			q.push(num);
			cur=next;
		}
	}
	for (int i=0; i<n; i++)
	{
		cout<<points[i]<<' ';
		for (int j=0; j<points[i]; j++)
			cout<<solved[i][j]<<' ';
		cout<<endl;
	}
}


#include<iostream>
#include<cstdio>
#include<string>
#include <queue>
#include <vector>
using namespace std;
typedef long long li;
typedef pair <int, int> pi;
#define mp make_pair
#define pb push_back
void solve ();
int main()
{
		freopen("input.txt","r",stdin);
#ifndef _DEBUG
	freopen("output.txt","w",stdout);
#endif
	 solve();
	return 0;
}
//#define int li
int n, t;
pair <int, int> skill[200][2];
int m;
pair < int, string > cards[200];
queue <pair <int, string> > q;
int d[200][200];
int points[200];
vector < string > solved[200];
pi newcur (pi cur)
{
	if ( cur.first<n-1 )
		return mp ( cur.first+1, cur.second );
	return mp (0, (cur.second+1)%2);
}
void solve()
{
	cin>>n>>t;
	for (int i=0; i<n; i++)
		cin>>skill[i][0].first>>skill[i][0].second>>skill[i][1].first>>skill[i][1].second;
	cin>>m;
	for (int i=0; i<m; i++)
	{
		cin>>cards[i].second>>cards[i].first;
		q.push(mp(i, cards[i].second));
	}
	pi cur=mp (0, 0);
	int curt=t;
	while (!q.empty())
	{
		pair <int, string> now=q.front();
		int num=now.first;
		int timer=max(1, cards[num].first-(skill[cur.first][cur.second].first+skill[cur.first][(cur.second+1)%2].second)-d[cur.first][num] );
		if ( timer<=curt )
		{
			curt-=timer;
			points[cur.first]++;
			solved[cur.first].pb ( now.second );
			q.pop();
		}
		else
		{
			d[cur.first][num]+=curt;
			curt=t;
			pi next=newcur(cur);
			q.pop();
			q.push(now);
			cur=next;
		}
	}
	for (int i=0; i<n; i++)
	{
		cout<<points[i]<<' ';
		for (int j=0; j<points[i]; j++)
			cout<<solved[i][j]<<' ';
		cout<<endl;
	}
}


#include <iostream>
#include <string>
#include <queue>
#include <algorithm>
using namespace std;
int n, t, a[100][2], b[100][2], m, i, c[100], d[100][100], tt, k=0, v, j;
string s[100];
queue <int> q;
vector <int> ans[100];
int main()
{
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	cin>>n>>t;
	for(i=0; i<n; i++)
		cin>>a[i][0]>>b[i][0]>>a[i][1]>>b[i][1];
	cin>>m;
	for(i=0; i<m; i++)
	{
		cin>>s[i]>>c[i];
		q.push(i);
	}
	i=0;
	while(!q.empty())
	{
		tt=t;
		while(tt!=0&&!q.empty())
		{
			k=q.front();
			q.pop();
			v=max(1, c[k]-a[i%n][i/n]-b[i%n][((i/n)+1)%2]-d[i%n][k]);
			if(tt>=v)
				tt-=v, ans[i%n].push_back(k);
			else
				d[i%n][k]+=tt, tt=0, q.push(k);
		}
		i++;
		if(i==2*n)
			i=0;
	}
	for(i=0; i<n; i++)
	{
		cout<<ans[i].size();
		for(j=0; j<ans[i].size(); j++)
			cout<<" "<<s[ans[i][j]];
		cout<<"\n";
	}
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;
int n, t, a[101][2], b[101][2], m, i, j, c[101], d[101][101], x=0, k=0, v, tt;
string s[101];
bool used[101];
vector <string> ans[101];
int main()
{
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
		cin>>n>>t;
        for(i=0; i<n; i++)
                cin>>a[i][0]>>b[i][0]>>a[i][1]>>b[i][1];
        cin>>m;
        for(i=0; i<m; i++)
        {
                cin>>s[i];
                cin>>c[i];
        }
        i=0;
        while(x!=m)
        {
                tt=t;
                while(tt&&x!=m)
                {
                        while(used[k]&&x!=m)
                                if(k==m-1)
                                        k=0;
                                else
                                        k++;
                        v=max(1, c[k]-(a[i%n][i/n]+b[i%n][(i%n+1)%2])-d[i%n][k]);
                        if(tt>=v)
                        {
                                tt-=v;
                                ans[i%n].push_back(s[k]);
                                used[k]=true;
                                x++;
                                k++;
                                if(k==m)
                                        k=0;
                                while(used[k]&&x!=m)
                                        if(k==m-1)
                                                k=0;
                                        else
                                                k++;
                        }
                        else
                        {
                                d[i%n][k]+=tt;
                                k++;
                                if(k==m)
                                        k=0;
                        while(used[k]&&x!=m)
                                if(k==m-1)
                                        k=0;
                                else
                                        k++;
                                break;
                        }
                }
                i++;
                if(i==2*n)
                        i=0;
        }
        for(i=0; i<n; i++)
        {
                cout<<ans[i].size();
                for(j=0; j<ans[i].size(); j++)
                        cout<<" "<<ans[i][j];
                cout<<"\n";
        }
        return 0;
}
/*
2 2
1 1 1 1
1 1 1 1
3
home
1
car
1
brother
1
*/


#include <iostream>
#include <string>
#include <vector>
using namespace std;
int n, t, a[101][2], b[101][2], m, i, j, c[101], d[101][101], x=0, k=0, v, tt;
string s[101];
bool used[101];
vector <string> ans[101];
int main()
{
        cin>>n>>t;
        for(i=0; i<n; i++)
                cin>>a[i][0]>>b[i][0]>>a[i][1]>>b[i][1];
        cin>>m;
        for(i=0; i<m; i++)
        {
                cin>>s[i];
                cin>>c[i];
        }
        i=0;
        while(x!=m)
        {
                tt=t;
                while(tt&&x!=m)
                {
                        while(used[k]&&x!=m)
                                if(k==m-1)
                                        k=0;
                                else
                                        k++;
                        v=max(1, c[k]-(a[i%n][i/n]+b[i%n][(i%n+1)%2])-d[i%n][k]);
                        if(tt>=v)
                        {
                                tt-=v;
                                ans[i%n].push_back(s[k]);
                                used[k]=true;
                                x++;
                                k++;
                                if(k==m)
                                        k=0;
                                while(used[k]&&x!=m)
                                        if(k==m-1)
                                                k=0;
                                        else
                                                k++;
                        }
                        else
                        {
                                d[i%n][k]+=tt;
                                k++;
                                if(k==m)
                                        k=0;
                        while(used[k]&&x!=m)
                                if(k==m-1)
                                        k=0;
                                else
                                        k++;
                                break;
                        }
                }
                i++;
                if(i==2*n)
                        i=0;
        }
        for(i=0; i<n; i++)
        {
                cout<<ans[i].size();
                for(j=0; j<ans[i].size(); j++)
                        cout<<" "<<ans[i][j];
                cout<<"\n";
        }
        return 0;
}
/*
2 2
1 1 1 1
1 1 1 1
3
home
1
car
1
brother
1
*/


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <queue>
#include <sstream>
#include <numeric>
#include <functional>
#include <set>
#include <cmath>
#include <stack>
#include <fstream>
#include <cassert>
#ifdef HOME_PC
#include <ctime>
#endif
using namespace std;

#pragma comment(linker,"/stack:16000000")
#pragma warning (disable : 4996)

#define ALL(v) v.begin(),v.end()
#define SZ(v) (int)v.size()
#define mset(A,x) memset((A),(x),sizeof(A))
#define FOR(i,start,N) for(int i=(start);i<(N);++i)
#define FORSZ(i,start,v) FOR(i,start,SZ(v))
#define REPSZ(i,v) FORSZ(i,0,v)
#define FORE(i,start,N) FOR(i,start,N+1)
#define make_unique(v) v.resize(unique(ALL(v))-v.begin())
#define debug(x) cout<<#x<<" = "<<x<<endl;
#define adebug(A,N) FOR(i,0,N) cout<<#A<<"["<<i<<"] = "<<A[i]<<endl;
#define adebug2d(a,n,m) FOR(i,0,n) { FOR(j,0,m) { cout<<a[i][j]<<" ";} cout<<endl;}
#define vdebug(v) REPSZ(i,v) cout<<#v<<"["<<i<<"] = "<<v[i]<<endl;
#define selfx(x,f,a) x = f(x,a)
#define sqr(x) ((x)*(x))

typedef pair<int,int> pii;
typedef long long i64;
typedef vector<int> VI; typedef vector< vector<int> > VVI;
typedef vector<string> VS;

const int inf = 1<<25;
const double eps = 1e-9;

struct Card
{
    string word;
    int complexity,id;
};
istream& operator>>(istream& in,Card& card)
{
    in>>card.word>>card.complexity;
    return in;
}

struct Team
{
public:
    Team() { mset(timeSpentOnCard,0);}

    int GuessTime(const Card& card,int p)
    {
        return max(1,card.complexity - (explanation[p] + understanding[p^1]) - timeSpentOnCard[card.id]);
    }
    int GetScores() { return guessed.size();}
    void AddGuessed(const string& word) { guessed.push_back(word);}
    void AddGuessTime(int cardId,int time)
    {
        timeSpentOnCard[cardId] += time;
    }
    VS GetGuessed() { return guessed;}

    int understanding[2];
    int explanation[2];
    int timeSpentOnCard[111];
    VS guessed;
};

istream& operator>>(istream& in,Team& team)
{
    FOR(i,0,2)
        cin>>team.explanation[i] >> team.understanding[i];
    return in;
}

int main()
{
#ifdef HOME_PC
    //freopen ("input.txt","r",stdin);
    //freopen ("in.txt","r",stdin);
    //freopen ("output.txt","w",stdout);
#else
    freopen ("input.txt","r",stdin);
    freopen ("output.txt","w",stdout);
#endif

    int n,t;
    cin>>n>>t;

    vector<Team> teams(n);
    FOR(i,0,n)
        cin>>teams[i];
    int m;
    cin>>m;
    vector<Card> cards(m);
    FOR(i,0,m)
        cin>>cards[i], cards[i].id = i;

    queue<Card> Q;
    REPSZ(i,cards)
        Q.push(cards[i]);

    for(int p = 0;!Q.empty();p^=1)
    {
        for(int team = 0; team < n && !Q.empty(); ++team)
        {
            int timeLeft = t;
            while(!Q.empty() && timeLeft > 0)
            {
                Card cur = Q.front(); Q.pop();
                int guessTime = teams[team].GuessTime(cur,p);
                if(guessTime <= timeLeft)
                {
                    timeLeft -= guessTime;
                    teams[team].AddGuessed(cur.word);
                }
                else
                {
                    teams[team].AddGuessTime(cur.id,timeLeft);
                    timeLeft = 0;
                    Q.push(cur);
                }
            }

        }

    }

    FOR(i,0,n)
    {
        cout<<teams[i].GetScores();
        VS guessed = teams[i].GetGuessed();
        REPSZ(i,guessed)
            cout<<" "<<guessed[i];
        printf("\n");
    }

#ifdef HOME_PC
    cerr<<endl<<"Execution time = "<<clock()<<" ms"<<endl;
#endif
    return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <queue>
#include <sstream>
#include <numeric>
#include <functional>
#include <set>
#include <cmath>
#include <stack>
#include <fstream>
#include <cassert>
#ifdef HOME_PC
#include <ctime>
#endif
using namespace std;

#pragma comment(linker,"/stack:16000000")
#pragma warning (disable : 4996)

#define ALL(v) v.begin(),v.end()
#define SZ(v) (int)v.size()
#define mset(A,x) memset((A),(x),sizeof(A))
#define FOR(i,start,N) for(int i=(start);i<(N);++i)
#define FORSZ(i,start,v) FOR(i,start,SZ(v))
#define REPSZ(i,v) FORSZ(i,0,v)
#define FORE(i,start,N) FOR(i,start,N+1)
#define make_unique(v) v.resize(unique(ALL(v))-v.begin())
#define debug(x) cout<<#x<<" = "<<x<<endl;
#define adebug(A,N) FOR(i,0,N) cout<<#A<<"["<<i<<"] = "<<A[i]<<endl;
#define adebug2d(a,n,m) FOR(i,0,n) { FOR(j,0,m) { cout<<a[i][j]<<" ";} cout<<endl;}
#define vdebug(v) REPSZ(i,v) cout<<#v<<"["<<i<<"] = "<<v[i]<<endl;
#define selfx(x,f,a) x = f(x,a)
#define sqr(x) ((x)*(x))

typedef pair<int,int> pii;
typedef long long i64;
typedef vector<int> VI; typedef vector< vector<int> > VVI;
typedef vector<string> VS;

const int inf = 1<<25;
const double eps = 1e-9;

struct Card
{
    string word;
    int complexity,id;
};
istream& operator>>(istream& in,Card& card)
{
    in>>card.word>>card.complexity;
    return in;
}

struct Team
{
public:
    Team() { mset(timeSpentOnCard,0);}

    int GuessTime(const Card& card,int p)
    {
        return max(1,card.complexity - (explanation[p] + understanding[p^1]) - timeSpentOnCard[card.id]);
    }
    int GetScores() { return guessed.size();}
    void AddGuessed(const string& word) { guessed.push_back(word);}
    void AddGuessTime(int cardId,int time)
    {
        timeSpentOnCard[cardId] += time;
    }
    VS GetGuessed() { return guessed;}

    int understanding[2];
    int explanation[2];
    int timeSpentOnCard[111];
    VS guessed;
};

istream& operator>>(istream& in,Team& team)
{
    FOR(i,0,2)
        cin>>team.explanation[i] >> team.understanding[i];
    return in;
}

int main()
{
#ifdef HOME_PC
    //freopen ("input.txt","r",stdin);
    //freopen ("in.txt","r",stdin);
    //freopen ("output.txt","w",stdout);
#else
    freopen ("input.txt","r",stdin);
    freopen ("output.txt","w",stdout);
#endif

    int n,t;
    cin>>n>>t;

    vector<Team> teams(n);
    FOR(i,0,n)
        cin>>teams[i];
    int m;
    cin>>m;
    vector<Card> cards(m);
    FOR(i,0,m)
        cin>>cards[i], cards[i].id = i;

    queue<Card> Q;
    REPSZ(i,cards)
        Q.push(cards[i]);

    for(int p = 0;!Q.empty();p^=1)
    {
        for(int team = 0; team < n && !Q.empty(); ++team)
        {
            int timeLeft = t;
            while(!Q.empty() && timeLeft > 0)
            {
                Card cur = Q.front(); Q.pop();
                int guessTime = teams[team].GuessTime(cur,p);
                if(guessTime <= timeLeft)
                {
                    timeLeft -= guessTime;
                    teams[team].AddGuessed(cur.word);
                }
                else
                {
                    timeLeft = 0;
                    teams[team].AddGuessTime(cur.id,guessTime);
                    Q.push(cur);
                }
            }

        }

    }

    FOR(i,0,n)
    {
        cout<<teams[i].GetScores();
        VS guessed = teams[i].GetGuessed();
        REPSZ(i,guessed)
            cout<<" "<<guessed[i];
        printf("\n");
    }

#ifdef HOME_PC
    cerr<<endl<<"Execution time = "<<clock()<<" ms"<<endl;
#endif
    return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <queue>
#include <sstream>
#include <numeric>
#include <functional>
#include <set>
#include <cmath>
#include <stack>
#include <fstream>
#include <cassert>
#ifdef HOME_PC
#include <ctime>
#endif
using namespace std;

#pragma comment(linker,"/stack:16000000")
#pragma warning (disable : 4996)

#define ALL(v) v.begin(),v.end()
#define SZ(v) (int)v.size()
#define mset(A,x) memset((A),(x),sizeof(A))
#define FOR(i,start,N) for(int i=(start);i<(N);++i)
#define FORSZ(i,start,v) FOR(i,start,SZ(v))
#define REPSZ(i,v) FORSZ(i,0,v)
#define FORE(i,start,N) FOR(i,start,N+1)
#define make_unique(v) v.resize(unique(ALL(v))-v.begin())
#define debug(x) cout<<#x<<" = "<<x<<endl;
#define adebug(A,N) FOR(i,0,N) cout<<#A<<"["<<i<<"] = "<<A[i]<<endl;
#define adebug2d(a,n,m) FOR(i,0,n) { FOR(j,0,m) { cout<<a[i][j]<<" ";} cout<<endl;}
#define vdebug(v) REPSZ(i,v) cout<<#v<<"["<<i<<"] = "<<v[i]<<endl;
#define selfx(x,f,a) x = f(x,a)
#define sqr(x) ((x)*(x))

typedef pair<int,int> pii;
typedef long long i64;
typedef vector<int> VI; typedef vector< vector<int> > VVI;
typedef vector<string> VS;

const int inf = 1<<25;
const double eps = 1e-9;

struct Card
{
    string word;
    int complexity,id;
};
istream& operator>>(istream& in,Card& card)
{
    in>>card.word>>card.complexity;
    return in;
}

struct Team
{
public:
    Team() { mset(timeSpentOnCard,0);}

    int GuessTime(const Card& card,int p)
    {
        return max(1,card.complexity - (explanation[p] + understanding[p^1]) - timeSpentOnCard[card.id]);
    }
    int GetScores() { return guessed.size();}
    void AddGuessed(const string& word) { guessed.push_back(word);}
    void AddGuessTime(int cardId,int time)
    {
        timeSpentOnCard[cardId] += time;
    }
    VS GetGuessed() { return guessed;}

    int understanding[2];
    int explanation[2];
    int timeSpentOnCard[111];
    VS guessed;
};

istream& operator>>(istream& in,Team& team)
{
    FOR(i,0,2)
        cin>>team.explanation[i] >> team.understanding[i];
    return in;
}

int main()
{
#ifdef HOME_PC
    //freopen ("input.txt","r",stdin);
    //freopen ("in.txt","r",stdin);
    //freopen ("output.txt","w",stdout);
#else
    freopen ("input.txt","r",stdin);
    freopen ("output.txt","w",stdout);
#endif

    int n,t;
    cin>>n>>t;

    vector<Team> teams(n);
    FOR(i,0,n)
        cin>>teams[i];
    int m;
    cin>>m;
    vector<Card> cards(m);
    FOR(i,0,m)
        cin>>cards[i], cards[i].id = i;

    queue<Card> Q;
    REPSZ(i,cards)
        Q.push(cards[i]);

    for(int p = 0;!Q.empty();p^=1)
    {
        for(int team = 0; team < n && !Q.empty(); ++team)
        {
            int timeLeft = t;
            while(!Q.empty() && timeLeft > 0)
            {
                Card cur = Q.front(); Q.pop();
                int guessTime = teams[team].GuessTime(cur,p);
                if(guessTime < timeLeft)
                {
                    timeLeft -= guessTime;
                    teams[team].AddGuessed(cur.word);
                }
                else
                {
                    timeLeft = 0;
                    teams[team].AddGuessTime(cur.id,guessTime);
                    Q.push(cur);
                }
            }

        }

    }

    FOR(i,0,n)
    {
        cout<<teams[i].GetScores();
        VS guessed = teams[i].GetGuessed();
        REPSZ(i,guessed)
            cout<<" "<<guessed[i];
        printf("\n");
    }

#ifdef HOME_PC
    cerr<<endl<<"Execution time = "<<clock()<<" ms"<<endl;
#endif
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <bitset>       //UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <iomanip>      //do setprecision
#include <ctime>
#include <complex>
using namespace std;

#define FOR(i,b,e) for(int i=(b);i<(e);++i)
#define FORQ(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i)
#define REP(x, n) for(int x = 0; x < (n); ++x)

#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double

const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;

#define MR 110

pair < int, int > T[MR][2];

queue < pair < string, int > > Q;

int d[MR][MR];  //ile czasu team spedzil nad dana karta
int c[MR];

pair < int, vector < string > > res[MR];

int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int n, t;
    cin >> n >> t;
    REP(i,n)
        cin >> T[i][0].ST >> T[i][0].ND >> T[i][1].ST >> T[i][1].ND;
    int m;
    cin >> m;
    REP(i,m)
    {
        pair < string, int > p;
        p.ND = i;
        cin >> p.ST >> c[i];
        Q.push(p);
    }
    int wsk = 0;    //z ktorego teamtu
    bool sel = 0;   //ktory gracz wyjasnia
    int left = t;   //ile czasu zostalo
    while(!Q.empty())
    {
        if(!left)   //zmiana teamu najpierw
        {
            if(wsk == n-1)
            {
                wsk = 0;
                sel = !sel; //zmiana gracza
            }
            else    //nastepny team bierze sie za rozwiazywanie
                wsk++;
            left = t;
        }
        //wylicz ile czasu spedza
        pair < string, int > p = Q.front(); Q.pop();
        int timE = max(1,c[p.ND]-(T[wsk][sel].ST+T[wsk][!sel].ND)-d[wsk][p.ND]);
        if(timE <= left)    //zgadl
        {
            left -= timE;
            res[wsk].ST++;  //dodaj punkty
            res[wsk].ND.PB(p.ST);
        }
        else    //nie udalo sie
        {
            Q.push(p);
            d[wsk][p.ND] += left;
            if(wsk == n-1)
            {
                wsk = 0;
                sel = !sel; //zmiana gracza
            }
            else    //nastepny team bierze sie za rozwiazywanie
                wsk++;
            left = t;
        }
    }
    REP(i,n)
    {
        cout << res[i].ST;
        REP(j,(int)res[i].ND.size())
            cout << " " << res[i].ND[j];
        cout << "\n";
    }
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <bitset>       //UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <iomanip>      //do setprecision
#include <ctime>
#include <complex>
using namespace std;

#define FOR(i,b,e) for(int i=(b);i<(e);++i)
#define FORQ(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i)
#define REP(x, n) for(int x = 0; x < (n); ++x)

#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double

const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;

#define MR 110

pair < int, int > T[MR][2];

queue < pair < string, int > > Q;

int d[MR][MR];  //ile czasu team spedzil nad dana karta
int c[MR];

pair < int, vector < string > > res[MR];

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int n, t;
    cin >> n >> t;
    REP(i,n)
        cin >> T[i][0].ST >> T[i][0].ND >> T[i][1].ST >> T[i][1].ND;
    int m;
    cin >> m;
    REP(i,m)
    {
        pair < string, int > p;
        p.ND = i;
        cin >> p.ST >> c[i];
        Q.push(p);
    }
    int wsk = 0;    //z ktorego teamtu
    bool sel = 0;   //ktory gracz wyjasnia
    int left = t;   //ile czasu zostalo
    while(!Q.empty())
    {
        //wylicz ile czasu spedza
        pair < string, int > p = Q.front(); Q.pop();
        int timE = max(1,c[p.ND]-(T[wsk][sel].ST+T[wsk][!sel].ND)-d[wsk][p.ND]);
        if(timE <= left)    //zgadl
        {
            left -= timE;
            res[wsk].ST++;  //dodaj punkty
            res[wsk].ND.PB(p.ST);
        }
        else    //nie udalo sie
        {
            Q.push(p);
            d[wsk][p.ND] += left;
            if(wsk == n-1)
            {
                wsk = 0;
                sel = !sel; //zmiana gracza
            }
            else    //nastepny team bierze sie za rozwiazywanie
                wsk++;
            left = t;
        }
    }
    REP(i,n)
    {
        cout << res[i].ST;
        REP(j,(int)res[i].ND.size())
            cout << " " << res[i].ND[j];
        cout << "\n";
    }
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <bitset>       //UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <iomanip>      //do setprecision
#include <ctime>
#include <complex>
using namespace std;

#define FOR(i,b,e) for(int i=(b);i<(e);++i)
#define FORQ(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i)
#define REP(x, n) for(int x = 0; x < (n); ++x)

#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double

const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;

#define MR 110

pair < int, int > T[MR][2];

queue < pair < string, int > > Q;

int d[MR][MR];  //ile czasu team spedzil nad dana karta
int c[MR];

pair < int, vector < string > > res[MR];

int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int n, t;
    cin >> n >> t;
    REP(i,n)
        cin >> T[i][0].ST >> T[i][0].ND >> T[i][1].ST >> T[i][1].ND;
    int m;
    cin >> m;
    REP(i,m)
    {
        pair < string, int > p;
        p.ND = i;
        cin >> p.ST >> c[i];
        Q.push(p);
    }
    int wsk = 0;    //z ktorego teamtu
    bool sel = 0;   //ktory gracz wyjasnia
    int left = t;   //ile czasu zostalo
    while(!Q.empty())
    {
        //wylicz ile czasu spedza
        pair < string, int > p = Q.front(); Q.pop();
        int timE = max(1,c[p.ND]-(T[wsk][sel].ST+T[wsk][!sel].ND)-d[wsk][p.ND]);
        if(timE <= left)    //zgadl
        {
            left -= timE;
            res[wsk].ST++;  //dodaj punkty
            res[wsk].ND.PB(p.ST);
        }
        else    //nie udalo sie
        {
            Q.push(p);
            d[wsk][p.ND] += left;
            if(wsk == n-1)
            {
                wsk = 0;
                sel = !sel; //zmiana gracza
            }
            else    //nastepny team bierze sie za rozwiazywanie
                wsk++;
            left = t;
        }
    }
    REP(i,n)
    {
        cout << res[i].ST;
        REP(j,(int)res[i].ND.size())
            cout << " " << res[i].ND[j];
        cout << "\n";
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <queue>
using namespace std;
#pragma warning (disable:4996)

struct team {int a1, b1, a2, b2;};
struct card {char name[21]; int c, d[100];};
int max(int a, int b) {return a>b ? a : b;};
int main(void)
{
    int n,t,i,j,m,p,tt,ttt;
    string str;
    card cards[100];
    card crd;
    team teams[100];
    queue<card> stck;
    vector<string> ans[100];
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);

    scanf("%d %d",&n,&t);

    for (i=0;i<n;i++)
    {
        scanf("%d %d %d %d",&teams[i].a1,&teams[i].b1,&teams[i].a2,&teams[i].b2);
    }
    scanf("%d",&m);
    for (i=0;i<m;i++)
    {
        scanf("%s",&cards[i].name);
        scanf("%d",&cards[i].c);
        for (j=0;j<n;j++) cards[i].d[j]=0;
        stck.push(cards[i]);
    }
    i=-1;
    p=0;
    ttt=t;
    while (stck.empty()==0)
    {
        i++;
        if (ttt==0) { ttt=t; continue;}
        if (i==n) {p=1-p; i=0;}
        crd=stck.front();
        stck.pop();
        if (p==0) {tt=max(1,crd.c-(teams[i].a1+teams[i].b2)-crd.d[i]);} else {tt=max(1,crd.c-(teams[i].a2+teams[i].b1)-crd.d[i]);};
        if (tt<=ttt) {ans[i].push_back(crd.name); ttt-=tt; i--;} else {crd.d[i]+=ttt; ttt=t; stck.push(crd);}
    }
    for (i=0;i<n;i++)
    {
        printf("%d ",ans[i].size());
        vector<string> :: iterator ii;
        for (ii=ans[i].begin(); ii!=ans[i].end(); ii++)
             {str=*ii; cout << str; printf(" ");}
        printf("\n");
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <queue>
using namespace std;
#pragma warning (disable:4996)

struct team {int a1, b1, a2, b2;};
struct card {char name[21]; int c, d[100];};
int max(int a, int b) {return a>b ? a : b;};
int main(void)
{
    int n,t,i,j,m,p,tt,ttt;
    string str;
    card cards[100];
    card crd;
    team teams[100];
    queue<card> stck;
    vector<string> ans[100];
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);

    scanf("%d %d",&n,&t);

    for (i=0;i<n;i++)
    {
        scanf("%d %d %d %d",&teams[i].a1,&teams[i].b1,&teams[i].a2,&teams[i].b2);
    }
    scanf("%d",&m);
    for (i=0;i<m;i++)
    {
        scanf("%s",&cards[i].name);
        scanf("%d",&cards[i].c);
        for (j=0;j<n;j++) cards[i].d[j]=0;
        stck.push(cards[i]);
    }
    i=-1;
    p=0;
    ttt=t;
    while (stck.empty()==0)
    {
        i++;
        if (i==n) {p=1-p; i=0;}
        crd=stck.front();
        stck.pop();
        if (p==0) {tt=max(1,crd.c-(teams[i].a1+teams[i].b2)-crd.d[i]);} else {tt=max(1,crd.c-(teams[i].a2+teams[i].b1)-crd.d[i]);};
        if (tt<=ttt) {ans[i].push_back(crd.name); ttt-=tt; i--;} else {crd.d[i]+=ttt; ttt=t; stck.push(crd);}
    }
    for (i=0;i<n;i++)
    {
        printf("%d ",ans[i].size());
        vector<string> :: iterator ii;
        for (ii=ans[i].begin(); ii!=ans[i].end(); ii++)
             {str=*ii; cout << str; printf(" ");}
        printf("\n");
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <queue>
using namespace std;
#pragma warning (disable:4996)

struct team {int a1, b1, a2, b2;};
struct card {char name[21]; int c, d[100];};
int max(int a, int b) {return a>b ? a : b;};
int main(void)
{
    int n,t,i,j,m,p,tt,ttt;
    string str;
    card cards[100];
    card crd;
    team teams[100];
    queue<card> stck;
    vector<string> ans[100];
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);

    scanf("%d %d",&n,&t);

    for (i=0;i<n;i++)
    {
        scanf("%d %d %d %d",&teams[i].a1,&teams[i].b1,&teams[i].a2,&teams[i].b2);
    }
    scanf("%d",&m);
    for (i=0;i<m;i++)
    {
        scanf("%s",&cards[i].name);
        scanf("%d",&cards[i].c);
        for (j=0;j<n;j++) cards[i].d[j]=0;
        stck.push(cards[i]);
    }
    i=-1;
    p=0;
    ttt=t;
    while (stck.empty()==0)
    {
        i++;
        if (i==n) {p=1-p; i=0;}

        crd=stck.front();
        stck.pop();
        if (p==0) {tt=max(1,crd.c-(teams[i].a1+teams[i].b2)-crd.d[i]);} else {tt=max(1,crd.c-(teams[i].a2+teams[i].b1)-crd.d[i]);};
        if (tt<=ttt) {ans[i].push_back(crd.name); ttt-=tt; i--;} else {crd.d[i]=tt-ttt; ttt=t; stck.push(crd);}
    }
    for (i=0;i<n;i++)
    {
        printf("%d ",ans[i].size());
        vector<string> :: iterator ii;
        for (ii=ans[i].begin(); ii!=ans[i].end(); ii++)
             {str=*ii; cout << str; printf(" ");}
        printf("\n");
    }

    return 0;
}


#include <cstdio>
#include <string.h>
#include <iostream>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <vector>

using namespace std;

int a[100][2], b[100][2];
int n, t, m;

vector <int> wins[100];

string words[100];
int c[100];

int d[100][100];
int u[100];

int main()
{
	freopen("input.txt", "r",  stdin);
	freopen("output.txt", "w",  stdout);

	cin >> n >> t;

	for (int i = 0; i < n; i++)
	{
		cin >> a[i][0] >> b[i][0] >> a[i][1] >> b[i][1];
	}

	cin >> m;

	for (int i = 0; i < m; i++)
	{
		cin >> words[i] >> c[i];
	}

	int r = 0;
	memset(u, 0, sizeof(u));

	int team = 0, player = 0;

	int w = 0;

	memset(d, 0, sizeof(d));

	while (w != m)
	{

		int k = t;
		while (k > 0)
		{

			while (u[r])
			{
				r = (r + 1) % m;
			}
			int p = max(1, c[r] - (a[team][player] + b[team][1 - player]) - d[r][team]);
			if (p > k)
			{
				d[r][team] += k;
				k = 0;
				r = (r + 1) % m;
			}
			else
			{
				k -= p;
				u[r] = true;
				wins[team].push_back(r);
				w++;
				if (w == m)
				{
					break;
				}
			}
		}

		team = (team + 1) % n;
		if (team == 0)
		{
			player = 1 - player;
		}

	}

	for (int i = 0; i < n; i++)
	{
		cout << wins[i].size() << " ";
		for (int j = 0; j < (int)wins[i].size(); j++)
		{
			cout << words[wins[i][j]] << " ";
		}
		cout << endl;
	}

	return 0;
}


#include <vector>
#include <cstring>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <queue>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <stdlib.h>
#include <ctime>
#include <string>
#include <complex>
using namespace std;
#define all(a) a.begin(),a.end()
#define forn(i,n) for(int i=0;i<(n);++i)
#define lng long long
#define SQ(a) ((a)*(a))
#define forv(i,v) for(int i=0;i<(int)v.size();++i)
#define mp make_pair
#define pb push_back
#define ABS(a) ((a)<0?-(a):(a))
#define iinf 1000000000
#define linf SQ(1LL*iinf)
#define left asdleft
#define prev asdprev
#define eps 1e-8
#define y1 asdy1
#define y0 asdy0
#define EQ(a,b) abs((a)-(b))<eps
#define PI 3.1415926535897932384626433832795
#define DEG2RAD PI/180
#define ulng unsigned lng
#define dinf = 1e200

int n,m;
int A[110][2];
int D[110][110];
string word[110];
vector<string> guessed[110];
int T;

#define taska ""
int main(int argc, char **argv){
#ifdef __ASD__
    freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#else
    //freopen(taska".in","r",stdin);freopen(taska".out","w",stdout);
    freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#endif

    //ios_base::sync_with_stdio(false);

    cin>>n>>T;
    forn(i,n){
        int a1,a2,b1,b2;
        cin>>a1>>b1>>a2>>b2;
        A[i][0]=a1+b2;
        A[i][1]=a2+b1;
    }
    cin>>m;
    queue<pair<int,int> > qu;
    forn(i,m){
        int c;
        cin>>word[i]>>c;
        qu.push(mp(c,i));
    }

    int cur=0;
    while(!qu.empty()){
        int tl=T;
        while(!qu.empty()){
            int w=qu.front().second;
            int C=qu.front().first;
            qu.pop();
            int t=max(1,C-A[cur][0]-D[cur][w]);
            if(t>=tl){
                D[cur][w]+=tl;
                qu.push(mp(C,w));
                break;
            }
            guessed[cur].pb(word[w]);
            tl-=t;
        }
        swap(A[cur][0],A[cur][1]);
        cur=(cur+1)%n;
    }

    forn(i,n){
        cout<<guessed[i].size();
        forv(j,guessed[i])
            cout<<' '<<guessed[i][j];
        cout<<'\n';
    }

    return 0;
}


#include <vector>
#include <cstring>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <queue>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <stdlib.h>
#include <ctime>
#include <string>
#include <complex>
using namespace std;
#define all(a) a.begin(),a.end()
#define forn(i,n) for(int i=0;i<(n);++i)
#define lng long long
#define SQ(a) ((a)*(a))
#define forv(i,v) for(int i=0;i<(int)v.size();++i)
#define mp make_pair
#define pb push_back
#define ABS(a) ((a)<0?-(a):(a))
#define iinf 1000000000
#define linf SQ(1LL*iinf)
#define left asdleft
#define prev asdprev
#define eps 1e-8
#define y1 asdy1
#define y0 asdy0
#define EQ(a,b) abs((a)-(b))<eps
#define PI 3.1415926535897932384626433832795
#define DEG2RAD PI/180
#define ulng unsigned lng
#define dinf = 1e200

int n,m;
int A[110][2];
int D[110][110];
string word[110];
vector<string> guessed[110];
int T;

#define taska ""
int main(int argc, char **argv){
#ifdef __ASD__
    freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#else
    //freopen(taska".in","r",stdin);freopen(taska".out","w",stdout);
    freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#endif

    //ios_base::sync_with_stdio(false);

    cin>>n>>T;
    forn(i,n){
        int a1,a2,b1,b2;
        cin>>a1>>b1>>a2>>b2;
        A[i][0]=a1+b2;
        A[i][1]=a2+b1;
    }
    cin>>m;
    queue<pair<int,int> > qu;
    forn(i,m){
        int c;
        cin>>word[i]>>c;
        qu.push(mp(c,i));
    }

    int cur=0;
    while(!qu.empty()){
        int tl=T;
        while(tl&&!qu.empty()){
            int w=qu.front().second;
            int C=qu.front().first;
            qu.pop();
            int t=max(1,C-A[cur][0]-D[cur][w]);
            if(t>tl){
                D[cur][w]+=tl;
                qu.push(mp(C,w));
                break;
            }
            guessed[cur].pb(word[w]);
            tl-=t;
        }
        swap(A[cur][0],A[cur][1]);
        cur=(cur+1)%n;
    }

    forn(i,n){
        cout<<guessed[i].size();
        forv(j,guessed[i])
            cout<<' '<<guessed[i][j];
        cout<<'\n';
    }

    return 0;
}


#include <vector>
#include <cstring>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <queue>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <stdlib.h>
#include <ctime>
#include <string>
#include <complex>
using namespace std;
#define all(a) a.begin(),a.end()
#define forn(i,n) for(int i=0;i<(n);++i)
#define lng long long
#define SQ(a) ((a)*(a))
#define forv(i,v) for(int i=0;i<(int)v.size();++i)
#define mp make_pair
#define pb push_back
#define ABS(a) ((a)<0?-(a):(a))
#define iinf 1000000000
#define linf SQ(1LL*iinf)
#define left asdleft
#define prev asdprev
#define eps 1e-8
#define y1 asdy1
#define y0 asdy0
#define EQ(a,b) abs((a)-(b))<eps
#define PI 3.1415926535897932384626433832795
#define DEG2RAD PI/180
#define ulng unsigned lng
#define dinf = 1e200

int n,m;
int A[110][2];
int D[110][110];
string word[110];
vector<string> guessed[110];
int T;

#define taska ""
int main(int argc, char **argv){
#ifdef __ASD__
    freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#else
    //freopen(taska".in","r",stdin);freopen(taska".out","w",stdout);
    freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#endif

    //ios_base::sync_with_stdio(false);

    cin>>n>>T;
    forn(i,n){
        int a1,a2,b1,b2;
        cin>>a1>>b1>>a2>>b2;
        A[i][0]=a1+b2;
        A[i][1]=a2+b1;
    }
    cin>>m;
    queue<pair<int,int> > qu;
    forn(i,m){
        int c;
        cin>>word[i]>>c;
        qu.push(mp(c,i));
    }

    int cur=0;
    while(!qu.empty()){
        int tl=T;
        while(!qu.empty()){
            int w=qu.front().second;
            int C=qu.front().first;
            qu.pop();
            int t=max(1,C-A[cur][0]-D[cur][w]);
            if(t>tl){
                D[cur][w]+=tl;
                qu.push(mp(C,w));
                break;
            }
            guessed[cur].pb(word[w]);
            tl-=t;
        }
        swap(A[cur][0],A[cur][1]);
        cur=(cur+1)%n;
    }

    forn(i,n){
        cout<<guessed[i].size();
        forv(j,guessed[i])
            cout<<' '<<guessed[i][j];
        cout<<'\n';
    }

    return 0;
}


#include <vector>
#include <cstring>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <queue>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <stdlib.h>
#include <ctime>
#include <string>
#include <complex>
using namespace std;
#define all(a) a.begin(),a.end()
#define forn(i,n) for(int i=0;i<(n);++i)
#define lng long long
#define SQ(a) ((a)*(a))
#define forv(i,v) for(int i=0;i<(int)v.size();++i)
#define mp make_pair
#define pb push_back
#define ABS(a) ((a)<0?-(a):(a))
#define iinf 1000000000
#define linf SQ(1LL*iinf)
#define left asdleft
#define prev asdprev
#define eps 1e-8
#define y1 asdy1
#define y0 asdy0
#define EQ(a,b) abs((a)-(b))<eps
#define PI 3.1415926535897932384626433832795
#define DEG2RAD PI/180
#define ulng unsigned lng
#define dinf = 1e200

int n,m;
int A[110][2];
int D[110][110];
string word[110];
vector<string> guessed[110];
int T;

#define taska ""
int main(int argc, char **argv){
#ifdef __ASD__
    freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#else
    //freopen(taska".in","r",stdin);freopen(taska".out","w",stdout);
    freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#endif

    //ios_base::sync_with_stdio(false);

    cin>>n>>T;
    forn(i,n){
        int a1,a2,b1,b2;
        cin>>a1>>b1>>a2>>b2;
        A[i][0]=a1+b2;
        A[i][1]=a2+b1;
    }
    cin>>m;
    queue<pair<int,int> > qu;
    forn(i,m){
        int c;
        cin>>word[i]>>c;
        qu.push(mp(c,i));
    }

    int cur=0;
    while(!qu.empty()){
        swap(A[cur][0],A[cur][1]);
        int tl=T;
        while(!qu.empty()){
            int w=qu.front().second;
            int C=qu.front().first;
            qu.pop();
            int t=max(1,C-A[cur][0]-D[cur][w]);
            if(t>tl){
                D[cur][w]+=tl;
                qu.push(mp(C,w));
                break;
            }
            guessed[cur].pb(word[w]);
            tl-=t;
        }
        cur=(cur+1)%n;
    }

    forn(i,n){
        cout<<guessed[i].size();
        forv(j,guessed[i])
            cout<<' '<<guessed[i][j];
        cout<<'\n';
    }

    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <iostream>

using namespace std;

#pragma comment(linker, "/STACK:160000000")

typedef long long ll;
typedef pair<int, int> pii;

const int N = 10000000;

int a[200][2];
int b[200][2];
int c[200];
int d[200][200];
vector <int> ans[200];
int q[N];
char s[200][200];

int main() {
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	int n, t;
	scanf("%d%d", &n, &t);
	for (int i = 0; i < n; ++i) {
		scanf("%d%d%d%d", &a[i][0], &b[i][0], &a[i][1], &b[i][1]);
	}
	int m;
	scanf("%d ", &m);
	for (int i = 0; i < m; ++i) {
		gets(s[i]);
		scanf("%d ", &c[i]);
		q[i] = i;
	}

	int l = 0;
	int r = m;
	int cur_team = 0;
	int cur_man = 0;
	int left_time = t;

	while (l != r) {
		int cur_word = q[l++];
		if (l == N) {
			l = 0;
		}
		int need_time = max(1, c[cur_word] - (a[cur_team][cur_man] - b[cur_team][1 - cur_man]) - d[cur_team][cur_word]);
		if (need_time <= left_time) {
			left_time -= need_time;
			ans[cur_team].push_back(cur_word);
		} else {
			q[r++] = cur_word;
			if (r == N)
				r = 0;
			d[cur_team][cur_word] += left_time;
			left_time = 0;
		}
		if (left_time == 0) {
			++cur_team;
			if (cur_team == n) {
				cur_team = 0;
				cur_man ^= 1;
			}
			left_time = t;
		}
	}

	for (int i = 0; i < n; ++i) {
		printf("%d ", ans[i].size());
		for (int j = 0; j < ans[i].size(); ++j) {
			printf("%s ", s[ans[i][j]]);
		}
		printf("\n");
	}

	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <iostream>

using namespace std;

#pragma comment(linker, "/STACK:160000000")

typedef long long ll;
typedef pair<int, int> pii;

const int N = 10000000;

int a[200][2];
int b[200][2];
int c[200];
int d[200][200];
vector <int> ans[200];
int q[N];
char s[200][200];

int main() {
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	int n, t;
	scanf("%d%d", &n, &t);
	for (int i = 0; i < n; ++i) {
		scanf("%d%d%d%d", &a[i][0], &b[i][0], &a[i][1], &b[i][1]);
	}
	int m;
	scanf("%d ", &m);
	for (int i = 0; i < m; ++i) {
		gets(s[i]);
		scanf("%d ", &c[i]);
		q[i] = i;
	}

	int l = 0;
	int r = m;
	int cur_team = 0;
	int cur_man = 0;
	int left_time = t;

	while (l != r) {
		int cur_word = q[l++];
		if (l == N) {
			l = 0;
		}
		int need_time = max(1, c[cur_word] - (a[cur_team][cur_man] + b[cur_team][1 - cur_man]) - d[cur_team][cur_word]);
		if (need_time <= left_time) {
			left_time -= need_time;
			ans[cur_team].push_back(cur_word);
		} else {
			q[r++] = cur_word;
			if (r == N)
				r = 0;
			d[cur_team][cur_word] += left_time;
			left_time = 0;
		}
		if (left_time == 0) {
			++cur_team;
			if (cur_team == n) {
				cur_team = 0;
				cur_man ^= 1;
			}
			left_time = t;
		}
	}

	for (int i = 0; i < n; ++i) {
		printf("%d ", ans[i].size());
		for (int j = 0; j < ans[i].size(); ++j) {
			printf("%s ", s[ans[i][j]]);
		}
		printf("\n");
	}

	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <vector>

#pragma comment(linker,"/STACK:640000000")

using namespace std;

int n , t , m , a[101][2] , b[101][2] , c[101] , d[101][101] ;
string s[101];
queue <int> q;

map <int , vector <string> > score;

int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    scanf("%d%d",&n,&t);
    for (int i = 1 ; i <= n ; i ++ )
        scanf("%d%d%d%d",&a[i][0],&b[i][0],&a[i][1],&b[i][1]);
    scanf("%d",&m);
    for (int i = 1 ; i <= m ; i ++ )
    {
        cin >> s[i] >> c[i];
        q.push(i);
    }

    int gamer = 1 , type = 0 ;

    while (!q.empty() )
    {
        int T = t;
        while ( T > 0 )
        {
            if ( q.empty() ) break;
            int v = q.front();
            q.pop();
            int ti = max ( 1 , c[v] - (a[gamer][type] + b[gamer][(type + 1)%2]) - d[gamer][v]);
            if ( ti > T )
            {
                d[gamer][v] += T;
                q.push(v);
                break;
            }
            score[gamer].push_back(s[v]);
            T -= ti;
        }

        gamer ++ ;
        if ( gamer == n + 1 )
            gamer = 1 , type = (type + 1) % 2;
    }

    for (int i =1 ; i <= n ; i ++ )
    {
        printf("%d ",score[i].size() );
        for (int j = 0 ; j < score[i].size() ; j ++ )
            cout << score[i][j] << " ";
        cout << endl;
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <vector>

#pragma comment(linker,"/STACK:640000000")

using namespace std;

int n , t , m , a[101][2] , b[101][2] , c[101] , d[101][101] ;
string s[101];
queue <int> q;

map <int , vector <string> > score;

int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    scanf("%d%d",&n,&t);
    for (int i = 1 ; i <= n ; i ++ )
        scanf("%d%d%d%d",&a[i][0],&b[i][0],&a[i][1],&b[i][1]);
    scanf("%d",&m);
    for (int i = 1 ; i <= m ; i ++ )
    {
        cin >> s[i] >> c[i];
        q.push(i);
    }

    int gamer = 1 , type = 0 ;

    while (!q.empty() )
    {
        int T = t;
        while ( T > 0 )
        {
            if ( q.empty() ) break;
            int v = q.front();
            q.pop();
            int ti = max ( 1 , c[v] - (a[gamer][type] + a[gamer][(type + 1)%2]) - d[gamer][v]);
            if ( ti > T )
            {
                d[gamer][v] += ti;
                q.push(v);
                break;
            }
            score[gamer].push_back(s[v]);
            T -= ti;
        }

        gamer ++ ;
        if ( gamer == n + 1 )
            gamer = 1 , type = (type + 1) % 2;
    }

    for (int i =1 ; i <= n ; i ++ )
    {
        printf("%d ",score[i].size() );
        for (int j = 0 ; j < score[i].size() ; j ++ )
            cout << score[i][j] << " ";
        cout << endl;
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <vector>

#pragma comment(linker,"/STACK:640000000")

using namespace std;

int n , t , m , a[101][2] , b[101][2] , c[101] , d[101][101] ;
string s[101];
queue <int> q;

map <int , vector <string> > score;

int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    scanf("%d%d",&n,&t);
    for (int i = 1 ; i <= n ; i ++ )
        scanf("%d%d%d%d",&a[i][0],&b[i][0],&a[i][1],&b[i][1]);
    scanf("%d",&m);
    for (int i = 1 ; i <= m ; i ++ )
    {
        cin >> s[i] >> c[i];
        q.push(i);
    }

    int gamer = 1 , type = 0 ;

    while (!q.empty() )
    {
        int T = t;
        while ( T > 0 )
        {
            if ( q.empty() ) break;
            int v = q.front();
            q.pop();
            int ti = max ( 1 , c[v] - (a[gamer][type] + b[gamer][(type + 1)%2]) - d[gamer][v]);
            if ( ti > T )
            {
                d[gamer][v] += ti;
                q.push(v);
                break;
            }
            score[gamer].push_back(s[v]);
            T -= ti;
        }

        gamer ++ ;
        if ( gamer == n + 1 )
            gamer = 1 , type = (type + 1) % 2;
    }

    for (int i =1 ; i <= n ; i ++ )
    {
        printf("%d ",score[i].size() );
        for (int j = 0 ; j < score[i].size() ; j ++ )
            cout << score[i][j] << " ";
        cout << endl;
    }

    return 0;
}


#pragma comment(linker,"/STACK:300000000")
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
#include <memory.h>
#include <cstdio>
#include <sstream>
#include <deque>
#include <bitset>
#include <numeric>
#include <ctime>

using namespace std;

#define show(x) cout << #x << " = " << (x) << endl;
#define fori(i,n) for(int i = 0; i < (n); i++)
#define forn(i,n) for(int i = 1; i <= (n); i++)
#define forab(i,a,b) for(int i = (a); i <= (b); i++)
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
const double pi = 3.1415926535897932384626433832795;
template<class T> T abs(T &a) { return a >= 0 ? a : -a; };
template<class T> T sqr(T &x) { return x * x; }
typedef pair<int,int> ii;
typedef long long ll;
///////////////////////////////////////

int n, t, m;
int a[2][200], b[2][200];
vector<int> c;
vector<string> word;
deque<int> q;
vector<vector<int> > d;
vector<vector<int> > ans;

void process(int k, int who)
{
    int curT = t;
    while(true)
    {
        if(q.empty())
            return;
        int cur = q.front();
        q.pop_front();
        int T = max(1,c[cur]-(a[who][k]+b[1-who][k])-d[k][cur]);
        if(T < curT)
        {
            ans[k].push_back(cur);
            curT -= T;
        }
        else if(T > curT)
        {
            d[k][cur] += curT;
            q.push_back(cur);
            return;
        }
        else //????
        {
            ans[k].push_back(cur);
            return;
        }
    }
}

void print()
{
    fori(i,n)
    {
        cout << sz(ans[i]);
        fori(j,sz(ans[i]))
            cout << ' ' << word[ans[i][j]];
        cout << endl;
    }
}

__int32 main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    cin >> n >> t;
    ans.resize(n);
    fori(i,n)
        cin >> a[0][i] >> a[1][i] >> b[0][i] >> b[1][i];
    cin >> m;
    d.resize(n,vector<int>(m));
    c.resize(m);
    word.resize(m);
    fori(i,m)
        cin >> word[i] >> c[i];
    d.resize(m);
    fori(i,m)
        q.push_back(i);
    while(true)
        fori(j,2)
            fori(i,n)
            {
                process(i,j);
                if(q.empty())
                {
                    print();
                    return 0;
                }
            }
 }


#pragma comment(linker,"/STACK:300000000")
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
#include <memory.h>
#include <cstdio>
#include <sstream>
#include <deque>
#include <bitset>
#include <numeric>
#include <ctime>

using namespace std;

#define show(x) cout << #x << " = " << (x) << endl;
#define fori(i,n) for(int i = 0; i < (n); i++)
#define forn(i,n) for(int i = 1; i <= (n); i++)
#define forab(i,a,b) for(int i = (a); i <= (b); i++)
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
const double pi = 3.1415926535897932384626433832795;
template<class T> T abs(T &a) { return a >= 0 ? a : -a; };
template<class T> T sqr(T &x) { return x * x; }
typedef pair<int,int> ii;
typedef long long ll;
///////////////////////////////////////

int n, t, m;
int a[2][200], b[2][200];
vector<int> c;
vector<string> word;
deque<int> q;
vector<vector<int> > d;
vector<vector<int> > ans;

void process(int k, int who)
{
    int curT = t;
    while(true)
    {
        if(q.empty())
            return;
        int cur = q.front();
        q.pop_front();
        int T = max(1,c[cur]-(a[who][k]+b[1-who][k])-d[k][cur]);
        if(T < curT)
        {
            ans[k].push_back(cur);
            curT -= T;
        }
        else if(T > curT)
        {
            d[k][cur] += curT;
            q.push_back(cur);
            return;
        }
        else //????
        {
            ans[k].push_back(cur);
            return;
        }
    }
}

void print()
{
    fori(i,n)
    {
        cout << sz(ans[i]);
        fori(j,sz(ans[i]))
            cout << ' ' << word[ans[i][j]];
        cout << endl;
    }
}

__int32 main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    cin >> n >> t;
    ans.resize(n);
    fori(i,n)
        cin >> a[0][i] >> a[1][i] >> b[0][i] >> b[1][i];
    cin >> m;
    d.resize(n,vector<int>(m));
    c.resize(m);
    word.resize(m);
    fori(i,m)
        cin >> word[i] >> c[i];
    d.resize(m);
    fori(i,m)
        q.push_back(i);
    fori(j,2)
        fori(i,n)
        {
            process(i,j);
            if(q.empty())
            {
                print();
                return 0;
            }
        }
 }


#define MY

#include <string>
#include <vector>
//#pragma comment(linker, "/STACK:128000000")

#ifdef MY
#include <fstream>
using namespace std;
ifstream cin("input.txt");
ofstream cout("output.txt");
#else
#include <iostream>
using namespace std;
#endif

int main(){
    long long n, t, m;
    cin >> n >> t;
    vector< vector<int> > a(n, vector<int>(2));
    vector< vector<int> > b(n, vector<int>(2));
    for(int i = 0; i < n; ++i)
        cin >> a[i][0] >> b[i][0] >> a[i][1] >> b[i][1];
    cin >> m;
    vector< vector<int> > d(max(n, m), vector<int>(max(n, m)));
    vector< pair<string, int> > c(max(n, m));
    vector< pair<int, int> > p(max(n, m), pair<int, int>(0, 0) );
    for(int i = 0; i < m; ++i)
        cin >> c[i].first >> c[i].second;
    int cnt = 0, cc = 0;
    pair<int, int> cur = pair<int, int>(0, 0);
    while(cnt != m){
        int q = t, tt, i = cur.first, j = cur.second;
        while(q != 0 && cnt != m){
            while(p[cc].second != 0){
                ++cc;
                if (cc == m)
                    cc = 0;
            }
            tt = max(1, c[cc].second - (a[i][j] + b[i][(j + 1) % 2]) - d[i][cc]);
            if (tt <= q){
                p[cc].first = i;
                p[cc].second = ++cnt;
                q -= tt;
            }
            else{
                d[i][cc] += q;
                q = 0;
                ++cc;
            }
        }
        ++cur.first;
        if (cur.first == n)
            cur = pair<int, int>(0, (cur.second + 1) % 2);
    }
    for(int i = 0; i < n; ++i){
        int u = 0;
        for(int k = 0; k < m; ++k)
            if (p[k].first == i)
                ++u;
        cout << u << ' ';
        for(int j = 1; j <= cnt; ++j)
            for(int k = 0; k < m; ++k)
                if (p[k].first == i && p[k].second == j)
                    cout << c[k].first << ' ';
        cout << '\n';
    }
}


#define MY

#include <string>
#include <vector>
//#pragma comment(linker, "/STACK:128000000")

#ifdef MY
#include <fstream>
using namespace std;
ifstream cin("input.txt");
ofstream cout("output.txt");
#else
#include <iostream>
using namespace std;
#endif

int main(){
    long long n, t, m;
    cin >> n >> t;
    vector< vector<int> > a(n, vector<int>(2));
    vector< vector<int> > b(n, vector<int>(2));
    for(int i = 0; i < n; ++i)
        cin >> a[i][0] >> b[i][0] >> a[i][1] >> b[i][1];
    cin >> m;
    vector< vector<int> > d(max(n, m), vector<int>(max(n, m)));
    vector< pair<string, int> > c(max(n, m));
    vector< pair<int, int> > p(max(n, m), pair<int, int>(0, 0) );
    for(int i = 0; i < m; ++i)
        cin >> c[i].first >> c[i].second;
    int cnt = 0, cc = 0;
    pair<int, int> cur = pair<int, int>(0, 0);
    while(cnt != m){
        int q = t, tt, i = cur.first, j = cur.second;
        while(q != 0 && cnt != m){
            cc %= m;
            while(p[cc].second != 0){
                ++cc;
                if (cc == m)
                    cc = 0;
            }
            tt = max(1, c[cc].second - (a[i][j] + b[i][(j + 1) % 2]) - d[i][cc]);
            if (tt <= q){
                p[cc].first = i;
                p[cc].second = ++cnt;
                q -= tt;
            }
            else{
                d[i][cc] += q;
                q = 0;
                ++cc;
            }
        }
        ++cur.first;
        if (cur.first == n)
            cur = pair<int, int>(0, (cur.second + 1) % 2);
    }
    for(int i = 0; i < n; ++i){
        int u = 0;
        for(int k = 0; k < m; ++k)
            if (p[k].first == i)
                ++u;
        cout << u << ' ';
        for(int j = 1; j <= cnt; ++j)
            for(int k = 0; k < m; ++k)
                if (p[k].first == i && p[k].second == j)
                    cout << c[k].first << ' ';
        cout << '\n';
    }
}


#define MY

#include <string>
#include <vector>
//#pragma comment(linker, "/STACK:128000000")

#ifdef MY
#include <fstream>
using namespace std;
ifstream cin("input.txt");
ofstream cout("output.txt");
#else
#include <iostream>
using namespace std;
#endif

int main(){
    long long n, t, m;
    string s;
    cin >> n >> t;
    vector< vector<int> > a(n, vector<int>(2));
    vector< vector<int> > b(n, vector<int>(2));
    for(int i = 0; i < n; ++i)
        cin >> a[i][0] >> b[i][0] >> a[i][1] >> b[i][1];
    cin >> m;
    vector< vector<int> > d(n, vector<int>(m));
    vector< pair<string, int> > c(m);
    vector< pair<int, int> > p(m, pair<int, int>(0, 0) );
    for(int i = 0; i < m; ++i)
        cin >> c[i].first >> c[i].second;
    int cnt = 0, cc = 0;
    pair<int, int> cur = pair<int, int>(0, 0);
    while(cnt != m){
        int q = t, tt, i = cur.first, j = cur.second;
        while(q != 0 && cnt != m){
            while(p[cc].second != 0){
                ++cc;
                if (cc == m)
                    cc = 0;
            }
            tt = max(1, c[cc].second - (a[i][j] + b[i][(j + 1) % 2]) - d[i][cc]);
            if (tt <= q){
                p[cc].first = i;
                p[cc].second = ++cnt;
                q -= tt;
            }
            else{
                d[i][cc] += q;
                q = 0;
                ++cc;
            }
        }
        ++cur.first;
        if (cur.first == n)
            cur = pair<int, int>(0, (cur.second + 1) % 2);
    }
    for(int i = 0; i < n; ++i){
        int u = 0;
        for(int k = 0; k < m; ++k)
            if (p[k].first == i)
                ++u;
        cout << u << ' ';
        for(int j = 0; j <= cnt; ++j)
            for(int k = 0; k < m; ++k)
                if (p[k].first == i && p[k].second == j)
                    cout << c[k].first << ' ';
        cout << '\n';
    }
}


#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

int n, t;
int a[500], b[500];
int m;
char s[500][500];
int c[500];
int q[10000000];
int d[500][500];
vector<int> ans[500];

int Other(int i)
{
    if (i < n) return i+n;
    return i-n;
}

int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    scanf("%d%d", &n, &t);
    for (int i = 0; i < n; i++)
    {
        scanf("%d%d", &a[i], &b[i]);
        scanf("%d%d", &a[i+n], &b[i+n]);
    }
    scanf("%d", &m);
    for (int j = 0; j < m; j++)
    {
        scanf("%s", s[j]);
        scanf("%d", &c[j]);
    }
    int l = 0, r = m-1;
    for (int i = 0; i < m; i++)
        q[i] = i;

    int i = 0;
    int tt = t;
    while (true)
    {
        if (l > r) break;
        int curWord = q[l++];
        int j = Other(i);
        int teamId = min(i,j);
        int need = max(1, c[curWord] - a[i] - b[j] - d[teamId][curWord]);
        if (need <= tt)
        {
            tt -= need;
            ans[teamId].push_back(curWord);
        }
        else
        {
            d[teamId][curWord] += tt;
            tt = 0;
            q[++r] = curWord;
        }
        if (tt == 0)
        {
            i++;
            if (i == 2*n) i = 0;
            tt = t;
        }
    }

    for (int i = 0; i < n; i++)
    {
        printf("%d ", ans[i].size());
        for (int j = 0; j < ans[i].size(); j++)
            printf("%s ", s[ans[i][j]]);
        printf("\n");
    }

    return 0;
}


#include<iostream>
#include<vector>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
using namespace std;

int a[101][2], b[101][2], c[101];
int d[101][101];
vector<vector<int> > ans;
vector<string> word;
int main()
{
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);

	int n, t, m;
	cin >> n >> t;
	ans.resize(n);
	for(int i = 0; i < n; ++i)
		cin >> a[i][0] >> b[i][0] >> a[i][1] >> b[i][1];
	cin >> m;
	word.resize(m);
	for(int i = 0; i < m; ++i)
		cin >> word[i] >> c[i];
	queue<int> q;
	for(int i = 0; i < m; ++i)
		q.push(i);
	int cmd = 0, cur = 0, free, tim;
	while(!q.empty())
	{
		free = t;
		while(free > 0 && !q.empty())
		{
			int v = q.front();
			q.pop();
			tim = max(1, c[v]-(a[cmd][cur]+b[cmd][1-cur])-d[cmd][v]);
			if(tim > free)
			{
				d[cmd][v] += free;
				q.push(v);
				break;
			}
			else
			{
				ans[cmd].push_back(v);
				free -= tim;
			}
		}
		cmd++;
		if(cmd == n)
			cmd = 0, cur = 1 - cur;
	}
	for(int i = 0; i < n; ++i)
	{
		cout << ans[i].size() << ' ';
		for(int j = 0; j < ans[i].size(); ++j)
			cout << word[ans[i][j]] << ' ';
		cout << endl;
	}
	return 0;
}


#include<iostream>
#include<vector>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
using namespace std;

int a[101][2], b[101][2], c[101];
int d[101][101];
vector<vector<int> > ans;
vector<string> word;
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);

	int n, t, m;
	cin >> n >> t;
	ans.resize(n);
	for(int i = 0; i < n; ++i)
		cin >> a[i][0] >> b[i][0] >> a[i][1] >> b[i][1];
	cin >> m;
	word.resize(m);
	for(int i = 0; i < m; ++i)
		cin >> word[i] >> c[i];
	queue<int> q;
	for(int i = 0; i < m; ++i)
		q.push(i);
	int cmd = 0, cur = 0, free, tim;
	while(!q.empty())
	{
		int v = q.front();
		q.pop();

		free = t;
		while(true)
		{
			tim = max(1, c[v]-(a[cmd][cur]+b[cmd][1-cur])-d[cmd][v]);
			if(tim > free)
			{
				d[cmd][v] += t;
				q.push(v);
				break;
			}
			else
			{
				ans[cmd].push_back(v);
				free -= tim;
				if(!q.empty())
					v = q.front(), q.pop();
				else
					break;
			}
		}
		cmd++;
		if(cmd == n)
			cmd = 0, cur = 1 - cur;
	}
	for(int i = 0; i < n; ++i)
	{
		cout << ans[i].size() << ' ';
		for(int j = 0; j < ans[i].size(); ++j)
			cout << word[ans[i][j]] << ' ';
		cout << endl;
	}
	return 0;
}


#include<iostream>
#include<vector>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
using namespace std;

int a[101][2], b[101][2], c[101];
int d[101][101];
vector<vector<int> > ans;
vector<string> word;
int main()
{
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);

	int n, t, m;
	cin >> n >> t;
	ans.resize(n);
	for(int i = 0; i < n; ++i)
		cin >> a[i][0] >> b[i][0] >> a[i][1] >> b[i][1];
	cin >> m;
	word.resize(m);
	for(int i = 0; i < m; ++i)
		cin >> word[i] >> c[i];
	queue<int> q;
	for(int i = 0; i < m; ++i)
		q.push(i);
	int cmd = 0, cur = 0, free, tim;
	while(!q.empty())
	{
		int v = q.front();
		q.pop();

		free = t;
		while(true)
		{
			tim = max(1, c[v]-(a[cmd][cur]+b[cmd][1-cur])-d[cmd][v]);
			if(tim > free)
			{
				d[cmd][v] += free;
				q.push(v);
				break;
			}
			else
			{
				ans[cmd].push_back(v);
				free -= tim;
				if(!q.empty())
					v = q.front(), q.pop();
				else
					break;
			}
		}
		cmd++;
		if(cmd == n)
			cmd = 0, cur = 1 - cur;
	}
	for(int i = 0; i < n; ++i)
	{
		cout << ans[i].size() << ' ';
		for(int j = 0; j < ans[i].size(); ++j)
			cout << word[ans[i][j]] << ' ';
		cout << endl;
	}
	return 0;
}


#include<iostream>
#include<vector>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
using namespace std;

int a[101][2], b[101][2], c[101];
int d[101][101];
vector<vector<int> > ans;
vector<string> word;
int main()
{
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);

	int n, t, m;
	cin >> n >> t;
	ans.resize(n);
	for(int i = 0; i < n; ++i)
		cin >> a[i][0] >> b[i][0] >> a[i][1] >> b[i][1];
	cin >> m;
	word.resize(m);
	for(int i = 0; i < m; ++i)
		cin >> word[i] >> c[i];
	queue<int> q;
	for(int i = 0; i < m; ++i)
		q.push(i);
	int cmd = 0, cur = 0, free, tim;
	while(!q.empty())
	{
		int v = q.front();
		q.pop();

		free = t;
		while(true)
		{
			tim = max(1, c[v]-(a[cmd][cur]+b[cmd][1-cur])-d[cmd][v]);
			if(tim > free)
			{
				d[cmd][v] += t;
				q.push(v);
				break;
			}
			else
			{
				ans[cmd].push_back(v);
				free -= tim;
				if(!q.empty())
					v = q.front(), q.pop();
				else
					break;
			}
		}
		cmd++;
		if(cmd == n)
			cmd = 0, cur = 1 - cur;
	}
	for(int i = 0; i < n; ++i)
	{
		cout << ans[i].size() << ' ';
		for(int j = 0; j < ans[i].size(); ++j)
			cout << word[ans[i][j]] << ' ';
		cout << endl;
	}
	return 0;
}


#pragma comment (linker, "/stack:64000000")
#define _CRT_SECURE_NO_DEPRECATE
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define forn(i,n) for (int i = 0; i < int(n); i++)
#define fore(i,a,b) for (int i = int(a); i < int(b); i++)
#define ford(i,n) for (int i = int(n) - 1; i >= 0; i--)
#define for1(i,n) for (int i = 1; i <= int(n); i++)
#define mp make_pair
#define pb push_back
#define vi vector<int>
#define pii pair<int, int>
#define ll long long
#define ld long double
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()
#define y0 goodlife

int n, t, a[200][2], b[200][2], c[200], m, d[200][200];

string word[200];

vector<string> res[200];

queue<int> q;

int curTime;

void go (int team, int p) {
    int curWord = q.front();
    q.pop();
    int add = max (1, c[curWord] - (a[team][p] + b[team][!p]) - d[team][curWord]);
    if (add <= curTime) {
        curTime -= add;
        res[team].pb (word[curWord]);
    }
    else {
        d[team][curWord] += add;
        q.push (curWord);
        curTime = 0;
    }
}

int main () {
    freopen ("input.txt", "r", stdin);
    freopen ("output.txt", "w", stdout);
    cin >> n >> t;
    forn (i, n)
        cin >> a[i][0] >> b[i][0] >> a[i][1] >> b[i][1];
    cin >> m;
    forn (i, m)
        cin >> word[i] >> c[i];
    forn (i, m)
        q.push (i);
    while (!q.empty() ) {
        forn (i, n) {
            curTime = t;
            while (!q.empty() && curTime > 0) go (i, 0);
        }
        forn (i, n) {
            curTime = t;
            while (!q.empty() && curTime > 0) go (i, 1);
        }
    }
    forn (i, n) {
        cout << sz(res[i]) << ' ';
        forn (j, sz(res[i]) )
            cout << res[i][j] << ' ';
        cout << endl;
    }
}


#pragma comment (linker, "/stack:64000000")
#define _CRT_SECURE_NO_DEPRECATE
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define forn(i,n) for (int i = 0; i < int(n); i++)
#define fore(i,a,b) for (int i = int(a); i < int(b); i++)
#define ford(i,n) for (int i = int(n) - 1; i >= 0; i--)
#define for1(i,n) for (int i = 1; i <= int(n); i++)
#define mp make_pair
#define pb push_back
#define vi vector<int>
#define pii pair<int, int>
#define ll long long
#define ld long double
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()
#define y0 goodlife

int n, t, a[200][2], b[200][2], c[200], m, d[200][200];

string word[200];

vector<string> res[200];

queue<int> q;

int curTime;

void go (int team, int p) {
    int curWord = q.front();
    q.pop();
    int add = max (1, c[curWord] - (a[team][p] + b[team][!p]) - d[team][curWord]);
    if (add <= curTime) {
        curTime -= add;
        res[team].pb (word[curWord]);
    }
    else {
        d[team][curWord] += curTime;
        q.push (curWord);
        curTime = 0;
    }
}

int main () {
    freopen ("input.txt", "r", stdin);
    freopen ("output.txt", "w", stdout);
    cin >> n >> t;
    forn (i, n)
        cin >> a[i][0] >> b[i][0] >> a[i][1] >> b[i][1];
    cin >> m;
    forn (i, m)
        cin >> word[i] >> c[i];
    forn (i, m)
        q.push (i);
    while (!q.empty() ) {
        forn (i, n) {
            curTime = t;
            while (!q.empty() && curTime > 0) go (i, 0);
        }
        forn (i, n) {
            curTime = t;
            while (!q.empty() && curTime > 0) go (i, 1);
        }
    }
    forn (i, n) {
        cout << sz(res[i]) << ' ';
        forn (j, sz(res[i]) )
            cout << res[i][j] << ' ';
        cout << endl;
    }
}


#pragma comment (linker, "/stack:64000000")
#define _CRT_SECURE_NO_DEPRECATE
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define forn(i,n) for (int i = 0; i < int(n); i++)
#define fore(i,a,b) for (int i = int(a); i < int(b); i++)
#define ford(i,n) for (int i = int(n) - 1; i >= 0; i--)
#define for1(i,n) for (int i = 1; i <= int(n); i++)
#define mp make_pair
#define pb push_back
#define vi vector<int>
#define pii pair<int, int>
#define ll long long
#define ld long double
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()
#define y0 goodlife

int n, t, a[200][2], b[200][2], c[200], m, d[200][200];

string word[200];

vector<string> res[200];

queue<int> q;

int curTime;

void go (int team, int p) {
    int curWord = q.front();
    q.pop();
    int add = max (1, c[curWord] - (a[team][p] + b[team][!p]) - d[team][curWord]);
    if (add <= curTime) {
        curTime -= add;
        res[team].pb (word[curWord]);
    }
    else {
        d[team][curWord] += add;
        q.push (curWord);
        curTime = 0;
    }
}

int main () {
    freopen ("input.txt", "r", stdin);
    freopen ("output.txt", "w", stdout);
    cin >> n >> t;
    forn (i, n)
        cin >> a[i][0] >> b[i][0] >> a[i][1] >> b[i][1];
    cin >> m;
    forn (i, m)
        cin >> word[i] >> c[i];
    forn (i, m)
        q.push (i);
    forn (i, n) {
        curTime = t;
        while (!q.empty() && curTime > 0) go (i, 0);
    }
    forn (i, n) {
        curTime = t;
        while (!q.empty() && curTime > 0) go (i, 1);
    }
    forn (i, n) {
        cout << sz(res[i]) << ' ';
        forn (j, sz(res[i]) )
            cout << res[i][j] << ' ';
        cout << endl;
    }
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char s[110][110];
int c[110],a[110][110],b[110][110],d[110][110],use[110],cnt[110],check[110][110];
int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int n,t;
    while (scanf("%d%d",&n,&t)==2)
    {
        for (int i=0; i<n; i++)
            for (int j=0; j<2; j++)
                scanf("%d%d",&a[i][j],&b[i][j]);
        int m;
        scanf("%d",&m);
        for (int i=0; i<m; i++)
            scanf("%s%d",s[i],&c[i]);
        int k=0;
        memset(use,0,sizeof(use));
        memset(cnt,0,sizeof(cnt));
        bool end=false;
        while (!end)
        {
            for (int j=0; j<2&&!end; j++)
            {
                for (int i=0; i<n&&!end; i++)
                {
                    int time=t;
                    while (time>0&&!end)
                    {
                        int q;
                        if (j==0) q=1;
                        else q=0;
                        int cost=max(1,c[k]-(a[i][j]+b[i][q])-d[i][k]);
                        if (cost<=time)
                        {
                            time-=cost;
                            check[i][cnt[i]++]=k;
                            use[k]=1;
                        }
                        else
                        {
                            d[i][k]+=time;
                            k=(k+1)%m;
                            int pos=k;
                            while (use[k])
                            {
                                k=(k+1)%m;
                                if (k==pos)
                                {
                                    end=true;
                                    break;
                                }
                            }
                            break;
                        }
                        k=(k+1)%m;
                        int pos=k;
                        while (use[k])
                        {
                            k=(k+1)%m;
                            if (k==pos)
                            {
                                end=true;
                                break;
                            }
                        }
                    }
                }
            }
        }
        for (int i=0; i<n; i++)
        {
            printf("%d",cnt[i]);
            for (int j=0; j<cnt[i]; j++)
                printf(" %s",s[check[i][j]]);
            printf("\n");
        }
    }
    return 0;
}