Akhil and GF
Difficulty: Hard

After dating for a long time, Akhil is finally going to propose to his girlfriend. She is very strong in mathematics, and will accept his proposal, if and only if Akhil solves a problem given by her. The problem is given below. Help Akhil solve it.

Akhil is given two numbers N and M, and he has to tell her the remainder when $111\cdots \text{(N times)}$ is divided by $M$.  

**Input Format**  
The first line contains an integer $T$ i.e. the number of test cases.  
Each of the next $T$ lines contain two space separated integers, $N$ and $M$.  

**Output Format**  
$T$ lines each containing ouptut for the corresponding test case. 

**Constraints**  
$1 \le T \le 10001$  
$1 \le N \le 10^{16}$  
$2 \le M \le 10^9$  

**Sample Input 00**  

	3
	3 3
	4 7
	5 18
    
**Sample Output 00**  

	0
	5	
	5
    
**Explanation** 

  111 % 3  = 0  
  1111 % 7 = 5  
  11111%18 = 5  

Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:
#
# Complete the 'solve' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
#  1. LONG_INTEGER n
#  2. INTEGER m
#

def solve(n, m):
    # Write your code here




Reference solution (C++):
###C++  
```cpp  
#include<bits/stdc++.h>

using namespace std;

long long int power(long long int a, long long int b, long long int c)
{
    if(b==0) 
        return 1;
    if(b==1)
        return a%c;
    if(b%2 == 0)
    {
        long long int temp = power(a,b/2,c);
        return (temp*temp)%c;
    }
    else
    {
        long long int temp = power(a,b/2,c);
        temp = (temp*temp)%c;
        return (temp*a)%c;
    }
}

long long int func(long long int n, long long int m)
{
    if(n< 6)
    {
        if(n==0)
            return 0;
        if(n==1)    
            return 1;
        if(n==2)
            return 11%m;
        if(n==3)
            return 111%m;
        if(n==4)
            return 1111%m;
        if(n==5)
            return 11111%m;
    }
    if(n%2 == 0)
    {
        long long int temp = func(n/2 , m)%m;
        long long int temp1 = (power(10,n/2,m)*temp + temp)%m;
        return temp1;
    }
    else
    {
        long long int temp = func(n/2 , m)%m;
        long long int temp1 = (power(10,n/2+1,m)*temp + temp*10 + 1)%m;
        return temp1;
    }
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long int n,m;    
        cin>>n>>m;
        assert(n > 0);
        assert(m > 0);
        cout<<func(n,m)<<endl;
    }
    return 0;
}
```