### constant

MOD = 32768

### classes

class Matrix
  attr_accessor :w, :h, :m

  def initialize() @w = @h = 0; m = [] end
  def initialize(ary)
    @h = ary.length
    @w = (@h > 0) ? ary[0].length : 0
    @m = ary
  end

  def +(mat)
    return nil if @w != mat.w || @h != mat.h
    ary = @h.times.map{[]}
    for y in (0...@h)
      for x in (0...@w)
        ary[y][x] = (@m[y][x] + mat.m[y][x]) % MOD
      end
    end
    Matrix.new(ary)
  end

  def -(mat)
    return nil if @w != mat.w || @h != mat.h
    ary = @h.times.map{[]}
    for y in (0...@h)
      for x in (0...@w)
        ary[y][x] = (@m[y][x] - mat.m[y][x]) % MOD
      end
    end
    Matrix.new(ary)
  end

  def *(mat)
    if @w == 1 && @h == 1
      ary = mat.h.times.map{[]}
      d = @m[0][0]
      for y in (0...mat.h)
        for x in (0...mat.w)
          ary[y][x] = (d * mat.m[y][x]) % MOD
        end
      end
      return Matrix.new(ary)
    end

    if mat.w == 1 && mat.h == 1
      ary = @h.times.map{[]}
      d = mat.m[0][0]
      for y in (0...@h)
        for x in (0...@w)
          ary[y][x] = (@m[y][x] * d) % MOD
        end
      end
      return Matrix.new(ary)
    end

    if @w == mat.h
      ary = @h.times.map{[]}
      for y in (0...@h)
        for x in (0...mat.w)
          d = 0
          for i in (0...@w)
            d = (d + @m[y][i] * mat.m[i][x]) % MOD
          end
          ary[y][x] = d
        end
      end
      return Matrix.new(ary)
    end

    nil
  end

  def -@
    ary = @h.times.map{[]}
    for y in (0...@h)
      for x in (0...@w)
        ary[y][x] = -@m[y][x] % MOD
      end
    end
    Matrix.new(ary)
  end

  def add_col(mat)
    return nil if @h != mat.h
    ary = []
    for y in (0...@h)
      ary[y] = @m[y] + mat.m[y]
    end
    Matrix.new(ary)
  end
  
  def add_row(mat)
    return nil if @w != mat.w
    Matrix.new(@m + mat.m)
  end

  def indexed(idx0, idx1)
    #p ["indexed", @m, idx0.m, idx1.m]
    ary = idx0.w.times.map{[]}
    for y in (0...idx0.w)
      for x in (0...idx1.w)
        ary[y][x] = @m[idx0.m[0][y] - 1][idx1.m[0][x] - 1]
      end
    end
    Matrix.new(ary)
  end
  
  def transpose
    #p ["transpose", @h, @w, @m]
    ary = @w.times.map{[]}
    for y in (0...@h)
      for x in (0...@w)
        ary[x][y] = @m[y][x]
      end
    end
    Matrix.new(ary)
  end
  
  def print
    for y in (0...@h)
      puts @m[y].join(' ')
    end
  end
end

### subroutines

def assign(str)
  #p ["assign", str, $pos, str[$pos]]
  v = var(str)
  $pos += 1
  e0 = expr(str)
  $vars[v] = e0
end

def var(str)
  #p ["var", str, $pos, str[$pos]]
  v = str[$pos]
  $pos += 1
  v
end

def expr(str)
  #p ["expr", str, $pos, str[$pos]]
  e0 = term(str)
  while str[$pos] == '+' || str[$pos] == '-'
    op = str[$pos]
    $pos += 1
    t0 = term(str)
    if op == '+'
      e0 += t0
    else
      e0 -= t0
    end
  end
  #p ["  expr", e0]
  e0
end

def term(str)
  t0 = factor(str)
  while str[$pos] == '*'
    $pos += 1
    f0 = factor(str)
    t0 *= f0
  end
  t0
end

def factor(str)
  if str[$pos] == '-'
    $pos += 1
    return -factor(str)
  end

  primary(str)
end

def primary(str)
  #p ["primary", str, $pos, str[$pos]]
  pr0 = nil

  case str[$pos]
  when '0'..'9'
    pr0 = inum(str)
  when 'A'..'Z'
    pr0 = $vars[var(str)]
  when '['
    pr0 = matrix(str)
  when '('
    $pos += 1
    pr0 = expr(str)
    $pos += 1
  end

  while str[$pos] == '(' || str[$pos] == "'"
    if str[$pos] == '('
      $pos += 1
      idx0 = expr(str)
      $pos += 1
      idx1 = expr(str)
      $pos += 1
      pr0 = pr0.indexed(idx0, idx1)
    else
      $pos += 1
      pr0 = pr0.transpose
    end
  end

  pr0
end

def matrix(str)
  $pos += 1
  mt0 = row_seq(str)
  $pos += 1
  mt0
end

def row_seq(str)
  rs0 = row(str)
  while str[$pos] == ';'
    $pos += 1
    rs0 = rs0.add_row(row(str))
  end
  #p ["  row_seq", rs0]
  rs0
end

def row(str)
  r0 = expr(str)
  while str[$pos] == ' '
    $pos += 1
    r0 = r0.add_col(expr(str))
  end
  #p ["  row", r0]
  r0
end

def inum(str)
  num = 0
  while str[$pos] >= '0' && str[$pos] <= '9'
    num = num * 10 + str[$pos].to_i
    $pos += 1
  end
  Matrix.new([[num]])
end

### main

loop do
  n = gets.to_i
  break if n == 0

  $vars = {}

  n.times do
    $pos = 0
    line = gets.strip
    mtx = assign(line)
    mtx.print
    #p mtx
  end

  puts '-----'
end

Measured memory_bytes: 6612