#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long q;
  cin >> q;
  while (q--) {
    long long n, m;
    cin >> n;
    cin >> m;
    if (m > n || m % 10 == 0) {
      cout << 0 << "\n";
      continue;
    }
    long long p = (n / m) / 10;
    long long q = (n / m) % 10;
    long long sum = 0;
    if (m % 2 == 0)
      sum = p * (40LL);
    else if (m % 5 == 0)
      sum = p * (25LL);
    else
      sum = p * (45LL);
    long long num = 0;
    for (long long i = 1; i <= q; i++) {
      num += m;
      sum += (num % 10);
    }
    cout << sum << "\n";
  }
  return 0;
}
