// Arup Guha
// 9/11/2012
// Solution to 9/9/2012 UCF Individual Contest Problem: Factorial using BigInteger.

import java.util.*;
import java.math.*;

public class b2 {
	
	public static void main(String[] args) {
		
		Scanner stdin = new Scanner(System.in);
		
		// Just seed our table with the first couple factorials.
		BigInteger[] ans = new BigInteger[1001];
		ans[0] = new BigInteger("1");
		ans[1] = ans[0];
		
		// We are going to store the whole value here.
		for (int i=2; i<ans.length; i++) 
			ans[i] = ans[i-1].multiply(new BigInteger(""+i));
		
		// Now, we can output these.
		int numCases = stdin.nextInt();
		for (int i=0; i<numCases; i++) {
			int n = stdin.nextInt();
			System.out.println(find(n, ans));
		}
		
	}
	
	// Returns the least significant digit that isn't 0.
	public static int find(int n, BigInteger[] ans) {
		
		String s = ans[n].toString();
		int index = s.length() - 1;
		
		// Start at the end of the string and go backwards until we hit a non-zero digit.
		while (s.charAt(index) == '0') index--;
		return (int)(s.charAt(index) - '0');
	}
	
}