#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <string>
#include <iostream>
using namespace std;
#define int64 long long
#define INF 1000000000
#define EPS 1E-8

#define ALL(a) a.begin(), a.end()
#define PB(a, v) a.push_back(v)
#define MP(a,b) make_pair(a, b)
#define FOR(i, n) for (int i = 0; i < (n); i++)

int index(vector <string> &ss, string s)
{
	for (int i = 0; i < ss.size(); i++)
		if (ss[i] == s) return i;
	return 0;
}

int main()
{
#ifdef _DEBUG
	freopen("1166.in", "r", stdin);
#endif
	int n, m, it = 0;
	while (scanf("%d%d", &n, &m) == 2 && n)
	{
		printf("Case #%d\n", ++it);
		vector <int> a(n, 0);
		vector <string> ss(n);
		string s;
		for (int i = 0; i < n; i++)
			cin >> ss[i];
		for (int i = 0; i < m; i++)
		{ 
			int k1, k2, w; cin >> s; k1 = index(ss, s); cin >> s; k2 = index(ss, s); scanf("%d", &w);
			a[k1] += w; a[k2] -= w;
		}
		while (1)
		{
			int p, m;
			for (p = 0; p < n; p++)
				if (a[p] > 0) break;
			for (m = 0; m < n; m++)
				if (a[m] < 0) break;
			if (p >= n) break;
			int k = min(a[p], -a[m]);
			a[p] -= k; a[m] += k;
			printf("%s %s %d\n", ss[m].c_str(), ss[p].c_str(), k);
		}
		printf("\n");
	}
	return 0;
}
