#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <cctype>
using namespace std;

const int mask = 32767;


struct matrix{
	int a, b;	//a*b
	vector<vector<int> > data;

	matrix( int a_, int b_ )
		: a(a_), b(b_), data( a_, vector<int>(b_) ) {}
	matrix()
		: a(0), b(0) {}
	matrix( const vector<vector<int> > &d )
		: a(d.size()), b(d[0].size()), data(d) {}
	
	matrix operator +( const matrix &m ) const{
		matrix ret( a, b );
		
		for( int y = 0; y < a; ++y )
			for( int x = 0; x < b; ++x )
				ret.data[y][x] = (this->data[y][x] + m.data[y][x]) & mask;
		return ret;
	}
	
	matrix &operator +=( const matrix &m ){
		for( int y = 0; y < a; ++y )
			for( int x = 0; x < b; ++x )
				this->data[y][x] = (this->data[y][x] + m.data[y][x]) & mask;
		return *this;
	}

	matrix operator -( const matrix &m ) const{
		matrix ret( a, b );
		
		for( int y = 0; y < a; ++y )
			for( int x = 0; x < b; ++x )
				ret.data[y][x] = (this->data[y][x] - m.data[y][x]) & mask;
		return ret;
	}
	
	matrix &operator -=( const matrix &m ){
		for( int y = 0; y < a; ++y )
			for( int x = 0; x < b; ++x )
				this->data[y][x] = (this->data[y][x] - m.data[y][x]) & mask;
		return *this;
	}

	matrix operator -() const{
		matrix ret( a, b );

		for( int y = 0; y < a; ++y )
			for( int x = 0; x < b; ++x )
				ret.data[y][x] = -this->data[y][x] & mask;
		return ret;
	}
	
	matrix operator *( const matrix &m ) const{
		if( a == 1 && b == 1 ){
			matrix ret( m.a, m.b );
			for( int y = 0; y < m.a; ++y )
				for( int x = 0; x < m.b; ++x )
					ret.data[y][x] = (m.data[y][x] * this->data[0][0]) & mask;
			return ret;
		}
		if( m.a == 1 && m.b == 1 ){
			matrix ret( a, b );
			for( int y = 0; y < a; ++y )
				for( int x = 0; x < b; ++x )
					ret.data[y][x] = (this->data[y][x] * m.data[0][0]) & mask;
			return ret;
		}

		matrix ret( a, m.b );
		for( int i = 0; i < a; ++i ){
			for( int k = 0; k < m.b; ++k ){
				for( int j = 0; j < b; ++j ){
					ret.data[i][k] = (ret.data[i][k] + this->data[i][j] * m.data[j][k]) & mask;
				}
			}
		}
		return ret;
	}
	
	matrix trans() const{
		matrix ret( b, a );

		for( int y = 0; y < a; ++y )
			for( int x = 0; x < b; ++x )
				ret.data[x][y] = this->data[y][x];
		return ret;
	}

	matrix indexed( const matrix &m1, const matrix &m2 ) const{
		matrix ret( m1.b, m2.b );

		for( int y = 0; y < m1.b; ++y ){
			for( int x = 0; x < m2.b; ++x ){
				ret.data[y][x] =
					this->data	[ m1.data[0][y] - 1 ]
								[ m2.data[0][x] - 1 ];	//1-indexed
			}
		}
		return ret;
	}
	
	void appenda( const matrix &m ){
		this->data.insert( this->data.end(), m.data.begin(), m.data.end() );
		this->a += m.a;
	}
	
	void appendb( const matrix &m ){
		for( int i = 0; i < this->a; ++i ){
			this->data[i].insert( this->data[i].end(), m.data[i].begin(), m.data[i].end() );
		}
		
		this->b += m.b;
	}
};


matrix vars[26];


matrix assignment();
matrix expr();
matrix term();
matrix factor();
matrix primary();
matrix extra_primary();
matrix row_seq();
matrix row();



matrix assignment(){
	char v;
	cin >> v;
	cin.ignore();	//=

	vars[v - 'A'] = expr();
	cin.ignore();	//.

	return vars[v - 'A'];
}

matrix expr(){
	matrix t = term();
	while(true){
		switch( cin.peek() ){
			case '+':
				cin.ignore();
				t += term();
				break;
			case '-':
				cin.ignore();
				t -= term();
				break;
			default:
				return t;
		}
	}
}

matrix term(){
	matrix f = factor();
	while( cin.peek() == '*' ){
		cin.ignore();
		f = f * factor();
	}
	return f;
}

matrix factor(){
	if( cin.peek() == '-' ){
		cin.ignore();
		return -factor();
	}
	return extra_primary();
}

matrix primary(){
	char peek = cin.peek();

	if( peek == '-' ){
		cin.ignore();
		return -factor();
	}

	if( isdigit( peek ) ){
		matrix ret( 1, 1 );
		cin >> ret.data[0][0];
		return ret;
	}

	if( isalpha( peek ) ){
		cin.ignore();
		return vars[peek - 'A'];
	}

	if( peek == '[' ){
		cin.ignore();
		matrix ret = row_seq();
		cin.ignore();
		return ret;
	}
	
	if( peek == '(' ){
		cin.ignore();
		matrix ret = expr();
		cin.ignore();
		return ret;
	}

	matrix pr = primary();
	cin >> peek;
	if( peek == '\'' ){
		return pr.trans();
	}
	else{
		matrix m1 = expr();
		cin.ignore();	//,
		matrix m2 = expr();
		cin.ignore();	//)

		return pr.indexed( m1, m2 );
	}
}

matrix extra_primary(){
	matrix pr = primary();
	
	while(true){
		char peek = cin.peek();
		if( peek == '\'' ){
			cin.ignore();
			pr = pr.trans();
		}
		else if( peek == '(' ){
			cin.ignore();	//(
			matrix m1 = expr();
			cin.ignore();	//,
			matrix m2 = expr();
			cin.ignore();	//)
			
			pr = pr.indexed( m1, m2 );
		}
		else{
			return pr;
		}
	}
}

matrix row_seq(){
	matrix r = row();
	while( cin.peek() == ';' ){
		cin.ignore();
		r.appenda( row() );
	}

	return r;
}

matrix row(){
	matrix ret = expr();
	while( cin.peek() == ' ' ){
		cin.ignore();
		ret.appendb( expr() );
	}
	return ret;
}


int main(){
	int n;
	while( cin >> n, n != 0 ){
		for( int i = 0; i < n; ++i ){
			matrix res = assignment();

			for( int y = 0; y < res.a; ++y ){
				for( int x = 0; x < res.b; ++x ){
					cout << (res.data[y][x] & mask);
					if( x == res.b - 1 ){
						cout << '\n';
					}
					else{
						cout << ' ';
					}
				}
			}
		}
		cout << "-----\n";
	}
}

Measured memory_bytes: 1264