UVa 686 Goldbach’s Conjecture II Solution in Java

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * Created by dhruv.pancholi on 20/11/16.
 */
public class Main {

    public static void main(String[] args) throws FileNotFoundException {
        InputReader in = new InputReader(System.in);

        int limit = 2 << 14;
        SieveOfEratosthenes soe = new SieveOfEratosthenes(limit);
        List<Integer> primes = soe.getPrimes();
        int[] arr = new int[limit + 1];

        int p1, p2;
        for (int i = 0; i < primes.size(); i++) {
            p1 = primes.get(i);
            for (int j = i; j < primes.size(); j++) {
                p2 = primes.get(j);
                if (p1 + p2 > limit) break;
                arr[p1 + p2]++;
            }
        }

        int n = 0;
        while (true) {
            n = in.nextInt();
            if (n == 0) break;
            System.out.println(arr[n]);
        }
    }

    private static class SieveOfEratosthenes {
        private boolean[] a;

        public SieveOfEratosthenes(int N) {
            a = new boolean[N + 1];
            for (int i = 0; i <= N; i++) {
                a[i] = true;
            }
            a[0] = false;
            a[1] = false;

            int root = (int) Math.sqrt(N);
            for (int i = 2; i <= root; i++) {
                for (int j = 2 * i; j <= N; j += i) {
                    a[j] = false;
                }
            }
        }

        public boolean[] getArray() {
            return a;
        }

        public boolean isPrime(int n) {
            return a[n];
        }

        public List<Integer> getPrimes() {
            List<Integer> list = new ArrayList<Integer>();
            for (int i = 1; i < a.length; i++) {
                if (a[i]) {
                    list.add(i);
                }
            }
            return list;
        }
    }

    public static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}

Sample Input:

6
10
12
0

Sample Output:

1
2
1

UVa 583 Prime Factors Solution in Java

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * Created by dhruv.pancholi on 05/11/16.
 */
public class Main {

    public static List<Integer> getPrimeFactors(int N, List<Integer> primes) {
        List<Integer> factors = new ArrayList<Integer>();
        int index = 0;
        int factor = primes.get(index);
        int limit = (int) Math.sqrt(N) + 1;
        while (N != 1 && factor < limit) {
            while (N % factor == 0) {
                N /= factor;
                factors.add(factor);
            }
            factor = primes.get(++index);
        }
        if (N != 1) factors.add(N);
        return factors;
    }

    public static void main(String[] args) throws FileNotFoundException {
        InputReader in = new InputReader(System.in);

        SieveOfEratosthenes soe = new SieveOfEratosthenes((int) Math.sqrt(Integer.MAX_VALUE) * 10);
        List<Integer> primes = soe.getPrimes();


        int n;
        while (true) {
            n = in.nextInt();
            if (n == 0) break;
            List<Integer> factors = getPrimeFactors(Math.abs(n), primes);
            System.out.print(n + " = ");
            if (n < 0) System.out.print("-1 x ");
            for (int i = 0; i < factors.size() - 1; i++) {
                System.out.print(factors.get(i) + " x ");
            }
            System.out.println(factors.get(factors.size() - 1));
        }
    }

    private static class SieveOfEratosthenes {
        private boolean[] a;

        public SieveOfEratosthenes(int N) {
            a = new boolean[N + 1];
            for (int i = 0; i <= N; i++) {
                a[i] = true;
            }
            a[0] = false;
            a[1] = false;

            int root = (int) Math.sqrt(N);
            for (int i = 2; i <= root; i++) {
                for (int j = 2 * i; j <= N; j += i) {
                    a[j] = false;
                }
            }
        }

        public boolean[] getArray() {
            return a;
        }

        public boolean isPrime(int n) {
            return a[n];
        }

        public List<Integer> getPrimes() {
            List<Integer> list = new ArrayList<Integer>();
            for (int i = 1; i < a.length; i++) {
                if (a[i]) {
                    list.add(i);
                }
            }
            return list;
        }
    }

    public static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}

Sample Input:

-190
-191
-192
-193
-194
195
196
197
198
199
200
0

Sample Output:

-190 = -1 x 2 x 5 x 19
-191 = -1 x 191
-192 = -1 x 2 x 2 x 2 x 2 x 2 x 2 x 3
-193 = -1 x 193
-194 = -1 x 2 x 97
195 = 3 x 5 x 13
196 = 2 x 2 x 7 x 7
197 = 197
198 = 2 x 3 x 3 x 11
199 = 199
200 = 2 x 2 x 2 x 5 x 5

UVa 543 Goldbach’s Conjecture Solution in Java

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * Created by dhruv.pancholi on 20/11/16.
 */
public class Main {

    public static void main(String[] args) {

        InputReader in = new InputReader(System.in);

        SieveOfEratosthenes soe = new SieveOfEratosthenes(1000000);
        List<Integer> primes = soe.getPrimes();

        while (true) {
            int n = in.nextInt();
            if (n == 0) break;
            for (int i = 0; i < primes.size(); i++) {
                if (soe.isPrime(n - primes.get(i))) {
                    System.out.println(n + " = " + primes.get(i) + " + " + (n - primes.get(i)));
                    break;
                }
            }
        }
    }

    private static class SieveOfEratosthenes {
        private boolean[] a;

        public SieveOfEratosthenes(int N) {
            a = new boolean[N + 1];
            for (int i = 0; i <= N; i++) {
                a[i] = true;
            }
            a[0] = false;
            a[1] = false;

            int root = (int) Math.sqrt(N);
            for (int i = 2; i <= root; i++) {
                for (int j = 2 * i; j <= N; j += i) {
                    a[j] = false;
                }
            }
        }

        public boolean[] getArray() {
            return a;
        }

        public boolean isPrime(int n) {
            return a[n];
        }

        public List<Integer> getPrimes() {
            List<Integer> list = new ArrayList<Integer>();
            for (int i = 1; i < a.length; i++) {
                if (a[i]) {
                    list.add(i);
                }
            }
            return list;
        }
    }

    private static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (Exception e) {
                    System.exit(0);
                }
            }
            return tokenizer.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}

Sample Input:

8
20
42
0

Sample Output:

8 = 3 + 5
20 = 3 + 17
42 = 5 + 37

UVa 524 Prime Ring Problem Solution in Java

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * Created by dhruv.pancholi on 20/11/16.
 */
public class Main {

    private static boolean[] onStack = new boolean[16];

    private static void dfs(List<Integer>[] adjacency, String out, int node, int n, int N) {
        if (n == 1) {
            if (adjacency[node - 1].get(0) == 1) System.out.println(out);
            return;
        }
        for (Integer i : adjacency[node - 1]) {
            if (onStack[i - 1] || i > N) {
                continue;
            } else {
                out += " " + i;
                onStack[i - 1] = true;
                dfs(adjacency, out, i, n - 1, N);
                out = out.substring(0, out.lastIndexOf(" "));
                onStack[i - 1] = false;
            }
        }
    }

    public static void main(String[] args) throws FileNotFoundException {
        InputReader in = new InputReader(System.in);
        SieveOfEratosthenes soe = new SieveOfEratosthenes(50);
        List<Integer>[] adjacency;
        adjacency = new List[16];
        for (int i = 0; i < 16; i++) {
            adjacency[i] = new ArrayList<Integer>();
        }
        for (int i = 0; i < 16; i++) {
            for (int j = i + 1; j < 16; j++) {
                if (soe.isPrime(i + j + 2)) {
                    adjacency[i].add(j + 1);
                    adjacency[j].add(i + 1);
                }
            }
        }

        int c = 0;
        while (true) {
            LinkedList<Integer> stack = new LinkedList<Integer>();
            stack.add(1);
            onStack[0] = true;
            int n = in.nextInt();
            if (c != 0) System.out.println();
            System.out.println("Case " + (++c) + ":");
            if (n == 1) {
                System.out.println(1);
            } else {
                dfs(adjacency, "1", 1, n, n);
            }
        }
    }

    private static class SieveOfEratosthenes {
        private boolean[] a;

        public SieveOfEratosthenes(int N) {
            a = new boolean[N + 1];
            for (int i = 0; i <= N; i++) {
                a[i] = true;
            }
            a[0] = false;
            a[1] = false;

            int root = (int) Math.sqrt(N);
            for (int i = 2; i <= root; i++) {
                for (int j = 2 * i; j <= N; j += i) {
                    a[j] = false;
                }
            }
        }

        public boolean[] getArray() {
            return a;
        }

        public boolean isPrime(int n) {
            return a[n];
        }

        public List<Integer> getPrimes() {
            List<Integer> list = new ArrayList<Integer>();
            for (int i = 1; i < a.length; i++) {
                if (a[i]) {
                    list.add(i);
                }
            }
            return list;
        }
    }

    private static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (Exception e) {
                    System.exit(0);
                }
            }
            return tokenizer.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}

Sample Input:

6
8

Sample Output:

Case 1:
1 4 3 2 5 6
1 6 5 2 3 4

Case 2:
1 2 3 8 5 6 7 4
1 2 5 8 3 4 7 6
1 4 7 6 5 8 3 2
1 6 7 4 3 8 5 2

UVa 406 Prime Cuts Solution in Java

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class PrimeCuts {
    public static void main(String[] args) {
        InputReader in = new InputReader(System.in);
        SieveOfEratosthenes soe = new SieveOfEratosthenes(1000);
        boolean[] arr = soe.getArray();
        arr[1] = true;
        int[] agg = new int[1001];
        for (int i = 1; i < agg.length; i++) {
            agg[i] += agg[i - 1];
            if (arr[i]) agg[i]++;
        }

        while (true) {
            int n = in.nextInt();
            int c = in.nextInt();
            int startIndex = (agg[n] % 2 == 0) ? (agg[n] / 2) : (agg[n] / 2 + 1);
            startIndex -= (c - 1);
            startIndex = startIndex > 0 ? startIndex : 1;
            int endIndex = startIndex + ((agg[n] % 2 == 0) ? (2 * c - 1) : (2 * c - 2));
            endIndex = endIndex > n ? n : endIndex;

            System.out.print(n + " " + c + ":");
            int index = 0;
            for (int i = 1; i <= n; i++) {

                if (arr[i]) {
                    index++;
                    if (index >= startIndex && index <= endIndex) {
                        System.out.print(" " + i);
                    }
                }
            }
            System.out.println();
            System.out.println();
        }
    }

    public static class SieveOfEratosthenes {
        private boolean[] a;

        public SieveOfEratosthenes(int N) {
            a = new boolean[N + 1];
            for (int i = 0; i <= N; i++) {
                a[i] = true;
            }
            a[0] = false;
            a[1] = false;

            int root = (int) Math.sqrt(N);
            for (int i = 2; i <= root; i++) {
                for (int j = 2 * i; j <= N; j += i) {
                    a[j] = false;
                }
            }
        }

        public boolean[] getArray() {
            return a;
        }

        public boolean isPrime(int n) {
            return a[n];
        }

        public List<Integer> getPrimes() {
            List<Integer> list = new ArrayList<Integer>();
            for (int i = 1; i < a.length; i++) {
                if (a[i]) {
                    list.add(i);
                }
            }
            return list;
        }
    }

    public static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (Exception e) {
                    System.exit(0);
                }
            }
            return tokenizer.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}

Sample Input:

21 2
18 2
18 18
100 7

Sample Output:

21 2: 5 7 11

18 2: 3 5 7 11

18 18: 1 2 3 5 7 11 13 17

100 7: 13 17 19 23 29 31 37 41 43 47 53 59 61 67

UVa 10130 SuperSale solution in Java

import java.io.*;
import java.util.StringTokenizer;

public class SuperSale {

    public static void main(String[] args) throws FileNotFoundException {
        InputReader in = new InputReader(System.in);

        int T = in.nextInt();
        while (T-- != 0) {
            int N = in.nextInt();
            Object[] A = new Object[N];
            int sum = 0;
            for (int n = 0; n < N; n++) {
                A[n] = new Object(in.nextInt(), in.nextInt());
            }

            int G = in.nextInt();
            int MW;
            int max = 0;
            for (int g = 0; g < G; g++) {
                MW = in.nextInt();
                int[][] memo = new int[A.length + 1][31];

                for (int i = 1; i <= A.length; i++) {
                    for (int j = 0; j <= MW; j++) {
                        if (j + A[i - 1].weight <= MW) {
                            memo[i][j + A[i - 1].weight] = Math.max(memo[i - 1][j] + A[i - 1].price, memo[i][j]);
                        }
                        memo[i][j] = Math.max(memo[i][j], memo[i - 1][j]);
                    }
                }

                int localMax = 0;
                for (int j = 0; j <= MW; j++) {
                    localMax = Math.max(localMax, memo[A.length][j]);
                }
                max += localMax;
            }

            System.out.println(max);
        }
    }

    public static class Object {
        public int price;
        public int weight;

        public Object(int price, int weight) {
            this.price = price;
            this.weight = weight;
        }
    }
    
    public static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }
    }

}

Sample Input:

2
3
72 17
44 23
31 24
1
26
6
64 26
85 22
52 4
99 18
39 13
54 9
4
23
20
20
26

Sample Output:

72
514

ACM ICPC LA (Live Archive) 3619 solution in Java

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class SumOfDifferentPrimes {

    public static class SieveOfEratosthenes {
        private boolean[] a;

        public SieveOfEratosthenes(int N) {
            a = new boolean[N + 1];
            for (int i = 0; i < N; i++) {
                a[i] = true;
            }
            a[0] = false;
            a[1] = false;

            int root = (int) Math.sqrt(N);
            for (int i = 2; i <= root; i++) {
                for (int j = 2 * i; j <= N; j += i) {
                    a[j] = false;
                }
            }
        }

        public boolean isPrime(int n) {
            return a[n];
        }

        public List<Integer> getPrimes() {
            List<Integer> list = new ArrayList<Integer>();
            for (int i = 1; i < a.length; i++) {
                if (a[i]) {
                    list.add(i);
                }
            }
            return list;
        }
    }

    public static void main(String[] args) throws FileNotFoundException {
        InputReader in = new InputReader(System.in);
        SieveOfEratosthenes soe = new SieveOfEratosthenes(1120);
        List<Integer> primes = soe.getPrimes();
        int[][][] arr = new int[primes.size() + 1][1121][15];

        arr[0][0][0] = 1;

        for (int i = 1; i < primes.size() + 1; i++) {
            int prime = primes.get(i - 1);

            for (int j = 0; j <= 1120; j++) {
                for (int k = 0; k <= 14; k++) {
                    if ((j + prime) <= 1120 && k < 14) {
                        arr[i][j + prime][k + 1] += arr[i - 1][j][k];
                    }
                    arr[i][j][k] += arr[i - 1][j][k];
                }
            }
        }

        while (true) {
            int n = in.nextInt();
            int k = in.nextInt();
            if (n == 0 && k == 0) break;
            if (n == 0 || k == 0) {
                System.out.println(0);
                continue;
            }
            System.out.println(arr[primes.size()][n][k]);
        }
    }

    public static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}

Sample Input:

24 3
24 2
2 1
1 1
4 2
18 3
17 1
17 3
17 4
100 5
1000 10
1120 14
0 0

Sample Output:

2
3
1
0
0
2
1
0
1
55
200102899
2079324314

UVa 562 Dividing Coins Solution in Java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class DividingCoins {
    public static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }
    }

    public static void main(String[] args) {
        InputReader in = new InputReader(System.in);

        int T = in.nextInt();
        while (T-- != 0) {
            int N = in.nextInt();
            int[] A = new int[N];
            int sum = 0;
            for (int n = 0; n < N; n++) {
                A[n] = in.nextInt();
                sum += A[n];
            }

            int column = 0;
            boolean[][] B = new boolean[sum / 2 + 1][N + 1];
            for (int i = 0; i < N + 1; i++) {
                B[0][i] = true;
            }


            for (int i = 0; i < A.length; i++) {
                for (int j = 0; j <= sum / 2; j++) {
                    if (B[j][i]) {
                        B[j][i + 1] = true;
                        if ((j + A[i]) <= sum / 2) {
                            B[j + A[i]][i + 1] = true;
                        }
                    }
                }
            }

            int count = 0;
            for (int i = sum / 2; i >= 0; i--) {
                if (B[i][A.length]) break;
                count++;
            }

            System.out.println((sum % 2 == 0) ? 2 * count : 2 * count + 1);
        }
    }
}

Sample Input:

11
3 2 2 1
4 5 5 2 2
3 6 7 8
4 2 3 4 5
2 24 12
2 1 1
12 1 2 3 4 5 6 7 8 9 10 11 12
4 2 3 4 19
5 3 4 2 1 11
3 10 20 30
4 5 6 3 12

Sample Output:

1
0
5
0
12
0
0
10
1
0
2

SPOJ Factorial solution in Java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class FCTRL {


    public static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }
    }

    public static void main(String[] args) {
        InputReader in = new InputReader(System.in);

        List<Integer> fiveMultiples = new ArrayList<Integer>();
        for (int i = 1; i < 100; i++) {
            if (Math.pow(5, i) > 1000000000) break;
            fiveMultiples.add((int) Math.pow(5, i));
        }

        int T = in.nextInt();
        while (T-- != 0) {
            int num = in.nextInt();
            int count = 0;
            for (int x : fiveMultiples) {
                if (x > num) break;
                count += num / x;
            }
            System.out.println(count);
        }
    }
}

Sample Input:

6
3
60
100
1024
23456
8735373

Sample Output:

0
14
24
253
5861
2183837

SPOJ The Next Palindrome Solution in Java

Solved this problem with the help of BigInt as well, but gave TLE.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

    public static char[] getNextNum(char[] a) {
        boolean carry = true;
        for (int i = a.length - 1; i >= 0; i--) {
            if (!carry) break;

            if (a[i] == '9') {
                carry = true;
                a[i] = '0';
            } else {
                a[i] = (char) (a[i] + 1);
                carry = false;
            }
        }
        char[] b;
        if (carry) {
            b = new char[a.length + 1];
            b[0] = '1';
            for (int i = 0; i < a.length; i++) {
                b[i + 1] = a[i];
            }
        } else {
            b = a;
        }
        return b;
    }

    public static int compare(char[] a, char[] b) {
        if (a.length < b.length) return -1;
        if (a.length > b.length) return 1;
        for (int i = a.length - 1; i >= 0; i--) {
            if (a[i] > b[i]) return 1;
            if (a[i] < b[i]) return -1;
        }
        return 0;
    }

    public static boolean isPalindrome(char[] a) {
        for (int i = 0; i < a.length / 2; i++) {
            if (a[i] != a[a.length - i - 1]) return false;
        }
        return true;
    }

    public static char[] reverse(char[] a) {
        char[] b = new char[a.length];
        for (int i = 0; i < a.length; i++) {
            b[i] = a[a.length - i - 1];
        }
        return b;
    }

    public static char[] subArray(char[] a, int x, int y) {
        char[] b = new char[y - x];
        for (int i = x; i < y; i++) {
            b[i - x] = a[i];
        }
        return b;
    }

    public static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }
    }

    public static void main(String[] args) {
        InputReader in = new InputReader(System.in);
        int T = in.nextInt();
        while (T-- != 0) {
            String num = in.next();

            if (num.length() == 1) {
                System.out.println(11);
                continue;
            }

            char[] a = num.toCharArray();
            a = getNextNum(a);
            if (isPalindrome(a)) {
                System.out.println(a);
                continue;
            }

            if (a.length % 2 == 0) {
                char[] pre = subArray(a, 0, a.length / 2);
                char[] post = subArray(a, a.length / 2, a.length);

                if (compare(post, reverse(pre)) > 0) {
                    pre = getNextNum(pre);
                }

                System.out.print(pre);
                System.out.println(reverse(pre));
            } else {
                char[] pre = subArray(a, 0, a.length / 2 + 1);
                char[] post = subArray(a, a.length / 2 + 1, a.length);

                char[] evenPart = subArray(pre, 0, pre.length - 1);
                if (compare(post, reverse(evenPart)) > 0) {
                    pre = getNextNum(pre);
                }
                System.out.print(pre);
                System.out.println(reverse(subArray(pre, 0, pre.length - 1)));
            }

        }
    }
}