#include <bits/stdc++.h>
using namespace ::std;
const long double PI = acos(-1);
const long long MOD = 1000000000 + 7;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
int add(int a, int b, int m = MOD) { return (a + b) % m; }
int mul(long long a, long long b, int m = MOD) { return (a * b) % m; }
long long pow_mod(long long a, long long b, long long m = MOD) {
  long long res = 1LL;
  a = a % m;
  while (b) {
    if (b & 1) res = mul(res, a, m);
    b >>= 1;
    a = mul(a, a, m);
  }
  return res;
}
long long fastexp(long long a, long long b) {
  long long res = 1LL;
  while (b) {
    if (b & 1) res = res * a;
    b >>= 1;
    a *= a;
  }
  return res;
}
int gcdExtendido(int a, int b, int *x, int *y) {
  if (a == 0) {
    *x = 0;
    *y = 1;
    return b;
  }
  int x1, y1;
  int gcd = gcdExtendido(b % a, a, &x1, &y1);
  *x = y1 - (b / a) * x1;
  *y = x1;
  return gcd;
}
int modInverso(int a, int m) {
  int x, y;
  int g = gcdExtendido(a, m, &x, &y);
  if (g != 1)
    return -1;
  else
    return (x % m + m) % m;
}
const int MAX = 1e6 + 5;
int n;
int m;
int nodes;
vector<pair<int, int> > G[MAX];
int D[MAX];
int p[MAX];
int pa[MAX];
int val[MAX];
int res[MAX];
void process(int pos, int l, int r) {
  vector<int> v;
  for (int x = pos; x > 0; x /= 10) v.emplace_back(x % 10);
  reverse((v).begin(), (v).end());
  int par = l;
  for (int i = 0; i + 1 < v.size(); i++) {
    G[par].emplace_back(make_pair(nodes, v[i]));
    par = nodes;
    nodes += 1;
  }
  G[par].emplace_back(make_pair(r, v.back()));
}
void BFS(int src) {
  for (int i = 1; i < nodes; i++) {
    D[i] = -1;
    res[i] = 0;
  }
  D[src] = 0;
  p[src] = 0;
  val[src] = 0;
  res[src] = 0;
  vector<pair<int, int> > frontier;
  vector<pair<int, int> > new_frontier;
  frontier.emplace_back(make_pair(src, 0));
  while (!frontier.empty()) {
    new_frontier.clear();
    int L = 0, R = 0;
    while (L < frontier.size()) {
      while (R < frontier.size() and frontier[L].second == frontier[R].second)
        R += 1;
      for (int d = 0; d < 10; d++) {
        for (int i = L; i < R; i++) {
          int u = frontier[i].first;
          while (!G[u].empty() and G[u].back().second == d) {
            int v = G[u].back().first;
            if (D[v] == -1) {
              p[v] = d;
              pa[v] = u;
              D[v] = D[u] + 1;
              res[v] = add(mul(10, res[u]), d);
              if (new_frontier.empty())
                val[v] = 0;
              else {
                int last = new_frontier.back().first;
                if (make_pair(val[pa[last]], p[last]) == make_pair(val[u], d)) {
                  val[v] = new_frontier.back().second;
                } else {
                  val[v] = new_frontier.back().second + 1;
                }
              }
              new_frontier.emplace_back(make_pair(v, val[v]));
            }
            G[u].pop_back();
          }
        }
      }
      L = R;
    }
    swap(frontier, new_frontier);
  }
}
int main() {
  scanf("%d %d", &(n), &(m));
  nodes = n + 1;
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d %d", &(u), &(v));
    process(i, u, v);
    process(i, v, u);
  }
  for (int i = 1; i < nodes; i++) {
    sort((G[i]).begin(), (G[i]).end(), [&](pair<int, int> a, pair<int, int> b) {
      return a.second > b.second;
    });
  }
  BFS(1);
  for (int i = 2; i <= n; i++) {
    printf("%d\n", res[i]);
  }
  return 0;
}
