My solution uses just bc and bash builtins, and is extremely simple:
!/bin/bash
read numtests;
for ((i=1; i <= $numtests; i++)); do
read n
echo "define f (x) { if (x <= 1) return (1); return (f(x-1) * x); } f($n)" | bc
done
It works for any input I've given it (although, obviously, due to multiple invocations of bc it's not particularly fast... I have a faster one also working).
I rewrote it to use a different style for loop (b/c my SBANK uses this style and also inexplicably gives WA):
for i in seq $numtests
; do
read n
echo "define f (x) { if (x <= 1) return (1); return (f(x-1) * x); } f($n)" | bc
done
But it still doesn't work.
Does bc no longer exist in the bash chroot or something?