Factorial

Time limit1sMemory limit128 MB

Summary
For each n up to 1000, report the last non-zero digit of n!.
Level

Medium4 of 10

Topics
Math, Number theory, Implementation
Solved
No attempts yet

Problem

n!n! denotes the factorial of an integer nn, the product of all integers from 11 to nn. Factorials grow extremely quickly: 13!13! exceeds the range of a 32-bit integer on most computers, and 70!70! exceeds the range of most floating-point variables. We want to find the rightmost non-zero digit of n!n!. For example, 5!=1×2×3×4×5=1205! = 1 \times 2 \times 3 \times 4 \times 5 = 120, so the rightmost non-zero digit of 5!5! is 22. Likewise, 7!=1×2×3×4×5×6×7=50407! = 1 \times 2 \times 3 \times 4 \times 5 \times 6 \times 7 = 5040, so the rightmost non-zero digit of 7!7! is 44.

Input

The first line contains the number of test cases tt (0<t<150 < t < 15). Each of the following tt lines contains a single integer nn (0<n<10010 < n < 1001).

Output

For each test case, print the rightmost non-zero digit of n!n! on its own line.

Examples1

  1. Example 1

    Input
    1
    5
    
    Expected output
    2