@file:Suppress("NOTHING_TO_INLINE", "EXPERIMENTAL_FEATURE_WARNING", "OVERRIDE_BY_INLINE")

import java.io.PrintWriter
import java.util.StringTokenizer
import kotlin.math.*
import kotlin.random.*
import kotlin.collections.sort as _sort
import kotlin.collections.sortDescending as _sortDescending
import kotlin.io.println as iprintln

/** @author Spheniscine */
fun main() {
    output {
        val n = readInt()
        val A = readLongArray(n)

        var ans = 1L

        val quota = (n+1)/2

        trial@ for(i in 0 until min(n, certainty)) {
            A.swap(i, random.nextInt(i, n))
            val ar = A[i]
            val cnt = mutableMapOf<Long, Int>()
            for(d in ar.divisorsReversed().takeWhile { it >= ans }) cnt[d] = 0
            for(a in A) {
                val g = gcd(ar, a)
                if(g >= ans) cnt[g] = cnt[g]!! + 1
            }

            val ite = cnt.iterator()
            for((d, cn) in ite) {
                if(cn >= quota) {
                    ans = d
                    break@trial
                }
                ite.remove()
                for(d2 in cnt.keys) {
                    if(d % d2 == 0L) cnt[d2] = cnt[d2]!! + cn
                }
            }
        }

        println(ans)
    }
}
const val certainty = 30 // probability of failure = 2^-n
fun LongArray.swap(i: Int, j: Int) { this[i] = this[j].also { this[j] = this[i] } }
fun Long.divisorsReversed() = sequence {
    val n = this@divisorsReversed
    if(n <= 0) return@sequence
    val head = mutableListOf<Long>()

    for(p in 1..Long.MAX_VALUE) {
        val sq = p * p
        if(sq > n) break
        if(n % p == 0L) {
            head.add(p)
            if(sq != n) yield(n / p)
        }
    }

    yieldAll(head.asReversed())
}
tailrec fun gcd(a: Long, b: Long): Long = if(a == 0L) abs(b) else gcd(b % a, a)

/** IO code start */
//@JvmField val INPUT = File("input.txt").inputStream()
//@JvmField val OUTPUT = File("output.txt").outputStream()
@JvmField val INPUT = System.`in`
@JvmField val OUTPUT = System.out

@JvmField val _reader = INPUT.bufferedReader()
fun readLine(): String? = _reader.readLine()
fun readLn() = _reader.readLine()!!
@JvmField var _tokenizer: StringTokenizer = StringTokenizer("")
fun read(): String {
    while (_tokenizer.hasMoreTokens().not()) _tokenizer = StringTokenizer(_reader.readLine() ?: return "", " ")
    return _tokenizer.nextToken()
}
fun readInt() = read().toInt()
fun readDouble() = read().toDouble()
fun readLong() = read().toLong()
fun readStrings(n: Int) = List(n) { read() }
fun readLines(n: Int) = List(n) { readLn() }
fun readInts(n: Int) = List(n) { read().toInt() }
fun readIntArray(n: Int) = IntArray(n) { read().toInt() }
fun readDoubles(n: Int) = List(n) { read().toDouble() }
fun readDoubleArray(n: Int) = DoubleArray(n) { read().toDouble() }
fun readLongs(n: Int) = List(n) { read().toLong() }
fun readLongArray(n: Int) = LongArray(n) { read().toLong() }

@JvmField val _writer = PrintWriter(OUTPUT, false)
inline fun output(block: PrintWriter.() -> Unit) { _writer.apply(block).flush() }

/** shuffles and sort overrides to avoid quicksort attacks */
private inline fun <R, V> R._shuffle(rnd: Random, get: R.(Int) -> V, set: R.(Int, V) -> Unit, size: Int) {
    // Fisher-Yates shuffle algorithm
    for (i in size - 1 downTo 1) {
        val j = rnd.nextInt(i + 1)
        val temp = get(i)
        set(i, get(j))
        set(j, temp)
    }
}

@JvmField var _random: Random? = null
val random get() = _random ?: Random(0x594E215C123 * System.nanoTime()).also { _random = it }

fun IntArray.shuffle(rnd: Random = random) = _shuffle(rnd, IntArray::get, IntArray::set, size)
fun IntArray.sort() { shuffle(); _sort() }
fun IntArray.sortDescending() { shuffle(); _sortDescending() }

fun LongArray.shuffle(rnd: Random = random) = _shuffle(rnd, LongArray::get, LongArray::set, size)
fun LongArray.sort() { shuffle(); _sort() }
fun LongArray.sortDescending() { shuffle(); _sortDescending() }

fun DoubleArray.shuffle(rnd: Random = random) = _shuffle(rnd, DoubleArray::get, DoubleArray::set, size)
fun DoubleArray.sort() { shuffle(); _sort() }
fun DoubleArray.sortDescending() { shuffle(); _sortDescending() }

fun CharArray.shuffle(rnd: Random = random) = _shuffle(rnd, CharArray::get, CharArray::set, size)
inline fun CharArray.sort() { _sort() }
inline fun CharArray.sortDescending() { _sortDescending() }

inline fun <T: Comparable<T>> Array<out T>.sort() = _sort()
inline fun <T: Comparable<T>> Array<out T>.sortDescending() = _sortDescending()
inline fun <T: Comparable<T>> MutableList<out T>.sort() = _sort()
inline fun <T: Comparable<T>> MutableList<out T>.sortDescending() = _sortDescending()

fun `please stop removing these imports IntelliJ`() {
    iprintln(max(1, 2))
}


#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <vector>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <numeric>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long       LL;
typedef pair<int, int>  PII;
typedef vector<int>     VI;
typedef vector<PII>     VPII;
#define MM(a,x) memset(a,x,sizeof(a));
#define ALL(x)  (x).begin(), (x).end()
#define P(x)	cerr<<"["#x<<" = "<<(x)<<"]"<<endl;
#define PP(x,i)	cerr<<"["#x<<i<<" = "<<x[i]<<"]"<<endl;
#define P2(x,y)	cerr<<"["#x" = "<<(x)<<", "#y" = "<<(y)<<"]"<<endl;
#define TM(a,b)	cerr<<"["#a" -> "#b": "<<1e3*(b-a)/CLOCKS_PER_SEC<<"ms]\n";
#define ASSERT(e) if(!(e)) {cerr<<"!!!ASSERTION FAILED!!! in #"<<__LINE__<<": " <<#e; exit(0);}
#define UN(v)   sort(ALL(v)), v.resize(unique(ALL(v))-v.begin())
#define mp make_pair
#define pb push_back
#define x first
#define y second
struct _ {_() {ios_base::sync_with_stdio(0);}} _;
template<class A, class B> ostream& operator<<(ostream &o, pair<A, B> t) {o << "(" << t.x << ", " << t.y << ")"; return o;}
template<class T> string tostring(T x, int len = 0, char c = '0') {stringstream ss; ss << x; string r = ss.str(); if(r.length() < len) r = string(len - r.length(), c) + r; return r;}
template<class T> void PV(T a, T b, int n = 0, int w = 0, string s = " ") {int c = 0; while(a != b) {cout << tostring(*a++, w, ' '); if(a != b && (n == 0 || ++c % n)) cout << s; else cout << endl;}}
template<class T> inline bool chmin(T &a, T b) {return a > b ? a = b, 1 : 0;}
template<class T> inline bool chmax(T &a, T b) {return a < b ? a = b, 1 : 0;}
const LL linf = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int mod = int(1e9) + 7;
const int N = 1111111;

int n;
LL d[N];
LL a[N];
LL v[N];

int main() {
	cin >> n;
	for(int i = 0; i < n; i++) cin >> d[i];

	random_shuffle(d, d + n);
	LL res = 1;
	set<LL> S;
	int ii = 0;
	while(ii < n && S.size() < 13) S.insert(d[ii++]);
	for(LL x : S) {
		for(int i = 0; i < n; i++) v[i] = __gcd(x, d[i]);

		sort(v, v + n);

		vector<pair<LL, int>> c;
		int cc = 1;
		for(int i = 1; i <= n; i++) {
			if(i == n || v[i] != v[i - 1]) c.pb(mp(v[i - 1], cc)), cc = 1;
			else cc++;
		}

		vector<LL> factor;
		for(LL i = 1; i * i <= x; i++) {
			if(x % i == 0) {
				factor.pb(i);
				if(i * i < x) factor.pb(x / i);
			}
		}

		sort(ALL(factor));
		reverse(ALL(factor));
		for(LL i : factor) {
			if(i <= res) continue;
			int cnt = 0;
			for(auto& j : c) {
				if(j.x % i == 0) cnt += j.y;
			}
			if(2 * cnt >= n) chmax(res, i);
		}
	}
	cout << res << endl;
	return 0;
}


#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <vector>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <numeric>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long       LL;
typedef pair<int, int>  PII;
typedef pair<LL, LL>    PLL;
typedef vector<int>     VI;
typedef vector<LL>      VL;
typedef vector<PII>     VPII;
typedef vector<PLL>     VPLL;
#define MM(a,x) memset(a,x,sizeof(a));
#define ALL(x)  (x).begin(), (x).end()
#define P(x)	cerr<<"["#x<<" = "<<(x)<<"]\n"
#define PP(x,i)	cerr<<"["#x<<i<<" = "<<x[i]<<"]\n"
#define P2(x,y)	cerr<<"["#x" = "<<(x)<<", "#y" = "<<(y)<<"]\n"
#define TM(a,b)	cerr<<"["#a" -> "#b": "<<1e3*(b-a)/CLOCKS_PER_SEC<<"ms]\n";
#define FOR(it,v) for(__typeof(v.begin()) it=v.begin();it!=v.end();it++)
#define rep(i, n) for(int i = 0; i < n; i++)
#define UN(v) sort(ALL(v)), v.resize(unique(ALL(v))-v.begin())
#define mp make_pair
#define pb push_back
#define x first
#define y second
struct _ {_() {ios_base::sync_with_stdio(0);}} _;
template<class T> void PV(T a, T b) {while(a != b)cout << *a++, cout << (a != b ? " " : "\n");}
template<class T> inline bool chmin(T &a, T b) {return a > b ? a = b, 1 : 0;}
template<class T> inline bool chmax(T &a, T b) {return a < b ? a = b, 1 : 0;}
template<class T> string tostring(T x, int len = 0) {stringstream ss; ss << x; string r = ss.str(); if(r.length() < len) r = string(len - r.length(), '0') + r; return r;}
template<class T> void convert(string x, T& r) {stringstream ss(x); ss >> r;}
template<class A, class B> ostream& operator<<(ostream &o, pair<A, B> t) {o << "(" << t.x << ", " << t.y << ")"; return o;}
const int inf = 0x3f3f3f3f;
const int mod = int(1e9) + 7;
const int N = 1111111;

template<class T> inline T gcd(T a, T b) {while(b) {T t = a % b; a = b; b = t;} return a;}

int n;
LL d[N];

map<LL, int> M1;
vector<pair<int, LL> >pr;

int main() {
	cin >> n;
	for(int i = 0; i < n; i++) cin >> d[i];
	int cnt = 500000;
	for(int i = 0; i < cnt; i++) {
		int x = rand() % n;
		int y = rand() % n;
		LL g = gcd(d[x], d[y]);
		M1[g]++;
	}
	for(auto i : M1) pr.pb(mp(-i.second, i.first));
	sort(ALL(pr));

	VL v;
	for(int i = 0; i < pr.size() && i < 100; i++) v.pb(pr[i].y);

	sort(ALL(v));
	reverse(ALL(v));

	for(auto i : v) {
		int ct = 0;
		for(int j = 0; j < n; j++) if(d[j] % i == 0) ct++;
		if(2 * ct >= n) {cout << i << endl; return 0;}
	}
	return 0;
}


#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <vector>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <numeric>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long       LL;
typedef pair<int, int>  PII;
typedef vector<int>     VI;
typedef vector<PII>     VPII;
#define MM(a,x) memset(a,x,sizeof(a));
#define ALL(x)  (x).begin(), (x).end()
#define P(x)	cerr<<"["#x<<" = "<<(x)<<"]"<<endl;
#define PP(x,i)	cerr<<"["#x<<i<<" = "<<x[i]<<"]"<<endl;
#define P2(x,y)	cerr<<"["#x" = "<<(x)<<", "#y" = "<<(y)<<"]"<<endl;
#define TM(a,b)	cerr<<"["#a" -> "#b": "<<1e3*(b-a)/CLOCKS_PER_SEC<<"ms]\n";
#define ASSERT(e) if(!(e)) {cerr<<"!!!ASSERTION FAILED!!! in #"<<__LINE__<<": " <<#e; exit(0);}
#define UN(v)   sort(ALL(v)), v.resize(unique(ALL(v))-v.begin())
#define mp make_pair
#define pb push_back
#define x first
#define y second
struct _ {_() {ios_base::sync_with_stdio(0);}} _;
template<class A, class B> ostream& operator<<(ostream &o, pair<A, B> t) {o << "(" << t.x << ", " << t.y << ")"; return o;}
template<class T> string tostring(T x, int len = 0, char c = '0') {stringstream ss; ss << x; string r = ss.str(); if(r.length() < len) r = string(len - r.length(), c) + r; return r;}
template<class T> void PV(T a, T b, int n = 0, int w = 0, string s = " ") {int c = 0; while(a != b) {cout << tostring(*a++, w, ' '); if(a != b && (n == 0 || ++c % n)) cout << s; else cout << endl;}}
template<class T> inline bool chmin(T &a, T b) {return a > b ? a = b, 1 : 0;}
template<class T> inline bool chmax(T &a, T b) {return a < b ? a = b, 1 : 0;}
const LL linf = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int mod = int(1e9) + 7;
const int N = 1111111;

int n;
LL d[N];
LL a[N];

int main() {
	cin >> n;
	for(int i = 0; i < n; i++) cin >> d[i];
	LL res = 1;

	int T = 20;
	while(T--) {
		LL x = d[rand() % n];
		map<int, int> M;
		for(int i = 0; i < n; i++) {
			a[i] = __gcd(x, d[i]);
			M[a[i]]++;
		}
		vector<LL> factor;
		for(LL i = 1; i * i <= x; i++) {
			factor.pb(i);
			if(i * i < x) factor.pb(x / i);
		}
		for(LL i : factor) {
			int cnt = 0;
			for(auto j : M) {
				if(j.x % i == 0) cnt += j.y;
			}
			if(2 * cnt >= n) chmax(res, i);
		}
	}
	cout << res << endl;
	return 0;
}


#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <vector>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <numeric>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long       LL;
typedef pair<int, int>  PII;
typedef vector<int>     VI;
typedef vector<PII>     VPII;
#define MM(a,x) memset(a,x,sizeof(a));
#define ALL(x)  (x).begin(), (x).end()
#define P(x)	cerr<<"["#x<<" = "<<(x)<<"]"<<endl;
#define PP(x,i)	cerr<<"["#x<<i<<" = "<<x[i]<<"]"<<endl;
#define P2(x,y)	cerr<<"["#x" = "<<(x)<<", "#y" = "<<(y)<<"]"<<endl;
#define TM(a,b)	cerr<<"["#a" -> "#b": "<<1e3*(b-a)/CLOCKS_PER_SEC<<"ms]\n";
#define ASSERT(e) if(!(e)) {cerr<<"!!!ASSERTION FAILED!!! in #"<<__LINE__<<": " <<#e; exit(0);}
#define UN(v)   sort(ALL(v)), v.resize(unique(ALL(v))-v.begin())
#define mp make_pair
#define pb push_back
#define x first
#define y second
struct _ {_() {ios_base::sync_with_stdio(0);}} _;
template<class A, class B> ostream& operator<<(ostream &o, pair<A, B> t) {o << "(" << t.x << ", " << t.y << ")"; return o;}
template<class T> string tostring(T x, int len = 0, char c = '0') {stringstream ss; ss << x; string r = ss.str(); if(r.length() < len) r = string(len - r.length(), c) + r; return r;}
template<class T> void PV(T a, T b, int n = 0, int w = 0, string s = " ") {int c = 0; while(a != b) {cout << tostring(*a++, w, ' '); if(a != b && (n == 0 || ++c % n)) cout << s; else cout << endl;}}
template<class T> inline bool chmin(T &a, T b) {return a > b ? a = b, 1 : 0;}
template<class T> inline bool chmax(T &a, T b) {return a < b ? a = b, 1 : 0;}
const LL linf = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int mod = int(1e9) + 7;
const int N = 1111111;

int n;
LL d[N];
LL a[N];
LL v[N];

int main() {
	cin >> n;
	for(int i = 0; i < n; i++) cin >> d[i];
	LL res = 1;

	vector<LL> vv;
	for(int i = 0; i < 12; i++) vv.pb(d[rand() % n]);
	UN(vv);
	for(LL x : vv) {
		for(int i = 0; i < n; i++) v[i] = __gcd(x, d[i]);

		sort(v, v + n);

		vector<pair<LL, int>> c;
		int cc = 1;
		for(int i = 1; i <= n; i++) {
			if(i == n || v[i] != v[i - 1]) c.pb(mp(v[i - 1], cc)), cc = 1;
			else cc++;
		}

		vector<LL> factor;
		for(LL i = 1; i * i <= x; i++) {
			if(x % i == 0) {
				factor.pb(i);
				if(i * i < x) factor.pb(x / i);
			}
		}

		sort(ALL(factor));
		reverse(ALL(factor));
		for(LL i : factor) {
			if(i <= res) continue;
			int cnt = 0;
			for(auto& j : c) {
				if(j.x % i == 0) cnt += j.y;
			}
			if(2 * cnt >= n) {
				chmax(res, i);
				break;
			}
		}
	}
	cout << res << endl;
	return 0;
}


#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <vector>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <numeric>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long       LL;
typedef pair<int, int>  PII;
typedef vector<int>     VI;
typedef vector<PII>     VPII;
#define MM(a,x) memset(a,x,sizeof(a));
#define ALL(x)  (x).begin(), (x).end()
#define P(x)	cerr<<"["#x<<" = "<<(x)<<"]"<<endl;
#define PP(x,i)	cerr<<"["#x<<i<<" = "<<x[i]<<"]"<<endl;
#define P2(x,y)	cerr<<"["#x" = "<<(x)<<", "#y" = "<<(y)<<"]"<<endl;
#define TM(a,b)	cerr<<"["#a" -> "#b": "<<1e3*(b-a)/CLOCKS_PER_SEC<<"ms]\n";
#define ASSERT(e) if(!(e)) {cerr<<"!!!ASSERTION FAILED!!! in #"<<__LINE__<<": " <<#e; exit(0);}
#define UN(v)   sort(ALL(v)), v.resize(unique(ALL(v))-v.begin())
#define mp make_pair
#define pb push_back
#define x first
#define y second
struct _ {_() {ios_base::sync_with_stdio(0);}} _;
template<class A, class B> ostream& operator<<(ostream &o, pair<A, B> t) {o << "(" << t.x << ", " << t.y << ")"; return o;}
template<class T> string tostring(T x, int len = 0, char c = '0') {stringstream ss; ss << x; string r = ss.str(); if(r.length() < len) r = string(len - r.length(), c) + r; return r;}
template<class T> void PV(T a, T b, int n = 0, int w = 0, string s = " ") {int c = 0; while(a != b) {cout << tostring(*a++, w, ' '); if(a != b && (n == 0 || ++c % n)) cout << s; else cout << endl;}}
template<class T> inline bool chmin(T &a, T b) {return a > b ? a = b, 1 : 0;}
template<class T> inline bool chmax(T &a, T b) {return a < b ? a = b, 1 : 0;}
const LL linf = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int mod = int(1e9) + 7;
const int N = 1111111;

int n;
LL d[N];
LL a[N];
LL v[N];

int main() {
	cin >> n;
	for(int i = 0; i < n; i++) cin >> d[i];

	random_shuffle(d, d + n);
	LL res = 1;
	vector<LL> vv(d, d + 12);
	UN(vv);
	for(LL x : vv) {
		for(int i = 0; i < n; i++) v[i] = __gcd(x, d[i]);

		sort(v, v + n);

		vector<pair<LL, int>> c;
		int cc = 1;
		for(int i = 1; i <= n; i++) {
			if(i == n || v[i] != v[i - 1]) c.pb(mp(v[i - 1], cc)), cc = 1;
			else cc++;
		}

		vector<LL> factor;
		for(LL i = 1; i * i <= x; i++) {
			if(x % i == 0) {
				factor.pb(i);
				if(i * i < x) factor.pb(x / i);
			}
		}

		sort(ALL(factor));
		reverse(ALL(factor));
		for(LL i : factor) {
			if(i <= res) continue;
			int cnt = 0;
			for(auto& j : c) {
				if(j.x % i == 0) cnt += j.y;
			}
			if(2 * cnt >= n) {
				chmax(res, i);
				break;
			}
		}
	}
	cout << res << endl;
	return 0;
}


#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <vector>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <numeric>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long       LL;
typedef pair<int, int>  PII;
typedef vector<int>     VI;
typedef vector<PII>     VPII;
#define MM(a,x) memset(a,x,sizeof(a));
#define ALL(x)  (x).begin(), (x).end()
#define P(x)	cerr<<"["#x<<" = "<<(x)<<"]"<<endl;
#define PP(x,i)	cerr<<"["#x<<i<<" = "<<x[i]<<"]"<<endl;
#define P2(x,y)	cerr<<"["#x" = "<<(x)<<", "#y" = "<<(y)<<"]"<<endl;
#define TM(a,b)	cerr<<"["#a" -> "#b": "<<1e3*(b-a)/CLOCKS_PER_SEC<<"ms]\n";
#define ASSERT(e) if(!(e)) {cerr<<"!!!ASSERTION FAILED!!! in #"<<__LINE__<<": " <<#e; exit(0);}
#define UN(v)   sort(ALL(v)), v.resize(unique(ALL(v))-v.begin())
#define mp make_pair
#define pb push_back
#define x first
#define y second
struct _ {_() {ios_base::sync_with_stdio(0);}} _;
template<class A, class B> ostream& operator<<(ostream &o, pair<A, B> t) {o << "(" << t.x << ", " << t.y << ")"; return o;}
template<class T> string tostring(T x, int len = 0, char c = '0') {stringstream ss; ss << x; string r = ss.str(); if(r.length() < len) r = string(len - r.length(), c) + r; return r;}
template<class T> void PV(T a, T b, int n = 0, int w = 0, string s = " ") {int c = 0; while(a != b) {cout << tostring(*a++, w, ' '); if(a != b && (n == 0 || ++c % n)) cout << s; else cout << endl;}}
template<class T> inline bool chmin(T &a, T b) {return a > b ? a = b, 1 : 0;}
template<class T> inline bool chmax(T &a, T b) {return a < b ? a = b, 1 : 0;}
const LL linf = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int mod = int(1e9) + 7;
const int N = 1111111;

int n;
LL d[N];
LL a[N];
LL v[N];

int main() {
	cin >> n;
	for(int i = 0; i < n; i++) cin >> d[i];
	LL res = 1;

	vector<LL> vv;
	for(int i = 0; i < 15; i++) vv.pb(d[rand() % n]);
	UN(vv);
	for(LL x : vv) {
		for(int i = 0; i < n; i++) v[i] = __gcd(x, d[i]);

		sort(v, v + n);

		vector<pair<LL, int>> c;
		int cc = 1;
		for(int i = 1; i <= n; i++) {
			if(i == n || v[i] != v[i - 1]) c.pb(mp(v[i - 1], cc)), cc = 1;
			else cc++;
		}

		vector<LL> factor;
		for(LL i = 1; i * i <= x; i++) {
			if(x % i == 0) {
				factor.pb(i);
				if(i * i < x) factor.pb(x / i);
			}
		}

		sort(ALL(factor));
		reverse(ALL(factor));
		for(LL i : factor) {
			int cnt = 0;
			for(auto& j : c) {
				if(j.x % i == 0) cnt += j.y;
			}
			if(2 * cnt >= n) {
				chmax(res, i);
				break;
			}
		}
	}
	cout << res << endl;
	return 0;
}


#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <vector>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <numeric>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long       LL;
typedef pair<int, int>  PII;
typedef vector<int>     VI;
typedef vector<PII>     VPII;
#define MM(a,x) memset(a,x,sizeof(a));
#define ALL(x)  (x).begin(), (x).end()
#define P(x)	cerr<<"["#x<<" = "<<(x)<<"]"<<endl;
#define PP(x,i)	cerr<<"["#x<<i<<" = "<<x[i]<<"]"<<endl;
#define P2(x,y)	cerr<<"["#x" = "<<(x)<<", "#y" = "<<(y)<<"]"<<endl;
#define TM(a,b)	cerr<<"["#a" -> "#b": "<<1e3*(b-a)/CLOCKS_PER_SEC<<"ms]\n";
#define ASSERT(e) if(!(e)) {cerr<<"!!!ASSERTION FAILED!!! in #"<<__LINE__<<": " <<#e; exit(0);}
#define UN(v)   sort(ALL(v)), v.resize(unique(ALL(v))-v.begin())
#define mp make_pair
#define pb push_back
#define x first
#define y second
struct _ {_() {ios_base::sync_with_stdio(0);}} _;
template<class A, class B> ostream& operator<<(ostream &o, pair<A, B> t) {o << "(" << t.x << ", " << t.y << ")"; return o;}
template<class T> string tostring(T x, int len = 0, char c = '0') {stringstream ss; ss << x; string r = ss.str(); if(r.length() < len) r = string(len - r.length(), c) + r; return r;}
template<class T> void PV(T a, T b, int n = 0, int w = 0, string s = " ") {int c = 0; while(a != b) {cout << tostring(*a++, w, ' '); if(a != b && (n == 0 || ++c % n)) cout << s; else cout << endl;}}
template<class T> inline bool chmin(T &a, T b) {return a > b ? a = b, 1 : 0;}
template<class T> inline bool chmax(T &a, T b) {return a < b ? a = b, 1 : 0;}
const LL linf = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int mod = int(1e9) + 7;
const int N = 1111111;

int n;
LL d[N];
LL a[N];

int main() {
	cin >> n;
	for(int i = 0; i < n; i++) cin >> d[i];
	LL res = 1;

	int T = 10;
	while(T--) {
		LL x = d[rand() % n];
		vector<LL> v;
		for(int i = 0; i < n; i++) {
			v.pb(__gcd(x, d[i]));
		}

		sort(ALL(v));
		vector<int> c;
		int cc = 1;
		for(int i = 1; i <= n; i++) {
			if(i == n || v[i] != v[i - 1]) c.pb(cc), cc = 1;
			else cc++;
		}
		UN(v);
		assert(c.size() == v.size());

		vector<LL> factor;
		for(LL i = 1; i * i <= x; i++) {
			if(x % i == 0) {
				factor.pb(i);
				if(i * i < x) factor.pb(x / i);
			}
		}

		for(LL i : factor) {
			int cnt = 0;
			for(int j = 0; j < c.size(); j++) {
				if(v[j] % i == 0) cnt += c[j];
			}
			if(2 * cnt >= n) chmax(res, i);
		}
	}
	cout << res << endl;
	return 0;
}


#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <vector>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <numeric>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long       LL;
typedef pair<int, int>  PII;
typedef vector<int>     VI;
typedef vector<PII>     VPII;
#define MM(a,x) memset(a,x,sizeof(a));
#define ALL(x)  (x).begin(), (x).end()
#define P(x)	cerr<<"["#x<<" = "<<(x)<<"]"<<endl;
#define PP(x,i)	cerr<<"["#x<<i<<" = "<<x[i]<<"]"<<endl;
#define P2(x,y)	cerr<<"["#x" = "<<(x)<<", "#y" = "<<(y)<<"]"<<endl;
#define TM(a,b)	cerr<<"["#a" -> "#b": "<<1e3*(b-a)/CLOCKS_PER_SEC<<"ms]\n";
#define ASSERT(e) if(!(e)) {cerr<<"!!!ASSERTION FAILED!!! in #"<<__LINE__<<": " <<#e; exit(0);}
#define UN(v)   sort(ALL(v)), v.resize(unique(ALL(v))-v.begin())
#define mp make_pair
#define pb push_back
#define x first
#define y second
struct _ {_() {ios_base::sync_with_stdio(0);}} _;
template<class A, class B> ostream& operator<<(ostream &o, pair<A, B> t) {o << "(" << t.x << ", " << t.y << ")"; return o;}
template<class T> string tostring(T x, int len = 0, char c = '0') {stringstream ss; ss << x; string r = ss.str(); if(r.length() < len) r = string(len - r.length(), c) + r; return r;}
template<class T> void PV(T a, T b, int n = 0, int w = 0, string s = " ") {int c = 0; while(a != b) {cout << tostring(*a++, w, ' '); if(a != b && (n == 0 || ++c % n)) cout << s; else cout << endl;}}
template<class T> inline bool chmin(T &a, T b) {return a > b ? a = b, 1 : 0;}
template<class T> inline bool chmax(T &a, T b) {return a < b ? a = b, 1 : 0;}
const LL linf = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int mod = int(1e9) + 7;
const int N = 1111111;

int n;
LL d[N];
LL a[N];

int main() {
	cin >> n;
	for(int i = 0; i < n; i++) cin >> d[i];
	LL res = 1;

	int T = 10;
	while(T--) {
		LL x = d[rand() % n];
		vector<LL> v;
		for(int i = 0; i < n; i++) {
			v.pb(__gcd(x, d[i]));
		}

		sort(ALL(v));
		vector<int> c;
		int cc = 1;
		for(int i = 1; i <= n; i++) {
			if(i == n || v[i] != v[i - 1]) c.pb(cc), cc = 1;
			else cc++;
		}
		UN(v);
		assert(c.size() == v.size());

		vector<LL> factor;
		for(LL i = 1; i * i <= x; i++) {
			factor.pb(i);
			if(i * i < x) factor.pb(x / i);
		}
		for(LL i : factor) {
			int cnt = 0;
			for(int j = 0; j < c.size(); j++) {
				if(v[j] % i == 0) cnt += c[j];
			}
			if(2 * cnt >= n) chmax(res, i);
		}
	}
	cout << res << endl;
	return 0;
}


#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <vector>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <numeric>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long       LL;
typedef pair<int, int>  PII;
typedef vector<int>     VI;
typedef vector<PII>     VPII;
#define MM(a,x) memset(a,x,sizeof(a));
#define ALL(x)  (x).begin(), (x).end()
#define P(x)	cerr<<"["#x<<" = "<<(x)<<"]"<<endl;
#define PP(x,i)	cerr<<"["#x<<i<<" = "<<x[i]<<"]"<<endl;
#define P2(x,y)	cerr<<"["#x" = "<<(x)<<", "#y" = "<<(y)<<"]"<<endl;
#define TM(a,b)	cerr<<"["#a" -> "#b": "<<1e3*(b-a)/CLOCKS_PER_SEC<<"ms]\n";
#define ASSERT(e) if(!(e)) {cerr<<"!!!ASSERTION FAILED!!! in #"<<__LINE__<<": " <<#e; exit(0);}
#define UN(v)   sort(ALL(v)), v.resize(unique(ALL(v))-v.begin())
#define mp make_pair
#define pb push_back
#define x first
#define y second
struct _ {_() {ios_base::sync_with_stdio(0);}} _;
template<class A, class B> ostream& operator<<(ostream &o, pair<A, B> t) {o << "(" << t.x << ", " << t.y << ")"; return o;}
template<class T> string tostring(T x, int len = 0, char c = '0') {stringstream ss; ss << x; string r = ss.str(); if(r.length() < len) r = string(len - r.length(), c) + r; return r;}
template<class T> void PV(T a, T b, int n = 0, int w = 0, string s = " ") {int c = 0; while(a != b) {cout << tostring(*a++, w, ' '); if(a != b && (n == 0 || ++c % n)) cout << s; else cout << endl;}}
template<class T> inline bool chmin(T &a, T b) {return a > b ? a = b, 1 : 0;}
template<class T> inline bool chmax(T &a, T b) {return a < b ? a = b, 1 : 0;}
const LL linf = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int mod = int(1e9) + 7;
const int N = 1111111;

int n;
LL d[N];
LL a[N];
LL v[N];

int main() {
	cin >> n;
	for(int i = 0; i < n; i++) cin >> d[i];
	LL res = 1;

	vector<LL> vv;
	for(int i = 0; i < 15; i++) vv.pb(d[rand() % n]);
	UN(vv);
	for(LL x : vv) {
		for(int i = 0; i < n; i++) v[i] = __gcd(x, d[i]);

		sort(v, v + n);

		vector<pair<LL, int>> c;
		int cc = 1;
		for(int i = 1; i <= n; i++) {
			if(i == n || v[i] != v[i - 1]) c.pb(mp(v[i - 1], cc)), cc = 1;
			else cc++;
		}

		vector<LL> factor;
		for(LL i = 1; i * i <= x; i++) {
			if(x % i == 0) {
				factor.pb(i);
				if(i * i < x) factor.pb(x / i);
			}
		}

		sort(ALL(factor));
		reverse(ALL(factor));
		for(LL i : factor) {
			if(i <= res) continue;
			int cnt = 0;
			for(auto& j : c) {
				if(j.x % i == 0) cnt += j.y;
			}
			if(2 * cnt >= n) {
				chmax(res, i);
				break;
			}
		}
	}
	cout << res << endl;
	return 0;
}


#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <vector>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <numeric>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long       LL;
typedef pair<int, int>  PII;
typedef vector<int>     VI;
typedef vector<PII>     VPII;
#define MM(a,x) memset(a,x,sizeof(a));
#define ALL(x)  (x).begin(), (x).end()
#define P(x)	cerr<<"["#x<<" = "<<(x)<<"]"<<endl;
#define PP(x,i)	cerr<<"["#x<<i<<" = "<<x[i]<<"]"<<endl;
#define P2(x,y)	cerr<<"["#x" = "<<(x)<<", "#y" = "<<(y)<<"]"<<endl;
#define TM(a,b)	cerr<<"["#a" -> "#b": "<<1e3*(b-a)/CLOCKS_PER_SEC<<"ms]\n";
#define ASSERT(e) if(!(e)) {cerr<<"!!!ASSERTION FAILED!!! in #"<<__LINE__<<": " <<#e; exit(0);}
#define UN(v)   sort(ALL(v)), v.resize(unique(ALL(v))-v.begin())
#define mp make_pair
#define pb push_back
#define x first
#define y second
struct _ {_() {ios_base::sync_with_stdio(0);}} _;
template<class A, class B> ostream& operator<<(ostream &o, pair<A, B> t) {o << "(" << t.x << ", " << t.y << ")"; return o;}
template<class T> string tostring(T x, int len = 0, char c = '0') {stringstream ss; ss << x; string r = ss.str(); if(r.length() < len) r = string(len - r.length(), c) + r; return r;}
template<class T> void PV(T a, T b, int n = 0, int w = 0, string s = " ") {int c = 0; while(a != b) {cout << tostring(*a++, w, ' '); if(a != b && (n == 0 || ++c % n)) cout << s; else cout << endl;}}
template<class T> inline bool chmin(T &a, T b) {return a > b ? a = b, 1 : 0;}
template<class T> inline bool chmax(T &a, T b) {return a < b ? a = b, 1 : 0;}
const LL linf = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int mod = int(1e9) + 7;
const int N = 1111111;

int n;
LL d[N];
LL a[N];
LL v[N];

int main() {
	cin >> n;
	for(int i = 0; i < n; i++) cin >> d[i];
	LL res = 1;

	int T = 10;
	while(T--) {
		LL x = d[rand() % n];
		for(int i = 0; i < n; i++) v[i] = __gcd(x, d[i]);

		sort(v, v + n);

		vector<pair<LL, int>> c;
		int cc = 1;
		for(int i = 1; i <= n; i++) {
			if(i == n || v[i] != v[i - 1]) c.pb(mp(v[i - 1], cc)), cc = 1;
			else cc++;
		}

		vector<LL> factor;
		for(LL i = 1; i * i <= x; i++) {
			if(x % i == 0) {
				factor.pb(i);
				if(i * i < x) factor.pb(x / i);
			}
		}

		for(LL i : factor) {
			int cnt = 0;
			for(auto& j : c) {
				if(j.x % i == 0) cnt += j.y;
			}
			if(2 * cnt >= n) chmax(res, i);
		}
	}
	cout << res << endl;
	return 0;
}


//ITNOG
#include <bits/stdc++.h>

using namespace std;

#define int long long

const int maxN = 1000*1000 + 6;
const int mod = 1000*1000*1000 + 7;

vector<long long> ans;

long long a[maxN];
main()
{
//    ios_base::sync_with_stdio(0); cin.tie(0);
    srand(time(NULL));
    int n; scanf("%I64d", &n);
    for(int i = 0; i < n; i ++) scanf("%I64d", &a[i]);

    for(int z = 0; z < 22; z ++)
    {
	  int x = rand() % n;
	  int y = rand() % n;

	  int res = __gcd(a[x], a[y]);
	  for(long long i = 1; i * i <= res; i ++)
		if(res % i == 0)
		{
		    ans.push_back(i);
		    if(i * i < a[x]) ans.push_back(res / i);
		}
    }

    sort(ans.begin(), ans.end(), greater<int>()); ans.resize(unique(ans.begin(), ans.end()) - ans.begin());

    for(int i = 0; i < min((int)ans.size(), 80LL); i ++)
    {
	  int te = 0;
	  for(int j = 0; j < n; j ++)
		if(a[j] % ans[i] == 0) te ++;

	  if(te >= (n + 1) / 2) { printf("%I64d", ans[i]); return 0; }
    }
    reverse(ans.begin(), ans.end());

    for(int i = min((int)ans.size() - 1, 280LL); i >= 0; i --)
    {
	  int te = 0;
	  for(int j = 0; j < n; j ++)
		if(a[j] % ans[i] == 0) te ++;

	  if(te >= (n + 1) / 2) { printf("%I64d", ans[i]); return 0; }
    }

    cout << 1 << endl;
    return 0;
}


//ITNOG
#include <bits/stdc++.h>

using namespace std;

#define int long long

const int maxN = 1000*1000 + 6;
const int mod = 1000*1000*1000 + 7;

vector<long long> ans;

long long a[maxN];
main()
{
//    ios_base::sync_with_stdio(0); cin.tie(0);
    srand(time(NULL));
    int n; scanf("%I64d", &n);
    for(int i = 0; i < n; i ++) scanf("%I64d", &a[i]);

    for(int z = 0; z < 30; z ++)
    {
	  int x = rand() % n;

	  for(long long i = 1; i * i <= a[x]; i ++)
		if(a[x] % i == 0)
		{
		    ans.push_back(i);
		    if(i * i < a[x]) ans.push_back(a[x] / i);
		}
    }

    sort(ans.begin(), ans.end(), greater<int>()); ans.resize(unique(ans.begin(), ans.end()) - ans.begin());

    for(int i = 0; i < ans.size(); i ++)
    {
	  int te = 0;
	  for(int j = 0; j < n; j ++)
		if(a[j] % ans[i] == 0) te ++;

	  if(te >= (n + 1) / 2) { printf("%I64d", ans[i]); return 0; }
    }

    return 0;
}


#include<iostream>
#include<algorithm>
#include<queue>
#include<string>
#include<vector>
#include<map>
#include<list>
#include<set>
#include<cstring>
#include<ctime>
#include<intrin.h>
#include<cmath>
#include<iterator>
#include<functional>
#include<stack>
#define all(v) v.begin(), v.end()
#define setv(ar, val) memset(ar, val, sizeof(ar));
#define ll long long int
#define ull unsigned long long int
#define PI 3.1415926535897932384626433832795
#pragma comment(linker, "/STACK:1073741824")
using namespace std;

const int MAXN = 1e5 + 100, MAXD = 10;
int ar[MAXN];
set<int> have[MAXN * 4][MAXD];
ll t[MAXN * 4][MAXD], sum[MAXN * 4];
bool here[MAXN * 4];

void build(int v, int L, int R) {
	for (int i = 0; i < MAXD; i++)
		have[v][i].insert(i);
	if (L == R) {
		ll pw = 1;
		while (ar[L] > 0) {
			int d = ar[L] % 10;
			t[v][d] += pw;
			sum[v] += pw * d;
			ar[L] /= 10;
			pw *= 10;
		}
	}
	else {
		int m = (L + R) >> 1;
		build(v * 2, L, m);
		build(v * 2 + 1, m + 1, R);
		for (int i = 0; i < MAXD; i++)
			t[v][i] = t[v * 2][i] + t[v * 2 + 1][i];
		sum[v] = sum[v * 2] + sum[v * 2 + 1];
	}
}

void push(int v, bool leaf) {
	if (!here[v])
		return;
	if (!leaf) {
		for (int i = 0; i < MAXD; i++) {
			for (int p : have[v][i]) {
				have[v * 2][i].insert(all(have[v * 2][p]));
				have[v * 2 + 1][i].insert(all(have[v * 2 + 1][p]));
			}
		}
		here[v * 2] = here[v * 2 + 1] = true;
	}
	for (int i = 0; i < MAXD; i++) {
		for (int p : have[v][i]) {
			if (p == i)
				continue;
			sum[v] += (i - p) * t[v][p];
			t[v][i] += t[v][p];
			t[v][p] = 0;
		}
		have[v][i].clear();
		have[v][i].insert(i);
	}
	here[v] = false;
}

void update(int l, int r, int x, int y, int v, int L, int R) {
	if (l > R || r < L)
		return;
	if (l == L && r == R) {
		have[v][y].insert(all(have[v][x]));
		here[v] = true;
		push(v, l == r);
	}
	else {
		int m = (L + R) >> 1;
		push(v, false);
		push(v * 2, L == m);
		push(v * 2 + 1, m + 1 == R);
		update(l, min(m, r), x, y, v * 2, L, m);
		update(max(m + 1, l), r, x, y, v * 2 + 1, m + 1, R);
		for (int i = 0; i < MAXD; i++)
			t[v][i] = t[v * 2][i] + t[v * 2 + 1][i];
		sum[v] = sum[v * 2] + sum[v * 2 + 1];
	}
}

ll query(int l, int r, int v, int L, int R) {
	if (l > R || r < L)
		return 0;
	if (l == L && r == R) {
		return sum[v];
	}
	else {
		int m = (L + R) >> 1;
		push(v, false);
		push(v * 2, L == m);
		push(v * 2 + 1, m + 1 == R);
		return query(l, min(m, r), v * 2, L, m) +
			query(max(m + 1, l), r, v * 2 + 1, m + 1, R);
	}
}

int main() {
#pragma warning(disable : 4996)
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#define __builtin_popcount __popcnt
#endif
	int n, q;
	scanf("%d%d", &n, &q);

	for (int i = 0; i < n; i++) {
		scanf("%d", &ar[i]);
	}

	build(1, 0, n);

	for (int i = 0; i < q; i++) {
		int t, l, r, x, y;
		scanf("%d%d%d", &t, &l, &r);
		l--;
		r--;
		if (t == 1) {
			scanf("%d%d", &x, &y);
			if (x == y)
				continue;
			update(l, r, x, y, 1, 0, n);
		}
		else
			printf("%lld\n", query(l, r, 1, 0, n));
	}

	return 0;
}


#include<iostream>
#include<algorithm>
#include<queue>
#include<string>
#include<vector>
#include<map>
#include<list>
#include<set>
#include<cstring>
#include<ctime>
#include<intrin.h>
#include<cmath>
#include<iterator>
#include<functional>
#include<stack>
#define all(v) v.begin(), v.end()
#define setv(ar, val) memset(ar, val, sizeof(ar));
#define ll long long int
#define ull unsigned long long int
#define PI 3.1415926535897932384626433832795
#pragma comment(linker, "/STACK:1073741824")
using namespace std;

const int MAXN = 1e5 + 100, CP = 320, P = 320, MAXD = 10;
int ar[MAXN][10], fl[MAXN][10], nn[CP][MAXD], len[MAXN], par[CP][MAXD];
ll t[CP][MAXD], sum[CP];

void handle(int l, int r, int x, int y) {
	int p = l / P;
	for (int i = l; i <= r; i++) {
		ll pw = 1;
		for (int j = 0; j < len[i]; j++, pw *= 10) {
			int d = ar[i][j];
			if (nn[p][d] > fl[i][j])
				d = par[p][d];
			if (d == x) {
				ar[i][j] = y;
				fl[i][j] = nn[p][y];
				sum[p] += (y - x) * pw;
				t[p][x] -= pw;
				t[p][y] += pw;
			}
		}
	}
}

ll answer(int l, int r) {
	ll ans = 0;
	int p = l / P;
	for (int i = l; i <= r; i++) {
		ll pw = 1;
		for (int j = 0; j < len[i]; j++, pw *= 10) {
			int d = ar[i][j];
			if (nn[p][d] > fl[i][j])
				d = par[p][d];
			ans += pw * d;
		}
	}
	return ans;
}

void update(int l, int r, int x, int y) {
	int lp = l / P, rp = r / P;
	for (int i = lp + 1; i < rp; i++) {
		for (int j = 0; j < MAXD; j++) {
			if (par[i][j] == x) {
				nn[i][j]++;
				par[i][j] = y;
			}
		}
		sum[i] += (y - x) * t[i][x];
		t[i][y] += t[i][x];
		t[i][x] = 0;
	}
	if (lp == rp)
		handle(l, r, x, y);
	else {
		handle(l, (lp + 1) * P - 1, x, y);
		handle(rp * P, r, x, y);
	}
}

ll query(int l, int r) {
	int lp = l / P, rp = r / P;
	ll ans = 0;
	for (int i = lp + 1; i < rp; i++)
		ans += sum[i];
	if (lp == rp)
		ans += answer(l, r);
	else
		ans += answer(l, (lp + 1) * P - 1) + answer(rp * P, r);
	return ans;
}

int main() {
#pragma warning(disable : 4996)
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#define __builtin_popcount __popcnt
#endif
	int n, q;
	scanf("%d%d", &n, &q);

	for (int i = 0; i < n; i++) {
		int d = 1, p = i / P, a;
		scanf("%d", &a);
		while (a) {
			ar[i][len[i]] = a % 10;
			t[p][ar[i][len[i]]] += d;
			sum[p] += d * ar[i][len[i]];
			a /= 10;
			d *= 10;
			len[i]++;
		}
	}

	for (int i = 0; i < CP; i++)
		for (int j = 0; j < MAXD; j++)
			par[i][j] = j;

	for (int i = 0; i < q; i++) {
		int t, l, r, x, y;
		scanf("%d%d%d", &t, &l, &r);
		l--;
		r--;
		if (t == 1) {
			scanf("%d%d", &x, &y);
			update(l, r, x, y);
		}
		else
			printf("%lld\n", query(l, r));
	}

	return 0;
}


#include "bits/stdc++.h"
#define MAXN 100009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
using namespace std;

typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
int arr[MAXN];
ll tmp[11];
struct node{
	ll val[11];
	node(){
		memset(val,0,sizeof val);
	}
}s[MAXN<<2];
struct gothere{
	int link[11];
}lazy[MAXN<<2];
void build(int nd,int x,int y){
	for(int i=0;i<10;i++)
		lazy[nd].link[i]=i;
	if(x==y){
		ll pw=1;
		while(arr[x]>=1){
			s[nd].val[arr[x]%10]+=pw;
			pw*=10;
			arr[x]/=10;
		}
		return;
	}
	int mid=(x+y)>>1;
	build(nd<<1,x,mid);
	build(nd<<1|1,mid+1,y);
	for(int i=0;i<10;i++)
		s[nd].val[i]=s[nd<<1].val[i]+s[nd<<1|1].val[i];
}
void plant(int nd,gothere x){
	for(int i=0;i<10;i++)
		lazy[nd].link[i]=x.link[lazy[nd].link[i]];
}
void shift(int nd,int x,int y){
	if(x!=y){
		plant(nd<<1,lazy[nd]);
		plant(nd<<1|1,lazy[nd]);
	}
	for(int i=0;i<10;i++){
		tmp[i]=s[nd].val[i];
		s[nd].val[i]=0;
	}
	for(int i=0;i<10;i++)
		s[nd].val[lazy[nd].link[i]]+=tmp[i];
	for(int i=0;i<10;i++)
		lazy[nd].link[i]=i;
}
ll tap(int l,int r,int nd,int x,int y){
	shift(nd,x,y);
	if(l>y or x>r)
		return 0;
	if(l<=x and y<=r){
		ll res=0;
		for(int i=0;i<10;i++)
			res+=s[nd].val[i]*1LL*i;
		return res;
	}
	int mid=(x+y)>>1;
	return tap(l,r,nd<<1,x,mid)+tap(l,r,nd<<1|1,mid+1,y);
}
void change(int l,int r,int from,int to,int nd,int x,int y){
	shift(nd,x,y);
	if(l>y or x>r)
		return;
	if(l<=x and y<=r){
		lazy[nd].link[from]=to;
		shift(nd,x,y);
		return;
	}
	int mid=(x+y)>>1;
	change(l,r,from,to,nd<<1,x,mid);
	change(l,r,from,to,nd<<1|1,mid+1,y);
	for(int i=0;i<10;i++)
		s[nd].val[i]=s[nd<<1].val[i]+s[nd<<1|1].val[i];
}
int main(){
	#ifndef ONLINE_JUDGE
		freopen("file.in", "r", stdin);
	#endif
	int n,q;
	scanf("%d%d",&n,&q);
	for(int i=1;i<=n;i++)
		scanf("%d",arr+i);
	build(1,1,n);
	while(q--){
		int type,l,r;
		scanf("%d%d%d",&type,&l,&r);
		if(type==2)
			printf("%lld\n",tap(l,r,1,1,n));
		else{
			int x,y;
			scanf("%d%d",&x,&y);
			if(x!=y)
				change(l,r,x,y,1,1,n);
		}
	}
	return 0;
}


#include "bits/stdc++.h"
#define MAXN 100009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
using namespace std;

typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
int arr[MAXN];
ll tmp[11];
struct node{
	ll val[11];
	node(){
		memset(val,0,sizeof val);
	}
}s[MAXN<<2];
struct gothere{
	int link[11];
}lazy[MAXN<<2];
void build(int nd,int x,int y){
	for(int i=0;i<10;i++)
		lazy[nd].link[i]=i;
	if(x==y){
		ll pw=1;
		while(arr[x]>=1){
			s[nd].val[arr[x]%10]+=pw;
			pw*=10;
			arr[x]/=10;
		}
		return;
	}
r	int mid=(x+y)>>1;
	build(nd<<1,x,mid);
	build(nd<<1|1,mid+1,y);
	for(int i=0;i<10;i++)
		s[nd].val[i]=s[nd<<1].val[i]+s[nd<<1|1].val[i];
}
void plant(int nd,gothere x){
	for(int i=0;i<10;i++)
		lazy[nd].link[i]=x.link[lazy[nd].link[i]];
}
void shift(int nd){
	plant(nd<<1,lazy[nd]);
	plant(nd<<1|1,lazy[nd]);
	for(int i=0;i<10;i++){
		tmp[i]=s[nd].val[i];
		s[nd].val[i]=0;
	}
	for(int i=0;i<10;i++)
		s[nd].val[lazy[nd].link[i]]+=tmp[i];
	for(int i=0;i<10;i++)
		lazy[nd].link[i]=i;
}
ll tap(int l,int r,int nd,int x,int y){
	shift(nd);
	if(l>y or x>r)
		return 0;
	if(l<=x and y<=r){
		ll res=0;
		for(int i=0;i<10;i++)
			res+=s[nd].val[i]*1LL*i;
		return res;
	}
	int mid=(x+y)>>1;
	return tap(l,r,nd<<1,x,mid)+tap(l,r,nd<<1|1,mid+1,y);
}
void change(int l,int r,int from,int to,int nd,int x,int y){
	shift(nd);
	if(l>y or x>r)
		return;
	if(l<=x and y<=r){
		lazy[nd].link[from]=to;
		shift(nd);
		return;
	}
	int mid=(x+y)>>1;
	change(l,r,from,to,nd<<1,x,mid);
	change(l,r,from,to,nd<<1|1,mid+1,y);
	for(int i=0;i<10;i++)
		s[nd].val[i]=s[nd<<1].val[i]+s[nd<<1|1].val[i];
}
int main(){
	#ifndef ONLINE_JUDGE
		freopen("file.in", "r", stdin);
	#endif
	int n,q;
	scanf("%d%d",&n,&q);
	for(int i=1;i<=n;i++)
		scanf("%d",arr+i);
	build(1,1,n);
	while(q--){
		int type,l,r;
		scanf("%d%d%d",&type,&l,&r);
		if(type==2)
			printf("%lld\n",tap(l,r,1,1,n));
		else{
			int x,y;
			scanf("%d%d",&x,&y);
			if(x!=y)
				change(l,r,x,y,1,1,n);
		}
	}
	return 0;
}


#include "bits/stdc++.h"
#define MAXN 100009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
using namespace std;

typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
int arr[MAXN];
ll tmp[11];
struct node{
	int link[11];
	ll val[11];
	node(){
		memset(val,0,sizeof val);
	}
}s[MAXN<<2],lazy[MAXN<<2];
void build(int nd,int x,int y){
	for(int i=0;i<10;i++)
		s[nd].link[i]=lazy[nd].link[i]=i;
	if(x==y){
		ll pw=1;
		while(arr[x]>=1){
			s[nd].val[arr[x]%10]+=pw;
			pw*=10;
			arr[x]/=10;
		}
		return;
	}
	int mid=(x+y)>>1;
	build(nd<<1,x,mid);
	build(nd<<1|1,mid+1,y);
	for(int i=0;i<10;i++)
		s[nd].val[i]=s[nd<<1].val[i]+s[nd<<1|1].val[i];
}
void plant(int nd,node x){
	memcpy(tmp,s[nd].val,sizeof s[nd].val);
	for(int i=0;i<10;i++){
		lazy[nd].link[i]=x.link[lazy[nd].link[i]];
		s[nd].link[i]=x.link[s[nd].link[i]];
	}
	memset(s[nd].val,0,sizeof s[nd].val);
	for(int i=0;i<10;i++)
		s[nd].val[s[nd].link[i]]+=tmp[i];
}
void shift(int nd){
	plant(nd<<1,lazy[nd]);
	plant(nd<<1|1,lazy[nd]);
	for(int i=0;i<10;i++)
		lazy[nd].link[i]=i;
}
ll tap(int l,int r,int nd,int x,int y){
	if(l>y or x>r)
		return 0;
	if(l<=x and y<=r){
		ll res=0;
		for(int i=0;i<10;i++)
			res+=s[nd].val[i]*1LL*i;
		return res;
	}
	shift(nd);
	int mid=(x+y)>>1;
	return tap(l,r,nd<<1,x,mid)+tap(l,r,nd<<1|1,mid+1,y);
}
void change(int l,int r,int from,int to,int nd,int x,int y){
	if(l>y or x>r)
		return;
	if(l<=x and y<=r){
		lazy[nd].link[from]=to;
		plant(nd,lazy[nd]);
		return;
	}
	shift(nd);
	int mid=(x+y)>>1;
	change(l,r,from,to,nd<<1,x,mid);
	change(l,r,from,to,nd<<1|1,mid+1,y);
	for(int i=0;i<10;i++)
		s[nd].val[i]=s[nd<<1].val[i]+s[nd<<1|1].val[i];
}
int main(){
	#ifndef ONLINE_JUDGE
		freopen("file.in", "r", stdin);
	#endif
	int n,q;
	scanf("%d%d",&n,&q);
	for(int i=1;i<=n;i++)
		cin>>arr[i];
	build(1,1,n);
	while(q--){
		int type,l,r;
		scanf("%d%d%d",&type,&l,&r);
		if(type==2)
			printf("%lld\n",tap(l,r,1,1,n));
		else{
			int x,y;
			scanf("%d%d",&x,&y);
			if(x!=y)
				change(l,r,x,y,1,1,n);
		}
	}
	return 0;
}


#include "bits/stdc++.h"
#define MAXN 100009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
using namespace std;

typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
int arr[MAXN];
ll tmp[11];
struct node{
	ll val[11];
	node(){
		memset(val,0,sizeof val);
	}
}s[MAXN<<2];
struct gothere{
	int link[11];
}lazy[MAXN<<2];
void build(int nd,int x,int y){
	for(int i=0;i<10;i++)
		lazy[nd].link[i]=i;
	if(x==y){
		ll pw=1;
		while(arr[x]>=1){
			s[nd].val[arr[x]%10]+=pw;
			pw*=10;
			arr[x]/=10;
		}
		return;
	}
	int mid=(x+y)>>1;
	build(nd<<1,x,mid);
	build(nd<<1|1,mid+1,y);
	for(int i=0;i<10;i++)
		s[nd].val[i]=s[nd<<1].val[i]+s[nd<<1|1].val[i];
}
void plant(int nd,gothere x){
	for(int i=0;i<10;i++){
		tmp[i]=s[nd].val[i];
		s[nd].val[i]=0;
	}
	for(int i=0;i<10;i++)
		lazy[nd].link[i]=x.link[lazy[nd].link[i]];
	for(int i=0;i<10;i++)
		s[nd].val[lazy[nd].link[i]]+=tmp[i];
}
void shift(int nd){
	plant(nd<<1,lazy[nd]);
	plant(nd<<1|1,lazy[nd]);
	for(int i=0;i<10;i++)
		lazy[nd].link[i]=i;
}
ll tap(int l,int r,int nd,int x,int y){
	if(x!=y)
		shift(nd);
	else{
		plant(nd,lazy[nd]);
		for(int i=0;i<10;i++)
			lazy[nd].link[i]=i;
	}
	if(l>y or x>r)
		return 0;
	if(l<=x and y<=r){
		ll res=0;
		for(int i=0;i<10;i++)
			res+=s[nd].val[i]*1LL*i;
		return res;
	}
	int mid=(x+y)>>1;
	return tap(l,r,nd<<1,x,mid)+tap(l,r,nd<<1|1,mid+1,y);
}
void change(int l,int r,int from,int to,int nd,int x,int y){
	if(x!=y)
		shift(nd);
	else{
		plant(nd,lazy[nd]);
		for(int i=0;i<10;i++)
			lazy[nd].link[i]=i;
	}
	if(l>y or x>r)
		return;
	if(l<=x and y<=r){
		lazy[nd].link[from]=to;
		plant(nd,lazy[nd]);
		return;
	}
	int mid=(x+y)>>1;
	change(l,r,from,to,nd<<1,x,mid);
	change(l,r,from,to,nd<<1|1,mid+1,y);
	for(int i=0;i<10;i++)
		s[nd].val[i]=s[nd<<1].val[i]+s[nd<<1|1].val[i];
}
int main(){
	#ifndef ONLINE_JUDGE
		freopen("file.in", "r", stdin);
	#endif
	int n,q;
	scanf("%d%d",&n,&q);
	for(int i=1;i<=n;i++)
		scanf("%d",arr+i);
	build(1,1,n);
	while(q--){
		int type,l,r;
		scanf("%d%d%d",&type,&l,&r);
		if(type==2)
			printf("%lld\n",tap(l,r,1,1,n));
		else{
			int x,y;
			scanf("%d%d",&x,&y);
			if(x!=y)
				change(l,r,x,y,1,1,n);
		}
	}
	return 0;
}


#include "bits/stdc++.h"
#define MAXN 100009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
using namespace std;

typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
int arr[MAXN];
ll tmp[11];
struct node{
	int link[11];
	ll val[11];
	node(){
		memset(val,0,sizeof val);
	}
}s[MAXN<<2],lazy[MAXN<<2];
void build(int nd,int x,int y){
	for(int i=0;i<10;i++)
		s[nd].link[i]=lazy[nd].link[i]=i;
	if(x==y){
		ll pw=1;
		while(arr[x]>=1){
			s[nd].val[arr[x]%10]+=pw;
			pw*=10;
			arr[x]/=10;
		}
		return;
	}
	int mid=(x+y)>>1;
	build(nd<<1,x,mid);
	build(nd<<1|1,mid+1,y);
	for(int i=0;i<10;i++)
		s[nd].val[i]=s[nd<<1].val[i]+s[nd<<1|1].val[i];
}
void plant(int nd,node x){
	for(int i=0;i<10;i++){
		tmp[i]=s[nd].val[i];
		s[nd].val[i]=0;
	}
	for(int i=0;i<10;i++){
		lazy[nd].link[i]=x.link[lazy[nd].link[i]];
		s[nd].link[i]=x.link[s[nd].link[i]];
	}
	for(int i=0;i<10;i++)
		s[nd].val[s[nd].link[i]]+=tmp[i];
}
void shift(int nd){
	plant(nd<<1,lazy[nd]);
	plant(nd<<1|1,lazy[nd]);
	for(int i=0;i<10;i++)
		lazy[nd].link[i]=i;
}
ll tap(int l,int r,int nd,int x,int y){
	if(x!=y)
		shift(nd);
	if(l>y or x>r)
		return 0;
	if(l<=x and y<=r){
		ll res=0;
		for(int i=0;i<10;i++)
			res+=s[nd].val[i]*1LL*i;
		return res;
	}
	int mid=(x+y)>>1;
	return tap(l,r,nd<<1,x,mid)+tap(l,r,nd<<1|1,mid+1,y);
}
void change(int l,int r,int from,int to,int nd,int x,int y){
	if(x!=y)
		shift(nd);
	if(l>y or x>r)
		return;
	if(l<=x and y<=r){
		lazy[nd].link[from]=to;
		plant(nd,lazy[nd]);
		return;
	}
	int mid=(x+y)>>1;
	change(l,r,from,to,nd<<1,x,mid);
	change(l,r,from,to,nd<<1|1,mid+1,y);
	for(int i=0;i<10;i++)
		s[nd].val[i]=s[nd<<1].val[i]+s[nd<<1|1].val[i];
}
int main(){
	#ifndef ONLINE_JUDGE
		freopen("file.in", "r", stdin);
	#endif
	int n,q;
	scanf("%d%d",&n,&q);
	for(int i=1;i<=n;i++)
		scanf("%d",arr+i);
	build(1,1,n);
	while(q--){
		int type,l,r;
		scanf("%d%d%d",&type,&l,&r);
		if(type==2)
			printf("%lld\n",tap(l,r,1,1,n));
		else{
			int x,y;
			scanf("%d%d",&x,&y);
			if(x!=y)
				change(l,r,x,y,1,1,n);
		}
	}
	return 0;
}


#include "bits/stdc++.h"
#define MAXN 100009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
using namespace std;

typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
int arr[MAXN];
ll tmp[11];
struct node{
	int link[11];
	ll val[11];
	node(){
		memset(val,0,sizeof val);
	}
}s[MAXN<<2],lazy[MAXN<<2];
void build(int nd,int x,int y){
	for(int i=0;i<10;i++)
		s[nd].link[i]=lazy[nd].link[i]=i;
	if(x==y){
		ll pw=1;
		while(arr[x]>=1){
			s[nd].val[arr[x]%10]+=pw;
			pw*=10;
			arr[x]/=10;
		}
		return;
	}
	int mid=(x+y)>>1;
	build(nd<<1,x,mid);
	build(nd<<1|1,mid+1,y);
	for(int i=0;i<10;i++)
		s[nd].val[i]=s[nd<<1].val[i]+s[nd<<1|1].val[i];
}
void plant(int nd,node x){
	for(int i=0;i<10;i++){
		tmp[i]=s[nd].val[i];
		s[nd].val[i]=0;
	}
	for(int i=0;i<10;i++){
		lazy[nd].link[i]=x.link[lazy[nd].link[i]];
		s[nd].link[i]=x.link[s[nd].link[i]];
	}
	for(int i=0;i<10;i++)
		s[nd].val[s[nd].link[i]]+=tmp[i];
}
void shift(int nd){
	plant(nd<<1,lazy[nd]);
	plant(nd<<1|1,lazy[nd]);
	for(int i=0;i<10;i++)
		lazy[nd].link[i]=i;
}
ll tap(int l,int r,int nd,int x,int y){
	if(l>y or x>r)
		return 0;
	if(l<=x and y<=r){
		ll res=0;
		for(int i=0;i<10;i++)
			res+=s[nd].val[i]*1LL*i;
		return res;
	}
	shift(nd);
	int mid=(x+y)>>1;
	return tap(l,r,nd<<1,x,mid)+tap(l,r,nd<<1|1,mid+1,y);
}
void change(int l,int r,int from,int to,int nd,int x,int y){
	shift(nd);
	if(l>y or x>r)
		return;
	if(l<=x and y<=r){
		lazy[nd].link[from]=to;
		plant(nd,lazy[nd]);
		return;
	}
	int mid=(x+y)>>1;
	change(l,r,from,to,nd<<1,x,mid);
	change(l,r,from,to,nd<<1|1,mid+1,y);
	for(int i=0;i<10;i++)
		s[nd].val[i]=s[nd<<1].val[i]+s[nd<<1|1].val[i];
}
int main(){
	#ifndef ONLINE_JUDGE
		freopen("file.in", "r", stdin);
	#endif
	int n,q;
	scanf("%d%d",&n,&q);
	for(int i=1;i<=n;i++)
		scanf("%d",arr+i);
	build(1,1,n);
	while(q--){
		int type,l,r;
		scanf("%d%d%d",&type,&l,&r);
		if(type==2)
			printf("%lld\n",tap(l,r,1,1,n));
		else{
			int x,y;
			scanf("%d%d",&x,&y);
			if(x!=y)
				change(l,r,x,y,1,1,n);
		}
	}
	return 0;
}


#include "bits/stdc++.h"
#define MAXN 100009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
using namespace std;

typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
int arr[MAXN];
ll tmp[11];
struct node{
	int link[11];
	ll val[11];
	node(){
		memset(val,0,sizeof val);
	}
}s[MAXN<<2],lazy[MAXN<<2];
void build(int nd,int x,int y){
	for(int i=0;i<10;i++)
		s[nd].link[i]=lazy[nd].link[i]=i;
	if(x==y){
		ll pw=1;
		while(arr[x]>=1){
			s[nd].val[arr[x]%10]+=pw;
			pw*=10;
			arr[x]/=10;
		}
		return;
	}
	int mid=(x+y)>>1;
	build(nd<<1,x,mid);
	build(nd<<1|1,mid+1,y);
	for(int i=0;i<10;i++)
		s[nd].val[i]=s[nd<<1].val[i]+s[nd<<1|1].val[i];
}
void plant(int nd,node x){
	for(int i=0;i<10;i++){
		tmp[i]=s[nd].val[i];
		s[nd].val[i]=0;
	}
	for(int i=0;i<10;i++){
		lazy[nd].link[i]=x.link[lazy[nd].link[i]];
		s[nd].link[i]=x.link[s[nd].link[i]];
	}
	for(int i=0;i<10;i++)
		s[nd].val[s[nd].link[i]]+=tmp[i];
}
void shift(int nd){
	plant(nd<<1,lazy[nd]);
	plant(nd<<1|1,lazy[nd]);
	for(int i=0;i<10;i++)
		lazy[nd].link[i]=i;
}
ll tap(int l,int r,int nd,int x,int y){
	if(l>y or x>r)
		return 0;
	if(l<=x and y<=r){
		ll res=0;
		for(int i=0;i<10;i++)
			res+=s[nd].val[i]*1LL*i;
		return res;
	}
	shift(nd);
	int mid=(x+y)>>1;
	return tap(l,r,nd<<1,x,mid)+tap(l,r,nd<<1|1,mid+1,y);
}
void change(int l,int r,int from,int to,int nd,int x,int y){
	if(x!=y)
		shift(nd);
	if(l>y or x>r)
		return;
	if(l<=x and y<=r){
		lazy[nd].link[from]=to;
		plant(nd,lazy[nd]);
		return;
	}
	int mid=(x+y)>>1;
	change(l,r,from,to,nd<<1,x,mid);
	change(l,r,from,to,nd<<1|1,mid+1,y);
	for(int i=0;i<10;i++)
		s[nd].val[i]=s[nd<<1].val[i]+s[nd<<1|1].val[i];
}
int main(){
	#ifndef ONLINE_JUDGE
		freopen("file.in", "r", stdin);
	#endif
	int n,q;
	scanf("%d%d",&n,&q);
	for(int i=1;i<=n;i++)
		scanf("%d",arr+i);
	build(1,1,n);
	while(q--){
		int type,l,r;
		scanf("%d%d%d",&type,&l,&r);
		if(type==2)
			printf("%lld\n",tap(l,r,1,1,n));
		else{
			int x,y;
			scanf("%d%d",&x,&y);
			if(x!=y)
				change(l,r,x,y,1,1,n);
		}
	}
	return 0;
}


#include "bits/stdc++.h"
#define MAXN 100009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
using namespace std;

typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
int arr[MAXN];
ll tmp[11];
struct node{
	ll val[11];
	node(){
		memset(val,0,sizeof val);
	}
}s[MAXN<<2];
struct gothere{
	int link[11];
}lazy[MAXN<<2];
void build(int nd,int x,int y){
	for(int i=0;i<10;i++)
		lazy[nd].link[i]=i;
	if(x==y){
		ll pw=1;
		while(arr[x]>=1){
			s[nd].val[arr[x]%10]+=pw;
			pw*=10;
			arr[x]/=10;
		}
		return;
	}
	int mid=(x+y)>>1;
	build(nd<<1,x,mid);
	build(nd<<1|1,mid+1,y);
	for(int i=0;i<10;i++)
		s[nd].val[i]=s[nd<<1].val[i]+s[nd<<1|1].val[i];
}
void plant(int nd,gothere x){
	for(int i=0;i<10;i++)
		lazy[nd].link[i]=x.link[lazy[nd].link[i]];
}
void shift(int nd){
	plant(nd<<1,lazy[nd]);
	plant(nd<<1|1,lazy[nd]);
	for(int i=0;i<10;i++){
		tmp[i]=s[nd].val[i];
		s[nd].val[i]=0;
	}
	for(int i=0;i<10;i++)
		s[nd].val[lazy[nd].link[i]]+=tmp[i];
	for(int i=0;i<10;i++)
		lazy[nd].link[i]=i;
}
ll tap(int l,int r,int nd,int x,int y){
	shift(nd);
	if(l>y or x>r)
		return 0;
	if(l<=x and y<=r){
		ll res=0;
		for(int i=0;i<10;i++)
			res+=s[nd].val[i]*1LL*i;
		return res;
	}
	int mid=(x+y)>>1;
	return tap(l,r,nd<<1,x,mid)+tap(l,r,nd<<1|1,mid+1,y);
}
void change(int l,int r,int from,int to,int nd,int x,int y){
	shift(nd);
	if(l>y or x>r)
		return;
	if(l<=x and y<=r){
		lazy[nd].link[from]=to;
		shift(nd);
		return;
	}
	int mid=(x+y)>>1;
	change(l,r,from,to,nd<<1,x,mid);
	change(l,r,from,to,nd<<1|1,mid+1,y);
	for(int i=0;i<10;i++)
		s[nd].val[i]=s[nd<<1].val[i]+s[nd<<1|1].val[i];
}
int main(){
	#ifndef ONLINE_JUDGE
		freopen("file.in", "r", stdin);
	#endif
	int n,q;
	scanf("%d%d",&n,&q);
	for(int i=1;i<=n;i++)
		scanf("%d",arr+i);
	build(1,1,n);
	while(q--){
		int type,l,r;
		scanf("%d%d%d",&type,&l,&r);
		if(type==2)
			printf("%lld\n",tap(l,r,1,1,n));
		else{
			int x,y;
			scanf("%d%d",&x,&y);
			if(x!=y)
				change(l,r,x,y,1,1,n);
		}
	}
	return 0;
}


#include "bits/stdc++.h"
#define MAXN 100009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
using namespace std;

typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
int arr[MAXN];
ll tmp[11];
struct node{
	int link[11];
	ll val[11];
	node(){
		memset(val,0,sizeof val);
	}
}s[MAXN<<2],lazy[MAXN<<2];
void build(int nd,int x,int y){
	for(int i=0;i<10;i++)
		s[nd].link[i]=lazy[nd].link[i]=i;
	if(x==y){
		ll pw=1;
		while(arr[x]>=1){
			s[nd].val[arr[x]%10]+=pw;
			pw*=10;
			arr[x]/=10;
		}
		return;
	}
	int mid=(x+y)>>1;
	build(nd<<1,x,mid);
	build(nd<<1|1,mid+1,y);
	for(int i=0;i<10;i++)
		s[nd].val[i]=s[nd<<1].val[i]+s[nd<<1|1].val[i];
}
void plant(int nd,node x){
	for(int i=0;i<10;i++){
		tmp[i]=s[nd].val[i];
		s[nd].val[i]=0;
	}
	for(int i=0;i<10;i++){
		lazy[nd].link[i]=x.link[lazy[nd].link[i]];
		s[nd].link[i]=x.link[s[nd].link[i]];
	}
	for(int i=0;i<10;i++)
		s[nd].val[s[nd].link[i]]+=tmp[i];
}
void shift(int nd){
	plant(nd<<1,lazy[nd]);
	plant(nd<<1|1,lazy[nd]);
	for(int i=0;i<10;i++)
		lazy[nd].link[i]=i;
}
ll tap(int l,int r,int nd,int x,int y){
	if(l>y or x>r)
		return 0;
	if(l<=x and y<=r){
		ll res=0;
		for(int i=0;i<10;i++)
			res+=s[nd].val[i]*1LL*i;
		return res;
	}
	shift(nd);
	int mid=(x+y)>>1;
	return tap(l,r,nd<<1,x,mid)+tap(l,r,nd<<1|1,mid+1,y);
}
void change(int l,int r,int from,int to,int nd,int x,int y){
	if(l>y or x>r)
		return;
	if(l<=x and y<=r){
		lazy[nd].link[from]=to;
		plant(nd,lazy[nd]);
		return;
	}
	shift(nd);
	int mid=(x+y)>>1;
	change(l,r,from,to,nd<<1,x,mid);
	change(l,r,from,to,nd<<1|1,mid+1,y);
	for(int i=0;i<10;i++)
		s[nd].val[i]=s[nd<<1].val[i]+s[nd<<1|1].val[i];
}
int main(){
	#ifndef ONLINE_JUDGE
		freopen("file.in", "r", stdin);
	#endif
	int n,q;
	scanf("%d%d",&n,&q);
	for(int i=1;i<=n;i++)
		scanf("%d",arr+i);
	build(1,1,n);
	while(q--){
		int type,l,r;
		scanf("%d%d%d",&type,&l,&r);
		if(type==2)
			printf("%lld\n",tap(l,r,1,1,n));
		else{
			int x,y;
			scanf("%d%d",&x,&y);
			if(x!=y)
				change(l,r,x,y,1,1,n);
		}
	}
	return 0;
}


#include <vector>
#include <iostream>
#include <algorithm>
#include <cassert>

using namespace std;

const int kMaxN = 400000;

long long Count[kMaxN][10];
int Remap[kMaxN][10];
bool Remapped[kMaxN][10];

void pull(int node) {
    for (int i = 0; i < 10; ++i)
        Count[node][i] = Count[node * 2][i] + Count[node * 2 + 1][i];
}

void push(int node, int b, int e) {
    static long long aux[10];
    memset(aux, 0, sizeof(aux));

    for (int i = 0; i < 10; ++i) {
        aux[Remap[node][i]] += Count[node][i];
        if (Remapped[node][i] && b < e) {
            Remap[node * 2][i] = Remap[node][i];
            Remap[node * 2 + 1][i] = Remap[node][i];
            Remapped[node * 2][i] = Remapped[node * 2 + 1][i] = true;
        }
        Remap[node][i] = i;
        Remapped[node][i] = false;
    }

    memcpy(Count[node], aux, sizeof(aux));
}

void treeInit(int node, int b, int e, vector<int> &V) {
    for (int i = 0; i < 10; ++i)
        Remap[node][i] = i;

    if (b == e) {
        int no = V[b], pw = 1;
        while (no) {
            int dig = no % 10; no /= 10;
            Count[node][dig] += pw; pw *= 10;
        }
    } else {
        int m = (b + e) / 2;
        treeInit(node * 2, b, m, V);
        treeInit(node * 2 + 1, m + 1, e, V);
        pull(node);
    }
}

void treeUpdate(int node, int b, int e, int l, int r, int x, int y) {
    push(node, b, e);
    if (l > r) return;

    if (b == l && e == r) {
        Remap[node][x] = y;
        Remapped[node][x] = true;
        push(node, b, e);
    } else {
        int m = (b + e) / 2;
        treeUpdate(node * 2, b, m, l, min(r, m), x, y);
        treeUpdate(node * 2 + 1, m + 1, e, max(l, m + 1), r, x, y);
        pull(node);
    }
}

long long treeQuery(int node, int b, int e, int l, int r) {
    push(node, b, e);
    if (l > r) return 0;

    if (b == l && e == r) {
        long long ans = 0;
        for (int i = 0; i < 10; ++i) {
            ans += Count[node][i] * i;
        }
        return ans;
    }

    int m = (b + e) / 2;
    return treeQuery(node * 2, b, m, l, min(r, m)) +
        treeQuery(node * 2 + 1, m + 1, e, max(m + 1, l), r);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, m; cin >> n >> m;

    vector<int> V(n);
    for (int i = 0; i < n; ++i) {
        cin >> V[i];
    }

    treeInit(1, 0, n - 1, V);

    while (m--) {
        int type, l, r;
        cin >> type >> l >> r;
        --l; --r;

        if (type == 1) {
            int x, y; cin >> x >> y;
            treeUpdate(1, 0, n - 1, l, r, x, y);
        } else {
            cout << treeQuery(1, 0, n - 1, l, r) << '\n';
        }
    }
    return 0;
}


#include <vector>
#include <iostream>
#include <algorithm>
#include <cassert>
#include <cstring>

using namespace std;

const int kMaxN = 400000;

long long Count[kMaxN][10];
int Remap[kMaxN][10];
bool Remapped[kMaxN][10];

void pull(int node) {
    for (int i = 0; i < 10; ++i)
        Count[node][i] = Count[node * 2][i] + Count[node * 2 + 1][i];
}

void push(int node, int b, int e) {
    static long long aux[10];
    memset(aux, 0, sizeof(aux));

    for (int i = 0; i < 10; ++i) {
        aux[Remap[node][i]] += Count[node][i];
        if (Remapped[node][i] && b < e) {
            Remap[node * 2][i] = Remap[node][i];
            Remap[node * 2 + 1][i] = Remap[node][i];
            Remapped[node * 2][i] = Remapped[node * 2 + 1][i] = true;
        }
        Remap[node][i] = i;
        Remapped[node][i] = false;
    }

    memcpy(Count[node], aux, sizeof(aux));
}

void treeInit(int node, int b, int e, vector<int> &V) {
    for (int i = 0; i < 10; ++i)
        Remap[node][i] = i;

    if (b == e) {
        int no = V[b], pw = 1;
        while (no) {
            int dig = no % 10; no /= 10;
            Count[node][dig] += pw; pw *= 10;
        }
    } else {
        int m = (b + e) / 2;
        treeInit(node * 2, b, m, V);
        treeInit(node * 2 + 1, m + 1, e, V);
        pull(node);
    }
}

void treeUpdate(int node, int b, int e, int l, int r, int x, int y) {
    push(node, b, e);
    if (l > r) return;

    if (b == l && e == r) {
        Remap[node][x] = y;
        Remapped[node][x] = true;
        push(node, b, e);
    } else {
        int m = (b + e) / 2;
        treeUpdate(node * 2, b, m, l, min(r, m), x, y);
        treeUpdate(node * 2 + 1, m + 1, e, max(l, m + 1), r, x, y);
        pull(node);
    }
}

long long treeQuery(int node, int b, int e, int l, int r) {
    push(node, b, e);
    if (l > r) return 0;

    if (b == l && e == r) {
        long long ans = 0;
        for (int i = 0; i < 10; ++i) {
            ans += Count[node][i] * i;
        }
        return ans;
    }

    int m = (b + e) / 2;
    return treeQuery(node * 2, b, m, l, min(r, m)) +
        treeQuery(node * 2 + 1, m + 1, e, max(m + 1, l), r);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, m; cin >> n >> m;

    vector<int> V(n);
    for (int i = 0; i < n; ++i) {
        cin >> V[i];
    }

    treeInit(1, 0, n - 1, V);

    while (m--) {
        int type, l, r;
        cin >> type >> l >> r;
        --l; --r;

        if (type == 1) {
            int x, y; cin >> x >> y;
            treeUpdate(1, 0, n - 1, l, r, x, y);
        } else {
            cout << treeQuery(1, 0, n - 1, l, r) << '\n';
        }
    }
    return 0;
}


#include <vector>
#include <iostream>
#include <algorithm>
#include <cassert>
#include <cstring>

using namespace std;

const int kMaxN = 400000;

long long Count[kMaxN][10];
int Remap[kMaxN][10];

void push(int node, int b, int e) {
    static long long aux[10];

    if (b != e) {
        for (int son = 2 * node; son < 2 * node + 2; ++son) {
            for (int i = 0; i < 10; ++i) {
                Remap[son][i] = Remap[node][Remap[son][i]];
            }
        }
    }

    memset(aux, 0, sizeof(aux));
    for (int i = 0; i < 10; ++i) {
        aux[Remap[node][i]] += Count[node][i];
        Remap[node][i] = i;
    }
    memcpy(Count[node], aux, sizeof(aux));

}

void pull(int node, int b, int e) {
    if (b != e) {
        int m = (b + e) / 2;
        push(node * 2, b, m);
        push(node * 2 + 1, m + 1, e);
    }

    for (int i = 0; i < 10; ++i)
        Count[node][i] = Count[node * 2][i] + Count[node * 2 + 1][i];
}

void treeInit(int node, int b, int e, vector<int> &V) {
    for (int i = 0; i < 10; ++i)
        Remap[node][i] = i;

    if (b == e) {
        int no = V[b], pw = 1;
        while (no) {
            int dig = no % 10; no /= 10;
            Count[node][dig] += pw; pw *= 10;
        }
    } else {
        int m = (b + e) / 2;
        treeInit(node * 2, b, m, V);
        treeInit(node * 2 + 1, m + 1, e, V);
        pull(node, b, e);
    }
}

void treeUpdate(int node, int b, int e, int l, int r, int x, int y) {
    push(node, b, e);
    if (l > r) return;

    if (b == l && e == r) {
        Remap[node][x] = y;
        push(node, b, e);
    } else {
        int m = (b + e) / 2;
        treeUpdate(node * 2, b, m, l, min(r, m), x, y);
        treeUpdate(node * 2 + 1, m + 1, e, max(l, m + 1), r, x, y);
        pull(node, b, e);
    }
}

long long treeQuery(int node, int b, int e, int l, int r) {
    push(node, b, e);
    if (l > r) return 0;

    if (b == l && e == r) {
        long long ans = 0;
        for (int i = 0; i < 10; ++i) {
            ans += Count[node][i] * i;
        }
        return ans;
    }

    int m = (b + e) / 2;
    return treeQuery(node * 2, b, m, l, min(r, m)) +
        treeQuery(node * 2 + 1, m + 1, e, max(m + 1, l), r);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, m; cin >> n >> m;

    vector<int> V(n);
    for (int i = 0; i < n; ++i) {
        cin >> V[i];
    }

    treeInit(1, 0, n - 1, V);

    while (m--) {
        int type, l, r;
        cin >> type >> l >> r;
        --l; --r;

        if (type == 1) {
            int x, y; cin >> x >> y;
            treeUpdate(1, 0, n - 1, l, r, x, y);
        } else {
            cout << treeQuery(1, 0, n - 1, l, r) << '\n';
        }
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef pair<int,int> ii;

long long n,q,a[500005],it[500005][15],lazy[500005][15];
long long power[20];
void build_tree(long long k,long long l,long long r,long long d){
    if(l==r){
        long long amount=0,num=0,s=a[l];
        while(s!=0){
            long long di=s%10;
            if(di==d)
                amount+=power[num];
            num++;
            s/=10;
        }
        it[k][d]=amount;
        lazy[k][d]=d;
        return ;
    }
    long long mid=(l+r)/2;
    build_tree(k*2,l,mid,d);
    build_tree(k*2+1,mid+1,r,d);
    it[k][d]=it[k*2][d]+it[k*2+1][d];
    lazy[k][d]=lazy[k*2][d]=lazy[k*2+1][d];
}
void lazyupdate(long long k,long long l,long long r){
    if(l!=r){
        long long rem[15];
        for(int i=0;i<=9;i++){
            lazy[k*2][i]=lazy[k][lazy[k*2][i]];
            lazy[k*2+1][i]=lazy[k][lazy[k*2+1][i]];
            rem[i]=it[k*2][i];
            it[k*2][i]=0;
            it[k*2][lazy[k*2][i]]+=rem[i];
            rem[i]=it[k*2+1][i];
            it[k*2+1][i]=0;
            it[k*2+1][lazy[k*2+1][i]]+=rem[i];
        }
    }
    for(int i=0;i<=9;i++)
        lazy[k][i]=i;
}

void update(long long k,long long l,long long r,long long L,long long R,long long x,long long y){
    lazyupdate(k,l,r);
    if(R<l||r<L||L>R) return ;
    if(L<=l&&r<=R){
        it[k][y]+=it[k][x];
        if(lazy[k][x]==x)
        lazy[k][x]=y;
        it[k][x]=0;
        return ;
    }
    long long mid=(l+r)/2;
    update(k*2,l,mid,L,R,x,y);
    update(k*2+1,mid+1,r,L,R,x,y);
    for(int i=0;i<=9;i++)
        it[k][i]=it[k*2][i]+it[k*2+1][i];
}

long long get(long long k,long long l,long long r,long long L,long long R){
    lazyupdate(k,l,r);
    if(R<l||r<L||L>R) return 0;
    if(L<=l&&r<=R){
        long long num=0;
        for(int i=0;i<=9;i++)
            num+=it[k][i]*i;
        return num;
    }
    long long mid=(l+r)/2;
    return get(k*2,l,mid,L,R)+get(k*2+1,mid+1,r,L,R);
    for(int i=0;i<=9;i++)
        it[k][i]=it[k*2][i]+it[k*2+1][i];
}
int main(){
    cin.tie(0),cout.tie(0),ios::sync_with_stdio(0);
    cin>>n>>q;
    power[0]=1;
    for(int i=1;i<=18;i++)
        power[i]=power[i-1]*10;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=0;i<=9;i++)
        build_tree(1,1,n,i);
    long long l,r,type,x,y;
    while(q--){
        cin>>type>>l>>r;
        if(type==1){
            cin>>x>>y;
            //cout<<get(1,1,n,l,r)<<"\n";
            if(x!=y)
            update(1,1,n,l,r,x,y);
            //cout<<get(1,1,n,l,r)<<"\n";
        }
        else
            cout<<get(1,1,n,l,r)<<"\n";
    }
}


#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef pair<int,int> ii;

long long n,q,a[500005],it[500005][15],lazy[500005][15];
long long power[20];
void build_tree(long long k,long long l,long long r,long long d){
    if(l==r){
        long long amount=0,num=0,s=a[l];
        while(s!=0){
            long long di=s%10;
            if(di==d)
                amount+=power[num];
            num++;
            s/=10;
        }
        it[k][d]=amount;
        lazy[k][d]=d;
        return ;
    }
    long long mid=(l+r)/2;
    build_tree(k*2,l,mid,d);
    build_tree(k*2+1,mid+1,r,d);
    it[k][d]=it[k*2][d]+it[k*2+1][d];
    lazy[k][d]=lazy[k*2][d]=lazy[k*2+1][d];
}
void lazyupdate(long long k,long long l,long long r){
    if(l!=r){
        long long rem1[15],rem2[15];
        for(int i=0;i<=9;i++){
            lazy[k*2][i]=lazy[k][lazy[k*2][i]];
            lazy[k*2+1][i]=lazy[k][lazy[k*2+1][i]];
        }
        memset(rem1,0,sizeof(rem1));
        for(int i=0;i<=9;i++){
            rem1[lazy[k*2][i]]+=it[k*2][i];
            it[k*2][i]=0;
        }
        for(int i=0;i<=9;i++)
            it[k*2][i]+=rem1[i];
        memset(rem2,0,sizeof(rem2));
        for(int i=0;i<=9;i++){
            rem2[lazy[k*2+1][i]]+=it[k*2+1][i];
            it[k*2+1][i]=0;
        }
        for(int i=0;i<=9;i++)
            it[k*2+1][i]+=rem2[i];
    }
    for(int i=0;i<=9;i++)
        lazy[k][i]=i;
}

void update(long long k,long long l,long long r,long long L,long long R,long long x,long long y){
    lazyupdate(k,l,r);
    if(R<l||r<L||L>R) return ;
    if(L<=l&&r<=R){
        it[k][y]+=it[k][x];
        lazy[k][x]=y;
        it[k][x]=0;
        return ;
    }
    long long mid=(l+r)/2;
    update(k*2,l,mid,L,R,x,y);
    update(k*2+1,mid+1,r,L,R,x,y);
    for(int i=0;i<=9;i++)
        it[k][i]=it[k*2][i]+it[k*2+1][i];
}

long long get(long long k,long long l,long long r,long long L,long long R){
    lazyupdate(k,l,r);
    if(R<l||r<L||L>R) return 0;
    if(L<=l&&r<=R){
        long long num=0;
        for(int i=0;i<=9;i++)
            num+=it[k][i]*i;
        return num;
    }
    long long mid=(l+r)/2;
    return get(k*2,l,mid,L,R)+get(k*2+1,mid+1,r,L,R);
    for(int i=0;i<=9;i++)
        it[k][i]=it[k*2][i]+it[k*2+1][i];
}
int main(){
    cin.tie(0),cout.tie(0),ios::sync_with_stdio(0);
    cin>>n>>q;
    power[0]=1;
    for(int i=1;i<=18;i++)
        power[i]=power[i-1]*10;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=0;i<=9;i++)
        build_tree(1,1,n,i);
    long long l,r,type,x,y;
    while(q--){
        cin>>type>>l>>r;
        if(type==1){
            cin>>x>>y;
            //cout<<get(1,1,n,l,r)<<"\n";
            if(x!=y)
            update(1,1,n,l,r,x,y);
            //cout<<get(1,1,n,l,r)<<"\n";
        }
        else
            cout<<get(1,1,n,l,r)<<"\n";
    }
}


#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef pair<int,int> ii;

long long n,q,a[500005],it[500005][15],lazy[500005][15];
long long power[20];
void build_tree(long long k,long long l,long long r,long long d){
    if(l==r){
            long long idem=1;
            while(a[l] > 0){
                it[k][a[l]%10]+=idem;
                idem*=10;
                a[l]/=10;
            }
        //it[k][d]=amount;
        lazy[k][d]=d;
        return ;
    }
    long long mid=(l+r)/2;
    build_tree(k*2,l,mid,d);
    build_tree(k*2+1,mid+1,r,d);
    it[k][d]=it[k*2][d]+it[k*2+1][d];
    lazy[k][d]=d;
}
void lazyupdate(long long k,long long l,long long r){
    if(l!=r){
        long long rem1[15],rem2[15];
        for(long long i=0;i<=9;i++){
            lazy[k*2][i]=lazy[k][lazy[k*2][i]];
            lazy[k*2+1][i]=lazy[k][lazy[k*2+1][i]];
        }
        for(long long i=0;i<=9;i++)
            rem1[i]=0;
        for(long long i=0;i<=9;i++)
            rem1[lazy[k*2][i]]+=it[k*2][i];
        for(long long i=0;i<=9;i++)
            it[k*2][i]=rem1[i];
        for(long long i=0;i<=9;i++)
            rem2[i]=0;
        for(long long i=0;i<=9;i++)
            rem2[lazy[k*2+1][i]]+=it[k*2+1][i];
        for(long long i=0;i<=9;i++)
            it[k*2+1][i]=rem2[i];
    }
    for(long long i=0;i<=9;i++)
        lazy[k][i]=i;
}

void update(long long k,long long l,long long r,long long L,long long R,long long x,long long y){
    lazyupdate(k,l,r);
    if(R<l||r<L||L>R) return ;
    if(L<=l&&r<=R){
        lazy[k][x]=y;
        it[k][y]+=it[k][x];
        it[k][x]=0;
        return ;
    }
    long long mid=(l+r)/2;
    update(k*2,l,mid,L,R,x,y);
    update(k*2+1,mid+1,r,L,R,x,y);
    for(long long i=0;i<=9;i++)
        it[k][i]=it[k*2][i]+it[k*2+1][i];
}

long long get(long long k,long long l,long long r,long long L,long long R){
    lazyupdate(k,l,r);
    if(R<l||r<L||L>R) return 0LL;
    if(L<=l&&r<=R){
        long long num=0LL;
        for(long long i=1;i<=9;i++)
            num+=it[k][i]*i;
        return num;
    }
    long long mid=(l+r)/2;
    return get(k*2,l,mid,L,R)+get(k*2+1,mid+1,r,L,R);
}
int main(){
    cin.tie(0),cout.tie(0),ios::sync_with_stdio(0);
    cin>>n>>q;
    power[0]=1;
    for(long long i=1;i<=18;i++)
        power[i]=power[i-1]*10LL;
    for(long long i=1;i<=n;i++)
        cin>>a[i];
    for(long long i=0;i<=9;i++)
        build_tree(1,1,n,i);
    long long l,r,type,x,y;
    while(q--){
        cin>>type>>l>>r;
        if(type==1){
            cin>>x>>y;
            //cout<<get(1,1,n,l,r)<<"\n";
            if(x!=y)
            update(1,1,n,l,r,x,y);
            //cout<<get(1,1,n,l,r)<<"\n";
        }
        else
            cout<<get(1,1,n,l,r)<<"\n";
    }
}


#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef pair<int,int> ii;

long long n,q,a[500005],it[500005][15],lazy[500005][15];
long long power[20];
void build_tree(long long k,long long l,long long r,long long d){
    if(l==r){
        long long amount=0,num=0,s=a[l];
            while(s!=0){
                long long di=s%10;
                if(di==d)
                    amount+=power[num];
                num++;
                s/=10;
            }
            it[k][d]=amount;
            lazy[k][d]=d;
        return ;
    }
    long long mid=(l+r)/2;
    build_tree(k*2,l,mid,d);
    build_tree(k*2+1,mid+1,r,d);
    it[k][d]=it[k*2][d]+it[k*2+1][d];
    lazy[k][d]=d;
}
void lazyupdate(long long k,long long l,long long r){
    if(l!=r){
        long long rem1[15],rem2[15];
        for(long long i=0;i<=9;i++)
            rem1[i]=0;
        for(long long i=0;i<=9;i++){
            rem1[lazy[k*2][i]]+=it[k*2][i];
        }
        for(long long i=0;i<=9;i++)
            it[k*2][i]=rem1[i];
        for(long long i=0;i<=9;i++)
            rem2[i]=0;
        for(long long i=0;i<=9;i++){
            rem2[lazy[k*2+1][i]]+=it[k*2+1][i];
        }
        for(long long i=0;i<=9;i++)
            it[k*2+1][i]=rem2[i];
                for(long long i=0;i<=9;i++){
            lazy[k*2][i]=lazy[k][lazy[k*2][i]];
            lazy[k*2+1][i]=lazy[k][lazy[k*2+1][i]];
        }
    }
    for(long long i=0;i<=9;i++)
        lazy[k][i]=i;
}

void update(long long k,long long l,long long r,long long L,long long R,long long x,long long y){
    lazyupdate(k,l,r);
    if(R<l||r<L||L>R) return ;
    if(L<=l&&r<=R){
        lazy[k][x]=y;
        it[k][y]+=it[k][x];
        it[k][x]=0;
        return ;
    }
    long long mid=(l+r)/2;
    update(k*2,l,mid,L,R,x,y);
    update(k*2+1,mid+1,r,L,R,x,y);
    for(long long i=0;i<=9;i++)
        it[k][i]=it[k*2][i]+it[k*2+1][i];
}

long long get(long long k,long long l,long long r,long long L,long long R){
    lazyupdate(k,l,r);
    if(R<l||r<L||L>R) return 0LL;
    if(L<=l&&r<=R){
        long long num=0LL;
        for(long long i=1;i<=9;i++)
            num+=it[k][i]*i;
        return num;
    }
    long long mid=(l+r)/2;
    return get(k*2,l,mid,L,R)+get(k*2+1,mid+1,r,L,R);
    for(long long i=0;i<=9;i++)
        it[k][i]=it[k*2][i]+it[k*2+1][i];
}
int main(){
    cin.tie(0),cout.tie(0),ios::sync_with_stdio(0);
    cin>>n>>q;
    power[0]=1;
    for(long long i=1;i<=18;i++)
        power[i]=power[i-1]*10LL;
    for(long long i=1;i<=n;i++)
        cin>>a[i];
    for(long long i=0;i<=9;i++)
        build_tree(1,1,n,i);
    long long l,r,type,x,y;
    while(q--){
        cin>>type>>l>>r;
        if(type==1){
            cin>>x>>y;
            //cout<<get(1,1,n,l,r)<<"\n";
            if(x!=y)
            update(1,1,n,l,r,x,y);
            //cout<<get(1,1,n,l,r)<<"\n";
        }
        else
            cout<<get(1,1,n,l,r)<<"\n";
    }
}


#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef pair<int,int> ii;

long long n,q,a[500005],it[500005][15],lazy[500005][15];
long long power[20];
void build_tree(long long k,long long l,long long r,long long d){
    if(l==r){
        long long amount=0,num=0,s=a[l];
        while(s!=0){
            long long di=s%10;
            if(di==d)
                amount+=power[num];
            num++;
            s/=10;
        }
        it[k][d]=amount;
        lazy[k][d]=d;
        return ;
    }
    long long mid=(l+r)/2;
    build_tree(k*2,l,mid,d);
    build_tree(k*2+1,mid+1,r,d);
    it[k][d]=it[k*2][d]+it[k*2+1][d];
    lazy[k][d]=lazy[k*2][d]=lazy[k*2+1][d];
}
void lazyupdate(long long k,long long l,long long r){
    if(l!=r){
        long long rem[15];
        for(int i=0;i<=9;i++){
            lazy[k*2][i]=lazy[k*2][lazy[k][i]];
            lazy[k*2+1][i]=lazy[k*2+1][lazy[k][i]];
            rem[i]=it[k*2][i];
            it[k*2][i]=0;
            it[k*2][lazy[k*2][i]]+=rem[i];
            rem[i]=it[k*2+1][i];
            it[k*2+1][i]=0;
            it[k*2+1][lazy[k*2+1][i]]+=rem[i];
        }
    }
    for(int i=0;i<=9;i++)
        lazy[k][i]=i;
}

void update(long long k,long long l,long long r,long long L,long long R,long long x,long long y){
    lazyupdate(k,l,r);
    if(R<l||r<L||L>R) return ;
    if(L<=l&&r<=R){
        it[k][y]+=it[k][x];
        if(lazy[k][x]==x)
        lazy[k][x]=y;
        it[k][x]=0;
        return ;
    }
    long long mid=(l+r)/2;
    update(k*2,l,mid,L,R,x,y);
    update(k*2+1,mid+1,r,L,R,x,y);
    for(int i=0;i<=9;i++)
        it[k][i]=it[k*2][i]+it[k*2+1][i];
}

long long get(long long k,long long l,long long r,long long L,long long R){
    lazyupdate(k,l,r);
    if(R<l||r<L||L>R) return 0;
    if(L<=l&&r<=R){
        long long num=0;
        for(int i=0;i<=9;i++)
            num+=it[k][i]*i;
        return num;
    }
    long long mid=(l+r)/2;
    return get(k*2,l,mid,L,R)+get(k*2+1,mid+1,r,L,R);
    for(int i=0;i<=9;i++)
        it[k][i]=it[k*2][i]+it[k*2+1][i];
}
int main(){
    cin.tie(0),cout.tie(0),ios::sync_with_stdio(0);
    cin>>n>>q;
    power[0]=1;
    for(int i=1;i<=18;i++)
        power[i]=power[i-1]*10;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=0;i<=9;i++)
        build_tree(1,1,n,i);
    long long l,r,type,x,y;
    while(q--){
        cin>>type>>l>>r;
        if(type==1){
            cin>>x>>y;
            //cout<<get(1,1,n,l,r)<<"\n";
            if(x!=y)
            update(1,1,n,l,r,x,y);
            //cout<<get(1,1,n,l,r)<<"\n";
        }
        else
            cout<<get(1,1,n,l,r)<<"\n";
    }
}


#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef pair<int,int> ii;

long long n,q,a[500005],it[500005][15],lazy[500005][15];
long long power[20];
void build_tree(long long k,long long l,long long r,long long d){
    if(l==r){
        long long amount=0,num=0,s=a[l];
        while(s!=0){
            long long di=s%10;
            if(di==d)
                amount+=power[num];
            num++;
            s/=10;
        }
        it[k][d]=amount;
        lazy[k][d]=d;
        return ;
    }
    long long mid=(l+r)/2;
    build_tree(k*2,l,mid,d);
    build_tree(k*2+1,mid+1,r,d);
    it[k][d]=it[k*2][d]+it[k*2+1][d];
    lazy[k][d]=lazy[k*2][d]=lazy[k*2+1][d];
}
void lazyupdate(long long k,long long l,long long r){
    if(l!=r){
        long long rem[15];
        for(int i=0;i<=9;i++){
            lazy[k*2][i]=lazy[k][lazy[k*2][i]];
            lazy[k*2+1][i]=lazy[k][lazy[k*2+1][i]];
            rem[i]=it[k*2][i];
            it[k*2][i]=0;
            it[k*2][lazy[k*2][i]]+=rem[i];
            rem[i]=it[k*2+1][i];
            it[k*2+1][i]=0;
            it[k*2+1][lazy[k*2+1][i]]+=rem[i];
        }
    }
    for(int i=0;i<=9;i++)
        lazy[k][i]=i;
}

void update(long long k,long long l,long long r,long long L,long long R,long long x,long long y){
    lazyupdate(k,l,r);
    if(R<l||r<L||L>R) return ;
    if(L<=l&&r<=R){
        it[k][y]+=it[k][x];
        lazy[k][x]=y;
        it[k][x]=0;
        return ;
    }
    long long mid=(l+r)/2;
    update(k*2,l,mid,L,R,x,y);
    update(k*2+1,mid+1,r,L,R,x,y);
    for(int i=0;i<=9;i++)
        it[k][i]=it[k*2][i]+it[k*2+1][i];
}

long long get(long long k,long long l,long long r,long long L,long long R){
    lazyupdate(k,l,r);
    if(R<l||r<L||L>R) return 0;
    if(L<=l&&r<=R){
        long long num=0;
        for(int i=0;i<=9;i++)
            num+=it[k][i]*i;
        return num;
    }
    long long mid=(l+r)/2;
    return get(k*2,l,mid,L,R)+get(k*2+1,mid+1,r,L,R);
    for(int i=0;i<=9;i++)
        it[k][i]=it[k*2][i]+it[k*2+1][i];
}
int main(){
    cin.tie(0),cout.tie(0),ios::sync_with_stdio(0);
    cin>>n>>q;
    power[0]=1;
    for(int i=1;i<=18;i++)
        power[i]=power[i-1]*10;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=0;i<=9;i++)
        build_tree(1,1,n,i);
    long long l,r,type,x,y;
    while(q--){
        cin>>type>>l>>r;
        if(type==1){
            cin>>x>>y;
            //cout<<get(1,1,n,l,r)<<"\n";
            if(x!=y)
            update(1,1,n,l,r,x,y);
            //cout<<get(1,1,n,l,r)<<"\n";
        }
        else
            cout<<get(1,1,n,l,r)<<"\n";
    }
}


#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef pair<int,int> ii;

long long n,q,a[500005],it[500005][15],lazy[500005][15];
long long power[20];
void build_tree(long long k,long long l,long long r,long long d){
    if(l==r){
        long long amount=0,num=0,s=a[l];
        while(s!=0){
            long long di=s%10;
            if(di==d)
                amount+=power[num];
            num++;
            s/=10;
        }
        it[k][d]=amount;
        lazy[k][d]=d;
        return ;
    }
    long long mid=(l+r)/2;
    build_tree(k*2,l,mid,d);
    build_tree(k*2+1,mid+1,r,d);
    it[k][d]=it[k*2][d]+it[k*2+1][d];
    lazy[k][d]=lazy[k*2][d]=lazy[k*2+1][d];
}
void lazyupdate(long long k,long long l,long long r){
    if(l!=r){
        long long rem[15];
        for(int i=0;i<=9;i++){
            lazy[k*2][i]=lazy[k*2][lazy[k][i]];
            lazy[k*2+1][i]=lazy[k*2+1][lazy[k][i]];
            rem[i]=it[k*2][i];
            it[k*2][i]=0;
            it[k*2][lazy[k][i]]+=rem[i];
            rem[i]=it[k*2+1][i];
            it[k*2+1][i]=0;
            it[k*2+1][lazy[k][i]]+=rem[i];
        }
    }
    for(int i=0;i<=9;i++)
        lazy[k][i]=i;
}

void update(long long k,long long l,long long r,long long L,long long R,long long x,long long y){
    lazyupdate(k,l,r);
    if(R<l||r<L||L>R) return ;
    if(L<=l&&r<=R){
        it[k][y]+=it[k][x];
        lazy[k][x]=y;
        it[k][x]=0;
        return ;
    }
    long long mid=(l+r)/2;
    update(k*2,l,mid,L,R,x,y);
    update(k*2+1,mid+1,r,L,R,x,y);
    for(int i=0;i<=9;i++)
        it[k][i]=it[k*2][i]+it[k*2+1][i];
}

long long get(long long k,long long l,long long r,long long L,long long R){
    lazyupdate(k,l,r);
    if(R<l||r<L||L>R) return 0;
    if(L<=l&&r<=R){
        long long num=0;
        for(int i=0;i<=9;i++)
            num+=it[k][i]*i;
        return num;
    }
    long long mid=(l+r)/2;
    return get(k*2,l,mid,L,R)+get(k*2+1,mid+1,r,L,R);
    for(int i=0;i<=9;i++)
        it[k][i]=it[k*2][i]+it[k*2+1][i];
}
int main(){
    cin.tie(0),cout.tie(0),ios::sync_with_stdio(0);
    cin>>n>>q;
    power[0]=1;
    for(int i=1;i<=18;i++)
        power[i]=power[i-1]*10;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=0;i<=9;i++)
        build_tree(1,1,n,i);
    long long l,r,type,x,y;
    while(q--){
        cin>>type>>l>>r;
        if(type==1){
            cin>>x>>y;
            update(1,1,n,l,r,x,y);
        }
        else
            cout<<get(1,1,n,l,r)<<"\n";
    }
}


#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef pair<int,int> ii;

long long n,q,a[500005],it[500005][15],lazy[500005][15];
long long power[20];
void build_tree(long long k,long long l,long long r,long long d){
    if(l==r){
            long long idem=1;
            while(a[l] > 0){
                it[k][a[l]%10]+=idem;
                idem*=10;
                a[l]/=10;
            }
        //it[k][d]=amount;
        lazy[k][d]=d;
        return ;
    }
    long long mid=(l+r)/2;
    build_tree(k*2,l,mid,d);
    build_tree(k*2+1,mid+1,r,d);
    it[k][d]=it[k*2][d]+it[k*2+1][d];
    lazy[k][d]=d;
}
void lazyupdate(long long k,long long l,long long r){
    if(l!=r){
        long long rem1[15],rem2[15];
        for(long long i=0;i<=9;i++){
            lazy[k*2][i]=lazy[k][lazy[k*2][i]];
            lazy[k*2+1][i]=lazy[k][lazy[k*2+1][i]];
        }
        for(long long i=0;i<=9;i++)
            rem1[i]=0;
        for(long long i=0;i<=9;i++)
            rem1[lazy[k*2][i]]+=it[k*2][i];
        for(long long i=0;i<=9;i++)
            it[k*2][i]=rem1[i];
        for(long long i=0;i<=9;i++)
            rem2[i]=0;
        for(long long i=0;i<=9;i++)
            rem2[lazy[k*2+1][i]]+=it[k*2+1][i];
        for(long long i=0;i<=9;i++)
            it[k*2+1][i]=rem2[i];
    }
    for(long long i=0;i<=9;i++)
        lazy[k][i]=i;
}

void update(long long k,long long l,long long r,long long L,long long R,long long x,long long y){
    lazyupdate(k,l,r);
    if(R<l||r<L||L>R) return ;
    if(L<=l&&r<=R){
        lazy[k][x]=y;
        it[k][y]+=it[k][x];
        it[k][x]=0;
        return ;
    }
    long long mid=(l+r)/2;
    update(k*2,l,mid,L,R,x,y);
    update(k*2+1,mid+1,r,L,R,x,y);
    for(long long i=0;i<=9;i++)
        it[k][i]=it[k*2][i]+it[k*2+1][i];
}

long long get(long long k,long long l,long long r,long long L,long long R){
    lazyupdate(k,l,r);
    if(R<l||r<L||L>R) return 0LL;
    if(L<=l&&r<=R){
        long long num=0LL;
        for(long long i=1;i<=9;i++)
            num+=it[k][i]*i;
        return num;
    }
    long long mid=(l+r)/2;
    return get(k*2,l,mid,L,R)+get(k*2+1,mid+1,r,L,R);
    for(long long i=0;i<=9;i++)
        it[k][i]=it[k*2][i]+it[k*2+1][i];
}
int main(){
    cin.tie(0),cout.tie(0),ios::sync_with_stdio(0);
    cin>>n>>q;
    power[0]=1;
    for(long long i=1;i<=18;i++)
        power[i]=power[i-1]*10LL;
    for(long long i=1;i<=n;i++)
        cin>>a[i];
    for(long long i=0;i<=9;i++)
        build_tree(1,1,n,i);
    long long l,r,type,x,y;
    while(q--){
        cin>>type>>l>>r;
        if(type==1){
            cin>>x>>y;
            //cout<<get(1,1,n,l,r)<<"\n";
            if(x!=y)
            update(1,1,n,l,r,x,y);
            //cout<<get(1,1,n,l,r)<<"\n";
        }
        else
            cout<<get(1,1,n,l,r)<<"\n";
    }
}


#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef pair<int,int> ii;

long long n,q,a[500005],it[500005][15],lazy[500005][15];
long long power[20];
void build_tree(long long k,long long l,long long r,long long d){
    if(l==r){
        long long amount=0,num=0,s=a[l];
        while(s!=0){
            long long di=s%10;
            if(di==d)
                amount+=power[num];
            num++;
            s/=10;
        }
        if(s==0&&d==0)
        it[k][d]=1;
        else
        it[k][d]=amount;
        lazy[k][d]=d;
        return ;
    }
    long long mid=(l+r)/2;
    build_tree(k*2,l,mid,d);
    build_tree(k*2+1,mid+1,r,d);
    it[k][d]=it[k*2][d]+it[k*2+1][d];
    lazy[k][d]=lazy[k*2][d];
}
void lazyupdate(long long k,long long l,long long r){
    if(l!=r){
        long long rem1[15],rem2[15];
        for(int i=0;i<=9;i++){
            lazy[k*2][i]=lazy[k][lazy[k*2][i]];
            lazy[k*2+1][i]=lazy[k][lazy[k*2+1][i]];
        }
        memset(rem1,0,sizeof(rem1));
        for(int i=0;i<=9;i++){
            rem1[lazy[k*2][i]]+=it[k*2][i];
            it[k*2][i]=0;
        }
        for(int i=0;i<=9;i++)
            it[k*2][i]=rem1[i];
        memset(rem2,0,sizeof(rem2));
        for(int i=0;i<=9;i++){
            rem2[lazy[k*2+1][i]]+=it[k*2+1][i];
            it[k*2+1][i]=0;
        }
        for(int i=0;i<=9;i++)
            it[k*2+1][i]=rem2[i];
    }
    for(int i=0;i<=9;i++)
        lazy[k][i]=i;
}

void update(long long k,long long l,long long r,long long L,long long R,long long x,long long y){
    lazyupdate(k,l,r);
    if(R<l||r<L||L>R) return ;
    if(L<=l&&r<=R){
        it[k][y]+=it[k][x];
        lazy[k][x]=y;
        it[k][x]=0;
        return ;
    }
    long long mid=(l+r)/2;
    update(k*2,l,mid,L,R,x,y);
    update(k*2+1,mid+1,r,L,R,x,y);
    for(int i=0;i<=9;i++)
        it[k][i]=it[k*2][i]+it[k*2+1][i];
}

long long get(long long k,long long l,long long r,long long L,long long R){
    lazyupdate(k,l,r);
    if(R<l||r<L||L>R) return 0;
    if(L<=l&&r<=R){
        long long num=0;
        for(int i=0;i<=9;i++)
            num+=it[k][i]*i;
        return num;
    }
    long long mid=(l+r)/2;
    return get(k*2,l,mid,L,R)+get(k*2+1,mid+1,r,L,R);
    for(int i=0;i<=9;i++)
        it[k][i]=it[k*2][i]+it[k*2+1][i];
}
int main(){
    cin.tie(0),cout.tie(0),ios::sync_with_stdio(0);
    cin>>n>>q;
    power[0]=1;
    for(int i=1;i<=18;i++)
        power[i]=power[i-1]*10;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=0;i<=9;i++)
        build_tree(1,1,n,i);
    long long l,r,type,x,y;
    while(q--){
        cin>>type>>l>>r;
        if(type==1){
            cin>>x>>y;
            //cout<<get(1,1,n,l,r)<<"\n";
            if(x!=y)
            update(1,1,n,l,r,x,y);
            //cout<<get(1,1,n,l,r)<<"\n";
        }
        else
            cout<<get(1,1,n,l,r)<<"\n";
    }
}


#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef pair<int,int> ii;

long long n,q,a[500005],it[500005][15],lazy[500005][15];
long long power[20];
void build_tree(long long k,long long l,long long r,long long d){
    if(l==r){
            long long idem=1;
            while(a[l] > 0){
                it[k][a[l]%10]+=idem;
                idem*=10;
                a[l]/=10;
            }
        //it[k][d]=amount;
        lazy[k][d]=d;
        return ;
    }
    long long mid=(l+r)/2;
    build_tree(k*2,l,mid,d);
    build_tree(k*2+1,mid+1,r,d);
    it[k][d]=it[k*2][d]+it[k*2+1][d];
    lazy[k][d]=d;
}
void lazyupdate(long long k,long long l,long long r){
    long long rem[15];
    memset(rem,0,sizeof(rem));
    for(int i=0;i<=9;i++){
        rem[lazy[k][i]]+=it[k][i];
        it[k][i]=0;
    }
    if(l!=r){
        for(int i=0;i<=9;i++){
            lazy[k*2][i]=lazy[k][lazy[k*2][i]];
            lazy[k*2+1][i]=lazy[k][lazy[k*2+1][i]];
        }
    }
    for(int i=0;i<=9;i++){
        it[k][i]=rem[i];
        lazy[k][i]=i;
    }
}

void update(long long k,long long l,long long r,long long L,long long R,long long x,long long y){
    lazyupdate(k,l,r);
    if(R<l||r<L||L>R) return ;
    if(L<=l&&r<=R){
        it[k][y]+=it[k][x];
        it[k][x]=0;
        if(l!=r){
            for(int i=0;i<=9;i++){
                if(lazy[k*2][i]==x) lazy[k*2][i]=y;
                if(lazy[k*2+1][i]==x) lazy[k*2+1][i]=y;
            }
        }
        return ;
    }
    long long mid=(l+r)/2;
    update(k*2,l,mid,L,R,x,y);
    update(k*2+1,mid+1,r,L,R,x,y);
    for(long long i=0;i<=9;i++)
        it[k][i]=it[k*2][i]+it[k*2+1][i];
}

long long get(long long k,long long l,long long r,long long L,long long R){
    lazyupdate(k,l,r);
    if(R<l||r<L||L>R) return 0LL;
    if(L<=l&&r<=R){
        long long num=0LL;
        for(long long i=1;i<=9;i++)
            num+=it[k][i]*i;
        return num;
    }
    long long mid=(l+r)/2;
    return get(k*2,l,mid,L,R)+get(k*2+1,mid+1,r,L,R);
    for(long long i=0;i<=9;i++)
        it[k][i]=it[k*2][i]+it[k*2+1][i];
}
int main(){
    cin.tie(0),cout.tie(0),ios::sync_with_stdio(0);
    cin>>n>>q;
    power[0]=1;
    for(long long i=1;i<=18;i++)
        power[i]=power[i-1]*10LL;
    for(long long i=1;i<=n;i++)
        cin>>a[i];
    for(long long i=0;i<=9;i++)
        build_tree(1,1,n,i);
    long long l,r,type,x,y;
    while(q--){
        cin>>type>>l>>r;
        if(type==1){
            cin>>x>>y;
            //cout<<get(1,1,n,l,r)<<"\n";
            if(x!=y)
            update(1,1,n,l,r,x,y);
            //cout<<get(1,1,n,l,r)<<"\n";
        }
        else
            cout<<get(1,1,n,l,r)<<"\n";
    }
}


#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef pair<int,int> ii;

long long n,q,a[500005],it[500005][15],lazy[500005][15];
long long power[20];
void build_tree(long long k,long long l,long long r,long long d){
    if(l==r){
        long long amount=0,num=0,s=a[l];
        if(s==0&&d==0)
        it[k][d]=1;
        else{
            while(s!=0){
                long long di=s%10;
                if(di==d)
                    amount+=power[num];
                num++;
                s/=10;
            }
            it[k][d]=amount;
            lazy[k][d]=d;
        }
        return ;
    }
    long long mid=(l+r)/2;
    build_tree(k*2,l,mid,d);
    build_tree(k*2+1,mid+1,r,d);
    it[k][d]=it[k*2][d]+it[k*2+1][d];
    lazy[k][d]=lazy[k*2][d];
}
void lazyupdate(long long k,long long l,long long r){
    if(l!=r){
        long long rem1[15],rem2[15];
        for(int i=0;i<=9;i++){
            lazy[k*2][i]=lazy[k][lazy[k*2][i]];
            lazy[k*2+1][i]=lazy[k][lazy[k*2+1][i]];
        }
        memset(rem1,0,sizeof(rem1));
        for(int i=0;i<=9;i++){
            rem1[lazy[k*2][i]]+=it[k*2][i];
            it[k*2][i]=0;
        }
        for(int i=0;i<=9;i++)
            it[k*2][i]=rem1[i];
        memset(rem2,0,sizeof(rem2));
        for(int i=0;i<=9;i++){
            rem2[lazy[k*2+1][i]]+=it[k*2+1][i];
            it[k*2+1][i]=0;
        }
        for(int i=0;i<=9;i++)
            it[k*2+1][i]=rem2[i];
    }
    for(int i=0;i<=9;i++)
        lazy[k][i]=i;
}

void update(long long k,long long l,long long r,long long L,long long R,long long x,long long y){
    lazyupdate(k,l,r);
    if(R<l||r<L||L>R) return ;
    if(L<=l&&r<=R){
        it[k][y]+=it[k][x];
        lazy[k][x]=y;
        it[k][x]=0;
        return ;
    }
    long long mid=(l+r)/2;
    update(k*2,l,mid,L,R,x,y);
    update(k*2+1,mid+1,r,L,R,x,y);
    for(int i=0;i<=9;i++)
        it[k][i]=it[k*2][i]+it[k*2+1][i];
}

long long get(long long k,long long l,long long r,long long L,long long R){
    lazyupdate(k,l,r);
    if(R<l||r<L||L>R) return 0;
    if(L<=l&&r<=R){
        long long num=0;
        for(int i=0;i<=9;i++)
            num+=it[k][i]*i;
        return num;
    }
    long long mid=(l+r)/2;
    return get(k*2,l,mid,L,R)+get(k*2+1,mid+1,r,L,R);
    for(int i=0;i<=9;i++)
        it[k][i]=it[k*2][i]+it[k*2+1][i];
}
int main(){
    cin.tie(0),cout.tie(0),ios::sync_with_stdio(0);
    cin>>n>>q;
    power[0]=1;
    for(int i=1;i<=18;i++)
        power[i]=power[i-1]*10;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=0;i<=9;i++)
        build_tree(1,1,n,i);
    long long l,r,type,x,y;
    while(q--){
        cin>>type>>l>>r;
        if(type==1){
            cin>>x>>y;
            //cout<<get(1,1,n,l,r)<<"\n";
            if(x!=y)
            update(1,1,n,l,r,x,y);
            //cout<<get(1,1,n,l,r)<<"\n";
        }
        else
            cout<<get(1,1,n,l,r)<<"\n";
    }
}