#include <stdio.h>
//#include <math.h>
using namespace std;

int log10(int n) {
	int keta = 0;
	do{
		n /= 10;
		keta++;
	} while (n > 0);
	return keta;
}

int main() {
	const int MAX_N = 20;
	int N;
	int table[MAX_N][MAX_N];

	while (1) {
		for (int i = 0;i < MAX_N;i++) {
			for (int j = 0;j < MAX_N;j++) {
				table[i][j] = 0;
			}
		}
		scanf("%d", &N);
		if (!N)break;
		for (int i = 0;i < N;i++) {
			for (int j = 0;j < N;j++) {
				scanf("%d", &table[i][j]);
			}
		}

		for (int i = 0;i < N;i++) {
			for (int j = 0;j < N;j++) {
				table[i][N] += table[i][j];
				table[N][j] += table[i][j];
				table[N][N] += table[i][j];
			}
		}

		for (int i = 0;i <= N;i++) {
			for (int j = 0;j <= N;j++) {
				for (int k = 0;k < 5-log10(table[i][j]);k++) {
					printf(" ");
				}
				printf("%d", table[i][j]);
			}
			printf("\n");
		}
	}

	return 0;
}
#include<iostream>
#include<vector>
using namespace std;

int main(){
while(1){
	int a,b,c,n,p[1002],q[1002],r[1002],s[1002],Q[302];
	for(int i=0;i<302;i++)Q[i]=2;
	scanf("%d%d%d",&a,&b,&c);
	if(a==0&&b==0&&c==0)break;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d%d%d%d",&p[i],&q[i],&r[i],&s[i]);
		if(s[i]==1){Q[p[i]]=1; Q[q[i]]=1; Q[r[i]]=1;}
	}
	for(int i=0;i<n;i++){
		if(s[i]==0){
			if(Q[p[i]]==1&&Q[q[i]]==1)Q[r[i]]=0;
			if(Q[p[i]]==1&&Q[r[i]]==1)Q[q[i]]=0;
			if(Q[r[i]]==1&&Q[q[i]]==1)Q[p[i]]=0;
		}
	}
	for(int i=1;i<=a+b+c;i++){
		printf("%d\n",Q[i]);
	}
}
}