【題目敘述】http://codeforces.com/contest/1329/problem/B
#include <iostream>
#include <vector>
using namespace std;
int t, d, m;
long long ans;
int main() {
cin >> t;
while (t--){
cin >> d >> m;
ans = 1;
int cur = 1;
while (d){
ans *= min(cur, d)+1;
ans %= m;
d -= min(cur, d);
cur *= 2;
}
ans -= 1;
if (ans < 0) ans += m;
cout << ans << "\n";
}
}