#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::duration_cast<chrono::nanoseconds>(
                chrono::high_resolution_clock::now().time_since_epoch())
                .count());
const long long int N = 5005;
const long long int LG = 22;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, m;
    cin >> n >> m;
    long long int ans = 1;
    for (long long int i = 0; i < 32; i++) {
      long long int l = (1LL << i), r = (1LL << (i + 1)) - 1;
      r = min(r, n);
      long long int x = max(0LL, r - l + 1);
      ans = (ans * (x + 1)) % m;
    }
    ans = (ans - 1 + m) % m;
    cout << ans << endl;
  }
  return 0;
}
