#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t, n, m, i;
  cin >> t;
  while (t--) {
    long long int term, ans = 0, sum = 0, temp;
    cin >> n >> m;
    term = (n - (n % m)) / m;
    if (m % 10 == 0)
      ans = 0;
    else if (m % 5 == 0) {
      if (term & 1)
        ans = ((term + 1) / 2) * 5;
      else
        ans = (term / 2) * 5;
    } else if (m & 1) {
      ans = (term / 10) * 45;
      temp = term % 10;
      for (i = 0; i < temp; i++) {
        sum = (sum + m) % 10;
        ans += sum;
      }
    } else {
      ans = (term / 5) * 20;
      temp = term % 5;
      for (i = 0; i < temp; i++) {
        sum = (sum + m) % 10;
        ans += sum;
      }
    }
    cout << ans << endl;
  }
}
