#include <bits/stdc++.h>
using namespace std;
long long ansew(long long n, long long m) {
  long long ex;
  long long rep;
  long long ans = 0;
  ex = n / m;
  int undig = m % 10;
  switch (undig) {
    case 1: {
      rep = ex / 10;
      ans += (45 * rep);
      for (int i = 1; i <= ex % 10; i++) {
        ans += i;
      }
      return ans;
    }
    case 2: {
      rep = ex / 5;
      ans += (20 * rep);
      for (int i = 1; i <= ex % 5; i++) {
        ans += (2 * i) % 10;
      }
      return ans;
    }
    case 3: {
      rep = ex / 10;
      ans += (45 * rep);
      for (int i = 1; i <= ex % 10; i++) {
        ans += ((3 * i) % 10);
      }
      return ans;
    }
    case 4: {
      rep = ex / 5;
      ans += (20 * rep);
      for (int i = 1; i <= ex % 5; i++) {
        ans += (4 * i) % 10;
      }
      return ans;
    }
    case 5: {
      rep = ex / 2;
      ans += (5 * rep);
      for (int i = 1; i <= ex % 2; i++) {
        ans += (5 * i) % 10;
      }
      return ans;
    }
    case 6: {
      rep = ex / 5;
      ans += (20 * rep);
      for (int i = 1; i <= ex % 5; i++) {
        ans += (6 * i) % 10;
      }
      return ans;
    }
    case 7: {
      rep = ex / 10;
      ans += (45 * rep);
      for (int i = 1; i <= ex % 10; i++) {
        ans += ((7 * i) % 10);
      }
      return ans;
    }
    case 8: {
      rep = ex / 5;
      ans += (20 * rep);
      for (int i = 1; i <= ex % 5; i++) {
        ans += (8 * i) % 10;
      }
      return ans;
    }
    case 9: {
      rep = ex / 10;
      ans += (45 * rep);
      for (int i = 1; i <= ex % 10; i++) {
        ans += (9 * i) % 10;
      }
      return ans;
    }
    case 0: {
      ans = 0;
      return ans;
    }
  }
  return ans;
}
int main() {
  int q;
  cin >> q;
  long long n;
  long long m;
  long long sum;
  while (q--) {
    cin >> n >> m;
    sum = 0;
    sum = ansew(n, m);
    cout << sum << endl;
  }
  return 0;
}
