poj1995

//172k, 79ms
#include <iostream>

using namespace std;

//x的n次方对mod取模
int mod_pow(int x, int n, int mod) {
    int res = 1;
    while(n > 0) {
        if(n & 1) res = res * x % mod;
        x = (x % mod) * (x % mod) % mod;
        n >>= 1;
    }
    return res;
}

const int MAX_H = 45005;

int z, m, h;
int arr[MAX_H];

int main() {

    freopen("in.txt", "r", stdin);

    scanf("%d", &z);
    while(z--) {
        scanf("%d%d", &m, &h);
        int res = 0;
        int a, b;
        while(h--) {
            scanf("%d%d", &a, &b);
            res = res + mod_pow(a, b, m);
        }
        printf("%d\n", res % m);
    }

    fclose(stdin);

    return 0;
}
posted @ 2016-12-26 14:38  StevenLuke  阅读(169)  评论(0编辑  收藏  举报