abstract class Factor{
    abstract val value:Int
    abstract operator fun times(that:R):Quaternion
    abstract operator fun times(that:I):Quaternion
    abstract operator fun times(that:J):Quaternion
    abstract operator fun times(that:K):Quaternion
    override fun toString(): String {
        return "$value"
    }
}
interface Scala<T>{
    operator fun plus(that:T):T
}
class R(override val value:Int):Factor(), Scala<R>{
    override fun times(that: R): Quaternion = R(value * that.value).toQuaternion()
    override fun times(that: I): Quaternion = I(value * that.value).toQuaternion()
    override fun times(that: J): Quaternion = J(value * that.value).toQuaternion()
    override fun times(that: K): Quaternion = K(value * that.value).toQuaternion()
    override fun plus(that: R): R = R(value + that.value)
}
class I(override val value:Int):Factor(), Scala<I>{
    override fun times(that: R): Quaternion = I(value * that.value).toQuaternion()
    override fun times(that: I): Quaternion = R(-value * that.value).toQuaternion()
    override fun times(that: J): Quaternion = K(value * that.value).toQuaternion()
    override fun times(that: K): Quaternion = J(-value * that.value).toQuaternion()
    override fun plus(that: I): I = I(value + that.value)

}
class J(override val value:Int):Factor(), Scala<J>{
    override fun times(that: R): Quaternion = J(value * that.value).toQuaternion()
    override fun times(that: I): Quaternion = K(-value * that.value).toQuaternion()
    override fun times(that: J): Quaternion = R(-value * that.value).toQuaternion()
    override fun times(that: K): Quaternion = I(value * that.value).toQuaternion()
    override fun plus(that: J): J = J(value + that.value)
}
class K(override val value:Int):Factor(), Scala<K>{
    override fun times(that: R): Quaternion = K(value * that.value).toQuaternion()
    override fun times(that: I): Quaternion = J(value * that.value).toQuaternion()
    override fun times(that: J): Quaternion = I(-value * that.value).toQuaternion()
    override fun times(that: K): Quaternion = R(-value * that.value).toQuaternion()
    override fun plus(that: K): K = K(value + that.value)

}
fun R.toQuaternion():Quaternion = Quaternion.zero.copy(r = this)
fun I.toQuaternion():Quaternion = Quaternion.zero.copy(i = this)
fun J.toQuaternion():Quaternion = Quaternion.zero.copy(j = this)
fun K.toQuaternion():Quaternion = Quaternion.zero.copy(k = this)
data class Quaternion(val r:R, val i:I, val j:J, val k:K){
    operator fun plus(that: Quaternion):Quaternion = Quaternion(r + that.r, i + that.i, j + that.j, k + that.k)
    operator fun times(that:Quaternion):Quaternion {
        val (a, b, c, d) = that
        return allFactor().map{it * a + it * b + it * c + it * d}.fold(zero, Quaternion::plus)
    }
    private fun allFactor():Iterable<Factor> = listOf(r, i, j, k)
    companion object {
        val zero:Quaternion = Quaternion(R(0), I(0), J(0), K(0))
        operator fun invoke(r:Int, i:Int, j:Int, k:Int):Quaternion = Quaternion(R(r), I(i), J(j), K(k))
    }

    override fun toString(): String {
        return "$r $i $j $k"
    }
}
fun main(args:Array<String>):Unit {
    var n = readLine()!!.toInt()
    while (n != 0){
        for (i in 0 until n){
            val arg =  readLine()!!.split(' ').map(String::toInt)
            val (a, b, c, d) = arg.take(4)
            val (e, f, g, h) = arg.takeLast(4)
            println(Quaternion(a, b, c, d) * Quaternion(e, f, g, h))
        }
        n = readLine()!!.toInt()
    }
}


Measured memory_bytes: 32456