d=->n{n<10?n: d[n/10+n%10]} f=->n,k=1{(k..n).reduce(:*)||1} gets.to_i.times{l=gets.size-1;p d[l.times.reduce(0){|m,i|m+$_[i].hex*f[l-1,l-i]/f[i]}]}