#include <bits/stdc++.h>
using namespace std;
long long pw(long long a, long long n, long long m) {
  long long res = 1;
  while (n) {
    if (n & 1) res = res * a % m;
    a = a * a % m;
    n /= 2;
  }
  return res;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    long long s = 2;
    vector<long long> p(100, 1);
    for (long long i = 1; i < p.size(); i++) {
      p[i] = s;
      s = (s + p[i] * pw(2, i, m)) % m;
    }
    long long ans = 0;
    for (long long i = 0; i <= 30; i++) {
      if (n > (1 << i + 1) - 1) {
        ans = (ans + (1 << i) * p[i]) % m;
      } else {
        ans = (ans + p[i] * (n + 1 - (1 << i))) % m;
        break;
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
