#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long d, mod;
    cin >> d >> mod;
    long long bits[31];
    memset(bits, 0, sizeof bits);
    long long ans = 1;
    for (long long i = 0; i < 31; ++i) {
      bits[i] = min(pow(2, i), d - pow(2, i) + 1);
      bits[i] = max(bits[i], 0LL);
      ans *= 1 + bits[i];
      ans %= mod;
    }
    cout << (ans - 1 + mod) % mod << '\n';
  }
  return 0;
}
