#include<iostream>
#include<sstream>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cassert>

#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

const int mod = 32768;
typedef vector<vi> M;
M operator+(const M &a, const M &b){
	M c(a.size(), vi(a[0].size()));
	rep(i, c.size()) rep(j, c[0].size()) c[i][j] = (a[i][j] + b[i][j]) % mod;
	return c;
}
M operator-(const M &a, const M &b){
	M c(a.size(), vi(a[0].size()));
	rep(i, c.size()) rep(j, c[0].size()) c[i][j] = (a[i][j] - b[i][j] + mod) % mod;
	return c;
}
M operator*(int c, M a){
	rep(i, a.size()) rep(j, a[0].size()) a[i][j] = a[i][j] * c % mod;
	return a;
}
M operator*(const M &a, const M &b){
	if(a.size() == 1 && a[0].size() == 1) return a[0][0] * b;
	if(b.size() == 1 && b[0].size() == 1) return b[0][0] * a;
	M c(a.size(), vi(b[0].size()));
	rep(i, c.size()) rep(j, c[0].size()) rep(k, a[0].size())
	c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % mod;
	return c;
}
map<char, M> val;
string in;
int p;

M expr(), term(), primary(), matrix();
M expr(){
	M res = term();
	while(p < in.size() && (in[p] == '+' || in[p] == '-')){
		bool pl = in[p++] == '+';
		M b = term();
		if(pl) res = res + b;
		else res = res - b;
	}
	return res;
}
M term(){
	bool mi = 0;
	while(p < in.size() && in[p] == '-') mi ^= 1, p++;
	M res;
	p--;
	do{
		p++;
		while(p < in.size() && in[p] == '-') mi ^= 1, p++;
		M b = primary();
		if(res.empty()) res = b;
		else res = res * b;
	}while(p < in.size() && in[p] == '*');
	if(mi) res = (mod - 1) * res;
	return res;
}
M primary(){
	M res;
	if(isdigit(in[p])){
		int d = 0;
		while(isdigit(in[p])) d = (d * 10 + in[p++] - '0') % mod;
		res = M(1, vi(1, d));
	}
	else if(isalpha(in[p])){
		res = val[in[p++]];
	}
	else if(in[p] == '('){
		p++;
		res = expr();
		p++;
	}
	else{
		res = matrix();
	}
	while(p < in.size() && (in[p] == '(' || in[p] == '\'')){
		if(in[p] == '\''){
			p++;
			M tmp(res[0].size(), vi(res.size()));
			rep(i, res.size()) rep(j, res[0].size()) tmp[j][i] = res[i][j];
			swap(tmp, res);
			continue;
		}
		p++;
		M e = expr();
		p++;
		M f = expr();
		p++;
		M tmp(e[0].size(), vi(f[0].size()));
		rep(i, tmp.size()) rep(j, tmp[0].size()) tmp[i][j] = res[e[0][i] - 1][f[0][j] - 1];
		swap(tmp, res);
	}
	return res;
}
M row(){
	M res = expr();
	while(in[p] == ' '){
		p++;
		M r = expr();
		rep(i, r.size()) rep(j, r[0].size()) res[i].pb(r[i][j]);
	}
	return res;
}
M matrix(){
	p++;
	M res = row();
	while(in[p] == ';'){
		p++;
		M r = row();
		rep(i, r.size()) res.pb(r[i]);
	}
	p++;
	return res;
}

int main(){
	int n;
	while(cin >> n, n){
		cin.ignore();
		char v;
		while(n--){
			getline(cin, in);
			v = in[0];
			in = in.substr(2, in.size() - 3);
			p = 0;
			val[v] = expr();
			rep(i, val[v].size()) rep(j, val[v][0].size())
			cout << val[v][i][j] << (j == val[v][0].size()-1?"\n":" ");
		}
		cout << "-----" << endl;
	}
	return 0;
}

Measured memory_bytes: 1556