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

#define pb push_back
#define mp make_pair
#define st first
#define nd second

using namespace std;

typedef long long ll;	
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;

VL conf_input(int n){
	VL ret;
	int t;
	for (int i = 0; i < n; ++i){
		scanf("%d", &t);
		ret.pb(t);
	}
	return ret;
}

void conf_output(VL a, bool flag = true){
	if (a.size()){
		printf("%lld", a[0]);
		for (int i = 1; i < a.size(); ++i)
			printf(" %lld", a[i]);
	}
	if (flag)
		puts("");
}

PLL& positive(PLL &a){
	if (a.nd < 0){
		a.st *= -1;
		a.nd *= -1;
	}
}

PLL ctof(VL a){
	if (a.size() == 1)
		return mp(a[0], 1);
	else{
		PLL f1(a[0], 1), f2(a[1] * a[0] + 1, a[1]);
		for (int i = 2; i < a.size(); ++i){
			f1 = mp(a[i] * f2.st + f1.st, a[i] * f2.nd + f1.nd);
			swap(f1, f2);
		}
		positive(f2);
		return f2;
	}
}

VL ftoc(PLL a){
	VL ret;
	while (a.nd){
		positive(a);
		ll t = a.st / a.nd;
		if (a.st < 0 && a.st % a.nd)
			--t;
		ret.pb(t);
		a.st -= t * a.nd;
		swap(a.st, a.nd);
	}
	return ret;
}

int main(){
	int n1, n2;
	for (int casi = 1; scanf("%d%d", &n1, &n2) == 2 && n1 + n2; ++casi){
		VL confa = conf_input(n1), confb = conf_input(n2);
		PLL a = ctof(confa), b = ctof(confb);

		printf("Case %d:\n", casi);
		conf_output(ftoc(mp(a.st * b.nd + a.nd * b.st, a.nd * b.nd)));
		conf_output(ftoc(mp(a.st * b.nd - a.nd * b.st, a.nd * b.nd)));
		conf_output(ftoc(mp(a.st * b.st, a.nd * b.nd)));
		conf_output(ftoc(mp(a.st * b.nd, a.nd * b.st)));
	}
	return 0;
}
