#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  auto solve = [&]() {
    long long n, m;
    cin >> n >> m;
    long long res = 1;
    for (int i = 1; i < 31; i++) {
      long long diff = min(n + 1, (1ll << (i + 1))) - (1ll << i);
      if (diff > 0) res = res + diff + diff * res;
      res %= m;
    }
    cout << res % m << '\n';
  };
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
