#include <iostream>

using namespace std;
typedef long long LL;
LL a,b,p;
LL mul(LL a,LL b, LL p){
    LL res=0;
    while(b){
        if(b&1) res=(res+a)%p;
        a=(a+a)%p;
        b>>=1;
    }
    return res;
}
int main()
{
    int q;
    scanf("%d",&q);
    while(q--){
        scanf("%lld%lld%lld",&a,&b,&p);
    printf("%lld\n",mul(a,b,p));
    }
    
    return 0;
}