require 'matrix'

class String
	def replace_select
		return nil unless i = self =~ /,/	
		l = 0
		j = i
		until l == 1
			j -= 1
			l += 1 if self[j] == ?(
			l -= 1 if self[j] == ?)
		end
		r = 0
		k = i
		until l == 1
			k += 1
			r += 1 if self[j] == ?)
			r -= 1 if self[j] == ?(
		end
		self[0..j-1] + '.select(' + self[j+1..i-1] + ?@ + self[i+1..-1]	
	end
end

MOD = 2 ** 15

class Matrix
	def &(other)
		Matrix[*self.to_a.zip(other.to_a).map(&:flatten)]
	end
	def |(other)
		Matrix[*(self.to_a + other.to_a)]
	end
	def -@
		(-1 * self).map {|x| x % MOD}
	end
	def select(rows, cols)
		a =	rows.to_a[0].map {|i|
			cols.to_a[0].map {|j|
				self[i-1, j-1]
			}
		}
		Matrix[*a]
	end
	alias :orig_mul :*
	def *(other)
		if row_size * column_size == 1
			(self[0, 0] * other).map {|x| x % MOD}
		elsif other.row_size * other.column_size == 1
			(other[0, 0] * self).map {|x| x % MOD}
		else
			send(:orig_mul, other).map {|x| x % MOD}
		end
	end
	alias :orig_add :+
	def +(other)
		send(:orig_add, other).map {|x| x % MOD}
	end
	alias :orig_sub :-
	def -(other)
		send(:orig_sub, other).map {|x| x % MOD}
	end
	def to_s
		to_a.map{|row| row * ' '}*"\n"
	end
end

loop do
	dataset = gets.to_i
	break if dataset == 0
	a=b=c=d=e=f=g=h=i=j=k=l=m=n=o=p=q=r=s=t=u=v=w=x=y=z=nil
	dataset.times do
		exp = gets.chomp.chop
		exp.gsub!('--', '')
		exp.gsub!("''", '')
		exp.gsub!(?', '.transpose')
		while rep = exp.replace_select
			exp = rep
		end
		exp.gsub!(?@, ?,)
		exp.gsub!(' ', ?&)
		exp.gsub!(';', ?|)
		exp.gsub!(?[, ?()
		exp.gsub!(?], ?))
		exp.downcase!
		exp.gsub!(/(\d+)/, 'Matrix[[\1]]')
		puts eval(exp)
	end
	puts '-----' 
end