link
轮询快速幂
板子

#include <bits/stdc++.h>

using ll = long long;

auto main()->int {
    std::cin.tie(nullptr)->sync_with_stdio(false);

    int t;
    std::cin >> t;

    auto power = [&](ll a, ll b, ll p) {
        ll ans = 1 % p;
        for (; b; b >>= 1, a = a * a % p) {
            if (b & 1) {
                ans = ans * a % p;
            }
        }
        return ans;
    };

    auto solve = [&]() {
        int H, M;
        std::cin >> M >> H;

        ll ans = 0;
        for (int i = 1; i <= H; ++i) {
            ll A, B;
            std::cin >> A >> B;
            ans = (ans + power(A, B, M)) % M;
        }

        std::cout << ans % M << "\n";
    };

    while (t--) {
        solve();
    }

    return 0;
}

5min +\textbf{\textcolor{#37A01D}{+}} 一遍过,哦豁