#include <iostream>
#include <cstdio>
using namespace std;
#define rep(i,n) for(int i = 0; i < n; i++)

int n, m;
int t[13][13];
int main(){
  while(cin >> n, n){
    rep(i,13)rep(j,13)t[i][j]=0;
    rep(i,n)rep(j,n){
      cin >> m;
      t[i][j] = m;
      t[n][j] += m;
      t[i][n] += m;
      t[n][n] += m;
    }
    rep(i,n+1){
      rep(j,n+1){
	printf("%5d",t[i][j]);
      }
      cout << endl;
    }
  }
  return 0;
}
#include <stdio.h>
 
int main(void){
    int n;
    int a,b;
    int aSum, bSum;
    
    while (scanf("%d", &n), n!=0){
        aSum = bSum=0;
        for (int i=0; i<n; i++){
            scanf("%d %d", &a, &b);
            if (a>b) aSum += (a+b);
            else if (a<b) bSum += (a+b);
            else aSum += a, bSum += b;
        }
        printf("%d %d\n", aSum, bSum);
    }
    
    return 0;
}