#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 3e5 + 15;
int n, m;
long long dp[64];
long long get(long long x) { return 63 - __builtin_clzll(x); }
long long solve(long long i) {
  if (dp[i] != -1) return dp[i];
  long long res = 1 % m;
  for (long long k = i; true; k++) {
    if (k == get(n)) {
      long long y = (n ^ (1ll << k));
      res = (res + y) % m;
      break;
    } else {
      res = (res + (1ll << k) * solve(k + 1)) % m;
    }
  }
  return dp[i] = res % m;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long q;
  cin >> q;
  while (q--) {
    memset(dp, -1, sizeof(dp));
    cin >> n >> m;
    n++;
    cout << ((((solve(0) - 1) % m) + m) % m) << "\n";
  }
}
