You are given two integer numbers, the base a and the index b. You have to find the last digit of a^{b}.
 
Example 1:
Input:
a = "3", b = "10"
Output:
9
Explanation:
3^{10} = 59049. Last digit is 9.
Example 2:
Input:
a = "6", b = "2"
Output:
6
Explanation:
6^{2} = 36. Last digit is 6.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function getLastDigit() which takes two strings a,b as parameters and returns an integer denoting the last digit of a^{b}.
 
Expected Time Complexity: O(|b|)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= |a|,|b| <= 1000
Note:|a| = length of a and |b| = length of b. There will not be any test cases such that a^{b }is undefined.

Difficulty: EASY
Tags: Mathematics
Expected Time Complexity: O(|b|)
Expected Auxiliary Space: O(1)

Starter code:
#User function Template for python3

class Solution:
    def getLastDigit(self, a, b):
        # code here 

Solutions:
class Solution:

	def getLastDigit(self, a, b):
		a = int(a)
		b = int(b)
		if a == 0:
			return 0
		if b == 0:
			return 1
		if b == 1:
			return a % 10
		d = {}
		d[0] = 0
		d[1] = 1
		d[2] = [6, 2, 4, 8]
		d[3] = [1, 3, 9, 7]
		d[4] = [6, 4]
		d[5] = 5
		d[6] = 6
		d[7] = [1, 7, 9, 3]
		d[8] = [6, 8, 4, 2]
		d[9] = [1, 9]
		x = a % 10
		if x == 0 or x == 1 or x == 5 or (x == 6):
			return d[x]
		if x == 4 or x == 9:
			y = b % 2
			return d[x][y]
		if x == 2 or x == 3 or x == 7 or (x == 8):
			y = b % 4
			return d[x][y]


class Solution:

	def getLastDigit(self, a, b):
		a = int(a[-1])
		b = int(b)
		if a == 0:
			return 0
		elif b == 0:
			return 1
		elif b == 1:
			return a
		elif b % 4 == 0:
			return a ** 4 % 10
		else:
			return a ** (b % 4) % 10


class Solution:

	def getLastDigit(self, a, b):
		a = int(a)
		b = int(b)
		if a == 0 and b == 0:
			return 1
		if b == 0:
			return 1
		if a == 0:
			return 0
		if b % 4 == 0:
			res = 4
		else:
			res = b % 4
		num = pow(a, res)
		return num % 10


class Solution:

	def getLastDigit(self, a, b):
		(a, b) = (int(a), int(b))
		if b == 0:
			return 1
		else:
			d = {0: [0], 1: [1], 2: [2, 4, 8, 6], 3: [3, 9, 7, 1], 4: [4, 6], 5: [5], 6: [6], 7: [7, 9, 3, 1], 8: [8, 4, 2, 6], 9: [9, 1]}
			l = a % 10
			t = len(d[l])
			if b % t == 0:
				return d[l][t - 1]
			return d[l][b % t - 1]


class Solution:

	def getLastDigit(self, a, b):
		if b == '0':
			return 1
		a = int(a[-1])
		b = int(b[-2:])
		if a in (0, 1, 5, 6):
			return a
		if a not in (4, 9):
			if b & 1:
				a = 10 - a
			else:
				a = 4 if a in (2, 8) else 9
			b >>= 1
		if b & 1:
			return a
		else:
			return 10 - a


class Solution:

	def getLastDigit(self, x, y):
		x = int(x)
		y = int(y)
		if y == 0:
			return 1
		if y == 1:
			return x
		r = []
		y = str(y)
		y = y[::-1]
		y = int(y)
		temp = 0
		while y > 0:
			rem = y % 10
			temp = (temp * 10 + rem) % 4
			y //= 10
		last = x % 10
		if temp == 0:
			r.append(pow(last, 4) % 10)
		else:
			r.append(pow(last, temp) % 10)
		return r[-1]


class Solution:

	def getLastDigit(self, a, b):
		dict = {'1': [1], '2': [2, 4, 8, 6], '3': [3, 9, 7, 1], '4': [4, 6], '5': [5], '6': [6], '7': [7, 9, 3, 1], '8': [8, 4, 2, 6], '9': [9, 1]}
		last_dg = a[len(a) - 1]
		if b == '0':
			return 1
		if a == '0':
			return 0
		arr = dict[last_dg]
		l = len(arr)
		power = int(b) % l
		return arr[power - 1]


import math

class Solution:

	def getLastDigit(self, a, b):
		if b != '0':
			if a[-1] == '5':
				return 5
			elif a[-1] == '6':
				return 6
			elif a[-1] == '0':
				return 0
			elif a[-1] == '1':
				return 1
			else:
				b = int(b)
				a = int(a)
				if b % 4 == 0 and a % 2 == 0:
					return 6
				else:
					return a ** (b % 4) % 10
		else:
			return 1


class Solution:

	def getLastDigit(self, a, b):
		if b == '0':
			return 1
		n = int(a[-1])
		ar = [-1]
		ans = 1
		for i in range(4):
			ar.append(ans * n % 10)
			ans *= n
		ar[0] = ar[-1]
		ar.pop()
		return ar[int(b[-2:]) % 4]


class Solution:

	def getLastDigit(self, a, b):
		if b == '0':
			return 1
		if a[-1] == '1' or a[-1] == '0' or a[-1] == '6' or (a[-1] == '5'):
			return a[-1]
		b = int(b)
		s = {'2': [6, 2, 4, 8], '7': [1, 7, 9, 3], '8': [6, 8, 4, 2], '3': [1, 3, 9, 7]}
		if a[-1] in s:
			return s[a[-1]][b % 4]
		m = {'9': [1, 9], '4': [6, 4]}
		return m[a[-1]][b % 2]


import math

class Solution:

	def getLastDigit(self, a, b):
		if b == '0':
			return 1
		a = int(a[-1])
		b = int(b[-2:])
		if a in (0, 1, 5, 6):
			return a
		if a not in (4, 9):
			if b & 1:
				a = 10 - a
			else:
				a = 4 if a in (2, 8) else 9
			b >>= 1
		return a if b & 1 else 10 - a
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(a, b) = map(str, input().split())
		ob = Solution()
		print(ob.getLastDigit(a, b))


class Solution:

	def getLastDigit(self, a, b):
		a = int(a)
		b = int(b)
		if a == 0 and b == 0:
			return 1
		if a == 0:
			return 0
		if b == 0:
			return 1
		if b % 4 == 0:
			res = 4
		else:
			res = b % 4
		num = pow(a, res)
		return num % 10
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(a, b) = map(str, input().split())
		ob = Solution()
		print(ob.getLastDigit(a, b))


class Solution:

	def getLastDigit(self, a, b):
		p = int(a[-1])
		if len(b) >= 2:
			c = int(b[-2:])
		else:
			c = int(b)
		if c == 0:
			return 1
		if p == 0:
			return 0
		elif p == 1:
			return 1
		elif p == 2:
			if c % 4 == 0:
				return 6
			elif c % 4 == 1:
				return 2
			elif c % 4 == 2:
				return 4
			elif c % 4 == 3:
				return 8
		elif p == 3:
			if c % 4 == 0:
				return 1
			elif c % 4 == 1:
				return 3
			elif c % 4 == 2:
				return 9
			elif c % 4 == 3:
				return 7
		elif p == 4:
			if c % 2 == 0:
				return 6
			elif c % 2 == 1:
				return 4
		elif p == 5:
			return 5
		elif p == 6:
			return 6
		elif p == 7:
			if c % 4 == 0:
				return 1
			elif c % 4 == 1:
				return 7
			elif c % 4 == 2:
				return 9
			elif c % 4 == 3:
				return 3
		elif p == 8:
			if c % 4 == 0:
				return 6
			elif c % 4 == 1:
				return 8
			elif c % 4 == 2:
				return 4
			elif c % 4 == 3:
				return 2
		elif p == 9:
			if c % 2 == 0:
				return 1
			elif c % 2 == 1:
				return 9
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(a, b) = map(str, input().split())
		ob = Solution()
		print(ob.getLastDigit(a, b))


class Solution:

	def getLastDigit(self, a, b):
		a = int(a)
		b = int(b)
		table = {0: 1, 1: 1, 2: 4, 3: 4, 4: 2, 5: 1, 6: 1, 7: 4, 8: 4, 9: 2}
		if b == 0:
			return 1
		else:
			dig = a % 10
			rem = b % table[dig]
			if rem == 0:
				ans = dig ** table[dig]
			else:
				ans = dig ** rem
			ans = ans % 10
			return ans


class Solution:

	def getLastDigit(self, a, b):
		if b == '0':
			return 1
		if a[-1] == '5' or a[-1] == '6' or a[-1] == '0' or (a[-1] == '1'):
			return int(a[-1])
		if a[-1] == '2':
			if int(b) % 4 == 1:
				return 2
			elif int(b) % 4 == 2:
				return 4
			elif int(b) % 4 == 3:
				return 8
			elif int(b) % 4 == 0:
				return 6
		if a[-1] == '3':
			if int(b) % 4 == 1:
				return 3
			elif int(b) % 4 == 2:
				return 9
			elif int(b) % 4 == 3:
				return 7
			elif int(b) % 4 == 0:
				return 1
		if a[-1] == '4':
			if int(b) % 2 == 1:
				return 4
			elif int(b) % 2 == 0:
				return 6
		if a[-1] == '7':
			if int(b) % 4 == 1:
				return 7
			elif int(b) % 4 == 2:
				return 9
			elif int(b) % 4 == 3:
				return 3
			elif int(b) % 4 == 0:
				return 1
		if a[-1] == '8':
			if int(b) % 4 == 1:
				return 8
			elif int(b) % 4 == 2:
				return 4
			elif int(b) % 4 == 3:
				return 2
			elif int(b) % 4 == 0:
				return 6
		if a[-1] == '9':
			if int(b) % 2 == 1:
				return 9
			elif int(b) % 2 == 0:
				return 1


class Solution:

	def getLastDigit(self, a, b):
		a = int(a)
		b = int(b)
		if b == 0:
			return 1
		a = a % 10
		arr = []
		arr.append(a % 10)
		x = -1
		i = 2
		while arr[0] != x:
			val = arr[-1] * a
			arr.append(val % 10)
			x = arr[-1]
			i += 1
		arr.pop()
		n = b % len(arr)
		return arr[n - 1]


class Solution:

	def getLastDigit(self, a, b):
		if a == '0':
			return 0
		if b == '0':
			return 1
		if a[-1] == '0':
			return 0
		if int(a[-1]) % 2 == 0:
			if int(b) % 4 == 0:
				return 6
			else:
				rem = int(b) % 4
				val = 6 * int(a[-1]) ** rem
				return val % 10
		else:
			if a[-1] == '5' or a[-1] == '1' or a[-1] == '6':
				return a[-1]
			if int(b) % 4 == 0:
				return 1
			else:
				rem = int(b) % 4
				val = int(a[-1]) ** rem
				return val % 10


class Solution:

	def getLastDigit(self, a, b):
		a = int(a)
		b = int(b)
		return pow(a, b, 10)


class Solution:

	def getLastDigit(self, a, b):
		arr = []
		if int(b) == 0:
			return 1
		a = a[len(a) - 1]
		a = int(a)
		a4 = str(a * a * a * a)
		a4 = a4[len(a4) - 1]
		arr.append(a4)
		a = int(a)
		arr.append(a)
		a2 = str(a * a)
		a2 = a2[len(a2) - 1]
		arr.append(a2)
		a3 = str(a * a * a)
		a3 = a3[len(a3) - 1]
		arr.append(a3)
		if int(b) <= 4:
			return arr[int(b)]
		return arr[int(b) % 4]


class Solution:

	def getLastDigit(self, a, b):
		a = int(a) % 10
		b = int(b)
		if b == 0:
			return 1
		cycle = [a % 10]
		while True:
			nxt = cycle[-1] * a % 10
			if nxt == cycle[0]:
				break
			cycle.append(nxt)
		return cycle[(b - 1) % len(cycle)]


class Solution:

	def findNo(self, a):
		nums = []
		i = 1
		while True:
			temp = a ** i
			if temp % 10 in nums:
				return nums
			nums.append(temp % 10)
			i += 1

	def getLastDigit(self, a, b):
		if b == '0':
			return 1
		num1L = list(a)
		num1 = int(num1L[-1])
		num2 = int(b)
		nums = self.findNo(num1 % 10)
		len1 = len(nums)
		index = num2 % len1 - 1
		return nums[index]


class Solution:

	def getLastDigit(self, a, b):
		l = [5, 6, 1, 0]
		m = [2, 3, 7, 8]
		n = [4, 9]
		if int(b) == 0:
			return 1
		k = str(a[-1])
		k = int(k)
		if k in l:
			return k
		elif k in m:
			r = int(b) % 4
			if r == 0:
				k = str(k ** 4)
				k = int(k[-1])
				return k
			else:
				k = str(k ** r)
				k = int(k[-1])
				return k
		elif k in n:
			r = int(b) % 2
			if r == 0:
				k = str(k ** 2)
				k = int(k[-1])
				return k
			else:
				k = str(k ** r)
				k = int(k[-1])
				return k


class Solution:

	def getLastDigit(self, a, b):
		if int(b) == 0:
			return 1
		l = []
		x = 1
		for i in range(10):
			x *= int(a)
			if x % 10 not in l:
				l.append(x % 10)
			else:
				break
		l = [l[-1]] + l[:-1]
		return l[int(b) % len(l)]


import math

class Solution:

	def getLastDigit(self, a, b):
		a = int(a)
		b = int(b)
		return pow(a, b, 10)


class Solution:

	def getLastDigit(self, a, b):
		x = []
		i = 1
		(a, b) = (int(a), int(b))
		if b == 0:
			return 1
		while 1:
			if a ** i % 10 not in x:
				x.append(a ** i % 10)
			else:
				break
			i += 1
		y = b % len(x)
		if y == 0:
			return x[-1]
		else:
			return x[y - 1]


class Solution:

	def getLastDigit(self, a, b):
		if b == '0':
			return 1
		z = str(int(b) % 4)
		if a[-1] == '1':
			return 1
		elif a[-1] == '2':
			if z == '2':
				return 4
			elif z == '3':
				return 8
			elif z == '0':
				return '6'
			elif z == '1':
				return '2'
		elif a[-1] == '3':
			if z == '2':
				return 9
			elif z == '3':
				return 7
			elif z == '0':
				return 1
			elif z == '1':
				return '3'
		elif a[-1] == '4':
			x = str(int(b) % 2)
			if x == '0':
				return 6
			elif x == '1':
				return 4
		elif a[-1] == '5':
			return 5
		elif a[-1] == '6':
			return 6
		elif a[-1] == '7':
			if z == '1':
				return 7
			elif z == '2':
				return 9
			elif z == '3':
				return 3
			elif z == '0':
				return 1
		elif a[-1] == '8':
			if z == '2':
				return 4
			elif z == '3':
				return 2
			elif z == '0':
				return 6
			elif z == '1':
				return 8
		elif a[-1] == '9':
			x = str(int(b) % 2)
			if x == '0':
				return 1
			elif x == '1':
				return 9
		elif a[-1] == '0':
			return 0


class Solution:

	def getLastDigit(self, a, b):
		ain = int(a[-1])
		if b == '0':
			return 1
		bin = int(b[-2:]) % 4
		if bin == 0:
			bin = 4
		return ain ** bin % 10


class Solution:

	def getLastDigit(self, a, b):
		if len(b) == 1 and b[0] == '0':
			return 1
		if len(a) == 1 and a[0] == '0':
			return 0
		a = int(a[-1])
		m = 0
		for i in b:
			I = int(i)
			m = (m * 10 + I) % 4
		if m == 0:
			m = 4
		ans = pow(a, m, 10)
		return ans


class Solution:

	def getLastDigit(self, a, b):
		a = int(a[-1])
		b = int(b)
		ans = pow(a, b, 10)
		return ans


class Solution:

	def getLastDigit(self, a, b):
		if int(b) == 0:
			answer = 1
			return answer
		else:
			remainder = int(b) % 4
			if remainder == 0:
				string = str(pow(int(a), 4))
				return string[-1]
			else:
				string = str(pow(int(a), remainder))
				return string[-1]


import math

class Solution:

	def getLastDigit(self, a, b):
		dict = {}
		a = int(a)
		b = int(b)
		if b == 0:
			return 1
		if b % 4 == 0:
			return a * a * a * a % 10
		else:
			k = b % 4
			t = 1
			for i in range(k):
				t *= a
			return t % 10


class Solution:

	def getLastDigit(self, a, b):
		if not int(b):
			return 1
		cyclicity = {0: [0], 1: [1], 2: [2, 4, 8, 6], 3: [3, 9, 7, 1], 4: [4, 6], 5: [5], 6: [6], 7: [7, 9, 3, 1], 8: [8, 4, 2, 6], 9: [9, 1]}
		a = int(a) % 10
		arr = cyclicity[a]
		n = len(arr)
		b = int(b) % n
		if not b:
			b = n
		b -= 1
		return arr[b]


class Solution:

	def getLastDigit(self, a, b):
		if int(b) == 0:
			return 1
		t = int(a[-1])
		if t == 1:
			return 1
		elif t == 4 or t == 9:
			if int(b) % 2 == 0:
				b = 2
			else:
				b = int(b) % 2
		elif t == 5 or t == 6:
			return t
		elif int(b) % 4 == 0:
			b = 4
		else:
			b = int(b) % 4
		ans = int(t) ** int(b)
		ans = ans % 10
		return ans


class Solution:

	def getLastDigit(self, a, b):
		a = int(a)
		b = int(b)
		r = 1
		while b > 0:
			if b & 1 == 1:
				r = r * a % 10
			a = a * a % 10
			b >>= 1
		return r


class Solution:

	def getLastDigit(self, a, b):
		a = int(a)
		b = int(b)
		res = 1
		base = a
		while b > 1:
			if b % 2 == 0:
				b = b // 2
				base = base ** 2 % 10
			else:
				b -= 1
				res = res * base % 10
		return res


class Solution:

	def findCompliment(self, a):
		res = a
		temp = a * a % 10
		count = 1
		while res != temp:
			temp = temp * a % 10
			count += 1
		return count

	def getLastDigit(self, a, b):
		a = int(a[len(a) - 1])
		b = int(b)
		if b == 0:
			return 1
		compliment = self.findCompliment(a)
		if b % compliment == 0:
			return a ** compliment % 10
		return a ** (b % compliment) % 10


class Solution:

	def getLastDigit(self, a, b):
		if int(b) == 0:
			return 1
		a = int(a) % 10
		k = a
		t = 0
		visited = {i: False for i in range(10)}
		d = [0 for i in range(10)]
		while visited[k] == False and t <= int(b):
			visited[k] = True
			t += 1
			d[t] = k
			k = k * a % 10
			if visited[k]:
				break
		r = int(b) % t
		if r == 0:
			return d[t]
		return d[r]


import math

class Solution:

	def getLastDigit(self, a, b):
		a = int(a)
		b = int(b)
		base = a % 10
		if b > 100:
			exponent = b % 100
		else:
			exponent = b
		if exponent % 4 == 0 and exponent != 0:
			exponent = 4
		else:
			exponent = exponent % 4
		result = math.pow(base, exponent) % 10
		return int(result)


class Solution:

	def getLastDigit(self, a, b):
		a = int(a)
		b = int(b)
		if b == 0:
			return 1
		h = a % 10
		if h == 0 or h == 1 or h == 5 or (h == 6):
			return h
		elif h == 4 or h == 9:
			l = b % 2
			if l == 0:
				return h ** 2 % 10
			else:
				return h ** l % 10
		else:
			l = b % 4
			if l == 0:
				return h ** 4 % 10
			return h ** l % 10


class Solution:

	def getLastDigit(self, a, b):
		a = int(a)
		b = int(b)
		if b == 0:
			return 1
		elif a % 10 == 2 or a % 10 == 3 or a % 10 == 7 or (a % 10 == 8):
			if b % 4 == 1:
				sol = a % 10
			elif b % 4 == 2:
				sol = a * a % 10
			elif b % 4 == 3:
				sol = a * a * a % 10
			elif b % 4 == 0:
				sol = a * a * a * a % 10
		elif a % 10 == 4 or a % 10 == 9:
			if b % 2 == 0:
				sol = a * a % 10
			else:
				sol = a % 10
		else:
			sol = a % 10
		return sol


class Solution:

	def getLastDigit(self, a, b):
		unit = int(a) % 10
		power = int(b) % 4
		if power == 0 and int(b) != 0:
			power = 4
		number = unit ** power % 10
		return number


class Solution:

	def getLastDigit(self, a, b):
		if int(b) == 0:
			return 1
		if int(b) == 1:
			return int(str(a)[-1])
		else:
			d = int(str(a)[-1])
			e = []
			i = 2
			e.append(str(d)[-1])
			while str(d ** i)[-1] != str(d):
				e.append(str(d ** i)[-1])
				i += 1
			count = len(e)
			f = int(b) % int(count)
			return int(e[f - 1])


class Solution:

	def getLastDigit(self, a, b):
		if int(b) == 0:
			return 1
		elif int(a) % 10 == 1:
			return 1
		elif int(a) % 10 == 0:
			return 0
		elif int(a) % 10 == 5:
			return 5
		elif int(a) % 10 == 6:
			return 6
		elif int(b) % 4 == 0:
			c = int(a) % 10
			d = c ** 4 % 10
			return d
		else:
			c = int(a) % 10
			d = c ** (int(b) % 4) % 10
			return d


class Solution:

	def getLastDigit(self, a, b):
		a = int(a)
		b = int(b)
		if b == 0:
			return 1
		temp = a % 10
		if temp in [0, 1, 5, 6]:
			return temp
		elif temp == 2:
			rems = [6, 2, 4, 8]
			return rems[b % 4]
		elif temp == 3:
			rems = [1, 3, 9, 7]
			return rems[b % 4]
		elif temp == 4:
			rems = [6, 4]
			return rems[b % 2]
		elif temp == 7:
			rems = [1, 7, 9, 3]
			return rems[b % 4]
		elif temp == 8:
			rems = [6, 8, 4, 2]
			return rems[b % 4]
		elif temp == 9:
			rems = [1, 9]
			return rems[b % 2]


class Solution:

	def getLastDigit(self, a, b):
		if len(b) == 1:
			return int(a[-1]) ** int(b[-1]) % 10
		else:
			p = int(b[-2] + b[-1]) % 4
			if p == 0:
				p = 4
			return int(a[-1]) ** p % 10


class Solution:

	def getLastDigit(self, a, b):
		a = int(a[-1:])
		if a == 0 or a == 1:
			return a
		b = int(b)
		if b == 0:
			return 1
		b = b % 4
		if b == 0:
			b = 4
		return a ** b % 10


class Solution:

	def getLastDigit(self, a, b):
		if b == '0':
			return 1
		l = [[0], [1], [2, 4, 8, 6], [3, 9, 7, 1], [4, 6], [5], [6], [7, 9, 3, 1], [8, 4, 2, 6], [9, 1]]
		s = int(a[-1])
		k = int(b) - 1
		k = k % len(l[s])
		return str(l[s][k])


class Solution:

	def getLastDigit(self, a, b):
		ud = int(a) % 10
		div = int(b)
		if div == 0:
			return 1
		ans = -1
		two = [6, 2, 4, 8]
		three = [1, 3, 9, 7]
		four = [6, 4]
		seven = [1, 7, 9, 3]
		eight = [6, 8, 4, 2]
		nine = [1, 9]
		if ud == 0:
			ans = 0
		elif ud == 1:
			ans = 1
		elif ud == 2:
			ans = two[div % 4]
		elif ud == 3:
			ans = three[div % 4]
		elif ud == 4:
			ans = four[div % 2]
		elif ud == 5:
			ans = 5
		elif ud == 6:
			ans = 6
		elif ud == 7:
			ans = seven[div % 4]
		elif ud == 8:
			ans = eight[div % 4]
		elif ud == 9:
			ans = nine[div % 2]
		return ans


class Solution:

	def getLastDigit(self, a, b):
		a = int(a)
		a = a % 10
		b = int(b)
		if b == 0:
			return 1
		l = []
		l.append([0, 0, 0, 0])
		l.append([1, 1, 1, 1])
		l.append([2, 4, 8, 6])
		l.append([3, 9, 7, 1])
		l.append([4, 6, 4, 6])
		l.append([5, 5, 5, 5])
		l.append([6, 6, 6, 6])
		l.append([7, 9, 3, 1])
		l.append([8, 4, 2, 6])
		l.append([9, 1, 9, 1])
		b = (b + 3) % 4
		return l[a][b]


import math

class Solution:

	def getLastDigit(self, a, b):

		def mod(a, b):
			m = 0
			for i in range(len(b)):
				m = (m * 10 + int(b[i])) % a
			return int(m)
		(la, lb) = (len(a), len(b))
		if la == 1 and lb == 1 and (b[0] == '0') and (a[0] == '0'):
			return 1
		if lb == 1 and b[0] == '0':
			return 1
		if la == 1 and a[0] == '0':
			return 0
		exp = 4 if mod(4, b) == 0 else mod(4, b)
		res = int(math.pow(int(a[la - 1]), exp))
		return res % 10


class Solution:

	def getLastDigit(self, a, b):
		a = int(a)
		b = int(b)
		if b == 0:
			return 1
		count = 0
		d = []
		mul = 1
		while True:
			mul *= a
			if mul % 10 not in d:
				d.append(mul % 10)
			else:
				break
			count += 1
		rep = b % count
		ans = d[rep - 1]
		return ans


class Solution:

	def getLastDigit(self, a, b):
		a = int(a)
		b = int(b)
		x = a % 10
		y = b
		if a == 0:
			return 0
		if b == 0:
			return 1
		if b % 4 == 0:
			b = 4
		else:
			b = b % 4
		y = b % 10
		ans = x ** y
		return ans % 10


class Solution:

	def getLastDigit(self, a, b):
		return str(int(a[-1]) ** int(b[-2:]))[-1]


class Solution:

	def getLastDigit(self, a, b):

		def power(x, y):
			if y == 0:
				return 1
			if y == 1:
				return x
			res = power(x, y // 2)
			if y % 2 == 0:
				return res * res
			else:
				return res * res * x
		(a, b) = (int(a), int(b))
		if b == 0:
			return 1
		if b % 4 == 0:
			b = 4
		else:
			b = b % 4
		return power(a % 10, b) % 10