#include struct P { long long count = 0; long long value = 0; }; void solve() { std::string s; int mod; std::cin >> s >> mod; std::vector a; for (auto c: s) a.push_back(c - '0'); std::ranges::reverse(a); int n = a.size(); std::vector dp(n + 1, std::vector

(2)); dp[0][0].value = 0; dp[0][0].count = 1; long long p10 = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < 2; j++) { for (int digit = 0; digit < 10; digit++) { int greater = 1; if (digit < a[i]) greater = 0; if (j == 0 && digit == a[i]) greater = 0; dp[i + 1][greater].count += dp[i][j].count; dp[i + 1][greater].count %= mod; dp[i + 1][greater].value += (p10 * digit % mod * dp[i][j].count % mod) + dp[i][j].value; dp[i + 1][greater].value %= mod; } } p10 *= 10; p10 %= mod; } std::cout << dp.back()[0].value << '\n'; } int main() { std::cin.tie(0)->sync_with_stdio(0); std::cout << std::fixed << std::setprecision(16); int t = 1; std::cin >> t; while (t--) solve(); }