#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  long long t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    n++;
    long long cur = 1, res = 1;
    vector<long long> ans;
    while (cur * 2 <= n) {
      long long old = cur;
      cur *= 2;
      ans.push_back((cur - old + 1));
    }
    ans.push_back((n - cur + 1));
    for (auto i : ans) {
      if (i == 0) continue;
      res = (res * (i)) % m;
    }
    res = (res - 1 + m) % m;
    cout << res << "\n";
  }
  return 0;
}
