//
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
//
//#pragma GCC optimize(3,"Ofast","inline")
//#include<unordered_map>
//#include<unordered_set>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<functional>
#include<cstring>
#include<string>
#include<cstdlib>
#include<queue>
#include<map>
#include<algorithm>
#include<set>
#include<stack>
#include<vector>
#include<sstream>
#include<list>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
int mod=104857601;
const int maxn=2e5+5;
ll qpow(ll a,ll b,ll p)
{
    ll ans=1%p;
    while(b!=0)
    {
        if(b&1)
        {
            ans=(ans%p*a%p)%p;
        }
        a=(a*a)%p;
        b>>=1;
    }
    return ans;
}
int main()
{
    ll n,m;
    cin>>n>>m;
    cout<<qpow(10,n,m*m)/m;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for(ll (i)=(m);(i)<(n);++(i))
#define REP(i, n) FOR(i,n,0)
#define OF64 std::setprecision(40)

const ll MOD = 1000000007;
const ll INF = (ll) 1e15;

ll solve() {
    ll L, R;
    cin >> L >> R;
    ll ok = R + 1, ng = L - 1;
    while (abs(ok - ng) > 1) {
        ll mid = (ok + ng) / 2;
        if (mid - L >= L)
            ok = mid;
        else
            ng = mid;
    }
    ll num = std::max(R - ok + 1, 0LL);
    ll ret = (num + 1) * num / 2;
    return ret;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll Q;
    cin >> Q;
    REP(_, Q) {
        cout << solve() << endl;
    }
    return 0;
}