using System;
using System.Collections.Generic;
public class Test
{
class Program
{
public static int determine_value_at_position_of_an_array(List<int> arr, int potega)
{
int counter = 1;
int counter_2 = 0;
int current_value = 0;
for (; counter<= potega; counter++)
{
current_value = arr[counter_2];
if (counter_2==arr.Count-1)
{
counter_2 = 0;
}
else
{
counter_2++;
}
}
return current_value;
}
public static int get_last_number(int n)
{
string temp = Convert.ToString(n);
char tempo = Convert.ToChar((temp[temp.Length - 1]));
int result = (int)Char.GetNumericValue(tempo);
return result;
}
public static List<int> Pattern_Finder(int podstawa)
{
List<int> sequence = new List<int>();
int goal = (get_last_number(podstawa));
sequence.Add(goal);
int startowa_potega = 2;
int temp = 0;
while (true)
{
temp
= pow(podstawa
, startowa_potega
); if (get_last_number(temp) == goal)
{
break;
}
startowa_potega++;
sequence.Add(get_last_number(temp));
}
return sequence;
}
public
static int pow(int podstawa
, int potega
) {
int result = 1;
while (potega > 0)
{
result *= podstawa;
potega--;
}
return result;
}
public static int Answer_Finder(int podstawa, int potega)
{
switch (podstawa)
{
case 0:
return 1;
case 1:
return 1;
}
List<int> sequence = Pattern_Finder(podstawa);
int result = determine_value_at_position_of_an_array(sequence, potega);
return result;
}
public static void Main()
{
int n = Convert.ToInt32(Console.ReadLine());
while (n > 0)
{
string[] temp_values = Console.ReadLine().Split(' ');
int podstawa = Convert.ToInt32(temp_values[0]);
int potega = Convert.ToInt32(temp_values[1]);
Console.WriteLine(Answer_Finder(podstawa, potega));
}
}
}
}