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