#include <bits/stdc++.h>
using namespace std;
long long int t, m, d;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> t;
  while (t--) {
    cin >> d >> m;
    long long int ans = 1;
    for (long long int i = 1; i <= d; i *= 2) {
      ans = (ans * (min(d - i + 1, i * 2 - i) + 1) % m) % m;
    }
    cout << ((ans - 1) % m + m) % m << '\n';
  }
  return 0;
}
