#include <bits/stdc++.h>
using namespace std;
const int MN = 1e5 + 10;
int n, q, arr[MN];
struct node {
  long long val[10];
  int f[10];
  node(bool fl = false) {
    iota(f, f + 10, 0);
    memset(val, 0, sizeof val);
  }
  void F(int x, int y) {
    if (x ^ y) val[y] += val[x], val[x] = 0;
    for (int i = 0; i < 10; ++i)
      if (f[i] == x) f[i] = y;
  }
} seg[MN << 2];
node merge(node A, node B) {
  node ret;
  for (int i = 0; i < 10; ++i) ret.val[i] = A.val[i] + B.val[i];
  return ret;
}
void build(int id, int b, int e) {
  if (e - b == 1) {
    int t = arr[b], pw = 1;
    while (t) {
      seg[id].val[t % 10] += pw;
      pw *= 10, t /= 10;
    }
    return;
  }
  int mid = b + e >> 1;
  build(id << 1, b, mid), build(id << 1 | 1, mid, e);
  seg[id] = merge(seg[id << 1], seg[id << 1 | 1]);
}
long long T[10];
void shift(int id) {
  memset(T, 0, sizeof T);
  for (int i = 0; i < 10; ++i) T[seg[id].f[i]] += seg[id << 1].val[i];
  for (int i = 0; i < 10; ++i) seg[id << 1].val[i] = T[i];
  memset(T, 0, sizeof T);
  for (int i = 0; i < 10; ++i) T[seg[id].f[i]] += seg[id << 1 | 1].val[i];
  for (int i = 0; i < 10; ++i) seg[id << 1 | 1].val[i] = T[i];
  for (int i = 0; i < 10; ++i) {
    seg[id << 1].f[i] = seg[id].f[seg[id << 1].f[i]];
    seg[id << 1 | 1].f[i] = seg[id].f[seg[id << 1 | 1].f[i]];
  }
  for (int i = 0; i < 10; ++i) seg[id].f[i] = i;
}
void upd(int id, int b, int e, int l, int r, int x, int y) {
  if (r <= b || e <= l) return;
  if (l <= b && e <= r) {
    seg[id].F(x, y);
    return;
  }
  int mid = b + e >> 1;
  shift(id);
  upd(id << 1, b, mid, l, r, x, y);
  upd(id << 1 | 1, mid, e, l, r, x, y);
  seg[id] = merge(seg[id << 1], seg[id << 1 | 1]);
}
long long get(int id, int b, int e, int l, int r) {
  if (r <= b || e <= l) return 0;
  if (l <= b && e <= r) {
    long long ret = 0;
    for (int i = 0; i < 10; ++i) ret += 1ll * i * seg[id].val[i];
    return ret;
  }
  shift(id);
  int mid = b + e >> 1;
  return get(id << 1, b, mid, l, r) + get(id << 1 | 1, mid, e, l, r);
}
int main() {
  ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> q;
  for (int i = 0; i < n; ++i) cin >> arr[i];
  build(1, 0, n);
  while (q--) {
    int tp;
    cin >> tp;
    if (tp == 1) {
      int l, r, x, y;
      cin >> l >> r >> x >> y;
      upd(1, 0, n, l - 1, r, x, y);
    } else {
      int l, r;
      cin >> l >> r;
      cout << get(1, 0, n, l - 1, r) << '\n';
    }
  }
  return 0;
}
