Two friends, A and B, are playing the game of matchsticks. In this game, a group of N matchsticks is placed on the table. The players can pick any number of matchsticks from 1 to 4 (both inclusive) during their chance. The player who takes the last match stick wins the game. If A starts first, how many matchsticks should he pick on his 1st turn such that he is guaranteed to win the game or determine if it's impossible for him to win. Return -1 if it's impossible for A to win the game, else return the number of matchsticks should he pick on his 1st turn such that he is guaranteed to win.
Note : Consider both A and B play the game optimally.
Example 1:
Input:
N = 48
Output:
3
Explanation:
Player A is guaranteed a win if he
picks 3 matchsticks first.
Example 2:
Input:
N = 15
Output:
-1
Explanation:
Player A is guaranteed a loss no matter
how many matches he picks at first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchGame() which takes an Integer N as input and returns the answer.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{18}

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: Mathematics

Starter code: #User function Template for python3



class Solution:

    def matchGame(self, N):

         # code here

Solutions:
class Solution:

	def matchGame(self, n):
		if n % 5 == 0:
			return -1
		else:
			return n % 5


class Solution:

	def matchGame(self, N):
		if N % 5 == 0:
			return -1
		else:
			return N % 5


class Solution:

	def matchGame(self, n):
		if n % 5:
			return n % 5
		return -1


class Solution:

	def matchGame(self, n):
		if n == 0 or n % 5 == 0:
			return -1
		return n % 5


class Solution:

	def matchGame(self, N):
		if N % 5:
			return N % 5
		return -1


class Solution:

	def matchGame(self, N):
		if N % 5 == 0:
			return -1
		elif N % 5 == 1:
			return 1
		elif N % 5 == 2:
			return 2
		elif N % 5 == 3:
			return 3
		else:
			return 4


class Solution:

	def matchGame(self, N):
		if N < 5:
			return N
		elif N % 5 == 0:
			return -1
		return N % 5


class Solution:

	def matchGame(self, N):
		if N % 5 == 0:
			return -1
		return int(N % 5)


class Solution:

	def matchGame(self, N):
		matchstick = N % 5
		return matchstick if matchstick else -1


class Solution:

	def matchGame(self, N):
		for i in range(1, 5):
			if (N - i) % 5 == 0:
				return i
		return -1


class Solution:

	def matchGame(self, N):
		if N % 10 == 0 or N % 10 == 5:
			return -1
		if N % 10 <= 4:
			return N % 10
		if N % 10 <= 5:
			return -1
		if N % 10 <= 6:
			return 1
		if N % 10 <= 7:
			return 2
		if N % 10 <= 8:
			return 3
		if N % 10 <= 9:
			return 4
		return -1


class Solution:

	def matchGame(self, N):
		if N % (4 + 1) == 0:
			return -1
		return N - N // 5 * 5


class Solution:

	def matchGame(self, N):
		if N % 5 == 0:
			return -1
		else:
			return N % 5
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.matchGame(N))