Read pairs of matrices with dimension lines, multiply them when the inner dimensions match, and print each result row wrapped in vertical bars, or print undefined.
Matrix multiplication is a basic operation of linear algebra, with applications across mathematics, applied mathematics, computer graphics, physics, and engineering.
Two matrices can be multiplied only when their dimensions are compatible: the number of columns of the first matrix must equal the number of rows of the second matrix.
If A=[aij] is an m×n matrix and B=[bij] is an n×q matrix, the product AB is an m×q matrix. The product AB is defined as the m×q matrix C=[cij] with
cij=∑k=1naikbkj
Build a matrix multiplication calculator that multiplies two given matrices and prints the result. If the matrices cannot be multiplied, print undefined.
Input
The input consists of several test cases. For each test case, the first line gives four positive integers M,N,P,Q (1≤M,N,P,Q≤20). M and N are the dimensions of matrix A, while P and Q are the dimensions of matrix B. The next M lines hold matrix A, followed by P lines holding matrix B. Each row lists its entries separated by single spaces. The input ends with a line 0 0 0 0, which is not processed.
Output
For each test case, print a line Case #x:, where x is the case number starting from 1. On the following lines, print the result of the multiplication. When the product exists, print one row of the result matrix per line in the form | entries |, wrapped in | characters with entries separated by single spaces. When the matrices cannot be multiplied, print a single line undefined.