#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int t;
  cin >> t;
  while (t--) {
    long long int d;
    long long int m;
    cin >> d >> m;
    long long int x = 1;
    long long int k = 1;
    long long int count1 = 0;
    while (k <= d) {
      count1++;
      k <<= 1;
    }
    x = count1 - 1;
    long long int totalno = x + 1;
    long long int mynum = 1;
    long long int count2 = 0;
    while (count2 != x) {
      mynum <<= 1;
      count2++;
    }
    long long int ans = 1;
    for (long long int i = 0; i < totalno - 1; i++) {
      long long int k2 = 1;
      long long int count1 = 0;
      while (count1 != i) {
        k2 <<= 1;
        count1++;
      }
      ans *= (k2 + 1) % m;
      ans %= m;
    }
    ans *= ((d - mynum) + 2) % m;
    cout << ((ans - 1) % m + m) % m << "\n";
  }
}
