#include <iostream>
#include <map>
#include <set>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <memory.h>
#include <cstdlib>
using namespace std;

#define pb push_back

int n, T, k;
int a[1000][2], b[1000][2], c[1000], u[1000], cur = -1, crd, d[1000][1000], m;
char cw[1000][50];
vector<int> wrds[1000];

int next_card() {
    int crd = -1;
    for(int i = 1; i <= m; ++i) {
        if(!u[(cur+i)%m]) {
            crd = (cur+i)%m;
            break;
        }
    }
    cur = crd;
    return crd;
}

int main () {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    scanf("%d%d", &n, &T);
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < 2; ++j)
            scanf("%d%d", &a[i][j], &b[i][j]);
    }
    scanf("%d", &m);
    for(int i = 0; i < m; ++i)
        scanf("%s%d", cw[i], &c[i]);
    int t = 0, p = 0;
    while(1) {
        int k = T;
        while(k > 0) {
            crd = next_card();
            if(crd == -1) break;
            int l = min(max(1, c[crd] - a[t][p] - b[t][1-p] - d[t][crd]), k);
            d[t][crd] += l;
            k-=l;
            if(d[t][crd] >= c[crd]) {
                u[crd] = 1;
                wrds[t].pb(crd);
            }
        }
        if(crd == -1) break;
        if(++t >=n) { t = 0; p = 1-p; }
    }
    for(int i = 0; i < n; ++i) {
        printf("%d ", wrds[i].size());
        for(int j = 0; j < wrds[i].size(); ++j)
            printf("%s ", cw[wrds[i][j]]);
        printf("\n");
    }
    return 0;
}


#include <iostream>
#include <map>
#include <set>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <memory.h>
#include <cstdlib>
using namespace std;

#define pb push_back

int n, T, k;
int a[1000][2], b[1000][2], c[1000], u[1000], cur = -1, crd, d[1000][1000], m;
char cw[1000][50];
vector<int> wrds[1000];

int next_card() {
    int crd = -1;
    for(int i = 1; i <= m; ++i) {
        if(!u[(cur+i)%m]) {
            crd = (cur+i)%m;
            break;
        }
    }
    cur = crd;
    return crd;
}

int main () {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    scanf("%d%d", &n, &T);
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < 2; ++j)
            scanf("%d%d", &a[i][j], &b[i][j]);
    }
    scanf("%d", &m);
    for(int i = 0; i < m; ++i)
        scanf("%s%d", cw[i], &c[i]);
    int t = 0, p = 0;
    while(1) {
        int k = T;
        while(k > 0) {
            crd = next_card();
            if(crd == -1) break;
            int l = min(max(1, c[crd] - a[t][p] - b[t][1-p] - d[t][crd]), k);
            d[t][crd] += l;
            k-=l;
            if(d[t][crd] >= c[crd] - a[t][p] - b[t][1-p]) {
                u[crd] = 1;
                wrds[t].pb(crd);
            }
        }
        if(crd == -1) break;
        if(++t >=n) { t = 0; p = 1-p; }
    }
    for(int i = 0; i < n; ++i) {
        printf("%d ", wrds[i].size());
        for(int j = 0; j < wrds[i].size(); ++j)
            printf("%s ", cw[wrds[i][j]]);
        printf("\n");
    }
    return 0;
}


#include <iostream>
#include <map>
#include <set>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <memory.h>
#include <cstdlib>
using namespace std;

#define pb push_back

int n, T, k;
int a[1000][2], b[1000][2], c[1000], u[1000], cur = -1, crd, d[1000][1000], m;
char cw[1000][50];
vector<int> wrds[1000];

int next_card() {
    int crd = -1;
    for(int i = 1; i <= m; ++i) {
        if(!u[(cur+i)%m]) {
            crd = (cur+i)%m;
            break;
        }
    }
    cur = crd;
    return crd;
}

int main () {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    scanf("%d%d", &n, &T);
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < 2; ++j)
            scanf("%d%d", &a[i][j], &b[i][j]);
    }
    scanf("%d", &m);
    for(int i = 0; i < m; ++i)
        scanf("%s%d", cw[i], &c[i]);
    int t = 0, p = 0;
    while(1) {
        int k = T;
        while(k > 0) {
            crd = next_card();
            if(crd == -1) break;
            int l = min(max(1, c[crd] - a[t][p] + b[t][1-p] - d[t][crd]), k);
            d[t][crd] += l;
            k-=l;
            if(d[t][crd] >= c[crd]) {
                u[crd] = 1;
                wrds[t].pb(crd);
            }
        }
        if(crd == -1) break;
        if(++t >=n) { t = 0; p = 1-p; }
    }
    for(int i = 0; i < n; ++i) {
        printf("%d ", wrds[i].size());
        for(int j = 0; j < wrds[i].size(); ++j)
            printf("%s ", cw[wrds[i][j]]);
        printf("\n");
    }
    return 0;
}


#include <iostream>
#include <map>
#include <set>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <memory.h>
#include <cstdlib>
using namespace std;

#define pb push_back
#define maxn 110
int n, T, c[110], a1[maxn], b1[maxn], a2[maxn], b2[maxn], d[maxn][110], ptr, points, m;
char s[110][30];
bool flag[110];
vector<int> vp[maxn];
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%d%d", &a1[i], &b1[i], &a2[i], &b2[i]);
    }
    scanf("%d\n", &m);
    for(int i = 0; i<m; i++) {
        gets(s[i]);
        scanf("%d\n", &c[i]);
    }
    int cmd = 0, ptr = 0;
    while(true) {
        int t = T, k;
        while(t > 0) {
            k = max(1, c[ptr] - (a1[cmd] + b2[cmd]) - d[cmd][ptr]);
            if(t >= k) {
                points++;
                vp[cmd].pb(ptr);
                if(points == m)
                    break;
                flag[ptr] = true;
                //do { ptr = ptr-1; if(ptr == -1) ptr = m-1;  } while(flag[ptr]);
                do { ptr = (ptr + 1) % m;  } while(flag[ptr]);
                t -= k;
            }
            else{
                d[cmd][ptr] += t;
                t = 0;
                do { ptr = (ptr + 1) % m;  } while(flag[ptr]);
            }
        }
        if(points == m) break;
        swap(a1[cmd], a2[cmd]);
        swap(b1[cmd], b2[cmd]);
        cmd = (cmd + 1) % n;
    }
    for(int i = 0; i<n; i++) {
        printf("%d ", vp[i].size());
        for(int j = 0; j<vp[i].size(); j++) printf("%s ", s[vp[i][j]]);
        printf("\n");
    }
    return 0;
}


#include<iostream>
#include<stack>
#include<string>
#include<cstring>
#include<list>
#include<vector>
#include<map>
using namespace std;
int main(){
	freopen ("input.txt", "r", stdin) ;
	freopen ("output.txt", "w", stdout) ;
	int n, t;
	cin >> n >> t;
	int a[111][3], b[111][3];
	for (int i = 1; i <= n; i++)
		cin >> a[i][1] >> b[i][1] >> a[i][2] >> b[i][2];
	int m;
	cin >> m;
	list <pair <string, int> >  c;
	string ss;
	int h;
	for (int i = 1; i <= m; i++) {
		cin >> ss >> h;
		c.push_back(make_pair(ss, h));
	}
	map <pair <string, int>, int>  d; //  stroka - nom komandy, znachenie
	map <int, list <string> >  mp;
	int s = 0;
	int oc[111];
	memset(oc, 0, sizeof(oc));
	while (!c.empty()) {
		int tt = t;
		s++;
		if (s == 2 * n + 1)
			s = 1;
		int nom = s % n; // nomer komandy
		if (nom == 0)
			nom = n;
		int igr = (s > n) + 1;

		while (tt > 0 && !c.empty() ) {
			int p = c.front().second; // c[k]
			if (p - a[nom][igr] - b[nom][3 - igr] - d[make_pair(c.front().first, nom)] > tt) {
				d[make_pair(c.front().first, nom)] += tt;
				c.push_back(c.front());
				c.pop_front();
				break;
			}
			else {
				mp[nom].push_back(c.front().first);
				tt -= max(1, p - a[nom][igr] - b[nom][3 - igr] - d[make_pair(c.front().first, nom)]);
				oc[nom]++;
				c.pop_front();
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		cout << oc[i] << ' ';
		while (!mp[i].empty()) {
			cout << mp[i].front() << ' ';
			mp[i].pop_front();
		}
		cout << endl;
	}
}


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

#include <cstdio>
#include <cmath>
#include <string>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <queue>

using namespace std;

typedef long long LL;

int n, m, t;

int a[1 << 7][2], b[1 << 7][2];
char s[1 << 7][1 << 7];
int c[1 << 7];
int d[1 << 7][1 << 7];
int turn = 0;
int now = 0;

int q[1 << 7];
int qh, qt;

vector<int> res[1 << 7];

int main()
{
#ifndef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	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]);
	scanf("%d", &m);
	for(int i = 0; i < m; ++i)
	{
		scanf("%s", s[i]);
		scanf("%d", &c[i]);
	}
	qh = qt = 0;
	for(int i = 0; i < m; ++i)
		q[qt++] = i;

	while (qt != qh)
	{
		int time = t;
		while (qt != qh && time > 0)
		{
			int x = q[qh++];
			qh &= 127;
			int other = turn ^ 1;
			int add = max(1, c[x] - (a[now][turn] + b[now][other]) - d[now][x]);
			if (add <= time)
			{
				time -= add;
				res[now].push_back(x);
			}
			else
			{
				d[now][x] += time;
				time = 0;
				q[qt++] = x;
				qt &= 127;
			}
		}
		now++;
		if (now == n)
		{
			now = 0;
			turn ^= 1;
		}
	}
	for(int i = 0; i < n; ++i)
	{
		printf("%d", res[i].size());
		for(int j = 0; j < res[i].size(); ++j)
			printf(" %s", s[res[i][j]]);
		printf("\n");
	}
	return 0;
}


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

#include <cstdio>
#include <cmath>
#include <string>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <queue>

using namespace std;

typedef long long LL;

int n, m, t;

int a[1 << 7][2], b[1 << 7][2];
char s[1 << 7][1 << 7];
int c[1 << 7];
int d[1 << 7][1 << 7];
int turn = 0;
int now = 0;

int q[1 << 7];
int qh, qt;

vector<int> res[1 << 7];

int main()
{
#ifndef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	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]);
	scanf("%d", &m);
	for(int i = 0; i < m; ++i)
	{
		scanf("%s", s[i]);
		scanf("%d", &c[i]);
	}
	qh = qt = 0;
	for(int i = 0; i < m; ++i)
		q[qt++] = i;

	while (qt != qh)
	{
		int time = t;
		while (qt != qh && time > 0)
		{
			int x = q[qh++];
			qh &= 127;
			int other = turn ^ 1;
			int add = max(1, c[x] - (a[now][turn] + b[now][other]) - d[now][x]);
			if (add <= time)
			{
				time -= add;
				res[now].push_back(x);
			}
			else
			{
				d[now][x] += add;
				time = 0;
				q[qt++] = x;
				qt &= 127;
			}
		}
		now++;
		if (now == n)
		{
			now = 0;
			turn ^= 1;
		}
	}
	for(int i = 0; i < n; ++i)
	{
		printf("%d", res[i].size());
		for(int j = 0; j < res[i].size(); ++j)
			printf(" %s", s[res[i][j]]);
		printf("\n");
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:64000000")
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <set>
#include <map>
#include <cassert>
#include <queue>
#include <deque>
#include <stack>
#include <sstream>
#include <cstring>
#include <string>
#include <functional>
#include <numeric>

using namespace std;

#define MP make_pair
#define all(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << x << endl

const int INF = ((1 << 31) - 1);
const long long LLINF = (((1LL << 63) - 1LL));
const double EPS = 1e-9;
const double PI = 3.14159265358979323846;

typedef long long ll;
typedef long double LD;

int main() {
    freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
    int n, t;
    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];
        cin >> a[i][1] >> b[i][1];
    }
    int m;
    cin >> m;
    deque<pair<string, pair<int, int> > > words(0);
    for (int i = 0; i < m; ++i)
    {
        string s; int x;
        cin >> s >> x;
        words.push_back(MP(s, MP(x, i)));
    }
    int who = 0;
    vector<vector<int> > d(n, vector<int> (m, 0));
    vector<vector<string> > teamWords(m);
    while (!words.empty()) {
        who %= (2 * n);
        int i = who % n;
        int j = who / n;
        int k = words.front().second.second;
        int rem = t;
        int q = 1 - j;
        while(!words.empty() && rem) {
            k = words.front().second.second;
            int needT = max(1,words.front().second.first-(a[i][j] + b[i][q]) - d[i][k]);
            if (rem >= needT) {
                teamWords[i].push_back(words.front().first);
                words.pop_front();
                rem -= needT;
            } else {
                d[i][k] += rem;
                pair<string, pair<int, int> > C = words.front();
                words.pop_front();
                words.push_back(C);
                rem = 0;
            }
        }
        ++who;
    }
    for (int i = 0; i < n; ++i) {
        cout << teamWords[i].size() << " ";
        for (int j = 0; j < teamWords[i].size(); ++j)
            cout << teamWords[i][j] << " ";
        cout << "\n";
    }
    return 0;
}


#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <cmath>
#include <cctype>

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <list>
#include <iterator>
#include <functional>

using namespace std;

typedef long long int64;
int64 labs(int64 a){ return a<0 ? (-a) : a; }
int64 max(int64 a, int64 b){ return a>b ? a : b; }
int64 min(int64 a, int64 b){ return a<b ? a : b; }

#define mp make_pair
#define pb push_back
#define sqr(x) ((x)*(x))
#define forn(i, n) for(int (i)=0; (i)<(n); (i)++)
#define forr(i, n) for(int (i)=(n)-1; (i)>=0; (i)--)
#define forit(i,c) for((i)=(c).begin(); (i)!=(c).end(); (i)++)
#define all(x) (x).begin(), (x).end()
#define zero(a) memset((a), 0, sizeof(a))

typedef vector<int> vint;
typedef vector<bool> vbool;
typedef vector<int64> vint64;
typedef pair<int64, int64> pii;

struct Card
{
	string word;
	vint t;

	Card(const string & w, int c, int n) : word(w)
	{
		t.assign(n, c);
	}
};

struct Team
{
	int a[2], b[2];
	vector<Card> cards;

};

vector<Team> teams;
queue<Card> cards;

int main()
{
	int T;
	int n, m;
	cin >> n >> T;

	teams.resize(n);
	forn(i, n)
	{
		cin >> teams[i].a[0] >> teams[i].b[0] >> teams[i].a[1] >> teams[i].b[1];
	}

	cin >> m;
	string s;
	int c;
	forn(i, m)
	{
		cin >> s >> c;
		cards.push(Card(s, c, n));
	}

	for(int p = 0; !cards.empty(); p = !p)
	{
		for(int i = 0; i < n && !cards.empty(); i++)
		{
			int t = T;
			while(t > 0 && !cards.empty())
			{
				int tt = max(1, cards.front().t[i] - (teams[i].a[p] + teams[i].b[!p]));
				if (tt <= t)
				{
					teams[i].cards.push_back(cards.front());
					cards.pop();
				}
				else
				{
					cards.front().t[i] -= t;
					cards.push(cards.front());
					cards.pop();
				}
				t -= tt;
			}
		}
	}

	forn(i, n)
	{
		cout << teams[i].cards.size() << " ";
		forn(j, teams[i].cards.size())
		{
			cout << teams[i].cards[j].word << " ";
		}
		cout << endl;
	}

	return 0;
}


#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <cmath>
#include <cctype>

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <list>
#include <iterator>
#include <functional>

using namespace std;

typedef long long int64;
int64 labs(int64 a){ return a<0 ? (-a) : a; }
int64 max(int64 a, int64 b){ return a>b ? a : b; }
int64 min(int64 a, int64 b){ return a<b ? a : b; }

#define mp make_pair
#define pb push_back
#define sqr(x) ((x)*(x))
#define forn(i, n) for(int (i)=0; (i)<(n); (i)++)
#define forr(i, n) for(int (i)=(n)-1; (i)>=0; (i)--)
#define forit(i,c) for((i)=(c).begin(); (i)!=(c).end(); (i)++)
#define all(x) (x).begin(), (x).end()
#define zero(a) memset((a), 0, sizeof(a))

typedef vector<int> vint;
typedef vector<bool> vbool;
typedef vector<int64> vint64;
typedef pair<int64, int64> pii;

struct Card
{
	string word;
	vint t;

	Card(const string & w, int c, int n) : word(w)
	{
		t.assign(n, c);
	}
};

struct Team
{
	int a[2], b[2];
	vector<Card> cards;

};

vector<Team> teams;
queue<Card> cards;

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

	int T;
	int n, m;
	cin >> n >> T;

	teams.resize(n);
	forn(i, n)
	{
		cin >> teams[i].a[0] >> teams[i].b[0] >> teams[i].a[1] >> teams[i].b[1];
	}

	cin >> m;
	string s;
	int c;
	forn(i, m)
	{
		cin >> s >> c;
		cards.push(Card(s, c, n));
	}

	for(int p = 0; !cards.empty(); p = !p)
	{
		for(int i = 0; i < n && !cards.empty(); i++)
		{
			int t = T;
			while(t > 0 && !cards.empty())
			{
				int tt = max(1, cards.front().t[i] - (teams[i].a[p] + teams[i].b[!p]));
				if (tt <= t)
				{
					teams[i].cards.push_back(cards.front());
					cards.pop();
				}
				else
				{
					cards.front().t[i] -= t;
					cards.push(cards.front());
					cards.pop();
				}
				t -= tt;
			}
		}
	}

	forn(i, n)
	{
		cout << teams[i].cards.size() << " ";
		forn(j, teams[i].cards.size())
		{
			cout << teams[i].cards[j].word << " ";
		}
		cout << endl;
	}

	return 0;
}


#include <iostream>
#include <algorithm>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <ctime>
#include <string>
using namespace std;

int n,m,t,k;
deque<int>q;

struct item{
    string word;
    int diff;
    int num;
};

pair<int,int> next_player(pair<int,int>p){
    int i=p.first;
    int j=p.second;
    if(++i>=n){
        i=0;
        j=!j;
    }
    return make_pair(i , j);
}

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

    while (scanf("%d%d", &n, &t) >= 2) {
    //int ** skill = new int*[101];for(int i=0; i<101; ++i) skill[i]=new int[4];
    vector<vector<int>>a(n,2);
    vector<vector<int>>b(n,2);
    for(int i=0; i<n; ++i){
        for(int j=0; j<2; ++j){
        scanf("%d%d", &a[i][j],&b[i][j]);
        }
    }
    cin >> m;
    vector<item>vec(m);
    vector<int>scored(m);
    vector<string>words(m);
    vector<int>diff(m);
    int scorednum=0;
    int temp;
    string str1;
    item aaaa;
    for(int i=0; i<m; ++i){
        cin >> words[i] >> diff[i];
    }
    for(int i=0; i<m; ++i) q.push_back(i);
    int currtime=t;
    int needtime;
    vector<vector<int>>d(n,vector<int>(m));
    int top;
    pair<int,int>currplayer;
    currplayer = make_pair(0,0);
    int temp11=0;
    vector<vector<string>>ress(n);
    while(!q.empty()){
        currtime=t;
        while(currtime>0 && !q.empty()){
            top = q.front();
            q.pop_front();
            needtime = max(1,diff[top]-(a[currplayer.first][currplayer.second]+a[currplayer.first][!currplayer.second])-d[currplayer.first][top]);
            if(needtime <= currtime){
                scorednum++;
                d[currplayer.first][top] = needtime;
                currtime-=needtime;
                ress[currplayer.first].push_back(words[top]);
            }
            else{
                d[currplayer.first][top] += currtime;
                currtime=0;
                q.push_back(top);
                break;
            }
        }
        if(++currplayer.first>=n){
            currplayer.first=0;
            currplayer.second=!currplayer.second;
        }
    }
    for(int i=0; i<ress.size(); ++i){
        cout << ress[i].size();
        for(int j=0; j<ress[i].size(); ++j) cout << " " << ress[i][j];
        cout << endl;
    }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <ctime>
#include <string>
using namespace std;

int main(){
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int n,t;
    while (scanf("%d%d", &n, &t) >= 2) {
        vector<vector<int>>a(n,vector<int>(2)),b(n,vector<int>(2));
        for (int i = 0; i < n; i++)
            for (int i2 = 0; i2 < 2; i2++)
                scanf("%d%d", &a[i][i2], &b[i][i2]);
        int m;
        cin >> m;
        vector<string>words(m); vector<int>diff(m);
        for (int i = 0; i < m; i++) {
            char buf[30];
            scanf("%s%d", buf, &diff[i]);
            words[i] = buf;
        }
        vector<vector<string>>res(n);

        vector<vector<int>>d(n,vector<int>(m,0));
        deque<int>q;
        for(int i=0; i<m; ++i) q.push_back(i);

        int curp=0,curt=0;
        while(!q.empty()){
            int currtime=t;
            while(currtime>0 && !q.empty()){
                int top = q.front(); q.pop_front();
                int needtime = max(1, (diff[top]-(a[curt][curp]+b[curt][!curp])-d[curt][top]));
                if(currtime>=needtime){
                    currtime-=needtime;
                    res[curt].push_back(words[top]);
                }
                else{
                    q.push_back(top);
                    d[curt][top] += currtime;
                    currtime=0;
                }
            }
            if(++curt>=n){
                curt=0;
                curp=!curp;
            }
        }
        for(int i = 0; i < n; i++){
            printf("%d", res[i].size());
            for(int i2 = 0; i2 < res[i].size(); i2++)
                printf(" %s", res[i][i2].c_str());
                printf("\n");
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <ctime>
#include <string>
using namespace std;

int n,m,t,k;
deque<int>q;

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

    while (scanf("%d%d", &n, &t) >= 2) {
    vector<vector<int>>a(n,vector<int>(2));
    vector<vector<int>>b(n,vector<int>(2));
    for(int i=0; i<n; ++i){
        for(int j=0; j<2; ++j){
        scanf("%d%d", &a[i][j],&b[i][j]);
        }
    }
    cin >> m;
    vector<int>scored(m);
    vector<string>words(m);
    vector<int>diff(m);
    int temp;
    string str1;
   for (int i = 0; i < m; i++) {
      char buf[30];
      scanf("%s%d", buf, &diff[i]);
      words[i] = buf;
    }
    for(int i=0; i<m; ++i) q.push_back(i);
    int currtime=t;
    int needtime;
    vector<vector<int>>d(n,vector<int>(m,0));
    int top;
    pair<int,int>currplayer;
    currplayer = make_pair(0,0);
    vector<vector<string>>ress(n);
    while(!q.empty()){
        currtime=t;
        while(currtime>0 && !q.empty()){
            top = q.front();
            q.pop_front();
            needtime = max(1,diff[top]-(a[currplayer.first][currplayer.second]+a[currplayer.first][!currplayer.second])-d[currplayer.first][top]);
            if(needtime <= currtime){
                currtime-=needtime;
                ress[currplayer.first].push_back(words[top]);
            }
            else{
                d[currplayer.first][top] += currtime;
                currtime=0;
                q.push_back(top);
            }
        }
        if(++currplayer.first>=n){
            currplayer.first=0;
            currplayer.second=!currplayer.second;
        }
    }
    for(int i=0; i<ress.size(); ++i){
        cout << ress[i].size();
        for(int j=0; j<ress[i].size(); ++j) cout << " " << ress[i][j];
        cout << endl;
    }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <ctime>
#include <string>
using namespace std;

int n,m,t,k;
deque<int>q;

struct item{
    string word;
    int diff;
    int num;
};

pair<int,int> next_player(pair<int,int>p){
    int i=p.first;
    int j=p.second;
    if(++i>=n){
        i=0;
        j=!j;
    }
    return make_pair(i , j);
}

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

    cin >>n >>t;
    //int ** skill = new int*[101];for(int i=0; i<101; ++i) skill[i]=new int[4];
    int ** a = new int*[101]; for(int i=0; i<101; ++i) a[i]=new int[2];
    int ** b = new int*[101]; for(int i=0; i<101; ++i) b[i]=new int[2];
    for(int i=0; i<n; ++i){
        scanf("%d%d%d%d", &a[i][0],&b[i][0],&a[i][1],&b[i][1]);
    }
    cin >> m;
    vector<item>vec(m);
    vector<int>scored(m);
    int scorednum=0;
    int temp;
    string str1;
    item aaaa;
    for(int i=0; i<m; ++i){
        cin >> str1 >> temp;
        aaaa.word=str1;
        aaaa.diff=temp;
        aaaa.num=i;
        vec[i]=aaaa;
    }
    for(int i=0; i<m; ++i) q.push_back(i);
    int currtime=t;
    int needtime;
    int ** d = new int*[101];for(int i=0; i<101; ++i) d[i]=new int[101];
    for(int i=0; i<101; ++i)for(int j=0; j<101; ++j) d[i][j]=0;
    int * res = new int[101];
    for(int i=0; i<101; ++i)res[i]=0;
    item top;
    pair<int,int>currplayer;
    currplayer = make_pair(0,0);
    int temp11=0;
    vector<vector<string>>ress(n);
    while(!q.empty()){
        currtime=t;
        while(currtime>0 && !q.empty()){
            top = vec[q.front()];
            q.pop_front();
            temp11=0;
            temp11+=top.diff-d[currplayer.first][top.num];
            temp11-=a[currplayer.first][currplayer.second]+b[currplayer.first][!currplayer.second];
            needtime = max(1,top.diff-(a[currplayer.first][currplayer.second]+a[currplayer.first][!currplayer.second])-d[currplayer.first][top.num]);
            if(needtime <= currtime){
                scorednum++;
                d[currplayer.first][top.num] = needtime;
                currtime-=needtime;
                ress[currplayer.first].push_back(top.word);
            }
            else{
                d[currplayer.first][top.num] += currtime;
                currtime=0;
                q.push_back(top.num);
                break;
            }
        }
        currplayer = next_player(currplayer);
    }
    for(int i=0; i<ress.size(); ++i){
        cout << ress[i].size() <<" ";
        for(int j=0; j<ress[i].size(); ++j) cout << ress[i][j] << " ";
        cout << endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <ctime>
#include <string>
using namespace std;

int n,m,t,k;
deque<int>q;

struct item{
    string word;
    int diff;
    int num;
};

pair<int,int> next_player(pair<int,int>p){
    int i=p.first;
    int j=p.second;
    if(++i>=n){
        i=0;
        j=!j;
    }
    return make_pair(i , j);
}

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

    cin >>n >>t;
    //int ** skill = new int*[101];for(int i=0; i<101; ++i) skill[i]=new int[4];
    vector<vector<int>>a(n,2);
    vector<vector<int>>b(n,2);
    for(int i=0; i<n; ++i){
        for(int j=0; j<2; ++j){
        scanf("%d%d", &a[i][j],&b[i][j]);
        }
    }
    cin >> m;
    vector<item>vec(m);
    vector<int>scored(m);
    vector<string>words(m);
    vector<int>diff(m);
    int scorednum=0;
    int temp;
    string str1;
    item aaaa;
    for(int i=0; i<m; ++i){
        cin >> words[i] >> diff[i];
    }
    for(int i=0; i<m; ++i) q.push_back(i);
    int currtime=t;
    int needtime;
    vector<vector<int>>d(n,vector<int>(m));
    int top;
    pair<int,int>currplayer;
    currplayer = make_pair(0,0);
    int temp11=0;
    vector<vector<string>>ress(n);
    while(!q.empty()){
        currtime=t;
        while(currtime>0 && !q.empty()){
            top = q.front();
            q.pop_front();
            needtime = max(1,diff[top]-(a[currplayer.first][currplayer.second]+a[currplayer.first][!currplayer.second])-d[currplayer.first][top]);
            if(needtime <= currtime){
                scorednum++;
                d[currplayer.first][top] = needtime;
                currtime-=needtime;
                ress[currplayer.first].push_back(words[top]);
            }
            else{
                d[currplayer.first][top] += currtime;
                currtime=0;
                q.push_back(top);
                break;
            }
        }
        if(++currplayer.first>=n){
            currplayer.first=0;
            currplayer.second=!currplayer.second;
        }
    }
    for(int i=0; i<ress.size(); ++i){
        cout << ress[i].size();
        for(int j=0; j<ress[i].size(); ++j) cout << " " << ress[i][j];
        cout << endl;
    }
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>

using namespace std;

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
#define mp make_pair
#define sz(x) ((int)(x).size())

typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef pair<int, int> pii;

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

  int n, t;
  while (scanf("%d%d", &n, &t) >= 2) {
    vvi as(n, vi(2)), bs(n, vi(2));
    for (int i = 0; i < n; i++)
      for (int i2 = 0; i2 < 2; i2++)
        scanf("%d%d", &as[i][i2], &bs[i][i2]);

    int m;
    scanf("%d", &m);
    vector<string> ns(m); vi cs(m);
    for (int i = 0; i < m; i++) {
      char buf[30];
      scanf("%s%d", buf, &cs[i]);
      ns[i] = buf;
    }

    vector<vector<string> > res(n);

    vvi d(n, vi(m, 0));
    deque<int> dq;
    for (int i = 0; i < m; i++) dq.pb(i);

    int cur = 0, cpl = 0;
    while (!dq.empty()) {
      int curt = t;
      while (curt > 0 && !dq.empty()) {
        int card = dq.front(); dq.pop_front();
        int need = max(1, cs[card] - (as[cur][cpl] + bs[cur][!cpl]) - d[cur][card]);
        if (curt >= need) {
          curt -= need;
          res[cur].pb(ns[card]);
        } else {
          dq.pb(card);
          d[cur][card] += curt;
          curt = 0;
        }
      }

      if (++cur >= n) {
        cur = 0;
        cpl = !cpl;
      }
    }
    for (int i = 0; i < n; i++) {
      printf("%d", sz(res[i]));
      for (int i2 = 0; i2 < sz(res[i]); i2++)
        printf(" %s", res[i][i2].c_str());
      printf("\n");
    }
  }
  return 0;
}


#include <iostream>
#include <algorithm>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <ctime>
#include <string>
using namespace std;

int n,m,t,k;
deque<int>q;

struct item{
    string word;
    int diff;
    int num;
};

pair<int,int> next_player(pair<int,int>p){
    int i=p.first;
    int j=p.second;
    if(++i==n) i=0;
    j=!j;
    return make_pair(i , j);
}

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

    cin >>n >>t;
    //int ** skill = new int*[101];for(int i=0; i<101; ++i) skill[i]=new int[4];
    int ** a = new int*[101]; for(int i=0; i<101; ++i) a[i]=new int[2];
    int ** b = new int*[101]; for(int i=0; i<101; ++i) b[i]=new int[2];
    for(int i=0; i<n; ++i){
        scanf("%d%d%d%d", &a[i][0],&b[i][0],&a[i][1],&b[i][1]);
    }
    cin >> m;
    vector<item>vec(m);
    vector<int>scored(m);
    int scorednum=0;
    int temp;
    string str1;
    item aaaa;
    for(int i=0; i<m; ++i){
        cin >> str1 >> temp;
        aaaa.word=str1;
        aaaa.diff=temp;
        aaaa.num=i;
        vec[i]=aaaa;
    }
    for(int i=0; i<m; ++i) q.push_back(i);
    int currtime=t;
    int needtime;
    int ** d = new int*[101];for(int i=0; i<101; ++i) d[i]=new int[101];
    for(int i=0; i<101; ++i)for(int j=0; j<101; ++j) d[i][j]=0;
    int * res = new int[101];
    for(int i=0; i<101; ++i)res[i]=0;
    item top;
    pair<int,int>currplayer;
    currplayer = make_pair(0,0);
    int temp11=0;
    vector<vector<string>>ress(n);
    while(!q.empty()){
        currtime=t;
        while(currtime>0 && !q.empty()){
            top = vec[q.front()];
            q.pop_front();
            temp11=0;
            temp11+=top.diff-d[currplayer.first][top.num];
            temp11-=a[currplayer.first][currplayer.second]+b[currplayer.first][!currplayer.second];
            needtime = max(1,top.diff-(a[currplayer.first][currplayer.second]+a[currplayer.first][!currplayer.second])-d[currplayer.first][top.num]);
            if(needtime <= currtime){
                scorednum++;
                d[currplayer.first][top.num] = needtime;
                currtime-=needtime;
                ress[currplayer.first].push_back(top.word);
            }
            else{
                d[currplayer.first][top.num] += currtime;
                currtime=0;
                q.push_back(top.num);
                break;
            }
        }
        currplayer = next_player(currplayer);
    }
    for(int i=0; i<ress.size(); ++i){
        cout << ress[i].size() <<" ";
        for(int j=0; j<ress[i].size(); ++j) cout << ress[i][j] << " ";
        cout << endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <ctime>
#include <string>
using namespace std;

int n,m,t,k;
deque<int>q;

struct item{
    string word;
    int diff;
    int num;
};

pair<int,int> next_player(pair<int,int>p){
    int i=p.first;
    int j=p.second;
    if(++i==n) i=0;
    j=!j;
    return make_pair(i , j);
}

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

    cin >>n >>t;
    //int ** skill = new int*[101];for(int i=0; i<101; ++i) skill[i]=new int[4];
    int ** a = new int*[101]; for(int i=0; i<101; ++i) a[i]=new int[2];
    int ** b = new int*[101]; for(int i=0; i<101; ++i) b[i]=new int[2];
    for(int i=0; i<n; ++i){
        scanf("%d%d%d%d", &a[i][0],&b[i][0],&a[i][1],&b[i][1]);
    }
    cin >> m;
    vector<item>vec(m);
    vector<int>scored(m);
    int scorednum=0;
    int temp;
    string str1;
    item aaaa;
    for(int i=0; i<m; ++i){
        cin >> str1 >> temp;
        aaaa.word=str1;
        aaaa.diff=temp;
        aaaa.num=i;
        vec[i]=aaaa;
    }
    for(int i=0; i<m; ++i) q.push_back(i);
    int currtime=t;
    int needtime;
    int ** d = new int*[101];for(int i=0; i<101; ++i) d[i]=new int[101];
    for(int i=0; i<101; ++i)for(int j=0; j<101; ++j) d[i][j]=0;
    int * res = new int[101];
    for(int i=0; i<101; ++i)res[i]=0;
    item top;
    pair<int,int>currplayer;
    currplayer = make_pair(0,0);
    int temp11=0;
    vector<vector<string>>ress(n);
    while(!q.empty()){
        currtime=t;
        while(currtime>0 && !q.empty()){
            top = vec[q.front()];
            q.pop_front();
            temp11=0;
            temp11+=top.diff-d[currplayer.first][top.num];
            temp11-=a[currplayer.first][currplayer.second]+b[currplayer.first][!currplayer.second];
            needtime = max(1,temp11);
            if(needtime <= t){
                scorednum++;
                d[currplayer.first][top.num] = needtime;
                currtime-=needtime;
                ress[currplayer.first].push_back(top.word);
            }
            else{
                d[currplayer.first][top.num] += t;
                currtime=0;
                q.push_back(top.num);
                break;
            }
        }
        currplayer = next_player(currplayer);
    }
    for(int i=0; i<ress.size(); ++i){
        cout << ress[i].size() <<" ";
        for(int j=0; j<ress[i].size(); ++j) cout << ress[i][j] << " ";
        cout << endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <ctime>
#include <string>
using namespace std;

int n,m,t,k;
deque<int>q;

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

    while (scanf("%d%d", &n, &t) >= 2) {
    vector<vector<int>>a(n,vector<int>(2));
    vector<vector<int>>b(n,vector<int>(2));
    for (int i = 0; i < n; i++)
      for (int i2 = 0; i2 < 2; i2++)
        scanf("%d%d", &a[i][i2], &b[i][i2]);
    cin >> m;
    vector<string>words(m);
    vector<int>diff(m);
   for (int i = 0; i < m; i++) {
      char buf[30];
      scanf("%s%d", buf, &diff[i]);
      words[i] = buf;
    }
     vector<vector<string>>ress(n);

    vector<vector<int>>d(n,vector<int>(m,0));
    int top;
    for(int i=0; i<m; ++i) q.push_back(i);
   int curp=0,curt=0;int currtime=0; int needtime=0;
    while(!q.empty()){
       int currtime=t;
        while(currtime>0 && !q.empty()){
         int   top = q.front();
            q.pop_front();
          int  needtime = max(1, (diff[top]-(a[curt][curp]+a[curt][!curp])-d[curt][top]));
            if(currtime>=needtime){
                currtime-=needtime;
                ress[curt].push_back(words[top]);
            }
            else{
                 q.push_back(top);
                d[curt][top] += currtime;
                currtime=0;
            }
        }
        if(++curt>=n){
            curt=0;
            curp=!curp;
        }
    }
   for (int i = 0; i < n; i++) {
      printf("%d", ress[i].size());
      for (int i2 = 0; i2 < ress[i].size(); i2++)
        printf(" %s", ress[i][i2].c_str());
      printf("\n");
    }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <ctime>
#include <string>
using namespace std;

int n,m,t,k;
deque<int>q;

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

    while (scanf("%d%d", &n, &t) >= 2) {
    vector<vector<int>>a(n,vector<int>(2));
    vector<vector<int>>b(n,vector<int>(2));
    for (int i = 0; i < n; i++)
      for (int i2 = 0; i2 < 2; i2++)
        scanf("%d%d", &a[i][i2], &b[i][i2]);
    cin >> m;
    vector<int>scored(m);
    vector<string>words(m);
    vector<int>diff(m);
    int temp;
    string str1;
   for (int i = 0; i < m; i++) {
      char buf[30];
      scanf("%s%d", buf, &diff[i]);
      words[i] = buf;
    }
    for(int i=0; i<m; ++i) q.push_back(i);
    int currtime=t;
    int needtime;
    vector<vector<int>>d(n,vector<int>(m,0));
    int top;
    pair<int,int>currplayer;
    currplayer = make_pair(0,0);
    vector<vector<string>>ress(n);
    while(!q.empty()){
        currtime=t;
        while(currtime>0 && !q.empty()){
            top = q.front();
            q.pop_front();
            needtime = max(1, (diff[top]-(a[currplayer.first][currplayer.second]+a[currplayer.first][!currplayer.second])-d[currplayer.first][top]));
            if(needtime <= currtime){
                currtime-=needtime;
                ress[currplayer.first].push_back(words[top]);
            }
            else{
                d[currplayer.first][top] += currtime;
                currtime=0;
                q.push_back(top);
            }
        }
        if(++currplayer.first>=n){
            currplayer.first=0;
            currplayer.second=!currplayer.second;
        }
    }
   for (int i = 0; i < n; i++) {
      printf("%d", ress[i].size());
      for (int i2 = 0; i2 < ress[i].size(); i2++)
        printf(" %s", ress[i][i2].c_str());
      printf("\n");
    }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <ctime>
#include <string>
using namespace std;

int n,m,t,k;
deque<int>q;

struct item{
    string word;
    int diff;
    int num;
};

pair<int,int> next_player(pair<int,int>p){
    int i=p.first;
    int j=p.second;
    if(++i==m) i=0;
    j=!j;
    return make_pair(i , j);
}

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

    cin >>n >>t;
    //int ** skill = new int*[101];for(int i=0; i<101; ++i) skill[i]=new int[4];
    int ** a = new int*[101]; for(int i=0; i<101; ++i) a[i]=new int[2];
    int ** b = new int*[101]; for(int i=0; i<101; ++i) b[i]=new int[2];
    for(int i=0; i<n; ++i){
        scanf("%d%d%d%d", &a[i][0],&b[i][0],&a[i][1],&b[i][1]);
    }
    cin >> m;
    vector<item>vec(m);
    vector<int>scored(m);
    int scorednum=0;
    int temp;
    string str1;
    item aaaa;
    for(int i=0; i<m; ++i){
        cin >> str1 >> temp;
        aaaa.word=str1;
        aaaa.diff=temp;
        aaaa.num=i;
        vec[i]=aaaa;
    }
    for(int i=0; i<m; ++i) q.push_back(i);
    int currtime=t;
    int needtime;
    int ** d = new int*[101];for(int i=0; i<101; ++i) d[i]=new int[101];
    for(int i=0; i<101; ++i)for(int j=0; j<101; ++j) d[i][j]=0;
    int * res = new int[101];
    for(int i=0; i<101; ++i)res[i]=0;
    item top;
    pair<int,int>currplayer;
    currplayer = make_pair(0,0);
    int temp11=0;
    vector<vector<string>>ress(n);
    while(!q.empty()){
        currtime=t;
        while(currtime>0 && !q.empty()){
            top = vec[q.front()];
            q.pop_front();
            temp11=0;
            temp11+=top.diff-d[currplayer.first][top.num];
            temp11-=a[currplayer.first][currplayer.second]+b[currplayer.first][!currplayer.second];
            needtime = max(1,temp11);
            if(needtime <= t){
                scorednum++;
                d[currplayer.first][top.num] = needtime;
                currtime-=needtime;
                ress[currplayer.first].push_back(top.word);
            }
            else{
                d[currplayer.first][top.num] += t;
                currtime=0;
                q.push_back(top.num);
                break;
            }
        }
        currplayer = next_player(currplayer);
    }
    for(int i=0; i<ress.size(); ++i){
        cout << ress[i].size() <<" ";
        for(int j=0; j<ress[i].size(); ++j) cout << ress[i][j] << " ";
        cout << endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <ctime>
#include <string>
using namespace std;

int n,m,t,k;
deque<int>q;

struct item{
    string word;
    int diff;
    int num;
};

pair<int,int> next_player(pair<int,int>p){
    int i=p.first;
    int j=p.second;
    int curr = 2*i+j;
    curr++;
    curr%=2*n;
    return make_pair(curr/(2) , curr%(2));
}

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

    cin >>n >>t;
    //int ** skill = new int*[101];for(int i=0; i<101; ++i) skill[i]=new int[4];
    int ** a = new int*[101]; for(int i=0; i<101; ++i) a[i]=new int[2];
    int ** b = new int*[101]; for(int i=0; i<101; ++i) b[i]=new int[2];
    for(int i=0; i<n; ++i){
        scanf("%d%d%d%d", &a[i][0],&b[i][0],&a[i][1],&b[i][1]);
    }
    cin >> m;
    vector<item>vec(m);
    vector<int>scored(m);
    int scorednum=0;
    int temp;
    string str1;
    item aaaa;
    for(int i=0; i<m; ++i){
        cin >> str1 >> temp;
        aaaa.word=str1;
        aaaa.diff=temp;
        aaaa.num=i;
        vec[i]=aaaa;
    }
    for(int i=0; i<m; ++i) q.push_back(i);
    int currtime=t;
    int needtime;
    int ** d = new int*[101];for(int i=0; i<101; ++i) d[i]=new int[101];
    for(int i=0; i<101; ++i)for(int j=0; j<101; ++j) d[i][j]=0;
    int * res = new int[101];
    for(int i=0; i<101; ++i)res[i]=0;
    item top;
    pair<int,int>currplayer;
    currplayer = make_pair(0,0);
    int temp11=0;
    vector<vector<string>>ress(n);
    while(scorednum!=m && !q.empty()){
        currtime=t;
        currplayer = next_player(currplayer);
        while(currtime>0 && !q.empty()){
            top = vec[q.front()];
            q.pop_front();
            temp11=0;
            temp11+=top.diff-d[currplayer.first][top.num];
            temp11-=a[currplayer.first][currplayer.second]+b[currplayer.first][!currplayer.second];
            needtime = max(1,temp11);
            if(needtime <= t){
                scorednum++;
                d[currplayer.first][top.num] = needtime;
                currtime-=needtime;
                ress[currplayer.first].push_back(top.word);
            }
            else{
                d[currplayer.first][top.num] += t;
                currtime=0;
                q.push_back(top.num);
                break;
            }
        }
    }
    for(int i=0; i<ress.size(); ++i){
        cout << ress[i].size() <<" ";
        for(int j=0; j<ress[i].size(); ++j) cout << ress[i][j] << " ";
        cout << endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <ctime>
#include <string>
using namespace std;

int n,m,t,k;
deque<int>q;

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

    while (scanf("%d%d", &n, &t) >= 2) {
    vector<vector<int>>a(n,vector<int>(2));
    vector<vector<int>>b(n,vector<int>(2));
    for (int i = 0; i < n; i++)
      for (int i2 = 0; i2 < 2; i2++)
        scanf("%d%d", &a[i][i2], &b[i][i2]);
    cin >> m;
    vector<string>words(m);
    vector<int>diff(m);
   for (int i = 0; i < m; i++) {
      char buf[30];
      scanf("%s%d", buf, &diff[i]);
      words[i] = buf;
    }
    for(int i=0; i<m; ++i) q.push_back(i);
    int currtime=t;
    int needtime;
    vector<vector<int>>d(n,vector<int>(m,0));
    int top;
   int curp=0,curt=0;
    vector<vector<string>>ress(n);
    while(!q.empty()){
        currtime=t;
        while(currtime>0 && !q.empty()){
            top = q.front();
            q.pop_front();
            needtime = max(1, (diff[top]-(a[curt][curp]+a[curt][!curp])-d[curt][top]));
            if(needtime <= currtime){
                currtime-=needtime;
                ress[curt].push_back(words[top]);
            }
            else{
                d[curt][top] += currtime;
                currtime=0;
                q.push_back(top);
            }
        }
        if(++curt>=n){
            curt=0;
            curp=!curp;
        }
    }
   for (int i = 0; i < n; i++) {
      printf("%d", ress[i].size());
      for (int i2 = 0; i2 < ress[i].size(); i2++)
        printf(" %s", ress[i][i2].c_str());
      printf("\n");
    }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <ctime>
#include <string>
using namespace std;

int n,m,t,k;
deque<int>q;

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

    while (scanf("%d%d", &n, &t) >= 2) {
    vector<vector<int>>a(n,vector<int>(2));
    vector<vector<int>>b(n,vector<int>(2));
    for(int i=0; i<n; ++i){
        for(int j=0; j<2; ++j){
        scanf("%d%d", &a[i][j],&b[i][j]);
        }
    }
    cin >> m;
    vector<int>scored(m);
    vector<string>words(m);
    vector<int>diff(m);
    int temp;
    string str1;
   for (int i = 0; i < m; i++) {
      char buf[30];
      scanf("%s%d", buf, &diff[i]);
      words[i] = buf;
    }
    for(int i=0; i<m; ++i) q.push_back(i);
    int currtime=t;
    int needtime;
    vector<vector<int>>d(n,vector<int>(m,0));
    int top;
    pair<int,int>currplayer;
    currplayer = make_pair(0,0);
    vector<vector<string>>ress(n);
    while(!q.empty()){
        currtime=t;
        while(currtime>0 && !q.empty()){
            top = q.front();
            q.pop_front();
            needtime = max(1,diff[top]-(a[currplayer.first][currplayer.second]+a[currplayer.first][!currplayer.second])-d[currplayer.first][top]);
            if(needtime <= currtime){
                currtime-=needtime;
                ress[currplayer.first].push_back(words[top]);
            }
            else{
                d[currplayer.first][top] += currtime;
                currtime=0;
                q.push_back(top);
            }
        }
        if(++currplayer.first>=n){
            currplayer.first=0;
            currplayer.second=!currplayer.second;
        }
    }
   for (int i = 0; i < n; i++) {
      printf("%d", ress[i].size());
      for (int i2 = 0; i2 < ress[i].size(); i2++)
        printf(" %s", ress[i][i2].c_str());
      printf("\n");
    }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <ctime>
#include <string>
using namespace std;

int n,m,t,k;
deque<int>q;

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

    while (scanf("%d%d", &n, &t) >= 2) {
    vector<vector<int>>a(n,vector<int>(2));
    vector<vector<int>>b(n,vector<int>(2));
    for(int i=0; i<n; ++i){
        for(int j=0; j<2; ++j){
        scanf("%d%d", &a[i][j],&b[i][j]);
        }
    }
    cin >> m;
    vector<int>scored(m);
    vector<string>words(m);
    vector<int>diff(m);
    int temp;
    string str1;
   for (int i = 0; i < m; i++) {
      char buf[30];
      scanf("%s%d", buf, &diff[i]);
      words[i] = buf;
    }
    for(int i=0; i<m; ++i) q.push_back(i);
    int currtime=t;
    int needtime;
    vector<vector<int>>d(n,vector<int>(m));
    int top;
    pair<int,int>currplayer;
    currplayer = make_pair(0,0);
    vector<vector<string>>ress(n);
    while(!q.empty()){
        currtime=t;
        while(currtime>0 && !q.empty()){
            top = q.front();
            q.pop_front();
            needtime = max(1,diff[top]-(a[currplayer.first][currplayer.second]+a[currplayer.first][!currplayer.second])-d[currplayer.first][top]);
            if(needtime <= currtime){
                currtime-=needtime;
                ress[currplayer.first].push_back(words[top]);
            }
            else{
                d[currplayer.first][top] += currtime;
                currtime=0;
                q.push_back(top);
            }
        }
        if(++currplayer.first>=n){
            currplayer.first=0;
            currplayer.second=!currplayer.second;
        }
    }
    for(int i=0; i<ress.size(); ++i){
        cout << ress[i].size();
        for(int j=0; j<ress[i].size(); ++j) cout << " " << ress[i][j];
        cout << endl;
    }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <ctime>
#include <string>
using namespace std;

int n,m,t,k;
deque<int>q;

struct item{
    string word;
    int diff;
    int num;
};

pair<int,int> next_player(pair<int,int>p){
    int i=p.first;
    int j=p.second;
    if(++i>=n){
        i=0;
        j=!j;
    }
    return make_pair(i , j);
}

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

    while (scanf("%d%d", &n, &t) >= 2) {
    //int ** skill = new int*[101];for(int i=0; i<101; ++i) skill[i]=new int[4];
    vector<vector<int>>a(n,2);
    vector<vector<int>>b(n,2);
    for(int i=0; i<n; ++i){
        for(int j=0; j<2; ++j){
        scanf("%d%d", &a[i][j],&b[i][j]);
        }
    }
    cin >> m;
    vector<item>vec(m);
    vector<int>scored(m);
    vector<string>words(m);
    vector<int>diff(m);
    int scorednum=0;
    int temp;
    string str1;
    item aaaa;
    for(int i=0; i<m; ++i){
        cin >> words[i] >> diff[i];
    }
    for(int i=0; i<m; ++i) q.push_back(i);
    int currtime=t;
    int needtime;
    vector<vector<int>>d(n,vector<int>(m));
    int top;
    pair<int,int>currplayer;
    currplayer = make_pair(0,0);
    int temp11=0;
    vector<vector<string>>ress(n);
    while(!q.empty()){
        currtime=t;
        while(currtime>0 && !q.empty()){
            top = q.front();
            q.pop_front();
            needtime = max(1,diff[top]-(a[currplayer.first][currplayer.second]+a[currplayer.first][!currplayer.second])-d[currplayer.first][top]);
            if(needtime <= currtime){
                currtime-=needtime;
                ress[currplayer.first].push_back(words[top]);
            }
            else{
                d[currplayer.first][top] += currtime;
                currtime=0;
                q.push_back(top);
            }
        }
        if(++currplayer.first>=n){
            currplayer.first=0;
            currplayer.second=!currplayer.second;
        }
    }
    for(int i=0; i<ress.size(); ++i){
        cout << ress[i].size();
        for(int j=0; j<ress[i].size(); ++j) cout << " " << ress[i][j];
        cout << endl;
    }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <ctime>
#include <string>
using namespace std;

int n,m,t,k;
deque<int>q;

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

    while (scanf("%d%d", &n, &t) >= 2) {
    vector<vector<int>>a(n,vector<int>(2));
    vector<vector<int>>b(n,vector<int>(2));
    for (int i = 0; i < n; i++)
      for (int i2 = 0; i2 < 2; i2++)
        scanf("%d%d", &a[i][i2], &b[i][i2]);
    cin >> m;
    vector<string>words(m);
    vector<int>diff(m);
   for (int i = 0; i < m; i++) {
      char buf[30];
      scanf("%s%d", buf, &diff[i]);
      words[i] = buf;
    }
     vector<vector<string>>ress(n);

    vector<vector<int>>d(n,vector<int>(m,0));
    int top;
    for(int i=0; i<m; ++i) q.push_back(i);
   int curp=0,curt=0;
    while(!q.empty()){
       int currtime=t;
        while(currtime>0 && !q.empty()){
         int   top = q.front();
            q.pop_front();
          int  needtime = max(1, (diff[top]-(a[curt][curp]+a[curt][!curp])-d[curt][top]));
            if(currtime>=needtime){
                currtime-=needtime;
                ress[curt].push_back(words[top]);
            }
            else{
                 q.push_back(top);
                d[curt][top] += currtime;
                currtime=0;
            }
        }
        if(++curt>=n){
            curt=0;
            curp=!curp;
        }
    }
   for (int i = 0; i < n; i++) {
      printf("%d", ress[i].size());
      for (int i2 = 0; i2 < ress[i].size(); i2++)
        printf(" %s", ress[i][i2].c_str());
      printf("\n");
    }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <ctime>
#include <string>
using namespace std;

int n,m,t,k;
deque<int>q;

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

    while (scanf("%d%d", &n, &t) >= 2) {
    vector<vector<int>>a(n,vector<int>(2));
    vector<vector<int>>b(n,vector<int>(2));
    for(int i=0; i<n; ++i){
        for(int j=0; j<2; ++j){
        scanf("%d%d", &a[i][j],&b[i][j]);
        }
    }
    cin >> m;
    vector<int>scored(m);
    vector<string>words(m);
    vector<int>diff(m);
    int scorednum=0;
    int temp;
    string str1;
    for(int i=0; i<m; ++i){
        cin >> words[i] >> diff[i];
    }
    for(int i=0; i<m; ++i) q.push_back(i);
    int currtime=t;
    int needtime;
    vector<vector<int>>d(n,vector<int>(m));
    int top;
    pair<int,int>currplayer;
    currplayer = make_pair(0,0);
    vector<vector<string>>ress(n);
    while(!q.empty()){
        currtime=t;
        while(currtime>0 && !q.empty()){
            top = q.front();
            q.pop_front();
            needtime = max(1,diff[top]-(a[currplayer.first][currplayer.second]+a[currplayer.first][!currplayer.second])-d[currplayer.first][top]);
            if(needtime <= currtime){
                currtime-=needtime;
                ress[currplayer.first].push_back(words[top]);
            }
            else{
                d[currplayer.first][top] += currtime;
                currtime=0;
                q.push_back(top);
            }
        }
        if(++currplayer.first>=n){
            currplayer.first=0;
            currplayer.second=!currplayer.second;
        }
    }
    for(int i=0; i<ress.size(); ++i){
        cout << ress[i].size();
        for(int j=0; j<ress[i].size(); ++j) cout << " " << ress[i][j];
        cout << endl;
    }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <ctime>
#include <string>
using namespace std;

int n,m,t,k;
deque<int>q;

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

    while (scanf("%d%d", &n, &t) >= 2) {
    vector<vector<int>>a(n,vector<int>(2));
    vector<vector<int>>b(n,vector<int>(2));
    for (int i = 0; i < n; i++)
      for (int i2 = 0; i2 < 2; i2++)
        scanf("%d%d", &a[i][i2], &b[i][i2]);
    cin >> m;
    vector<string>words(m);
    vector<int>diff(m);
   for (int i = 0; i < m; i++) {
      char buf[30];
      scanf("%s%d", buf, &diff[i]);
      words[i] = buf;
    }
     vector<vector<string>>ress(n);

    vector<vector<int>>d(n,vector<int>(m,0));
    int top;
    for(int i=0; i<m; ++i) q.push_back(i);
   int curp=0,curt=0;int currtime=0; int needtime=0;
    while(!q.empty()){
       int currtime=t;
        while(currtime>0 && !q.empty()){
         int   top = q.front();
            q.pop_front();
          int  needtime = max(1, (diff[top]-(a[curt][curp]+a[curt][!curp])-d[curt][top]));
            if(needtime <= currtime){
                currtime-=needtime;
                ress[curt].push_back(words[top]);
            }
            else{
                d[curt][top] += currtime;
                currtime=0;
                q.push_back(top);
            }
        }
        if(++curt>=n){
            curt=0;
            curp=!curp;
        }
    }
   for (int i = 0; i < n; i++) {
      printf("%d", ress[i].size());
      for (int i2 = 0; i2 < ress[i].size(); i2++)
        printf(" %s", ress[i][i2].c_str());
      printf("\n");
    }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <ctime>
#include <string>
using namespace std;

int n,m,t,k;
deque<int>q;

struct item{
    string word;
    int diff;
    int num;
};

pair<int,int> next_player(pair<int,int>p){
    int i=p.first;
    int j=p.second;
    if(++i>=n){
        i=0;
        j=!j;
    }
    return make_pair(i , j);
}

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

    cin >>n >>t;
    //int ** skill = new int*[101];for(int i=0; i<101; ++i) skill[i]=new int[4];
    vector<vector<int>>a(n,2);
    vector<vector<int>>b(n,2);
    for(int i=0; i<n; ++i){
        for(int j=0; j<2; ++j){
        scanf("%d%d", &a[i][j],&b[i][j]);
        }
    }
    cin >> m;
    vector<item>vec(m);
    vector<int>scored(m);
    vector<string>words(m);
    vector<int>diffs(m);
    int scorednum=0;
    int temp;
    string str1;
    item aaaa;
    for(int i=0; i<m; ++i){
        cin >> words[i] >> diffs[i];
    }
    for(int i=0; i<m; ++i) q.push_back(i);
    int currtime=t;
    int needtime;
    vector<vector<int>>d(n,vector<int>(m));
    item top;
    pair<int,int>currplayer;
    currplayer = make_pair(0,0);
    int temp11=0;
    vector<vector<string>>ress(n);
    while(!q.empty()){
        currtime=t;
        while(currtime>0 && !q.empty()){
            top = vec[q.front()];
            q.pop_front();
            temp11=0;
            temp11+=top.diff-d[currplayer.first][top.num];
            temp11-=a[currplayer.first][currplayer.second]+b[currplayer.first][!currplayer.second];
            needtime = max(1,top.diff-(a[currplayer.first][currplayer.second]+a[currplayer.first][!currplayer.second])-d[currplayer.first][top.num]);
            if(needtime <= currtime){
                scorednum++;
                d[currplayer.first][top.num] = needtime;
                currtime-=needtime;
                ress[currplayer.first].push_back(top.word);
            }
            else{
                d[currplayer.first][top.num] += currtime;
                currtime=0;
                q.push_back(top.num);
                break;
            }
        }
        if(++currplayer.first>=n){
            currplayer.first=0;
            currplayer.second=!currplayer.second;
        }
    }
    for(int i=0; i<ress.size(); ++i){
        cout << ress[i].size();
        for(int j=0; j<ress[i].size(); ++j) cout << " " << ress[i][j];
        cout << endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <ctime>
#include <string>
using namespace std;

int n,m,t,k;
deque<int>q;

struct item{
    string word;
    int diff;
    int num;
};

pair<int,int> next_player(pair<int,int>p){
    int i=p.first;
    int j=p.second;
    if(++i==n) i=0;
    j=!j;
    return make_pair(i , j);
}

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

    cin >>n >>t;
    //int ** skill = new int*[101];for(int i=0; i<101; ++i) skill[i]=new int[4];
    int ** a = new int*[101]; for(int i=0; i<101; ++i) a[i]=new int[2];
    int ** b = new int*[101]; for(int i=0; i<101; ++i) b[i]=new int[2];
    for(int i=0; i<n; ++i){
        scanf("%d%d%d%d", &a[i][0],&b[i][0],&a[i][1],&b[i][1]);
    }
    cin >> m;
    vector<item>vec(m);
    vector<int>scored(m);
    int scorednum=0;
    int temp;
    string str1;
    item aaaa;
    for(int i=0; i<m; ++i){
        cin >> str1 >> temp;
        aaaa.word=str1;
        aaaa.diff=temp;
        aaaa.num=i;
        vec[i]=aaaa;
    }
    for(int i=0; i<m; ++i) q.push_back(i);
    int currtime=t;
    int needtime;
    int ** d = new int*[101];for(int i=0; i<101; ++i) d[i]=new int[101];
    for(int i=0; i<101; ++i)for(int j=0; j<101; ++j) d[i][j]=0;
    int * res = new int[101];
    for(int i=0; i<101; ++i)res[i]=0;
    item top;
    pair<int,int>currplayer;
    currplayer = make_pair(0,0);
    int temp11=0;
    vector<vector<string>>ress(n);
    while(!q.empty()){
        currtime=t;
        while(currtime>0 && !q.empty()){
            top = vec[q.front()];
            q.pop_front();
            temp11=0;
            temp11+=top.diff-d[currplayer.first][top.num];
            temp11-=a[currplayer.first][currplayer.second]+b[currplayer.first][!currplayer.second];
            needtime = max(1,top.diff-(a[currplayer.first][currplayer.second]+a[currplayer.first][!currplayer.second])-d[currplayer.first][top.num]);
            if(needtime <= currtime){
                scorednum++;
                d[currplayer.first][top.num] = needtime;
                currtime-=needtime;
                ress[currplayer.first].push_back(top.word);
            }
            else{
                d[currplayer.first][top.num] += t;
                currtime=0;
                q.push_back(top.num);
                break;
            }
        }
        currplayer = next_player(currplayer);
    }
    for(int i=0; i<ress.size(); ++i){
        cout << ress[i].size() <<" ";
        for(int j=0; j<ress[i].size(); ++j) cout << ress[i][j] << " ";
        cout << endl;
    }
    return 0;
}


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

using namespace std;

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

queue <pair <int, int> > q;
int a[105][3], b[105][3];
int d[105][105];
string name[105];
vector <string> ans[105];

int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int n, m, T;
    int i, j;
    scanf("%d%d", &n, &T);
    for (i = 0; i < n; ++i)
        for (j = 0; j < 2; ++j)
            scanf("%d%d", &a[i][j], &b[i][j]);
    scanf("%d", &m);
    for (i = 0; i < m; ++i)
    {
        string s;
        int x;
        cin >> s >> x;
        name[i] = s;
        q.push(make_pair(i, x));
    }
    i = 0;
    j = 0;
    int t = T;
    while (!q.empty())
    {
        int u = q.front().first;
        int x = q.front().second;
        q.pop();
        if (max(1, x - a[i][j] - b[i][1 - j] - d[i][u]) <= t)
        {
            t -= max(1, x - a[i][j] - b[i][1 - j] - d[i][u]);
            ans[i].push_back(name[u]);
        }
        else
        {
            d[i][u] += t;
            i++;
            if (i == n)
            {
                i = 0;
                j = 1 - j;
            }
            t = T;
            q.push(make_pair(u, x));
        }
    }
    for (i = 0; i < n; ++i)
    {
        printf("%d ", ans[i].size());
        for (j = 0; j < ans[i].size(); ++j)
            cout << ans[i][j] << " ";
        cout << endl;
    }
    return 0;
}


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

using namespace std;

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

queue <pair <int, int> > q;
int a[105][3], b[105][3];
int d[105][105];
string name[105];
vector <string> ans[105];

int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int n, m, T;
    int i, j;
    scanf("%d%d", &n, &T);
    for (i = 0; i < n; ++i)
        for (j = 0; j < 2; ++j)
            scanf("%d%d", &a[i][j], &b[i][j]);
    scanf("%d", &m);
    for (i = 0; i < m; ++i)
    {
        string s;
        int x;
        cin >> s >> x;
        name[i] = s;
        q.push(make_pair(i, x));
    }
    i = 0;
    j = 0;
    int t = T;
    while (!q.empty())
    {
        int u = q.front().first;
        int x = q.front().second;
        q.pop();
        if (max(1, x - a[i][j] - b[i][1 - j] - d[i][u]) <= t)
        {
            t -= max(1, x - a[i][j] - b[i][1 - j] - d[i][u]);
            ans[i].push_back(name[u]);
        }
        else
        {
            d[i][u] += t;
            i++;
            if (i == n)
            {
                i = 0;
                j++;
            }
            t = T;
            q.push(make_pair(u, x));
        }
    }
    for (i = 0; i < n; ++i)
    {
        printf("%d ", ans[i].size());
        for (j = 0; j < ans[i].size(); ++j)
            cout << ans[i][j] << " ";
        cout << endl;
    }
    return 0;
}


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

using namespace std;

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

queue <pair <int, int> > q;
int a[105][3], b[105][3];
int d[105][105];
string name[105];
vector <string> ans[105];

int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int n, m, T;
    int i, j;
    scanf("%d%d", &n, &T);
    for (i = 0; i < n; ++i)
        for (j = 0; j < 2; ++j)
            scanf("%d%d", &a[i][j], &b[i][j]);
    scanf("%d", &m);
    for (i = 0; i < m; ++i)
    {
        string s;
        int x;
        cin >> s >> x;
        name[i] = s;
        q.push(make_pair(i, x));
    }
    i = 0;
    j = 0;
    int t = T;
    while (!q.empty())
    {
        int u = q.front().first;
        int x = q.front().second;
        q.pop();
        if (max(1, x - a[i][j] - b[i][1 - j] - d[i][u]) <= t)
        {
            t -= max(1, x - a[i][j] - b[i][1 - j] - d[i][u]);
            ans[i].push_back(name[u]);
            if (t == 0)
            {
                i++;
                if (i == n)
                {
                    i = 0;
                    j = 1 - j;
                }
                t = T;
            }
        }
        else
        {
            d[i][u] += t;
            i++;
            if (i == n)
            {
                i = 0;
                j = 1 - j;
            }
            t = T;
            q.push(make_pair(u, x));
        }
    }
    for (i = 0; i < n; ++i)
    {
        printf("%d ", ans[i].size());
        for (j = 0; j < ans[i].size(); ++j)
            cout << ans[i][j] << " ";
        cout << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <queue>
#include <vector>

using namespace std;

vector<vector<string> > answ;

struct Word
{
    string s;
    int c;
    vector<int> d;
};

struct Player
{
    int pon, obs;
};

struct Team
{
    Player p1, p2;
};

void printansw()
{
    for(int i=0; i<answ.size(); i++)
    {
        cout <<answ[i].size() <<" ";
        for(int j=0; j<answ[i].size(); j++)
            cout <<answ[i][j] <<" ";
        cout <<endl;
    }
}

int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    vector<Team> teams;
    deque<Word> words;
    int n, t, m;
    cin >>n >>t;
    for(int i=0; i<n; i++)
    {
        Team t;
        cin >>t.p1.obs >>t.p1.pon >>t.p2.obs >>t.p2.pon;
        teams.push_back(t);
    }
    cin >>m;
    for(int i=0; i<m; i++)
    {
        Word w;
        cin >>w.s >>w.c;
        w.d.resize(n);
        words.push_back(w);
    }
    answ.resize(n);

    while(!words.empty())
    {
        //ходы всех перых игроков
        for(int i=0; i<n; i++)
        {
            int time=t;
            while(time>0)
            {
                Word w = words.front();
                words.pop_front();
                int p = max(1, w.c-(teams[i].p1.obs+teams[i].p2.pon)-w.d[i]);
                if(p<=time)
                {
                    answ[i].push_back(w.s);
                    time-=p;
                }
                else
                {
                    w.d[i]+=time;
                    words.push_back(w);
                    time=0;
                }
                if(words.empty())
                {
                    printansw();
                    return 0;
                }
            }
        }
        if(words.empty())
        {
            printansw();
            return 0;
        }
        //ходы всех вторых игроков
        for(int i=0; i<n; i++)
        {
            int time=t;
            while(time>0)
            {
                Word w = words.front();
                words.pop_front();
                int p = max(1, w.c-(teams[i].p2.obs+teams[i].p1.pon)-w.d[i]);
                if(p<=time)
                {
                    answ[i].push_back(w.s);
                    time-=p;
                }
                else
                {
                    w.d[i]+=time;
                    words.push_back(w);
                    time=0;
                }
                if(words.empty())
                {
                    printansw();
                    return 0;
                }
            }
        }
    }
    printansw();
    return 0;
}


#include <iostream>
#include <string>
#include <queue>
#include <vector>

using namespace std;

vector<vector<string> > answ;

struct Word
{
    string s;
    int c;
    vector<int> d;
};

struct Player
{
    int pon, obs;
};

struct Team
{
    Player p1, p2;
};

void printansw()
{
    for(int i=0; i<answ.size(); i++)
    {
        cout <<answ[i].size() <<" ";
        for(int j=0; j<answ[i].size(); j++)
            cout <<answ[i][j] <<" ";
        cout <<endl;
    }
}

int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    vector<Team> teams;
    deque<Word> words;
    int n, t, m;
    cin >>n >>t;
    for(int i=0; i<n; i++)
    {
        Team t;
        cin >>t.p1.obs >>t.p1.pon >>t.p2.obs >>t.p2.pon;
        teams.push_back(t);
    }
    cin >>m;
    for(int i=0; i<m; i++)
    {
        Word w;
        cin >>w.s >>w.c;
        w.d.resize(n);
        words.push_back(w);
    }
    answ.resize(n);
    while(!words.empty())
    {
        //ходы всех перых игроков
        for(int i=0; i<n; i++)
        {
            int time=t;
            while(time>=0)
            {
                Word w = words.front();
                words.pop_front();
                int p = max(1, w.c-(teams[i].p1.obs+teams[i].p2.pon)-w.d[i]);
                w.d[i]+=p;
                if(p<=time)
                {
                    answ[i].push_back(w.s);
                }
                else
                {
                    words.push_back(w);
                }
                time-=p;
                if(words.empty())
                {
                    printansw();
                    return 0;
                }
            }
        }
        //ходы всех вторых игроков
        for(int i=0; i<n; i++)
        {
            int time=t;
            while(time>=0)
            {
                Word w = words.front();
                words.pop_front();
                int p = max(1, w.c-(teams[i].p2.obs+teams[i].p1.pon)-w.d[i]);
                w.d[i]+=p;
                if(p<=time)
                {
                    answ[i].push_back(w.s);
                }
                else
                {
                    words.push_back(w);
                }
                time-=p;
                if(words.empty())
                {
                    printansw();
                    return 0;
                }
            }
        }
    }
    printansw();
    return 0;
}


#include <iostream>
#include <string>
#include <queue>
#include <vector>

using namespace std;

vector<vector<string> > answ;

struct Word
{
    string s;
    int c;
    vector<int> d;
};

struct Player
{
    int pon, obs;
};

struct Team
{
    Player p1, p2;
};

void printansw()
{
    for(int i=0; i<answ.size(); i++)
    {
        cout <<answ[i].size() <<" ";
        for(int j=0; j<answ[i].size(); j++)
            cout <<answ[i][j] <<" ";
        cout <<endl;
    }
}

int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    vector<Team> teams;
    deque<Word> words;
    int n, t, m;
    cin >>n >>t;
    for(int i=0; i<n; i++)
    {
        Team t;
        cin >>t.p1.obs >>t.p1.pon >>t.p2.obs >>t.p2.pon;
        teams.push_back(t);
    }
    cin >>m;
    for(int i=0; i<m; i++)
    {
        Word w;
        cin >>w.s >>w.c;
        w.d.resize(n);
        words.push_back(w);
    }
    answ.resize(n);
    while(!words.empty())
    {
        //ходы всех перых игроков
        for(int i=0; i<n; i++)
        {
            int time=t;
            while(time>0)
            {
                Word w = words.front();
                words.pop_front();
                int p = max(1, w.c-(teams[i].p1.obs+teams[i].p2.pon)-w.d[i]);
                if(p<=time)
                {
                    answ[i].push_back(w.s);
                    time-=p;
                }
                else
                {
                    w.d[i]+=p;
                    words.push_back(w);
                    time=0;
                }
                if(words.empty())
                {
                    printansw();
                    return 0;
                }
            }
        }
        //ходы всех вторых игроков
        for(int i=0; i<n; i++)
        {
            int time=t;
            while(time>0)
            {
                Word w = words.front();
                words.pop_front();
                int p = max(1, w.c-(teams[i].p2.obs+teams[i].p1.pon)-w.d[i]);
                if(p<=time)
                {
                    answ[i].push_back(w.s);
                    time-=p;
                }
                else
                {
                    w.d[i]+=p;
                    words.push_back(w);
                    time=0;
                }
                if(words.empty())
                {
                    printansw();
                    return 0;
                }
            }
        }
    }
    printansw();
    return 0;
}


#include <iostream>
#include <string>
#include <queue>
#include <vector>

using namespace std;

vector<vector<string> > answ;

struct Word
{
    string s;
    int c;
    vector<int> d;
};

struct Player
{
    int pon, obs;
};

struct Team
{
    Player p1, p2;
};

void printansw()
{
    for(int i=0; i<answ.size(); i++)
    {
        cout <<answ[i].size() <<" ";
        for(int j=0; j<answ[i].size(); j++)
            cout <<answ[i][j] <<" ";
        cout <<endl;
    }
}

int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    vector<Team> teams;
    deque<Word> words;
    int n, t, m;
    cin >>n >>t;
    for(int i=0; i<n; i++)
    {
        Team t;
        cin >>t.p1.obs >>t.p1.pon >>t.p2.obs >>t.p2.pon;
        teams.push_back(t);
    }
    cin >>m;
    for(int i=0; i<m; i++)
    {
        Word w;
        cin >>w.s >>w.c;
        w.d.resize(n);
        words.push_back(w);
    }
    answ.resize(n);
    while(!words.empty())
    {
        //ходы всех перых игроков
        for(int i=0; i<n; i++)
        {
            int time=t;
            while(time>0)
            {
                Word w = words.front();
                words.pop_front();
                int p = max(1, w.c-(teams[i].p1.obs+teams[i].p2.pon)-w.d[i]);
                w.d[i]+=p;
                if(p<=time)
                {
                    answ[i].push_back(w.s);
                }
                else
                {
                    words.push_back(w);
                }
                time-=p;
                if(words.empty())
                {
                    printansw();
                    return 0;
                }
            }
        }
        //ходы всех вторых игроков
        for(int i=0; i<n; i++)
        {
            int time=t;
            while(time>0)
            {
                Word w = words.front();
                words.pop_front();
                int p = max(1, w.c-(teams[i].p2.obs+teams[i].p1.pon)-w.d[i]);
                w.d[i]+=p;
                if(p<=time)
                {
                    answ[i].push_back(w.s);
                }
                else
                {
                    words.push_back(w);
                }
                time-=p;
                if(words.empty())
                {
                    printansw();
                    return 0;
                }
            }
        }
    }
    printansw();
    return 0;
}


#include <iostream>
#include <string>
#include <queue>
#include <vector>

using namespace std;

vector<vector<string> > answ;

struct Word
{
    string s;
    int c;
    vector<int> d;
};

struct Player
{
    int pon, obs;
};

struct Team
{
    Player p1, p2;
};

void printansw()
{
    for(int i=0; i<answ.size(); i++)
    {
        cout <<answ[i].size() <<" ";
        for(int j=0; j<answ[i].size(); j++)
            cout <<answ[i][j] <<" ";
        cout <<endl;
    }
}

int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    vector<Team> teams;
    deque<Word> words;
    int n, t, m;
    cin >>n >>t;
    for(int i=0; i<n; i++)
    {
        Team t;
        cin >>t.p1.obs >>t.p1.pon >>t.p2.obs >>t.p2.pon;
        teams.push_back(t);
    }
    cin >>m;
    for(int i=0; i<m; i++)
    {
        Word w;
        cin >>w.s >>w.c;
        w.d.resize(n);
        words.push_back(w);
    }
    answ.resize(n);
    while(!words.empty())
    {
        //ходы всех перых игроков
        for(int i=0; i<n; i++)
        {
            int time=t;
            while(time>0)
            {
                Word w = words.front();
                words.pop_front();
                int p = max(1, w.c-(teams[i].p1.obs+teams[i].p2.pon)-w.d[i]);
                if(p<=time)
                {
                    answ[i].push_back(w.s);
                    time-=p;
                }
                else
                {
                    w.d[i]+=p;
                    words.push_back(w);
                    time=0;
                }
                if(words.empty())
                {
                    printansw();
                    return 0;
                }
            }
        }
        if(words.empty())
        {
            printansw();
            return 0;
        }
        //ходы всех вторых игроков
        for(int i=0; i<n; i++)
        {
            int time=t;
            while(time>0)
            {
                Word w = words.front();
                words.pop_front();
                int p = max(1, w.c-(teams[i].p2.obs+teams[i].p1.pon)-w.d[i]);
                if(p<=time)
                {
                    answ[i].push_back(w.s);
                    time-=p;
                }
                else
                {
                    w.d[i]+=p;
                    words.push_back(w);
                    time=0;
                }
                if(words.empty())
                {
                    printansw();
                    return 0;
                }
            }
        }
    }
    printansw();
    return 0;
}


#include <iostream>
#include <string>
#include <queue>
#include <vector>

using namespace std;

vector<vector<string> > answ;

struct Word
{
    string s;
    int c;
    vector<int> d;
};

struct Player
{
    int pon, obs;
};

struct Team
{
    Player p1, p2;
};

void printansw()
{
    for(int i=0; i<answ.size(); i++)
    {
        cout <<answ[i].size() <<" ";
        for(int j=0; j<answ[i].size(); j++)
            cout <<answ[i][j] <<" ";
        cout <<endl;
    }
}

int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    vector<Team> teams;
    deque<Word> words;
    int n, t, m;
    cin >>n >>t;
    for(int i=0; i<n; i++)
    {
        Team t;
        cin >>t.p1.obs >>t.p1.pon >>t.p2.obs >>t.p2.pon;
        teams.push_back(t);
    }
    cin >>m;
    for(int i=0; i<m; i++)
    {
        Word w;
        cin >>w.s >>w.c;
        w.d.resize(n);
        words.push_back(w);
    }
    answ.resize(n);
    while(!words.empty())
    {
        //ходы всех перых игроков
        for(int i=0; i<n; i++)
        {
            int time=t;
            while(time>=0)
            {
                Word w = words.front();
                words.pop_front();
                int p = max(1, w.c-(teams[i].p1.obs+teams[i].p2.pon)-w.d[i]);
                w.d[i]+=p;
                if(p<=time)
                {
                    answ[i].push_back(w.s);
                }
                else
                {
                    words.push_back(w);
                }
                time-=p;
                if(words.empty())
                {
                    printansw();
                    return 0;
                }
            }
        }
        //ходы всех вторых игроков
        for(int i=0; i<n; i++)
        {
            int time=t;
            while(time>0)
            {
                Word w = words.front();
                words.pop_front();
                int p = max(1, w.c-(teams[i].p2.obs+teams[i].p1.pon)-w.d[i]);
                w.d[i]+=p;
                if(p<=time)
                {
                    answ[i].push_back(w.s);
                }
                else
                {
                    words.push_back(w);
                }
                time-=p;
                if(words.empty())
                {
                    printansw();
                    return 0;
                }
            }
        }
    }
    printansw();
    return 0;
}


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

using namespace std;

const int nmax = 100;

string name[nmax];
bool u[nmax];
int c[nmax], d[nmax][nmax];
int a[nmax][2], b[nmax][2];
vector<string> ans[nmax];

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

	int n, m, i, j, k, t;
	cin >> n >> t;
	for(i = 0; i < n; i++)
		for(j = 0; j < 2; j++)
			cin >> a[i][j] >> b[i][j];

	cin >> m;
	for(i = 0; i < m; i++)
		cin >> name[i] >> c[i];

	int t1 = 0, t2 = 0, solved = 0, card = 0, timeLeft = t;
	while(solved < m)
	{
		while(u[card]) card = (card + 1) % m;
		int timeNeeded = max(1, c[card] - (a[t1][t2] + b[t1][1 - t2]) - d[t1][card]);
		if (timeNeeded > timeLeft)
		{
			d[t1][card] += timeLeft;

			if (timeLeft > 0)
				card = (card + 1) % m;

			timeLeft = t;
			++t1;
			if (t1 == n)
			{
				t1 = 0;
				t2 = 1 - t2;
			}
		} else
		{
			u[card] = true;
			++solved;
			ans[t1].push_back(name[card]);
			timeLeft -= timeNeeded;
		}
	}

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

	return 0;
}


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

using namespace std;

const int nmax = 100;

string name[nmax];
bool u[nmax];
int c[nmax], d[nmax][nmax];
int a[nmax][2], b[nmax][2];
vector<string> ans[nmax];

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

	int n, m, i, j, k, t;
	cin >> n >> t;
	for(i = 0; i < n; i++)
		for(j = 0; j < 2; j++)
			cin >> a[i][j] >> b[i][j];

	cin >> m;
	for(i = 0; i < m; i++)
		cin >> name[i] >> c[i];

	int t1 = 0, t2 = 0, solved = 0, card = 0, timeLeft = t;
	while(solved < m)
	{
		while(u[card]) card = (card + 1) % m;
		int timeNeeded = max(1, c[card] - (a[t1][t2] + b[t1][1 - t2]) - d[t1][card]);
		if (timeNeeded > timeLeft)
		{
			d[t1][card] += timeLeft;

			timeLeft = t;
			if (timeLeft > 0)
				card = (card + 1) % m;
			++t1;
			if (t1 == n)
			{
				t1 = 0;
				t2 = 1 - t2;
			}
		} else
		{
			u[card] = true;
			++solved;
			ans[t1].push_back(name[card]);
			timeLeft -= timeNeeded;
		}
	}

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

	return 0;
}


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

using namespace std;

const int nmax = 100;

string name[nmax];
bool u[nmax];
int c[nmax], d[nmax][nmax];
int a[nmax][2], b[nmax][2];
vector<string> ans[nmax];

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

	int n, m, i, j, k, t;
	cin >> n >> t;
	for(i = 0; i < n; i++)
		for(j = 0; j < 2; j++)
			cin >> a[i][j] >> b[i][j];

	cin >> m;
	for(i = 0; i < m; i++)
		cin >> name[i] >> c[i];

	int t1 = 0, t2 = 0, solved = 0, card = 0, timeLeft = t;
	while(solved < m)
	{
		while(u[card]) card = (card + 1) % m;
		int timeNeeded = max(1, c[card] - (a[t1][t2] + b[t1][1 - t2]) - d[t1][card]);
		if (timeNeeded > timeLeft)
		{
			d[t1][card] += timeLeft;

			timeLeft = t;
			card = (card + 1) % m;
			++t1;
			if (t1 == n)
			{
				t1 = 0;
				t2 = 1 - t2;
			}
		} else
		{
			u[card] = true;
			++solved;
			ans[t1].push_back(name[card]);
			timeLeft -= timeNeeded;
		}
	}

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

	return 0;
}


#include <fstream>
//#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <cmath>
#include <vector>
#include <limits.h>
#include <queue>
using namespace std;

typedef long long ll;
typedef vector <int>::iterator v_iter;
typedef vector <int>::reverse_iterator rv_iter;

ifstream cin("input.txt"); ofstream cout("output.txt");
int n, t, m;
vector < vector<int> > a, b;
queue<int> time, num;
queue<string> name;
vector <vector<int> > d;
vector <vector<string> > got;

int main(){
	cin >> n >> t;
	a.resize(n); b.resize(n);got.resize(n);d.resize(n);
	for (int i = 0; i < n; i++){
		a[i].resize(2); b[i].resize(2);
		cin >> a[i][0] >> b[i][0] >> a[i][1] >> b[i][1];
	}
	cin >> m;
	for (int i = 0; i < n; i++)
		d[i].resize(m);
	for (int i = 0; i < m; i++){
		string tmp; int tmp1;cin >> tmp >> tmp1;
		time.push(tmp1); name.push(tmp); num.push(i);
	}
	int xod = 0, tt = 0;//����� ����
	for(;!time.empty();){
		if (t <= tt) {xod++; tt = 0; continue;}
		int tmp = time.front(), tmpnum = num.front(); string tmp1 = name.front(); time.pop(); name.pop(); num.pop();
		int solved = max(1, tmp - (a[xod % n][(xod / n) % 2] + b[xod % n][(xod / n + 1) % 2]) - d[xod % n][tmpnum]);
		if (solved + tt > t){
			d[xod % n][tmpnum] += t - tt;
			time.push(tmp);
			num.push(tmpnum);
			name.push(tmp1);
			tt = 0;
			xod++;
		} else{
			tt += solved;
			got[xod % n].push_back(tmp1);
		}
	}
	for (int i = 0; i < n; i++){
		cout << got[i].size() << ' ';
		for (int j = 0; j < got[i].size(); j++)
			cout << got[i][j] << ' ';
		cout << "\n";
	}
	return 0;
}


#include <fstream>
//#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <cmath>
#include <vector>
#include <limits.h>
#include <queue>
using namespace std;

typedef long long ll;
typedef vector <int>::iterator v_iter;
typedef vector <int>::reverse_iterator rv_iter;

ifstream cin("input.txt"); ofstream cout("output.txt");
int n, t, m;
vector < vector<int> > a, b;
queue<int> time, num;
queue<string> name;
vector <vector<int> > d;
vector <vector<string> > got;

int main(){
	cin >> n >> t;
	a.resize(n); b.resize(n);got.resize(n);d.resize(n);
	for (int i = 0; i < n; i++){
		a[i].resize(2); b[i].resize(2);
		cin >> a[i][0] >> b[i][0] >> a[i][1] >> b[i][1];
	}
	cin >> m;
	for (int i = 0; i < n; i++)
		d[i].resize(m);
	for (int i = 0; i < m; i++){
		string tmp; int tmp1;cin >> tmp >> tmp1;
		time.push(tmp1); name.push(tmp); num.push(i);
	}
	int xod = 0, tt = 0;//����� ����
	for(;!time.empty();){
		if (t <= tt) {xod++; tt = 0; continue;}
		int tmp = time.front(), tmpnum = num.front(); string tmp1 = name.front(); time.pop(); name.pop(); num.pop();
		int solved = max(1, tmp - (a[xod % n][(xod / n) % 2] + b[xod % n][(xod / n + 1) % 2]) - d[xod % n][tmpnum]);
		if (solved + tt > t){
			d[xod % n][tmpnum] += t;
			time.push(tmp);
			num.push(tmpnum);
			name.push(tmp1);
			tt = 0;
			xod++;
		} else{
			tt += solved;
			got[xod % n].push_back(tmp1);
		}
	}
	for (int i = 0; i < n; i++){
		cout << got[i].size() << ' ';
		for (int j = 0; j < got[i].size(); j++)
			cout << got[i][j] << ' ';
		cout << "\n";
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:64000000")
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <set>
#include <map>
#include <cassert>
#include <queue>
#include <deque>
#include <stack>
#include <sstream>
#include <cstring>
#include <string>
#include <functional>
#include <numeric>

using namespace std;

#define MP make_pair
#define all(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << x << endl

const int INF = ((1 << 23) - 1);
const long long LLINF = (((1LL << 63) - 1LL));
const double EPS = 1e-9;
const double PI = 3.14159265358979323846;

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

int main() {
    freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
    int n, t;
    cin >> n >> t;
    vector<int> speed(2 * n);
    for (int i = 0; i < n; ++i) {
        int a1, b1, a2, b2;
        cin >> a1 >> b1 >> a2 >> b2;
        speed[i] = a1 + b2;
        speed[i + n] = a2 + b1;
    }
    int m;
    cin >> m;
    vector<string> words(m);
    vector<int> cost(m);
    for (int i = 0; i < m; ++i) {
        cin >> words[i] >> cost[i];
    }
    vector<vector<int> > spentTime(m, vector<int>(n, 0));
    vector<vector<int> > score(n);
    deque<int> cards;
    for (int i = 0; i < m; ++i) {
        cards.push_back(i);
    }
    bool stop = false;
    int human = 0;
    while (!cards.empty() && !stop) {
        // start play
        int team = human % n;
        int remainTime = t;
        while (remainTime > 0 && !stop) {
            if (cards.empty()) {
                stop = true;
                break;
            }
            int card = cards.front();
            cards.pop_front();
            int reqTime = max(1, cost[card] - speed[human] - spentTime[card][team]);
            if (reqTime <= remainTime) {
                remainTime -= reqTime;
                score[team].push_back(card);
                continue;
            }
            else {
                spentTime[card][team] += remainTime;
                cards.push_back(card);
                break;
            }
        }
        human = (human + 1) % (2 * n);
    }
    for (int team = 0; team < n; ++team) {
        int res = 0;
        for (int i = 0; i < score[team].size(); ++i) {
            //res += cost[score[team][i]];
            res += 1;
        }
        cerr << res << " ";
        cout << res << " ";
        for (int i = 0; i < score[team].size(); ++i) {
            cerr << words[score[team][i]] << " ";
            cout << words[score[team][i]] << " ";
        }
        cout << endl;
        cerr << endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <ctime>
#include <string>
using namespace std;

int main(){
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int n,t;
    while (scanf("%d%d", &n, &t) >= 2) {
        vector<vector<int>>a(n,vector<int>(2)),b(n,vector<int>(2));
        for (int i = 0; i < n; i++)
            for (int i2 = 0; i2 < 2; i2++)
                scanf("%d%d", &a[i][i2], &b[i][i2]);
        int m;
        cin >> m;
        vector<string>words(m); vector<int>diff(m);
        for (int i = 0; i < m; i++) {
            char buf[30];
            scanf("%s%d", buf, &diff[i]);
            words[i] = buf;
        }
        vector<vector<string>>res(n);

        vector<vector<int>>d(n,vector<int>(m,0));
        deque<int>q;
        for(int i=0; i<m; ++i) q.push_back(i);

        int curp=0,curt=0;
        while(!q.empty()){
            int currtime=t;
            while(currtime>0 && !q.empty()){
                int top = q.front(); q.pop_front();
                int needtime = max(1, (diff[top]-(a[curt][curp]+b[curt][!curp])-d[curt][top]));
                if(currtime>=needtime){
                    currtime-=needtime;
                    res[curt].push_back(words[top]);
                }
                else{
                    q.push_back(top);
                    d[curt][top] += currtime;
                    currtime=0;
                }
            }
            if(++curt>=n){
                curt=0;
                curp=!curp;
            }
        }
        for(int i = 0; i < n; i++){
            printf("%d", res[i].size());
            for(int i2 = 0; i2 < res[i].size(); i2++)
                printf(" %s", res[i][i2].c_str());
                printf("\n");
        }
    }
    return 0;
}


#include <cstdio>
#include <math.h>
#include <stdlib.h>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <memory.h>

using namespace std;

int a[111][2], b[111][2];
string words[111];
int c[111];
char ch[50];
int spent[111][111];
bool guess[111];
vector <string> guesses[111];

int main(){
	memset(guess, 0, sizeof(guess));
	memset(guess, 0, sizeof(guesses));
	memset(spent, 0, sizeof(spent));

	int n;
	FILE *fin = fopen("input.txt", "r");
	FILE *fout = fopen("output.txt", "w");

	int t, time;
	fscanf(fin, "%d%d", &t, &time);

	for (int i = 0; i < t; i++){
		fscanf(fin, "%d%d%d%d", &a[i][0], &b[i][0], &a[i][1], &b[i][1]);
	}

	int m;
	fscanf(fin, "%d\n", &m);

	for (int i = 0; i < m; i++){
		fscanf(fin, "%s\n", ch);
		words[i].assign(ch);
		fscanf(fin, "%d", c + i);
	}

	int lwords = m;
	int k = 0;
	while (lwords){
		for (int q = 0; q < 2 && lwords; q++){
			for (int i = 0; i < t && lwords; i++){
				int q2 = (q + 1) % 2;
				int ltime = time;
				while (ltime && lwords){
					while (guess[k] && lwords){
						k = (k + 1) % m;
					}
					int need = max(1, c[k] - (a[i][q] + b[i][q2]) - spent[i][k]);
					if (need <= ltime){
						guess[k] = true;
						guesses[i].push_back(words[k]);
						ltime -= need;
						lwords--;
					}
					else {
						spent[i][k] += ltime;
						ltime = 0;
					}
					k = (k + 1) % m;
				}
			}
		}
	}

	for (int i = 0; i < t; i++){
		fprintf(fout, "%d", guesses[i].size());
		for (int j = 0; j < guesses[i].size(); j++) fprintf(fout, " %s", guesses[i][j].c_str());
		fprintf(fout, "\n");
	}

	fclose(fin);
//	system("pause");
	return 0;
}


#include <deque>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN = 128;
const int MAXL = 32;

struct card {
    char name[MAXL];
    int c;
    int idx;
};

int m, n;
deque <card> dq;
int a[MAXN][2], b[MAXN][2];
int d[MAXN][MAXN];
vector <card> res[MAXN];

inline void next(int &team, int &turn) {
    if (team == n - 1) team = 0, turn ^= 1;
    else ++team;
}

int main() {
    int i, j, k;
    int s, t;
    int team, turn;
    int q, c;
    int rem;
    int tot;
    card xx;

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

    scanf("%d %d", &n, &t);
    for (i=0; i<n; ++i) for (j=0; j<2; ++j) scanf("%d %d", a[i]+j, b[i]+j);
    scanf("%d", &m);
    for (i=0; i<m; ++i) {
        scanf("%s %d", xx.name, &xx.c);
        xx.idx = i;
        dq.push_back(xx);
    }

    i = 0, j = 0;
    while (!dq.empty()) {
        tot = t;
        while (!dq.empty()) {
            xx = dq.front();
            dq.pop_front();
            c = xx.c, q = j ^ 1, k = xx.idx;
            rem = max(1, c-(a[i][j]+b[i][q])-d[i][k]);

            if (tot >= rem) {
                res[i].push_back(xx);
                tot -= rem;
            } else {
                d[i][k] += rem;
                dq.push_back(xx);
                break;
            }
        }
        next(i, j);
    }

    for (i=0; i<n; ++i) {
        printf("%d ", res[i].size());
        for (j=0; j<res[i].size(); ++j) printf("%s%s", res[i][j].name, j==res[i].size()-1?"\n":" ");
    }

    return 0;
}