#include <bits/stdc++.h>   using namespace std;   long long int cnt(long long int x) {     long long int ans = 0;     while (x) {       x /= 2;       ans++;     }     return ans;   }   int main() {     int t;     cin >> t;     while (t--) {       long long int d, m;       cin >> d >> m;       long long int temp = cnt(d) - 1;       long long int ans = d - pow(2, temp) + 2;       for (long long int i = temp - 1; i >= 0; i--) {         ans = ans * (pow(2, i) + 1);         ans %= m;       }       ans = (ans - 1) % m;       if (ans < 0) ans = m + ans;       cout << ans << endl;     }   }   