#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10, mod = 1e9 + 7, inf = 1e9 + 10;
int solve(long long n, int m) {
  int ans = 1;
  for (long long i = 1; i <= n; i *= 2) {
    ans = 1ll * ans * ((min(n, 2 * i - 1) - i + 2) % m) % m;
  }
  ans--;
  if (ans < 0) ans += m;
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie();
  int q;
  cin >> q;
  while (q--) {
    int n, m;
    cin >> n >> m;
    cout << solve(n, m) << "\n";
  }
  return 0;
}
