#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define chmax(a,b) (a<b?(a=b,1):0)
#define chmin(a,b) (a>b?(a=b,1):0)
#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

ll MOD = 1<<15;
void mod(ll &a) {
  a = (a%MOD+MOD)%MOD;
}
template<class T>
struct Matrix : vector<vector<T> > {
  int h,w;
  Matrix(int h, int w) : h(h),w(w){
    assign(h,vector<T>(w));
  }
  Matrix(T a) : h(1),w(1) {
    mod(a);
    assign(1,vector<T>(1,a));
  }
  Matrix() {
  }
  T get(int i, int j) const {
    return (*this)[i][j];
  }
  Matrix operator+(const Matrix &rhs) const {
    assert(h==rhs.h&&w==rhs.w);
    Matrix res(*this);
    REP(i,h)REP(j,w) mod(res[i][j] += rhs[i][j]);
    return res;
  }
  Matrix operator-(const Matrix &rhs) const {
    assert(h==rhs.h&&w==rhs.w);
    Matrix res(*this);
    REP(i,h)REP(j,w) mod(res[i][j] -= rhs[i][j]);
    return res;
  }
  Matrix operator*(const Matrix &rhs) const {
    if (w==1&&h==1) {
      Matrix res(rhs);
      FOR(it,res)FOR(jt,*it) mod(*jt *= get(0,0));
      return res;
    }
    if (rhs.w==1&&rhs.h==1) {
      Matrix res(*this);
      FOR(it,res)FOR(jt,*it) mod(*jt *= rhs[0][0]);
      return res;
    }
    assert(w==rhs.h);
    Matrix res(h,rhs.w);
    REP(i,h) REP(j,rhs.w) REP(k,w)
      mod(res[i][j] += get(i,k)*rhs[k][j]);
    return res;
  }
  Matrix operator-() const {
    return *this * Matrix(-1);
  }  
  Matrix rowjoin(const Matrix &rhs) const {
    assert(w==rhs.w);
    Matrix res(h+rhs.h,w);
    REP(i,h)REP(j,w)res[i][j]=get(i,j);
    REP(i,rhs.h)REP(j,w)res[i+h][j]=rhs[i][j];
    return res;
  }
  Matrix coljoin(const Matrix &rhs) const {
    assert(h==rhs.h);
    Matrix res(h,w+rhs.w);
    REP(i,h)REP(j,w)res[i][j]=get(i,j);
    REP(i,h)REP(j,rhs.w)res[i][w+j]=rhs[i][j];
    return res;
  }
  Matrix index(const Matrix &v1, const Matrix &v2) {
    assert(v1.h==1&&v2.h==1);
    Matrix res(v1.w,v2.w);
    REP(i,v1.w)REP(j,v2.w) res[i][j] = get(v1[0][i]-1,v2[0][j]-1);
    return res;
  }
  Matrix transpose() const {
    Matrix res(w,h);
    REP(i,w) REP(j,h) res[i][j] = get(j,i);
    return res;
  }
};
template<class T>
ostream& operator<<(ostream &os, const Matrix<T> &A) {
  REP(i,A.h) REP(j,A.w) os << A[i][j] << (j==A.w-1?'\n':' ');
  return os;
}

typedef Matrix<ll> mat;

char s[100];
map<char, mat> mp;

mat term(int&);
mat fact(int&);
mat primary(int&);
mat row_seq(int&);
mat row(int&);

mat expr(int &i) {
  mat p = term(i);
  while(s[i]=='+' || s[i]=='-') {
    char c = s[i++];
    if (c == '+') {
      mat a = term(i);
      p = p+a;
    }
    else p = p-term(i);
  }
  return p;
}

mat term(int &i) {
  mat p = fact(i);
  while(s[i]=='*') {
    i++;
    p = p*fact(i);
  }
  return p;
}

mat fact(int &i) {
  if (s[i] == '-') {
    i++;
    mat p = fact(i);
    return -p;
  } else {
    return primary(i);
  }
}

mat primary(int &i) {
  mat p;
  if (isdigit(s[i])) {
    int num = 0;
    while(isdigit(s[i])) {
      num *= 10;
      num += s[i] - '0';
      i++;
    }
    p = mat(num);
  } else if (isalpha(s[i])) {
    char C = s[i++];
    assert(mp.count(C));
    p = mp[C];
  } else if (s[i] == '[') {
    i++;
    p = row_seq(i);
    assert(s[i++]==']');
  } else if (s[i] == '(') {
    i++;
    p = expr(i);
    assert(s[i++]==')');
  } else assert(0);
  while(s[i] == '\'' || s[i] == '(') {
    if (s[i] == '\'') p = p.transpose(), i++;
    else {
      i++;
      mat v1 = expr(i);
      assert(s[i++] == ',');
      mat v2 = expr(i);
      assert(s[i++] == ')');
      p = p.index(v1,v2);
    }
  }
  return p;
}

mat row_seq(int &i) {
  mat p = row(i);
  while(s[i] == ';') {
    i++;
    p = p.rowjoin(row(i));
  }
  return p;
}

mat row(int &i) {
  mat p = expr(i);
  while(s[i] == ' ') {
    i++;
    p = p.coljoin(expr(i));
  }
  return p;
}

int main() {
  int n;
  while(cin>>n,n) {
    mp.clear();
    string t;
    getline(cin,t);
    REP(i,n) {
      getline(cin,t);
      t = t.substr(0,t.size()-1);
      char var = t[0];
      t = t.substr(2);
      strcpy(s,t.c_str());
      int id = 0;
      mat e = expr(id);
      mp[var] = e;
      cout << e;
    }
    puts("-----");
  }
}

Measured memory_bytes: 1744