#include <bits/stdc++.h>
using namespace std;
long long dp[100002];
long long n;
long long dfs(long long a[], long long idx) {
  if (2 * idx > n) return 1;
  long long &res = dp[idx];
  if (res != -1) return res;
  long long ch = 0;
  for (long long i = idx * 2; i <= n; i += idx) {
    if (a[idx] < a[i]) ch = max(dfs(a, i), ch);
  }
  return res = ch + 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    long long ch = log2(n);
    long long lol = 1;
    vector<long long> chh;
    for (long long i = 0; i < (ch); ++i) {
      chh.push_back(lol);
      lol = lol * 2;
    }
    chh.push_back(n - pow(2, ch) + 1);
    long long ans = 1;
    for (auto x : chh) {
      ans = (ans * (x + 1)) % k;
    }
    cout << (ans + k - 1) % k << '\n';
  }
  return 0;
}
