#include <bits/stdc++.h>
using namespace std;
long long d, m;
long long a[50];
int main() {
  int t;
  cin >> t;
  for (t; t > 0; t--) {
    cin >> d >> m;
    long long k = 1, num = 0;
    while (k <= d) {
      if (k * 2 - 1 <= d) {
        a[num] += k;
        k *= 2;
        num++;
      } else {
        a[num] += d - k + 1;
        break;
      }
    }
    long long ans = 1;
    for (int i = 0; i < 50; ++i) {
      ans = (ans * (a[i] + 1)) % m;
      a[i] = 0;
    }
    ans--;
    if (ans < 0) ans += m;
    cout << ans << '\n';
  }
}
