#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const long double PI = 4 * atan((long double)1);
long long power(long long a, long long n) {
  long long res = 1;
  while (n) {
    if (n % 2) res *= a;
    a *= a;
    n /= 2;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int t;
  cin >> t;
  while (t--) {
    long long int d, m;
    cin >> d >> m;
    long long int ans = 1, v = 0;
    while (1) {
      long long int x = min(power(2, v + 1) - 1, d) - power(2, v) + 2;
      ans = ((ans % m) * (x % m)) % m;
      v++;
      if (power(2, v) > d) break;
    }
    cout << (ans - 1 + m) % m;
    cout << "\n";
  }
  return 0;
}
