import java.util.Scanner; import java.io.*;
class AraOfDigit{
public static void main(String[] args) throws IOException{
Scanner sc =new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter wr = new PrintWriter(System.out);
int iteration = sc.nextInt();
int count = 0;
while(count<iteration){
int n = sc.nextInt();
int mod = sc.nextInt();
String arr [] = new String[n];
****String[] arr_A =br.readLine().split(" ");****//Nullpointer Exception How to slove?
for(int i=0;i<n;i++) {
arr[i]=arr_A[i];
}
StringBuilder total = new StringBuilder();
for(int j=0;j<n;j++){
total.append(arr[j]);
}
int num = Integer.parseInt(total.toString());
num = num/10;
int op = num%mod;
System.out.println(op);
count++;
}
}
}